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 input value 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:

    1. @Tag("input")
    2. public class TextField extends Component {
    3. private static PropertyDescriptor<String, Optional<String>> PLACEHOLDER =
    4. PropertyDescriptors.optionalAttributeWithDefault("placeholder", "");
    5. return get(PLACEHOLDER);
    6. }
    7. public void setPlaceholder(String placeholder) {
    8. set(PLACEHOLDER, placeholder);
    9. }