A select lets people choose one option from a list, in a text field that opens a menu: a country, a role, a sort order. Use it when the options don't need to be visible at once; for a few that do, use radio buttons.
Usage
Each option has an id, which is the select's value, and a text.
import { createSelect } from 'material';
const select = createSelect({
label: 'Fruit',
name: 'fruit',
options: [
{ id: 'apple', text: 'Apple' },
{ id: 'banana', text: 'Banana' },
{ id: 'cherry', text: 'Cherry' },
],
});
select.on('change', ({ value }) => choose(value));
document.body.append(select.element);
<m-select name="fruit" label="Fruit">
<m-select-option value="apple">Apple</m-select-option>
<m-select-option value="banana">Banana</m-select-option>
<m-select-option value="cherry">Cherry</m-select-option>
</m-select>
<script type="module">
const select = document.querySelector('m-select');
select.addEventListener('change', (event) => choose(event.detail.value));
</script>
import { Select, SelectOption } from 'material/react';
import { choose } from './app';
export function Example() {
return (
<Select onChange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
<SelectOption value="apple">Apple</SelectOption>
<SelectOption value="banana">Banana</SelectOption>
<SelectOption value="cherry">Cherry</SelectOption>
</Select>
);
}
<script setup lang="ts">
import { MSelect, MSelectOption } from 'material/vue';
import { choose } from './app';
</script>
<template>
<MSelect @change="choose($event.detail.value)" name="fruit" label="Fruit">
<MSelectOption value="apple">Apple</MSelectOption>
<MSelectOption value="banana">Banana</MSelectOption>
<MSelectOption value="cherry">Cherry</MSelectOption>
</MSelect>
</template>
<script lang="ts">
import { Select, SelectOption } from 'material/svelte';
import { choose } from './app';
</script>
<Select onchange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
<SelectOption value="apple">Apple</SelectOption>
<SelectOption value="banana">Banana</SelectOption>
<SelectOption value="cherry">Cherry</SelectOption>
</Select>
import { Select, SelectOption } from 'material/solid';
import { choose } from './app';
export function Example() {
return (
<Select onChange={(event) => choose(event.detail.value)} name="fruit" label="Fruit">
<SelectOption value="apple">Apple</SelectOption>
<SelectOption value="banana">Banana</SelectOption>
<SelectOption value="cherry">Cherry</SelectOption>
</Select>
);
}
variant is filled (the default) or outlined, and density: 'compact' lowers the field,
as on a text field. A select fills its container's width.
Examples
A chosen option, a disabled one
value selects an option by its id. A disabled option stays in the list and can't be
chosen.
import { createSelect } from 'material';
const select = createSelect({
label: 'Plan',
variant: 'outlined',
value: 'pro',
supportingText: 'Billed monthly',
options: [
{ id: 'free', text: 'Free' },
{ id: 'pro', text: 'Professional' },
{ id: 'enterprise', text: 'Enterprise', disabled: true },
],
});
document.body.append(select.element);
<m-select value="pro" variant="outlined" label="Plan" supporting-text="Billed monthly">
<m-select-option value="free">Free</m-select-option>
<m-select-option value="pro">Professional</m-select-option>
<m-select-option value="enterprise" disabled>Enterprise</m-select-option>
</m-select>
import { useState } from 'react';
import { Select, SelectOption } from 'material/react';
export function Example() {
const [value, setValue] = useState("pro");
return (
<Select value={value} onChange={(event) => setValue(event.detail.value)} variant="outlined" label="Plan" supportingText="Billed monthly">
<SelectOption value="free">Free</SelectOption>
<SelectOption value="pro">Professional</SelectOption>
<SelectOption value="enterprise" disabled>Enterprise</SelectOption>
</Select>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSelect, MSelectOption } from 'material/vue';
const value = ref("pro");
</script>
<template>
<MSelect v-model="value" variant="outlined" label="Plan" supporting-text="Billed monthly">
<MSelectOption value="free">Free</MSelectOption>
<MSelectOption value="pro">Professional</MSelectOption>
<MSelectOption value="enterprise" disabled>Enterprise</MSelectOption>
</MSelect>
</template>
<script lang="ts">
import { Select, SelectOption } from 'material/svelte';
let value = $state("pro");
</script>
<Select bind:value variant="outlined" label="Plan" supportingText="Billed monthly">
<SelectOption value="free">Free</SelectOption>
<SelectOption value="pro">Professional</SelectOption>
<SelectOption value="enterprise" disabled>Enterprise</SelectOption>
</Select>
import { createSignal } from 'solid-js';
import { Select, SelectOption } from 'material/solid';
export function Example() {
const [value, setValue] = createSignal("pro");
return (
<Select value={value()} onChange={(event) => setValue(event.detail.value)} variant="outlined" label="Plan" supportingText="Billed monthly">
<SelectOption value="free">Free</SelectOption>
<SelectOption value="pro">Professional</SelectOption>
<SelectOption value="enterprise" disabled>Enterprise</SelectOption>
</Select>
);
}
Required, in error
setError(true, message) shows a message in place of the supporting text, and clearError()
restores it. An action here chooses an option and clears the error.
import { createSelect } from 'material';
const select = createSelect({
label: 'Role',
value: '',
required: true,
error: true,
supportingText: 'Choose a role',
options: [
{ id: 'admin', text: 'Administrator' },
{ id: 'editor', text: 'Editor' },
{ id: 'viewer', text: 'Viewer' },
],
});
document.body.append(select.element);
function chooseEditor() {
select.setValue('editor');
select.setError(false);
}
<m-select label="Role" supporting-text="Choose a role" required error>
<m-select-option value="admin">Administrator</m-select-option>
<m-select-option value="editor">Editor</m-select-option>
<m-select-option value="viewer">Viewer</m-select-option>
</m-select>
<script type="module">
const select = document.querySelector('m-select');
function chooseEditor() {
select.value = "editor";
select.toggleAttribute('error', false);
}
</script>
import { useState } from 'react';
import { Select, SelectOption } from 'material/react';
export function Example() {
const [value, setValue] = useState("");
const [error, setError] = useState(true);
const chooseEditor = () => {
setValue("editor");
setError(false);
};
return (
<Select value={value} onChange={(event) => setValue(event.detail.value)} label="Role" supportingText="Choose a role" required error={error}>
<SelectOption value="admin">Administrator</SelectOption>
<SelectOption value="editor">Editor</SelectOption>
<SelectOption value="viewer">Viewer</SelectOption>
</Select>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MSelect, MSelectOption } from 'material/vue';
const value = ref("");
const error = ref(true);
function chooseEditor() {
value.value = "editor";
error.value = false;
}
</script>
<template>
<MSelect v-model="value" label="Role" supporting-text="Choose a role" required :error="error">
<MSelectOption value="admin">Administrator</MSelectOption>
<MSelectOption value="editor">Editor</MSelectOption>
<MSelectOption value="viewer">Viewer</MSelectOption>
</MSelect>
</template>
<script lang="ts">
import { Select, SelectOption } from 'material/svelte';
let value = $state("");
let error = $state(true);
function chooseEditor() {
value = "editor";
error = false;
}
</script>
<Select bind:value label="Role" supportingText="Choose a role" required error={error}>
<SelectOption value="admin">Administrator</SelectOption>
<SelectOption value="editor">Editor</SelectOption>
<SelectOption value="viewer">Viewer</SelectOption>
</Select>
import { createSignal } from 'solid-js';
import { Select, SelectOption } from 'material/solid';
export function Example() {
const [value, setValue] = createSignal("");
const [error, setError] = createSignal(true);
const chooseEditor = () => {
setValue("editor");
setError(false);
};
return (
<Select value={value()} onChange={(event) => setValue(event.detail.value)} label="Role" supportingText="Choose a role" required error={error()}>
<SelectOption value="admin">Administrator</SelectOption>
<SelectOption value="editor">Editor</SelectOption>
<SelectOption value="viewer">Viewer</SelectOption>
</Select>
);
}
The menu is mounted in the select's own element. In a scrolling or clipping container, such as
a navigation drawer or a sheet, set menu: { container: document.body, maxHeight: '320px' }, or
layer: 'top' to show it in the browser's top layer. setOptions() replaces the options, for
a list loaded later. Validating a select in a form is planned for Showcase.
API
Options
optionsThe options
SelectOption[][]valueThe id of the option selected at first
stringundefinedvariantThe text field's style
'filled' | 'outlined''filled'densityThe field height
'default' | 'compact''default'labelThe floating label
stringundefinednameThe input's name, for forms
stringundefinedrequiredWhether a selection is required
booleanfalsedisabledWhether the select is disabled
booleanfalsesupportingTextHelper text under the field
stringundefinederrorThe error state
booleanfalseplacementThe menu's placement against the field
string'bottom-start'menuWhere the menu is mounted, its maximum height, whether it flips above the field, and its variant ('vertical' by default, or 'baseline') and colors
{ container?, maxHeight?, autoFlip?, variant?, color? }undefinedlayerRenders the menu beside the field and shows it in the top layer; menu.container is then not used
'top'undefinedonEvent handlers registered at creation
{ change?, open?, close? }undefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'An option
idThe option's value
stringtextIts text
stringdisabledWhether it can be chosen
booleanfalseiconHTML, usually an SVG, before the text
stringundefinedhasSubmenu / submenuNested options; a select with them is a menu button rather than a combobox
boolean / SelectOption[]undefineddataData of your own, carried with the option
unknownundefinedMethods
getValue() / setValue(value)The selected option's id
value: string | null | undefinedstring | null / SelectComponentclear()Clears the selection
SelectComponentgetText()The selected option's text
stringgetSelectedOption()The selected option
SelectOption | nullgetOptions() / setOptions(options)The options
options: SelectOption[]SelectOption[] / SelectComponentopen(interactionType?) / close()Opens or closes the menu; a disabled select does not open
interactionType?: 'mouse' | 'keyboard'SelectComponentisOpen()Whether the menu is open
booleansetDensity(density) / getDensity()The field height
density: 'default' | 'compact'SelectComponent / stringenable() / disable()The disabled state
SelectComponentsetError(error, message?) / clearError()The error state
error: boolean, message?: stringSelectComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionSelectComponentdestroy()Destroys the select and releases its menu
voidelementThe root, which is the text field's
HTMLElementtextFieldThe text field
TextFieldComponentEvents
changeThe selection changed
{ select, value, text, option, originalEvent? }. value is string | nullopen / closeThe menu opened or closed, however it was
{ select, originalEvent? }The web component's change carries { value }.
Styling
The select is a text field: its root carries both sets of classes, and the menu is its child.
.mtrl-select, .mtrl-select--open { }
.mtrl-select.mtrl-text-field--filled, .mtrl-select.mtrl-text-field--outlined { }
.mtrl-select .mtrl-text-field__input, .mtrl-select .mtrl-text-field__label { }
.mtrl-select .mtrl-text-field__trailing-icon { }
.mtrl-select > .mtrl-menu { }