RMWC Select component
npm install @rmwc/selectMenus display a list of choices on a transient sheet of material.
- Module @rmwc/select
- Import styles:
- Using CSS Loader
- import '@rmwc/select/styles';
- Or include stylesheets
- '@rmwc/select/select.css'
- '@material/select/dist/mdc.select.css'
- '@material/floating-label/dist/mdc.floating-label.css'
- '@material/notched-outline/dist/mdc.notched-outline.css'
- '@material/line-ripple/dist/mdc.line-ripple.css'
- '@material/list/dist/mdc.list.css'
- '@material/menu/dist/mdc.menu.css'
- '@material/menu-surface/dist/mdc.menu-surface.css'
- '@material/ripple/dist/mdc.ripple.css'
- MDC Docs: https://material.io/develop/web/components/input-controls/select-menus/
Selects come in three different styles: standard, outlined, and enhanced.
``jsx`
`jsx`
label="Outlined"
outlined
options={['Cookies', 'Pizza', 'Icecream']}
/>
`jsx`
label="Enhanced"
enhanced
options={['Cookies', 'Pizza', 'Icecream']}
/>
`jsx`
defaultValue="Cookies"
label="Enhanced"
enhanced={{ renderToPortal: true, anchorCorner: 'topLeft' }}
options={['Cookies', 'Pizza', 'Icecream']}
/>
`jsx`
label="With Icon"
defaultValue="Pizza"
helpText="Choose your favorite snack..."
icon="favorite"
options={['Cookies', 'Pizza', 'Icecream']}
/>
Select v14 from material-components-web has no width by default. The RMWC team has taken an active choice of giving Select a default width of 200px to stay true to the RMWC principle of introducing no breaking changes.
`jsx`
label="Overwritten width"
options={['Cookies', 'Pizza', 'Icecream']}
className="rmwc-select-readme-example"
/>
`jsx`
label="Required"
required
options={['Cookies', 'Pizza', 'Icecream']}
/>
`jsx`
label="Invalid"
invalid
options={['Cookies', 'Pizza', 'Icecream']}
/>
`jsx`
label="Disabled"
disabled
options={['Cookies', 'Pizza', 'Icecream']}
/>
The Select component has the same behaviors as a native HTML select and be both controlled and uncontrolled.
`jsx`
function Example() {
const [value, setValue] = React.useState('Cookies');
return (
label="Controlled"
options={['Cookies', 'Pizza', 'Icecream']}
value={value}
onChange={(evt) => setValue(evt.currentTarget.value)}
/>
);
}
`jsx`
label="Uncontrolled"
options={['Cookies', 'Pizza', 'Icecream']}
defaultValue="Cookies"
onChange={(evt) => console.log(evt.currentTarget.value)}
/>
To fit common use cases, RMWC Select provides a data driven method for rendering select menus. There are multiple formats you can pass data in, use the one that best fits your requirements. To make your label not float by default and to have an unselected blank value, set the placeholder prop to a blank string.
`jsx
function Example() {
// A controlled select Using a formatted array of options
const options = [
{
label: 'Cookies',
value: '1'
},
{
label: 'Pizza',
value: '2',
/** Any additional items will be passed to the
child ListItem as props */
'aria-disabled': true,
tabIndex: -1
},
{
label: 'Icecream',
value: '3'
}
];
return ;
}
`
`jsx`
label="Object map"
options={{ '1': 'Cookies', '2': 'Pizza', '3': 'Icecream' }}
/>
`jsx`
label="Simple Array"
placeholder="-- Select One --"
options={['Cookies', 'Pizza', 'Icecream']}
/>
If you want full control over the child ListItems, you can manually build the list yourself.
`jsx`
Both native and enhanced Selects can contain option groups. Just nest additional options arrays in your data.
`jsx
label="Formatted"
enhanced
options={[
{
label: 'Dinner',
options: [
{
label: 'Pizza',
value: '2'
}
]
},
{
label: 'Dessert',
options: [
{
label: 'Cookies',
value: '1'
},
{
label: 'Icecream',
value: '3'
}
]
}
]}
/>
`
`jsx`
| Name | Type | Description |
|------|------|-------------|
| disabled | boolean | Makes the Select disabled. |enhanced
| | EnhancedType | Renders a non native / enhanced dropdown |foundationRef
| | Ref | Advanced: A reference to the MDCFoundation. |helpText
| | ReactNode \| SelectHelperTextProps | Adds help text to the field |icon
| | IconPropT | Add a leading icon. |inputRef
| | (ref: null \| HTMLSelectElement<>) => void | A reference to the native select element. Not applicable when enhancedinvalid
is true. |
| | boolean | Makes the Select visually invalid. This is sometimes automatically my material-components-web. |label
| | string | A label for the form control. |options
| | OptionsType | Options accepts flat arrays, value => label maps, and more. See examples for details. |outlined
| | boolean | Makes the select outlined. |placeholder
| | string | Placeholder text for the form control. Set to a blank string to create a non-floating placeholder label. |required
| | boolean | Makes the Select required. |rootProps
| | Object | Props for the root element. By default, additional props spread to the native select element. |value
| | string` | The value for a controlled select. |