Select

Selects allow for selecting a single option from a list

() => {
const [value, setValue] = React.useState('');
return (
<Select
value={value}
onChange={(e) => setValue(e.target.value)}
label="Number"
>
<MenuItem value={0}>None</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty Thirty</MenuItem>
<MenuItem value={40}>Forty Forty Forty</MenuItem>
</Select>
);
};

Types

NameDescription
SelectChangeEventImport type for the onChange event.

Props

PropTypeDefaultDescription
autoWidthboolfalseIf true, the width of the popover will automatically be set according to the items inside the menu, otherwise it will be at least the width of the select input.
errorboolfalseIf true, the label is displayed in an error state
childrennodeThe option elements to populate the select with. Can be some MenuItem when native is false and option when native is true. ⚠️The MenuItem elements must be direct descendants when native is false.
classesobjectOverride or extend the styles applied to the component. See CSS API below for more details.
defaultValueanyThe default element value. Use when the component is not controlled.
displayEmptyboolfalseIf true, a value is displayed even if no items are selected. In order to display a meaningful value, a function should be passed to the renderValue prop which returns the value to be displayed when no items are selected. You can only use it when the native prop is false (default).
height"short" | "tall"Control how tall or short the component is
IconComponentelementTypeArrowDropDownIconThe icon that displays the arrow.
idstringThe id of the wrapper element or the select element when native.
inputelementAn Input element; does not have to be a material-ui specific Input.
inputPropsobjectAttributes applied to the input element. When native is true, the attributes are applied on the select element.
labelnodeSee OutlinedInput#label
labelIdstringThe ID of an element that acts as an additional label. The Select will be labelled by the additional label and the selected value.
labelWidthnumber0See OutlinedInput#label
MenuPropsobjectProps applied to the Menu element.
multipleboolfalseIf true, value must be an array and the menu will support multiple selections.
nativeboolfalseIf true, the component will be using a native select element.
onChangefuncCallback function fired when a menu item is selected. Signature: function(event: object, child?: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value (any). child: The react element that was selected when native is false (default).
onClosefuncCallback fired when the component requests to be closed. Use in controlled mode (see open). Signature: function(event: object) => void event: The event source of the callback.
onOpenfuncCallback fired when the component requests to be opened. Use in controlled mode (see open). Signature: function(event: object) => void event: The event source of the callback.
openboolControl select open state. You can only use it when the native prop is false (default).
renderValuefuncRender the selected value. You can only use it when the native prop is false (default). Signature: function(value: any) => ReactNode value: The value provided to the component.
SelectDisplayPropsobjectProps applied to the clickable div element.
valueanyThe input value. Providing an empty string will select no options. This prop is required when the native prop is false (default). Set to an empty string '' if you don't want any of the available options to be selected. If the value is an object it must have reference equality with the option in order to be selected. If the value is not an object, the string representation must match with the string representation of the option in order to be selected.

Examples

Error

() => {
const [value, setValue] = React.useState(10);
return (
<Select
value={value}
onChange={(e) => setValue(e.target.value)}
label="Number"
error
helperText="Required"
>
<MenuItem value="">None</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty Thirty</MenuItem>
<MenuItem value={40}>Forty Forty Forty</MenuItem>
</Select>
);
};

MultiSelect

() => {
const [value, setValue] = React.useState([]);
return (
<Select
value={value}
multiple
onChange={(e) => console.log(e.target.value) || setValue(e.target.value)}
renderValue={(value) => value.join(', ')}
label="Number"
>
<MenuItem value="Ten">
<Checkbox checked={value.indexOf('Ten') > -1} />
<ListItemText primary="Ten" />
</MenuItem>
<MenuItem value="Twenty">
<Checkbox checked={value.indexOf('Twenty') > -1} />
<ListItemText primary="Twenty" />
</MenuItem>
<MenuItem value="Thirty">
<Checkbox checked={value.indexOf('Thirty') > -1} />
<ListItemText primary="Thirty" />
</MenuItem>
<MenuItem value="Forty">
<Checkbox checked={value.indexOf('Forty') > -1} />
<ListItemText primary="Forty" />
</MenuItem>
</Select>
);
};

label-less

<Select defaultValue="1">
<MenuItem value="1">One</MenuItem>
<MenuItem value="2">Two</MenuItem>
</Select>

Tall label-less

() => {
const [value, setValue] = React.useState(10);
return (
<Select
height="tall"
value={value}
onChange={(e) => setValue(e.target.value)}
>
<MenuItem value="">None</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty Thirty</MenuItem>
<MenuItem value={40}>Forty Forty Forty</MenuItem>
</Select>
);
};

Read Only

() => {
const [value, setValue] = React.useState(10);
return (
<Select
labelId="demo-simple-select-readonly-label"
id="demo-simple-select-readonly"
value={value}
label="Read Only"
inputProps={{ readOnly: true }}
>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
);
};

Disabled State

() => {
const [value, setValue] = React.useState(10);
return (
<Select
labelId="demo-simple-select-disabled-label"
id="demo-simple-select-disabled"
value={value}
label="Disabled State"
disabled={true}
>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
);
};