Select
The Select component presents options in a dropdown menu.
This guide focuses on the usage of the <mdui-select> component. For information on dropdown menu items, see the <mdui-menu-item> section.
Usage
Import the component:
import 'mdui/components/select.js';
import 'mdui/components/menu-item.js';
Import the TypeScript type:
import type { Select } from 'mdui/components/select.js';
import type { MenuItem } from 'mdui/components/menu-item.js';
Example:
<mdui-select value="item-1">
<mdui-menu-item value="item-1">Item 1</mdui-menu-item>
<mdui-menu-item value="item-2">Item 2</mdui-menu-item>
</mdui-select>Multiple Selection
By default, the component allows only a single selection. The value of the <mdui-select> component corresponds to the value of the currently selected <mdui-menu-item>.
To allow multiple selections, add the multiple attribute. In this case, the value of <mdui-select> becomes an array containing the value properties of the currently selected <mdui-menu-item> components.
Note: When multiple selection is enabled, the value property of <mdui-select> is an array and can only be accessed and accessed and updated in JavaScript.
Helper Text
Use the label attribute to display a label above the select.
Use the placeholder attribute to display placeholder text when no value is selected.
Use the helper attribute to display helper text at the bottom of the select. Alternatively, use the helper slot to provide helper text.
Read-Only State
To make the select read-only, add the readonly attribute.
Disabled State
To disable the select, add the disabled attribute.
Clearable
The clearable attribute displays a clear button on the right when the select has a value.
You can customize the clear button using the clear slot.
Dropdown Menu Position
Use the placement attribute to set the dropdown menu position.
Text Alignment
To align the text to the right, add the end-aligned attribute.
Prefix, Suffix, and Icons
Use the icon and end-icon attributes to add Material Icons to the left and right of the select. Alternatively, use the icon and end-icon slots to add elements to the select.
Use the prefix and suffix attributes to add text to the left and right of the select. Alternatively, use the prefix and suffix slots to add text elements. They appear when the select is focused or has a value.
API
Properties
| Attribute | Property | Reflect | Type | Default |
|---|---|---|---|---|
variant | variant | 'filled' | 'outlined' | 'filled' | |
Defines the select variant. Possible values:
| ||||
multiple | multiple | boolean | false | |
Enables multiple selections. | ||||
name | name | string | '' | |
Name of the select, which is submitted with form data. | ||||
value | value | string | string[] | '' | |
Value of the select, which is submitted with form data. If | ||||
defaultValue | string | string[] | '' | ||
Default selected value. The select resets to this value when the form is reset. JavaScript only. | ||||
label | label | string | - | |
Label text. | ||||
placeholder | placeholder | string | - | |
Placeholder text. | ||||
helper | helper | string | - | |
Helper text displayed below the select. Alternatively, use | ||||
clearable | clearable | boolean | false | |
Makes the select clearable. | ||||
clear-icon | clearIcon | string | - | |
Material Icons name for the clear button displayed on the right when clearable. Alternatively, use | ||||
placement | placement | 'auto' | 'bottom' | 'top' | 'auto' | |
Select placement. Possible values:
| ||||
end-aligned | endAligned | boolean | false | |
Aligns text to the right. | ||||
prefix | prefix | string | - | |
Prefix text of the select. Displayed only when the select is focused or has a value. Alternatively, use | ||||
suffix | suffix | string | - | |
Suffix text of the select. Displayed only when the select is focused or has a value. Alternatively, use | ||||
icon | icon | string | - | |
Material Icons name for the prefix icon. Alternatively, use | ||||
end-icon | endIcon | string | - | |
Material Icons name for the suffix icon. Alternatively, use | ||||
error-icon | errorIcon | string | - | |
Material Icons name displayed on the right when form field validation fails. Alternatively, use | ||||
form | form | string | - | |
Associates the select with a This lets the select work with any form in the document, not just the one it is nested in. | ||||
readonly | readonly | boolean | false | |
Makes the select read-only. | ||||
disabled | disabled | boolean | false | |
Disables the select. | ||||
required | required | boolean | false | |
Requires a selection when the form is submitted. | ||||
validity | ValidityState | - | ||
A | ||||
validationMessage | string | - | ||
Validation message. Empty string when valid. | ||||
autofocus | autofocus | boolean | false | |
Whether the element is focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
The element's tab order when navigating with the Tab key. | ||||
Methods
| Name | Parameters | Returns |
|---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it is invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the message is non-empty, the field is considered invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional object parameter may include a | ||
blur | void | |
Removes focus from the element. | ||
Events
| Name |
|---|
focus |
Emitted when the select gains focus. |
blur |
Emitted when the select loses focus. |
change |
Emitted when the selected value changes. |
invalid |
Emitted when the form control's validity is checked and it does not meet the constraints. |
clear |
Emitted when the clear button is clicked. Can be prevented with |
Slots
| Name |
|---|
| (default) |
|
icon |
Left icon. |
end-icon |
Right icon. |
error-icon |
Right icon when validation fails. |
prefix |
Left text. |
suffix |
Right text. |
clear-button |
Clear button. |
clear-icon |
Icon in the clear button. |
helper |
Bottom helper text. |
CSS Parts
| Name |
|---|
chips |
Container for option chips when |
chip |
Individual chip representing each multi-select option. |
chip__button |
The |
chip__label |
Text part of the chip. |
chip__delete-icon |
Delete icon within the chip. |
text-field |
Text field, i.e., |
text-field__container |
Container for the text field. |
text-field__icon |
Icon within the text field. |
text-field__end-icon |
Right-side icon within the text field. |
text-field__error-icon |
Icon displayed in the text field upon validation failure. |
text-field__prefix |
Text on the left side of the text field. |
text-field__suffix |
Text on the right side of the text field. |
text-field__label |
Label text displayed above the text field. |
text-field__input |
The |
text-field__clear-button |
Clear button within the text field. |
text-field__clear-icon |
Icon within the clear button of the text field. |
text-field__supporting |
Container for supporting information at the bottom of the text field, including helper and error messages. |
text-field__helper |
Helper text displayed at the bottom of the text field. |
text-field__error |
Error message displayed at the bottom of the text field. |
menu |
Dropdown menu, i.e., |