Select
React Bootstrap 5 Select
React Select fields components are used for collecting user provided information from a list of options.
Info: This documentation may contain syntax introduced in the MDB5 React 4.0.0 and can be incompatible with previous versions. For old MDBSelect documentation please follow the link.
Note: Read the API tab to find all available options and advanced customization
Basic example
Select fields components are used for collecting user provided information from a list of options.
Basic example of select component that allows you to choose from a number of options.
Multiselect
Add multiple
property to the select element to activate multiple mode.
Select with label
It is possible to add select label by setting the
label
property.
Select with placeholder
Use placeholder
property to set placeholder for select input. The placeholder
will be displayed when input is focused and no option is selected.
Disabled select
Add disabled
attribute to the select component to disable select input.
Disabled options
Use disabled
key on option element to disable specific option.
Custom content
Custom content will be displayed at the end of the select dropdown.
Visible options
Use visibleOptions
property to change the number of options that will be
displayed in the select dropdown without scrolling.
Secondary text
Add secondaryText
key to the specific options to display secondary text.
Search
Set search
property to enable options filtering.
Select with search inside a modal
Select with icons
Add icon
property to the specific options to display the option icon.
Validation
Set validation
option to true
to enable component validation. The
validFeedback
and invalidFeedback
options allow to modify the
validation messages.
Controlled value
Set value
prop to control the component value. The onValueChange
event allows to handle the value change.
Single selection
For a standard single selection, type of the value
should be a number.
Multi selection
For a multiselect, type of the value
should be an array of numbers.
Controlled open state
Set open
property to control the component open state. The onOpen
and
onClose
events allow to handle the open state change.
Auto select
Set autoSelect
option to true
to enable selecting on Tab press.
Select with CSS classes
In order to use select with additional CSS classes you need to place it in the div
wrapper and use CSS classes on that
div. In case you use display classes like .d-lg-none
and select is not visible on page load you need to use code from this example to update label width when select became visible.
Select - API
Import
Properties
MDBSelect
Name | Type | Default | Description | Example |
---|---|---|---|---|
autoSelect
|
boolean | false |
Enables auto selecting on Tab press |
<MDBSelect autoSelect />
|
animationVariants
|
animationVariants?: {
open?: Record
|
{
open: {
opacity: 1,
transform: 'scaleY(1)',
transition: {
duration: 0.2,
},
},
closed: {
opacity: 0,
transform: 'scaleY(0.8)',
transition: {
duration: 0.2,
},
},
}
|
Defines animation variants |
<MDBSelect animationVariants={{
open: { height: 'auto', overflow: 'hidden' },
closed: { height: '0px' }
}} />
|
value
|
Array |
undefined |
Enables controlling selected options. Pass value of the option you want to choose, or array for multiselect. |
<MDBSelect value={multiselect ? [1, 2] : 1} />
|
open
|
boolean | undefined |
Enables controlling the open state.
Typical use case expects handlers on onOpen and onClose events
|
<MDBSelect open={isOpen} onOpen={() => setIsOpen(true)} />
|
clearBtn
|
boolean | false |
Add clear btn to MDBSelect |
<MDBSelect clearBtn />
|
contrast
|
boolean | false |
Adjust input colors for dark backgrounds |
<MDBSelect contrast />
|
data
|
SelectData[] | {} |
Add data to MDBSelect |
<MDBSelect data={[ { text: 'One', value: 1 } ]} />
|
disabled
|
boolean | false |
Disables MDBSelect component. Adds disabled to the input. |
<MDBSelect disabled />
|
displayedLabels
|
number | 5 |
The maximum number of comma-separated list of options displayed on the Multiselect. If a user selects more options than that value, the X options selected text will be displayed instead |
<MDBSelect displayedLabels={3} />
|
inputClassName
|
string | '' |
Add class to MDBSelect input element |
<MDBSelect inputClassName='test' />
|
invalidFeedback
|
string | '' |
Invalid feedback for MDBSelect |
<MDBSelect invalidFeedback='test' />
|
label
|
string | '' |
Add label to select |
<MDBSelect label='test' />
|
noResultsText
|
string | 'No results' |
Change text in option search if there is no records in search value. |
<MDBSelect noResultLabel="test" />
|
optionHeight
|
number | 38 |
Defines a height of the options (in px) |
<MDBSelect visibleOptions="3" />
|
optionsSelectedLabel
|
string | 'options selected' |
The text which is displayed on the Multiselect when there are more than 5 (default) options selected, e.g. 7 options selected |
<MDBSelect optionsSelectedLabel='options checked' />
|
placeholder
|
string | '' |
Add placeholder to MDBSelect |
<MDBSelect placeholder="test" />
|
preventFirstSelection
|
boolean | false |
Prevents selecting first option by default |
<MDBSelect preventFirstSelected />
|
search
|
boolean | false |
Add search to MDBSelect input in dropdown |
<MDBSelect search />
|
searchFn
|
(query: string, data: ExtendedSelectData[]) => ExtendedSelectData[] |
- |
Customize search function |
<MDBSelect search searchFn={(query, data) => data.filter((opt) => opt.value == query)} />
|
searchLabel
|
string | 'Example Label' |
Change label of input |
<MDBSelect searchLabel="test" />
|
selectAll
|
boolean | true |
Displays select all option in multiselect dropdown |
<MDBSelect selectAll={false} />
|
selectAllLabel
|
string | 'Select all' |
Change label to select all option in multiselect |
<MDBSelect selectAllLabel="test" />
|
size
|
'default' | 'lg' | 'sm' | 'default' |
Size of the MDBSelect |
<MDBSelect size='lg' />
|
multiple
|
boolean | false |
Change select to multiselect |
<MDBSelect multiple />
|
validation
|
boolean | false |
Enables validation for the MDBSelect |
<MDBSelect validation />
|
validFeedback
|
string | '' |
Valid feedback for MDBSelect |
<MDBSelect validFeedback='test' />
|
visibleOptions
|
string | number | '5' |
Change visible options in MDBSelect |
<MDBSelect visibleOptions="3" />
|
Advanced types
SelectData
Methods
Name | Type | Default | Description | Example |
---|---|---|---|---|
onChange
|
(data: SelectData[] | SelectData) => void | - |
This method returns a selected item in the MDBSelect or an array of items in multiple |
<MDBSelect onChange={(val) => setSelectValue(val)} />
|
onOpen
|
() => void | - |
Fires when the select demands to be opened. |
<MDBSelect onOpen={() => console.log('open')} />
|
onOpened
|
() => void | - |
Fires after select is opened. |
<MDBSelect onOpened={() => console.log('opened')} />
|
onClose
|
() => void | - |
Fires when the select demands to be closed. |
<MDBSelect onClose={() => console.log('close')} />
|
onClosed
|
() => void | - |
Fires after the select is closed. |
<MDBSelect onClose={() => console.log('closed')} />
|
CSS variables
As part of MDB’s evolving CSS variables approach, select now use local CSS variables for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
Select CSS variables are in different classes which belong to this component. To make it easier to use them, you can find below all of the used CSS variables.