The small theme variant can be used to make Custom Field’s label, helper, and error message smaller.
Custom Field does not 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 MoneyField.java CustomFieldSizeVariants.java Money.java
Expand code
MoneyField.java package com.vaadin.demo.component.customfield;
import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.component.textfield.TextField;
// tag::snippet[]
public class MoneyField extends CustomField<Money> {
private TextField amount;
private Select<String> currency;
public MoneyField(String label) {
this();
setLabel(label);
}
public MoneyField() {
amount = new TextField();
currency = new Select();
currency.setItems("AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "USD");
currency.setWidth("6em");
// aria-label for screen readers
amount.getElement()
.executeJs("const amount = this.shadowRoot.querySelector('[part=\"value\"]');" +
"amount.setAttribute('aria-label', 'Amount');" +
"amount.removeAttribute('aria-labelledby');");
currency.getElement()
.executeJs("const currency = this.shadowRoot.querySelector('vaadin-select-text-field').shadowRoot.querySelector('[part=\"input-field\"]');" +
"currency.setAttribute('aria-label', 'Currency');" +
"currency.removeAttribute('aria-labelledby');");
HorizontalLayout layout = new HorizontalLayout(amount, currency);
// Removes default spacing
layout.setSpacing(false);
// Adds small amount of space between the components
layout.getThemeList().add("spacing-s");
add(layout);
}
public void addThemeVariant(String theme) {
getElement().getThemeList().add(theme);
amount.addThemeName(theme);
currency.getElement().getThemeList().add(theme);
}
@Override
protected Money generateModelValue() {
return new Money(amount.getValue(), currency.getValue());
}
@Override
protected void setPresentationValue(Money money) {
amount.setValue(money.getAmount());
currency.setValue(money.getCurrency());
}
}
// end::snippet[]
CustomFieldSizeVariants.java package com.vaadin.demo.component.customfield;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
@Route("custom-field-size-variants")
public class CustomFieldSizeVariants extends Div {
public CustomFieldSizeVariants() {
// tag::snippet[]
MoneyField moneyField = new MoneyField("Price");
moneyField.addThemeVariant("small");
// end::snippet[]
add(moneyField);
}
}
Money.java package com.vaadin.demo.component.customfield;
// tag::snippet[]
public class Money {
private String amount;
private String currency;
public Money(String amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
}
// end::snippet[]
CDC76788-C339-411B-89BF-510C3603EF59