MDUIDocs
Copy llms.txt linkCopy llms-full.txt linkView this page in MarkdownDiscuss this page with ChatGPTDiscuss full project docs with ChatGPT
Preset Colors
Custom Color
Extract from Wallpaper
Please select a wallpaper
Getting Started
AI-Assisted Development
Styles
Integration with Frameworks
Components
Avatar Badge BottomAppBar Button ButtonIcon Card Checkbox Chip CircularProgress Collapse Dialog Divider Dropdown Fab Icon Layout LinearProgress List Menu NavigationBar NavigationDrawer NavigationRail Radio RangeSlider Select SegmentedButton Slider Snackbar Switch Tabs TextField Tooltip TopAppBar
Functions
Libraries

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:

Item 1 Item 2
<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>

Examples

Variants

Use the variant attribute to change the select's shape.

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

AttributePropertyReflectTypeDefault
variantvariant'filled' | 'outlined''filled'

Defines the select variant. Possible values:

  • filled: Solid background with strong visual emphasis.
  • outlined: Bordered with lighter visual emphasis.
multiplemultiplebooleanfalse

Enables multiple selections.

namenamestring''

Name of the select, which is submitted with form data.

valuevaluestring | string[]''

Value of the select, which is submitted with form data.

If multiple is not set, the value is a string; otherwise, it is an array of strings. HTML attributes can only set string values; array values must be set via the JavaScript property.

defaultValuestring | string[]''

Default selected value. The select resets to this value when the form is reset. JavaScript only.

labellabelstring-

Label text.

placeholderplaceholderstring-

Placeholder text.

helperhelperstring-

Helper text displayed below the select. Alternatively, use slot="helper".

clearableclearablebooleanfalse

Makes the select clearable.

clear-iconclearIconstring-

Material Icons name for the clear button displayed on the right when clearable. Alternatively, use slot="clear-icon".

placementplacement'auto' | 'bottom' | 'top''auto'

Select placement. Possible values:

  • auto: Automatically determined.
  • bottom: Below the input.
  • top: Above the input.
end-alignedendAlignedbooleanfalse

Aligns text to the right.

prefixprefixstring-

Prefix text of the select. Displayed only when the select is focused or has a value. Alternatively, use slot="prefix".

suffixsuffixstring-

Suffix text of the select. Displayed only when the select is focused or has a value. Alternatively, use slot="suffix".

iconiconstring-

Material Icons name for the prefix icon. Alternatively, use slot="icon".

end-iconendIconstring-

Material Icons name for the suffix icon. Alternatively, use slot="end-icon".

error-iconerrorIconstring-

Material Icons name displayed on the right when form field validation fails. Alternatively, use slot="error-icon".

formformstring-

Associates the select with a <form> element. Set this to the id of a <form> in the same document. If omitted, the select uses its parent <form>, if any.

This lets the select work with any form in the document, not just the one it is nested in.

readonlyreadonlybooleanfalse

Makes the select read-only.

disableddisabledbooleanfalse

Disables the select.

requiredrequiredbooleanfalse

Requires a selection when the form is submitted.

validityValidityState-

A ValidityState object that represents the element's validity states.

validationMessagestring-

Validation message. Empty string when valid.

autofocusautofocusbooleanfalse

Whether the element is focused when the page loads.

tabindextabIndexnumber-

The element's tab order when navigating with the Tab key.

Methods

NameParametersReturns
checkValidity
boolean

Checks the validity of the form field. If it is invalid, it triggers an invalid event and returns false. If valid, it returns true.

reportValidity
boolean

Checks the validity of the form field. If it is invalid, it triggers an invalid event, returns false, and displays a validation message. If valid, it returns true.

setCustomValidity
  • message: string
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
  • options: FocusOptions (Optional)
void

Sets focus on the element. An optional object parameter may include a preventScroll property. If preventScroll is set to true, the page will not scroll to bring the focused element into view.

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 event.preventDefault().

Slots

Name
(default)

<mdui-menu-item> elements.

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 multiple is enabled.

chip

Individual chip representing each multi-select option.

chip__button

The <button> element within the chip.

chip__label

Text part of the chip.

chip__delete-icon

Delete icon within the chip.

text-field

Text field, i.e., <mdui-text-field> element.

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 <input> element within the text field.

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., <mdui-menu> element.

On this page