Dropdown
Dropdown for selecting an item from a larger set
Dropdown with state management
Dropdown is a controlled component. This means that the selected option, if any, must be provided as the value prop. The onChange function updates the value when the user selects a new option.
In this example, notice how value is stored within a React.useState object. The onChange function updates the state when the user selects a new option.
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-1">Select a state</Label> <Dropdown id="example-1" size="large" value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Dropdown sizes
Large
This is the default size for Dropdown.
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-2">Select a state</Label> <Dropdown id="example-2" size="large" value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Small
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-3">Select a state</Label> <Dropdown id="example-3" size="small" value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Widths
By default, the Dropdown component renders as an inline-block element. Its width is determined by the option with the longest text.
The isFullWidth prop can be used to set the width to 100%.
Full width
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-4">Select a state</Label> <Dropdown id="example-4" isFullWidth value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Disabled selects
The isDisabled prop visually and functionally disables the Dropdown.
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-5">Select a state</Label> <Dropdown id="example-5" isDisabled value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Dropdown with an error
The hasError prop can be used to visually represent an error.
This prop only changes the select dropdown’s color. It should be used alongside an error message that helps users advance through the form.
function DropdownExample() { const [value, setValue] = React.useState('ca'); return ( <div> <Label for="example-6">Select a state</Label> <Dropdown id="example-6" hasError value={value} onChange={v => setValue(v)}> <option value="ny">New York</option> <option value="ca">California</option> <option value="tn">Tennessee</option> <option value="fl">Florida</option> </Dropdown> </div> ); }
Usage with TypeScript
The Dropdown can be passed a TypeScript type for the value prop.
This is useful when you are using Dropdown to select between values of an Enum. This same type will be received by the onChange prop.
enum State {CA,NY,TN,FL,}function DropdownExample() {const [value, setValue] = React.useState<State>(State.CA);return (<div><Label for="example-7">Select a state</Label><Dropdown<State> id="example-7" hasError value={value} onChange={v => setValue(v)}><option value={State.NY}>New York</option><option value={State.CA}>California</option><option value={State.TN}>Tennessee</option><option value={State.FL}>Florida</option></Dropdown></div>);}
Props
Dropdown
valuerequiredThe
valueof the<option>that is selected. See React documentation on<select>and controlled components to learn more.TypeTonChangerequiredA function that is fired when the value of the select changes. The new
valueis passed to the function.Type(value: T, event: React.ChangeEvent<HTMLSelectElement>) => voidchildrenA collection of HTML
<option>elements.TypeReact.ReactNodeidAdds a HTML
idattribute to the select. This is used for linking the HTML with a Label.TypestringisDisabledVisually and functionally disables the select dropdown.
TypebooleanDefaultfalseisRequiredAdds
requiredHTML attribute to element, indicating that an option with a non-empty string value must be selected.TypebooleanDefaultfalsehasErrorMakes the radio and text color red.
TypebooleanDefaultfalsesizeChanges the select’s font-size, height, and padding.
Type'small' | 'large'Default'large'isFullWidthSet the
<select>'s width to100%as opposed to the default behavior of only taking up the necessary width.TypebooleanDefaultfalseonClickFunction that is fired when the value of the select changes.
Type(event: React.MouseEvent<HTMLSelectElement, MouseEvent>) => voidDefault(): void => {}onFocusFires when the select receives focus.
Type(event: React.FocusEvent<HTMLSelectElement>) => voidonBlurFires when the select loses focus.
Type(event: React.FocusEvent<HTMLSelectElement>) => voiddataTestIdA selector hook into the React component for use in automated testing environments.
TypestringdataTestdeprecatedA selector hook into the React component for use in automated testing environments.
Note:Deprecated in favor of the
dataTestIdpropTypestringnameAdds
nameHTML attribute to element, indicating the property name associated with the selected value.TypestringaccessibilityLabelThis adds an
aria-labelto the element. It should only be used in cases where the<Dropdown>doesn't have or can't be associated with a related<label>. Learn more about the importance of using labels.Typestring