ComboBoxElement
<vaadin-combo-box> is a web component for choosing a value from a filterable list of options presented in a dropdown overlay. The options can be provided as a list of strings or objects by setting #/elements/vaadin-combo-box#property-items property on the element.
<vaadin-combo-box id="combo-box"></vaadin-combo-box>document.querySelector('#combo-box').items = ['apple', 'orange', 'banana'];When the selected value is changed, a value-changed event is triggered.
Item rendering
To customize the content of the <vaadin-combo-box-item> elements placed in the dropdown, use #/elements/vaadin-combo-box#property-renderer property which accepts a function. The renderer function is called with root, comboBox, and model as arguments.
Generate DOM content by using model object properties if needed, and append it to the root element. The comboBox reference is provided to access the combo-box element state. Do not set combo-box properties in a renderer function.
const comboBox = document.querySelector('#combo-box');
comboBox.items = [{'label': 'Hydrogen', 'value': 'H'}];
comboBox.renderer = (root, comboBox, model) => {
const item = model.item;
root.innerHTML = `${model.index}: ${item.label} <b>${item.value}</b>`;
};Renderer is called on the opening of the combo-box and each time the related model is updated. Before creating new content, it is recommended to check if there is already an existing DOM element in root from a previous renderer call for reusing it. Even though combo-box uses infinite scrolling, reducing DOM operations might improve performance.
The following properties are available in the model argument:
| Property | Type | Description |
|---|---|---|
index | Number | Index of the item in the items array |
item | String or Object | The item reference |
selected | Boolean | True when item is selected |
focused | Boolean | True when item is focused |
Lazy Loading with Function Data Provider
In addition to assigning an array to the items property, you can alternatively use the #/elements/vaadin-combo-box#property-dataProvider function property. The <vaadin-combo-box> calls this function lazily, only when it needs more data to be displayed.
Note that when using function data providers, the total number of items needs to be set manually. The total number of items can be returned in the second argument of the data provider callback:
comboBox.dataProvider = async (params, callback) => {
const API = 'https://demo.vaadin.com/demo-data/1.0/filtered-countries';
const { filter, page, pageSize } = params;
const index = page * pageSize;
const res = await fetch(`${API}?index=${index}&count=${pageSize}&filter=${filter}`);
if (res.ok) {
const { result, size } = await res.json();
callback(result, size);
}
};Styling
The following shadow DOM parts are available for styling:
| Part name | Description |
|---|---|
label | The label element |
input-field | The element that wraps prefix, value and buttons |
field-button | Set on both clear and toggle buttons |
clear-button | The clear button |
error-message | The error message element |
helper-text | The helper text element wrapper |
required-indicator | The required state indicator element |
toggle-button | The toggle button |
overlay | The overlay container |
content | The overlay content |
loader | The loading indicator shown while loading items |
The following state attributes are available for styling:
| Attribute | Description |
|---|---|
disabled | Set when the element is disabled |
has-value | Set when the element has a value |
has-label | Set when the element has a label |
has-helper | Set when the element has helper text or slot |
has-error-message | Set when the element has an error message |
has-tooltip | Set when the element has a slotted tooltip |
invalid | Set when the element is invalid |
focused | Set when the element is focused |
focus-ring | Set when the element is keyboard focused |
readonly | Set when the element is readonly |
opened | Set when the overlay is opened |
loading | Set when loading items from the data provider |
The following custom CSS properties are available for styling:
| Custom CSS property |
|---|
--vaadin-combo-box-overlay-max-height |
--vaadin-combo-box-overlay-width |
--vaadin-field-default-width |
--vaadin-input-field-background |
--vaadin-input-field-border-color |
--vaadin-input-field-border-radius |
--vaadin-input-field-border-width |
--vaadin-input-field-bottom-end-radius |
--vaadin-input-field-bottom-start-radius |
--vaadin-input-field-button-text-color |
--vaadin-input-field-container-gap |
--vaadin-input-field-disabled-background |
--vaadin-input-field-disabled-text-color |
--vaadin-input-field-error-color |
--vaadin-input-field-error-font-size |
--vaadin-input-field-error-font-weight |
--vaadin-input-field-error-line-height |
--vaadin-input-field-gap |
--vaadin-input-field-helper-color |
--vaadin-input-field-helper-font-size |
--vaadin-input-field-helper-font-weight |
--vaadin-input-field-helper-line-height |
--vaadin-input-field-label-color |
--vaadin-input-field-label-font-size |
--vaadin-input-field-label-font-weight |
--vaadin-input-field-label-line-height |
--vaadin-input-field-padding |
--vaadin-input-field-placeholder-color |
--vaadin-input-field-required-indicator |
--vaadin-input-field-required-indicator-color |
--vaadin-input-field-top-end-radius |
--vaadin-input-field-top-start-radius |
--vaadin-input-field-value-color |
--vaadin-input-field-value-font-size |
--vaadin-input-field-value-font-weight |
--vaadin-input-field-value-line-height |
--vaadin-item-overlay-padding |
Internal components
In addition to <vaadin-combo-box> itself, the following internal components are themable:
<vaadin-combo-box-item>- has the same API as #/elements/vaadin-item.
See Styling Components documentation.
Properties
A space-separated list of IDs referencing the elements that describe the component for screen reader users. The referenced elements are announced in addition to the helper text and the error message.
String used to label the component for screen reader users.
A space-separated list of IDs referencing the elements that label the component for screen reader users.
If true, the user can input a value that is not present in the items list. value property will be set to the input value in this case. Also, when value is set programmatically, the input value will be set to reflect that value.
A pattern matched against individual characters the user inputs.
Set true to prevent the overlay from opening automatically.
If true, the input text gets fully selected when the field is focused using click or touch / tap.
Set to true to display the clear icon which clears the input. This also enables clearing the input when pressing Esc key.
Function that provides items lazily. Receives arguments params, callback
Error to show when the field is invalid.
A subset of items, filtered based on the user input. Filtered items can be assigned directly to omit the internal filtering functionality. The items can be of either String or Object type.
String used for the helper text.
A function used to generate CSS class names for dropdown items based on the item. The return value should be the generated class name as a string, or multiple class names separated by whitespace characters.
Path for the id of the item. If items is an array of objects, the itemIdPath is used to compare and identify the same item in the selection and in filteredItems (items given by the dataProvider callback).
A function that is used to generate the label for dropdown items based on the item. Receives one argument:
Path for label of the item. If items is an array of objects, the itemLabelPath is used to fetch the displayed string label for each item.
Path for the value of the item. If items is an array of objects, the itemValuePath: is used to fetch the string value for the selected item.
Set to true to enable manual validation mode. This mode disables automatic constraint validation, allowing you to control the validation process yourself. You can still trigger constraint validation manually with the validate() method or use checkValidity() to assess the component's validity without affecting the invalid state. In manual validation mode, you can also manipulate the invalid property directly through your application logic without conflicts with the component's internal validation.
Controls which item is automatically set to be selected, for example on Enter, when the typed filter only partially matches its label. The item that will be selected is highlighted in the dropdown while typing:
A hint to the user of what can be entered in the field.
Custom function for rendering the content of every item. Receives three arguments:
The selected item from the items array.
Functions
Returns true if the field value satisfies all constraints (if any).
Clears the cached pages and reloads data from dataprovider when needed.
Requests an update for the content of items. While performing the update, it invokes the renderer (passed in the renderer property) once an item.