14.2.2 • Published 13 days ago

@rmwc/select v14.2.2

Weekly downloads
7,311
License
MIT
Repository
github
Last release
13 days ago

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.

<Select label="Standard" options={['Cookies', 'Pizza', 'Icecream']} />
<Select
  label="Outlined"
  outlined
  options={['Cookies', 'Pizza', 'Icecream']}
/>
<Select
  label="Enhanced"
  enhanced
  options={['Cookies', 'Pizza', 'Icecream']}
/>
<Select
  label="Enhanced"
  enhanced={{ renderToPortal: true, anchorCorner: 'topLeft' }}
  options={['Cookies', 'Pizza', 'Icecream']}
/>
<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.

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

Validation

<Select
  label="Required"
  required
  options={['Cookies', 'Pizza', 'Icecream']}
/>
<Select
  label="Invalid"
  invalid
  options={['Cookies', 'Pizza', 'Icecream']}
/>
<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.

function Example() {
  const [value, setValue] = React.useState('Cookies');
  return (
    <Select
      label="Controlled"
      options={['Cookies', 'Pizza', 'Icecream']}
      value={value}
      onChange={(evt) => setValue(evt.currentTarget.value)}
    />
  );
}
<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.

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} />;
}
<Select
  label="Object map"
  options={{ '1': 'Cookies', '2': 'Pizza', '3': 'Icecream' }}
/>
<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.

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

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

        {
          label: 'Icecream',
          value: '3'
        }
      ]
    }
  ]}
/>
<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. | | invalid | boolean | Makes the Select visually invalid. This is sometimes automatically my material-components-web. | | label | string | A label for the form control. | | options | string[] \| FormattedOption[] \| { [value: string]: string } | 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. |

14.2.2

13 days ago

14.2.0

20 days ago

14.2.1

19 days ago

14.1.5

21 days ago

14.1.4

1 month ago

14.1.3

1 month ago

14.1.2

2 months ago

14.1.1

2 months ago

14.1.0

2 months ago

14.0.12

2 months ago

14.0.11

3 months ago

14.0.10

3 months ago

14.0.9

3 months ago

14.0.8

3 months ago

14.0.7

4 months ago

14.0.6

4 months ago

14.0.5

4 months ago

14.0.4

5 months ago

14.0.1-alpha.0

8 months ago

14.0.2-alpha.3

6 months ago

14.0.2-alpha.0

8 months ago

14.0.2-alpha.1

7 months ago

14.0.2-alpha.6

6 months ago

14.0.2-alpha.7

6 months ago

14.0.2-alpha.4

6 months ago

14.0.2-alpha.5

6 months ago

14.0.0

6 months ago

14.0.1

5 months ago

14.0.0-alpha.0

9 months ago

14.0.2

5 months ago

14.0.3

5 months ago

8.0.8

11 months ago

8.0.7

1 year ago

8.0.6

1 year ago

8.0.5

1 year ago

8.0.4

1 year ago

8.0.3

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.7

4 years ago

6.0.6

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.1

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.0

4 years ago

6.0.0-rc.4

4 years ago

6.0.0-rc.3

4 years ago

6.0.0-rc.2

4 years ago

6.0.0-rc.1

4 years ago

6.0.0-rc.0

4 years ago

6.0.0-alpha.16

4 years ago

6.0.0-alpha.14

4 years ago

6.0.0-alpha.15

4 years ago

6.0.0-alpha.13

4 years ago

6.0.0-alpha.12

4 years ago

6.0.0-alpha.11

4 years ago

6.0.0-alpha.9

4 years ago

6.0.0-alpha.10

4 years ago

6.0.0-alpha.8

4 years ago

6.0.0-alpha.7

4 years ago

5.7.2

4 years ago

6.0.0-alpha.6

4 years ago

6.0.0-alpha.3

4 years ago

5.7.1

5 years ago

5.7.0

5 years ago

5.6.0

5 years ago

5.5.2

5 years ago

5.5.1

5 years ago

5.5.0

5 years ago

5.4.3

5 years ago

5.4.2

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.3.1

5 years ago

5.3.0

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.2.0-alpha.1

5 years ago

5.2.0-alpha.0

5 years ago

5.1.8

5 years ago

5.1.7

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.30-rc.0

5 years ago

5.0.29-rc.0

5 years ago

5.0.28-rc.0

5 years ago

5.0.27-rc.0

5 years ago

5.0.26-rc.0

5 years ago

5.0.25-rc.0

5 years ago

5.0.24-rc.0

5 years ago

5.0.23-rc.0

5 years ago

5.0.23-alpha.0

5 years ago

5.0.22-alpha.0

5 years ago

5.0.21-alpha.0

5 years ago

5.0.20-alpha.0

5 years ago

5.0.19-alpha.0

5 years ago

5.0.18-alpha.0

5 years ago

5.0.17-alpha.0

5 years ago

5.0.16-alpha.0

5 years ago

5.0.15-alpha.0

5 years ago

5.0.14-alpha.0

5 years ago

5.0.13-alpha.0

5 years ago

5.0.12-alpha.0

5 years ago

5.0.11-alpha.0

5 years ago

5.0.8-alpha.0

5 years ago

5.0.7-alpha.0

5 years ago

5.0.6-alpha.0

5 years ago

5.0.5-alpha.0

5 years ago

5.0.4-alpha.0

5 years ago

5.0.3-alpha.0

5 years ago

5.0.2-alpha.0

5 years ago

5.0.1-alpha.0

5 years ago

5.0.0-alpha.0

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.0

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-alpha.7

6 years ago

2.0.0-alpha.6

6 years ago

2.0.0-alpha.5

6 years ago

2.0.0-alpha.4

6 years ago

2.0.0-alpha.3

6 years ago

2.0.0-alpha.2

6 years ago

2.0.0-alpha.1

6 years ago

2.0.0-alpha.0

6 years ago

1.10.1-alpha.0

6 years ago

1.10.0-alpha.0

6 years ago