Select
The Select component is designed to present a list of options in a dropdown menu.
This guide focuses on the usage of the <mdui-select>
component. For information on dropdown menu items, please refer to 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 is set to single-select. The value
of the <mdui-select>
component corresponds to the value
of the currently selected <mdui-menu-item>
.
To enable multi-select, 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 selections are enabled, the value
of <mdui-select>
is an array. This value can only be read and set using JavaScript properties.
Helper Text
The label
attribute allows you to display label text above the select.
The placeholder
attribute allows you to display placeholder text when no value is selected.
The helper
attribute allows you to display helper text at the bottom of the select. Alternatively, use the helper
slot to set 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 adds 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
You can set the dropdown menu position using the placement
attribute.
Text Alignment
To align the text to the right, add the end-aligned
attribute.
Prefix, Suffix, and Icons
You can add Material Icons to the left and right of the select by setting the icon
and end-icon
attributes. Alternatively, use the icon
and end-icon
slots to add elements to the select.
You can add text to the left and right of the select by setting the prefix
and suffix
attributes. Alternatively, use the prefix
and suffix
slots to add text elements. This text is displayed when the select is focused or has a value.
API
Properties
Attribute | Property | Reflect | Type | Default |
---|---|---|---|---|
variant | variant | 'filled' | 'outlined' | 'filled' | |
Defines the select style. 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. Resets to this value when form is reset. Can only be set via JavaScript property. | ||||
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 | |
Allows the select to be cleared. | ||||
clear-icon | clearIcon | string | - | |
Material Icons name for the clear button displayed on the right of the select 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 attribute allows select elements to be associated with | ||||
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 | - | ||
The element's validation message. This is empty if the element meets its constraints. | ||||
autofocus | autofocus | boolean | false | |
Determines if the element should be focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
Specifies the order in which the element receives focus when navigating with the Tab key. |
Methods
Name | Parameters | Returns |
---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the text is non-empty, it indicates that the field is invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional parameter can be an object with 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 doesn't 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 multiselect 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., |