Select
- Usage
- Styling
- Dividers
- Disabled Items
- Overlay
- Basic Features
- Read-Only & Disabled
- Style Variants
- Placeholder
- Empty Selection Item (Flow)
- Custom Item Label
- Custom Item Presentation
- Best Practices
- Related Components
Select allows users to choose a single value from a list of options presented in an overlay.
new tab
Select<String> select = new Select<>();
select.setLabel("Sort by");
select.setItems("Most recent first", "Rating: high to low",
"Rating: low to high", "Price: high to low",
"Price: low to high");
select.setValue("Most recent first");
add(select);
The drop-down menu can be opened with a click, up and down arrow keys, or by typing the initial character of one of the options.
Dividers
Dividers can be used to group related options. Use dividers sparingly to avoid creating unnecessary visual clutter.
new tab
Select<String> select = new Select<>();
select.setLabel("Sort by");
select.setItems("Most recent first", "Rating: high to low",
"Rating: low to high", "Price: high to low",
"Price: low to high");
select.addComponents("Most recent first", new Hr());
select.addComponents("Rating: low to high", new Hr());
select.setValue("Most recent first");
add(select);
For large data sets, it’s preferable to use Combo Box instead of Select. This allows users to filter the list of options.
Disabled Items
Items can be disabled. This prevents users from selecting them, while still showing that the items would be available for selection under different circumstances.
new tab
Select<String> select = new Select<>();
select.setLabel("Size");
select.setItems("XS (out of stock)", "S", "M", "L", "XL");
select.setItemEnabledProvider(
item -> !"XS (out of stock)".equals(item));
select.setValue("XL");
add(select);
Caution
|
Accessibility
Some assistive technologies might not announce disabled options.
|
Overlay
You can customize the position and the width of the Select overlay, where the list of options is rendered. You can also add CSS class names to the overlay to customize its styles.
Overlay Position
The overlay is shown on top of the input field by default. You can configure the placement so that it doesn’t cover the input field.
Overlay Width
The overlay is as wide as the options in it and at least as wide as the input field. The overlay width can be overridden to any fixed width in cases where the default width is too narrow.
Overlay Class Name
Like other field components with overlays, Select provides dedicated API to set a CSS class name on the overlay for styling. See Styling Component Instances for more information.
Select<String> select = new Select<>();
select.addOverlayClassName("locales");
Basic Features
The following features, common to most input field components, are supported:
Label
The label is used to identify the input field. It supports plain-text content, and its length is limited to the width of the field. Helpers and Tooltips can be used to provide additional information that doesn’t fit into the label.
Visible labels are strongly recommended for all input fields. In cases where the built-in label cannot be used, an external element can be associated as the field’s label through the aria-labelledby
attribute. Fields without any visible label should include an invisible label for assistive technologies with the aria-label
attribute.
Helper
Helpers are used to provide additional information that the user may need to enter in the field, such as format requirements or explanations of the field’s purpose below the field.
A style variant is available for rendering the helper above the field.
In addition to plain text, helpers can contain components and HTML elements. However, complex and interactive content is likely to have accessibility issues.
Tooltip
Tooltips are small text pop-ups displayed on hover, and on keyboard-focus. They can be used to provide additional information about a field. This can be useful in situations where an always visible Helper is not appropriate. Helpers are generally recommended in favor of tooltips, though, as they provide much better discoverability and mobile support. See the Tooltip documentation for more information.
Prefix
A prefix element — rendered at the start of the field — can be used to display units, icons, and similar visual cues to the field’s purpose or format.
External & Invisible Labels (ARIA)
Visible labels are strongly recommended for all input fields. In situations where the built-in label cannot be used, an external element can be associated as the field’s label through its element id
. Fields without any visible label should be provided an invisible label for assistive technologies like screen readers.
<!-- Associates external element as label: -->
<label id="external-label">This is the label</label>
<vaadin-select accessible-name-ref="external-label">...
<!-- Invisible label for screen readers: -->
<vaadin-select accessible-name="This is the label">...
new tab
Select<String> field = new Select<>();
field.setLabel("Label");
field.setHelperText("Helper text");
field.setPlaceholder("Placeholder");
field.setTooltipText("Tooltip text");
field.setPrefixComponent(VaadinIcon.VAADIN_H.create());
Read-Only & Disabled
Fields used to display values should be set to read-only
mode to prevent editing. Read-only fields are focusable and visible to screen readers. They can display tooltips. Their values can be selected and copied.
Fields that are currently unavailable should be disabled
. The reduced contrast of disabled fields makes them inappropriate for displaying information. They can’t be focused or display tooltips. They’re invisible to screen readers, and their values cannot be selected and copied.
Disabled fields can be useful in situations where they can become enabled based on some user action. Consider hiding fields entirely if there’s nothing the user can do to make them editable.
new tab
Select<String> readonlyField = new Select<>();
readonlyField.setReadOnly(true);
Select<String> disabledField = new Select<>();
disabledField.setEnabled(false);
Style Variants
The following style variants can be applied:
Text Alignment
Three different text alignments are supported: left
, which is the default; center
; and right
.
Right-alignment is recommended for numerical values when presented in vertical groups. This tends to aid interpretation and comparison of values.
Small Variant
The small variant can be used to make individual fields more compact. The default size of fields can be customized with style properties.
Helper Above Field
The helper can be rendered above the field, and below the label.
Borders
Borders can be applied to the field surface by providing a value (e.g., 1px
) to the --vaadin-input-field-border-width
CSS property. This can be applied globally to all input fields using the html
selector, or to individual component instances. Borders are required to achieve WCAG 2.1 level AA conformant color contrast with the default Lumo styling of fields.
You can override the default border color with the --vaadin-input-field-border-color
property.
new tab
Select<String> field = new Select<>();
field.addThemeVariants(SelectVariant.LUMO_SMALL,
SelectVariant.LUMO_ALIGN_RIGHT,
SelectVariant.LUMO_HELPER_ABOVE_FIELD);
field.getStyle().set("--vaadin-input-field-border-width", "1px");
Placeholder
Use the placeholder feature to provide an inline text prompt for the field. Don’t create or use a separate item for this purpose.
new tab
Select<String> select = new Select<>();
select.setLabel("Size");
select.setItems("XS", "S", "M", "L", "XL");
select.setPlaceholder("Select size");
add(select);
Empty Selection Item (Flow)
An empty item can be set as the first option. Use it in cases where you want to allow users to clear their selection. The value of the empty item is represented as null
.
Customizing Empty Selection Caption
The label for the empty item is customizable. The caption that you set replaces the placeholder for the empty selection item.
new tab
select.setEmptySelectionAllowed(true);
select.setEmptySelectionCaption("Unknown size");
Custom Item Label
When using complex values, a label can be set to represent the item value as plain text.
new tab
Select<Person> select = new Select<>();
select.setLabel("Assignee");
// Display full name of the person as item text and selected value label
select.setItemLabelGenerator(Person::getFullName);
List<Person> people = DataService.getPeople(5);
select.setItems(people);
When using custom item renderers with rich content, a label can be set to represent the item value when it’s selected.
new tab
Select<Person> select = new Select<>();
select.setLabel("Assignee");
// Use a custom renderer for items in the dropdown
select.setRenderer(SelectCustomRendererLabel.createPersonRenderer());
// Display full name of the person as selected value label
select.setItemLabelGenerator(Person::getFullName);
List<Person> people = DataService.getPeople(5);
select.setItems(people);
Note
|
Flow-Specific
When using
The same applies when using a data source that may contain null values. |
Custom Item Presentation
Items can be rendered with rich content instead of plain text. This can be useful to provide information in a more legible fashion than appending it to the item text.
new tab
Select<Person> select = new Select<>();
select.setLabel("Choose doctor");
select.setRenderer(new ComponentRenderer<>(person -> {
FlexLayout wrapper = new FlexLayout();
wrapper.setAlignItems(Alignment.CENTER);
// NOTE
// We are using inline styles here to keep the example simple.
// We recommend placing CSS in a separate style sheet and to
// encapsulating the styling in a new component.
Image image = new Image();
image.setSrc(person.getPictureUrl());
image.setAlt("Portrait of " + person.getFirstName() + " "
+ person.getLastName());
image.setWidth("var(--lumo-size-m)");
image.getStyle().set("margin-right", "var(--lumo-space-s)");
Div info = new Div();
info.setText(person.getFirstName() + " " + person.getLastName());
Div profession = new Div();
profession.setText(person.getProfession());
profession.getStyle().set("font-size", "var(--lumo-font-size-s)");
profession.getStyle().set("color",
"var(--lumo-secondary-text-color)");
info.add(profession);
wrapper.add(image, info);
return wrapper;
}));
select.setItems(items);
add(select);
Best Practices
Don’t Use as a Menu
Select is an input field component, not a generic menu component. Use Menu Bar to create overlays for actions.
Related Components
Component | Usage Recommendation |
---|---|
Better accessibility than Select, as all options are visible without user interaction. | |
Filterable list of options. Appropriate for large sets of options. Supports lazy-loading entry of custom values. | |
Scrollable inline list of options. Supports single and multi-select. | |
Overlay menus for items that trigger actions. |
F53C23C7-4798-402A-A7C9-47416795D4C5