Using API Helpers for Defining Component Properties
Java
In your component API for a given property, e.g. value
of an input field, you want to ensure a couple of things are always true:
The getter and setter use the same property or attribute
The getter return value is either the type used by the setter, e.g.
String
for an inputvalue
or an optional version of it, i.e.Optional<String>
if the property is not mandatory.
PropertyDescriptors will automatically take these points into consideration for you.
PropertyDescriptor
instances are created using the helpers available in the PropertyDescriptors
class. There are different helper methods depending on how you want your property to work:
for mapping to an element attribute with a given default value
You can use the optionalAttributeWithDefault
e.g. for a placeholder
in a TextField as the placeholder is not mandatory:
@Tag("input")
public class TextField extends Component {
private static PropertyDescriptor<String, Optional<String>> PLACEHOLDER =
PropertyDescriptors.optionalAttributeWithDefault("placeholder", "");
return get(PLACEHOLDER);
}
public void setPlaceholder(String placeholder) {
set(PLACEHOLDER, placeholder);
}