Documentation

Documentation versions (currently viewingVaadin 23)

You are viewing documentation for Vaadin 23. View latest documentation

Custom Field

Custom Field is a component for wrapping multiple components as a single field. It has the same features as Input Fields, such as its label, helper, validation, and data binding. Use it to create custom input components.

Open in a
new tab
Source code
custom-field-basic.ts
DateRangePicker.java
CustomFieldBasic.java
LocalDateRange.java
Appointment.java

Basic Usage

Custom Field is optimized for wrapping the following components:

It can also be used to give a label, helper, and other field features for components that don’t have them built-in, such as List Box.

Value Type and Format

Custom Field handles the value types and formats differently in Java and TypeScript.

Java

Custom Field is a generic class that requires a value type. The value type can be anything: String, List, a bean, and so forth.

Implement the generateModelValue and setPresentationValue methods to define how to build the Custom Field value from the child component values and how to propagate the Custom Field value to the child components respectively.

The following example shows how to set up value propagation, using a bean as the value type:

Source code
Java
class Phone {
    private final String code;
    private final String number;

    public Phone(String code, String number) {
        this.code = code;
        this.number = number;
    }

    public String getCode() {
        return code;
    }

    public String getNumber() {
        return number;
    }
}

class PhoneField extends CustomField<Phone> {
    private final Select code = new Select();
    private final TextField number = new TextField();

    public PhoneField() {
        ...

        add(code, number);
    }

    @Override
    protected String generateModelValue() {
        return new Phone(code.getValue(), number.getValue());
    }

    @Override
    protected void setPresentationValue(Phone value) {
        code.setValue(value.getCode());
        number.setValue(value.getNumber());
    }
}

TypeScript

Custom Field only supports string values. The default value format is a string concatenation of the child component values, separated by the \t character.

You can customize the value format by defining your own value formatter and parser, as shown in the following example:

Source code
TypeScript
render() {
  return html`
    <!-- Phone Custom Field -->
    <vaadin-custom-field
      .formatValue="${([code, number]: unknown[]) => {
        return code && number ? [code, number].join('|') : '';
      }}"
      .parseValue="${(value: string) => {
        return value ? value.split('|') : ['', ''];
      }}"
    >
      <!-- Country code -->
      <vaadin-select></vaadin-select>

      <!-- Phone number -->
      <vaadin-text-field></vaadin-text-field>
    </vaadin-custom-field>
  `
}

Native Input Fields

Custom Field works with native HTML elements.

The whitespace variant can be used when components without an outer margin are used within Custom Field to compensate for the missing space between the label and the component itself.

Open in a
new tab
Source code
custom-field-native-input.ts
PaymentInformationField.java
CustomFieldNativeInput.java
PaymentInformation.java

Supported Features

Size Variants

The small theme variant can be used to make Custom Field’s label, helper, and error message smaller. Custom Field doesn’t propagate its theme variant to its internal components, meaning each internal component’s theme variant must be set individually.

Open in a
new tab
Source code
custom-field-size-variants.ts
MoneyField.java
CustomFieldSizeVariants.java
Money.java

CB7FDF39-7653-4DF0-A0C0-9C2A2EE7EDBA