Skip to content

Instantly share code, notes, and snippets.

@webfirmframework
Last active January 29, 2025 07:46
Show Gist options
  • Select an option

  • Save webfirmframework/37a7cb17dc735e936bccf04596d5fa1b to your computer and use it in GitHub Desktop.

Select an option

Save webfirmframework/37a7cb17dc735e936bccf04596d5fa1b to your computer and use it in GitHub Desktop.
Stateful Input in wffweb-12
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.OnInput;
import com.webfirmframework.wffweb.tag.html.formsandinputs.Input;
import com.webfirmframework.wffweb.wffbm.data.WffBMObject;
/**
* Usage:
* <pre>
* <code>final StatefulInputwffweb12 statefulInputwffweb12 = new StatefulInputwffweb12(this);
* final String inputValue = statefulInputwffweb12.getAttributeByName(AttributeNameConstants.VALUE) instanceof Value value ? value.getValue() : "";
* System.out.println("inputValue = " + inputValue);</code>
* </pre>
*/
public class StatefulInputwffweb12 extends Input {
public StatefulInputwffweb12(AbstractHtml base, AbstractAttribute... attributes) {
super(base, attributes);
develop();
}
private void develop() {
if (super.getAttributeByName(AttributeNameConstants.ONINPUT) != null) {
throw new IllegalArgumentException("OnInput attribute should not be explicitly given to this tag.");
}
final OnInput onInput = new OnInput("return true;",
event -> {
final WffBMObject data = event.data();
final String attrValue = (String) data.getValue("attrValue");
// print just for testing
// System.out.println("attrValue = " + attrValue);
if (super.getAttributeByName(AttributeNameConstants.VALUE) instanceof final Value value) {
//updateClient must be false to avoid syncing the changes to the client browser page
value.setValue(false, attrValue);
} else {
//updateClient must be false to avoid syncing the changes to the client browser page
super.addAttributes(false, new Value(attrValue));
}
return null;
},
"return {attrValue: source.value};", null);
super.addAttributes(onInput);
}
}
@webfirmframework
Copy link
Copy Markdown
Author

Usage:

AbstractHtml parent = null;
final StatefulInputwffweb12 statefulInputwffweb12 = new StatefulInputwffweb12(parent);
final String inputValue = statefulInputwffweb12.getAttributeByName(AttributeNameConstants.VALUE) instanceof final Value value ? value.getValue() : "";
System.out.println("inputValue = " + inputValue);

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