Skip to content

Instantly share code, notes, and snippets.

@webfirmframework
Created January 31, 2018 08:35
Show Gist options
  • Save webfirmframework/e01427cc8acb8a2e48dee61f7d76d442 to your computer and use it in GitHub Desktop.
Save webfirmframework/e01427cc8acb8a2e48dee61f7d76d442 to your computer and use it in GitHub Desktop.
Example for Sateful input field which syncs the value state to the server on change value event.
import com.webfirmframework.wffweb.tag.html.AbstractHtml;
import com.webfirmframework.wffweb.tag.html.attribute.AttributeNameConstants;
import com.webfirmframework.wffweb.tag.html.attribute.Value;
import com.webfirmframework.wffweb.tag.html.attribute.core.AbstractAttribute;
import com.webfirmframework.wffweb.tag.html.attribute.event.form.OnChange;
import com.webfirmframework.wffweb.tag.html.formsandinputs.Input;
public class StatefulInput extends Input {
public StatefulInput(AbstractHtml base, AbstractAttribute... attributes) {
super(base, attributes);
develop();
}
private void develop() {
if (super.getAttributeByName(AttributeNameConstants.ONCHANGE) != null) {
throw new IllegalArgumentException("OnChange attribute should not be explicitly given in this tag.");
}
final Value valueAttr;
{
Value value = (Value) super.getAttributeByName(AttributeNameConstants.VALUE);
valueAttr = value != null ? value : new Value("");
}
OnChange onChange = new OnChange("return true;",
(bm, ev) -> {
String value = (String) bm.getValue("attrValue");
//updateClient must be false to avoid synching the changes to the client browser page
valueAttr.setValue(false, value);
return null;
},
"return {attrValue: source.value};", null);
super.addAttributes(valueAttr, onChange);
}
}
@webfirmframework
Copy link
Author

webfirmframework commented Jan 31, 2018

Usage:- new StatefulInput(this, new Type(Type.TEXT), new Name("First Name"));

Developers guide to get started

Convert HTML5 to webfirmframework Java/Kotlin Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment