RMWC
14.3.5 arrow_drop_down

Select Menus

Menus 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/

Select Styles

Selects come in three different styles: standard, outlined, and enhanced.

Standard
Standardarrow_drop_downarrow_drop_up
<Select label="Standard" options={['Cookies', 'Pizza', 'Icecream']} />
Outlined
Outlined
arrow_drop_downarrow_drop_up
<Select
  label="Outlined"
  outlined
  options={['Cookies', 'Pizza', 'Icecream']}
/>
Enhanced
Enhancedarrow_drop_downarrow_drop_up
<Select
  label="Enhanced"
  enhanced
  options={['Cookies', 'Pizza', 'Icecream']}
/>
Enhanced renderToPortal
Enhancedarrow_drop_downarrow_drop_up
<Select
  defaultValue="Cookies"
  label="Enhanced"
  enhanced={{ renderToPortal: true, anchorCorner: 'topLeft' }}
  options={['Cookies', 'Pizza', 'Icecream']}
/>
With Options
favoriteWith Iconarrow_drop_downarrow_drop_up

Choose your favorite snack...

<Select
  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.

With overwritten width
Overwritten widtharrow_drop_downarrow_drop_up
<Select
  label="Overwritten width"
  options={['Cookies', 'Pizza', 'Icecream']}
  className="rmwc-select-readme-example"
/>

Validation

Required
Requiredarrow_drop_downarrow_drop_up
<Select
  label="Required"
  required
  options={['Cookies', 'Pizza', 'Icecream']}
/>
Invalid
Invalidarrow_drop_downarrow_drop_up
<Select
  label="Invalid"
  invalid
  options={['Cookies', 'Pizza', 'Icecream']}
/>
Disabled
Disabledarrow_drop_downarrow_drop_up
<Select
  label="Disabled"
  disabled
  options={['Cookies', 'Pizza', 'Icecream']}
/>

Controlled / Uncontrolled

The Select component has the same behaviors as a native HTML select and be both controlled and uncontrolled.

Controlled
Controlledarrow_drop_downarrow_drop_up
function Example() {
  const [value, setValue] = React.useState('Cookies');
  return (
    <Select
      label="Controlled"
      options={['Cookies', 'Pizza', 'Icecream']}
      value={value}
      onChange={(evt) => setValue(evt.currentTarget.value)}
    />
  );
}
Uncontrolled
Uncontrolledarrow_drop_downarrow_drop_up
<Select
  label="Uncontrolled"
  options={['Cookies', 'Pizza', 'Icecream']}
  defaultValue="Cookies"
  onChange={(evt) => console.log(evt.currentTarget.value)}
/>

Data Driven Selects

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.

Formatted Options
Arrayarrow_drop_downarrow_drop_up
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 <Select label="Array" options={options} />;
}
Value => Label Map
Object maparrow_drop_downarrow_drop_up
<Select
  label="Object map"
  options={{ '1': 'Cookies', '2': 'Pizza', '3': 'Icecream' }}
/>
Array
Simple Arrayarrow_drop_downarrow_drop_up
<Select
  label="Simple Array"
  placeholder="-- Select One --"
  options={['Cookies', 'Pizza', 'Icecream']}
/>

Manually Building the List

If you want full control over the child ListItems, you can manually build the list yourself.

Manually Built
Manualarrow_drop_downarrow_drop_up
<Select label="Manual" defaultValue="Cookies">
  <option value="Cookies">Cookies</option>
  <option value="Pizza">Pizza</option>
  <option value="Icecream">Icecream</option>
</Select>

Option Groups

Both native and enhanced Selects can contain option groups. Just nest additional options arrays in your data.

Option Groups: Formatted
Formattedarrow_drop_downarrow_drop_up
    Dinner
    Dessert
<Select
  label="Formatted"
  enhanced
  options={[
    {
      label: 'Dinner',
      options: [
        {
          label: 'Pizza',
          value: '2'
        }
      ]
    },
    {
      label: 'Dessert',
      options: [
        {
          label: 'Cookies',
          value: '1'
        },

        {
          label: 'Icecream',
          value: '3'
        }
      ]
    }
  ]}
/>
Options Groups: Manually Built
Manually Builtarrow_drop_downarrow_drop_up
<Select label="Manually Built">
  <optgroup label="Dinner">
    <option value="Pizza">Pizza</option>
  </optgroup>
  <optgroup label="Dessert">
    <option value="Cookies">Cookies</option>
    <option value="Icecream">Icecream</option>
  </optgroup>
</Select>

Select

A Select Component

Props

NameTypeDescription
disabledbooleanMakes the Select disabled.
enhancedEnhancedTypeRenders a non native / enhanced dropdown
foundationRefRef<MDCSelectFoundation<>>Advanced: A reference to the MDCFoundation.
helpTextReactNode | SelectHelperTextPropsAdds help text to the field
iconIconPropTAdd a leading icon.
inputRef(ref: null | HTMLSelectElement<>) => voidA reference to the native select element. Not applicable when `enhanced` is true.
invalidbooleanMakes the Select visually invalid. This is sometimes automatically my material-components-web.
labelstringA label for the form control.
optionsOptionsTypeOptions accepts flat arrays, value => label maps, and more. See examples for details.
outlinedbooleanMakes the select outlined.
placeholderstringPlaceholder text for the form control. Set to a blank string to create a non-floating placeholder label.
requiredbooleanMakes the Select required.
rootPropsObjectProps for the root element. By default, additional props spread to the native select element.
valuestringThe value for a controlled select.
Themes
  • Baseline
  • Crane
  • Fortnightly
  • Miami
  • Dark
  • Theme your App!
    Place this tag around the root of your App, or anywhere you want to apply a custom theme.

    import { ThemeProvider } from 'rmwc/Theme';

    <ThemeProvider options={{
      primary: '#6200ee',
      secondary: '#03dac4',
      error: '#b00020',
      background: '#fff',
      surface: '#fff',
      onPrimary: 'rgba(255, 255, 255, 1)',
      onSecondary: 'rgba(0, 0, 0, 0.87)',
      onSurface: 'rgba(0, 0, 0, 0.87)',
      onError: '#fff',
      textPrimaryOnBackground: 'rgba(0, 0, 0, 0.87)',
      textSecondaryOnBackground: 'rgba(0, 0, 0, 0.54)',
      textHintOnBackground: 'rgba(0, 0, 0, 0.38)',
      textDisabledOnBackground: 'rgba(0, 0, 0, 0.38)',
      textIconOnBackground: 'rgba(0, 0, 0, 0.38)',
      textPrimaryOnLight: 'rgba(0, 0, 0, 0.87)',
      textSecondaryOnLight: 'rgba(0, 0, 0, 0.54)',
      textHintOnLight: 'rgba(0, 0, 0, 0.38)',
      textDisabledOnLight: 'rgba(0, 0, 0, 0.38)',
      textIconOnLight: 'rgba(0, 0, 0, 0.38)',
      textPrimaryOnDark: 'white',
      textSecondaryOnDark: 'rgba(255, 255, 255, 0.7)',
      textHintOnDark: 'rgba(255, 255, 255, 0.5)',
      textDisabledOnDark: 'rgba(255, 255, 255, 0.5)',
      textIconOnDark: 'rgba(255, 255, 255, 0.5)'
    }}>
      <App />
    </ThemeProvider >