React Multi Select Component
Multi Select
Customize the native <select>s with a powerful React Multi Select component that changes the element’s initial appearance and brings some new functionalities.
Other frameworks
CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue components. To learn more please visit the following pages.
Basic example#
A clear demonstration of implementing a basic React Multi Select dropdown, highlighting essential options and configurations.
const options = [ { value: 0, label: 'Angular', }, { value: 1, label: 'Bootstrap', }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} label="Framework" text="Please select your framework." />
const options = [ { value: 0, label: 'Angular', }, { value: 1, label: 'Bootstrap', }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} />
Search#
You can configure the search functionality within the component. The search
property determines how the search input element is enabled and behaves. It accepts multiple types to provide flexibility in configuring search behavior. By default is set to true
.
Disable search#
To disable the default search input element, please set search
property to false
like in the example below:
const options = [ { value: 0, label: 'Angular', }, { value: 1, label: 'Bootstrap', }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} label="Framework" text="Please select your framework." search={false}/>
Global search#
Added in v5.8.0To enable the global search functionality within the React Multi Select component, please set search
property to 'global'
. When global search is turned on, the user can perform searches across the entire component, regardless of where their focus is within the component. This allows for a more flexible and intuitive search experience, ensuring the search input is recognized from any point within the component.
const options = [ { value: 0, label: 'Angular', }, { value: 1, label: 'Bootstrap', }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} label="Framework" text="Please select your framework." search="global" />
External search#
When external search is enabled (search="external"
), the component delegates search operations to an external API. This setup is ideal for scenarios where third-party services or custom backend implementations power search capabilities.
<CMultiSelect options={...} onFilterChange={...} search="external" />
If you want to enable both external (API-handled) and global search:
<CMultiSelect options={...} onFilterChange={...} search={{ external: true, global: true }} />
Please check the External Data section to check a working example of how to handle external data.
Modes#
Allow create options#
The allowCreateOptions
property in the <CMultiselect />
component allows users to create new options in addition to selecting pre-existing ones from a list.
When this property is set to true, the user can type in a new option in the search input field of the multiselect component, and if the option does not already exist in the list, it will be created and added to the list of available options. This can be useful when the list of available options is not comprehensive or when the user needs to select an option that is not already available.
It's worth noting that this feature may not always be appropriate, depending on the specific use case of the multiselect component. In some cases, it may be preferable to restrict the user to selecting only pre-existing options, while in other cases, allowing for the creation of new options may be essential. It's important to consider the specific requirements of your application when deciding whether to enable this feature.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect allowCreateOptions options={options} />
Clear search on select#
The clearSearchOnSelect
property is a Boolean attribute that can be used with the MultiSelect component in the CoreUI React library.
When clearSearchOnSelect
is set to true
, the search input field in the MultiSelect component will be cleared as soon as the user selects an option from the dropdown list. This means that the search query will be reset and the user will be able to start a new search immediately.
By default, clearSearchOnSelect
is set to false
, which means that the search input field will retain the user's search query even after an option has been selected. This can be useful in situations where the user needs to select multiple options from the dropdown list that match the same search query.
To use the clearSearchOnSelect
property with the MultiSelect component in the CoreUI React library, you simply need to set it to true
or false
as appropriate in your code. For example:
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect clearSearchOnSelect options={options} />
In this example, the clearSearchOnSelect
property is set to true
, which means that the search input field will be cleared as soon as the user selects an option from the dropdown list.
Selection types#
The React Multi Select component enables users to present selected items in multiple ways. Users can choose to display selected items as a counter, tags, or a list of comma-separated values.
To change the way that selected items are displayed, you can use the selectionType
property to set the presentation type.
Tags#
By default, the selectionType
property is set to "tags"
, which displays selected options as tags. Users can remove a selected option by pressing the "X" button.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} selectionType="tags" />
Counter#
In the example below, selected items from the list are counted and presented in the form of a counter. To use this option, set the selectionType
to "counter"
.
To further customize the counter, the selectionTypeCounterText
property allows you to set the text that is displayed after the counter.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} selectionType="counter" />
Text#
To display the list of selected options as comma-separated text values, set the selectionType
property to "text"
.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect options={options} selectionType="text" />
Single Selection#
Set the multiple
boolean property to false
and allow select only one element.
const options = [ { value: 0, label: 'Angular', }, { value: 1, label: 'Bootstrap', disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect multiple={false} options={options} />
Custom options and options group labels#
The CoreUI React Multi Select component provides the flexibility to personalize options and group labels by utilizing custom templates. You can easily customize the options using the optionsTemplate
, and for groups, you can use optionsGroupsTemplate
, as demonstrated in the examples below:
import { cifPl, cifDe, cifUs, cifEs, cifGb } from '@coreui/icons'import CIcon from '@coreui/icons-react'
const flags = { de: cifDe, es: cifEs, gb: cifGb, pl: cifPl, us: cifUs}
const countries = [ { value: 'pl', label: 'Poland', }, { value: 'de', label: 'Germany', }, { value: 'us', label: 'United States', }, { value: 'es', label: 'Spain', }, { value: 'gb', label: 'United Kingdom', },]
const cities = [ { label: 'United States', code: 'us', options: [ { value: 'au', label: 'Austin', }, { value: 'ch', label: 'Chicago', }, { value: 'la', label: 'Los Angeles', }, { value: 'ny', label: 'New York', }, { value: 'sa', label: 'San Jose', }, ], }, { label: 'United Kingdom', code: 'gb', options: [ { value: 'li', label: 'Liverpool', }, { value: 'lo', label: 'London', }, { value: 'ma', label: 'Manchester', }, ], },]
return ( <CRow> <CCol md={6}> <CMultiSelect label="Select country" options={countries} optionsStyle="text" optionsTemplate={ (option) => ( <div className="d-flex"> <CIcon className="me-3" icon={flags[option.value]} size="xl"/> {option.label} </div> ) } /> </CCol> <CCol md={6}> <CMultiSelect label="Select city" options={cities} optionsGroupsTemplate={ (option) => ( <div className="d-flex align-items-center"> <CIcon className="me-3" icon={flags[option.code]} size="xl"/> {option.label} </div> ) } /> </CCol> </CRow>)
Coordinated selection#
The selection of React select components can be coordinated by dynamically updating city options based on the selected country. Use the resetSelectionOnOptionsChange
prop on the city select component to reset the selected city whenever the country changes, ensuring synchronized selections.
import { cifPl, cifDe, cifUs, cifEs, cifGb } from '@coreui/icons'import CIcon from '@coreui/icons-react'
const [selectedCountry, setSelectedCountry] = useState()const [selectedCity, setSelectedCity] = useState()const [cities, setCities] = useState([])
const countries = [ { value: 'pl', label: 'Poland', cities: [ { value: 'warszawa', label: 'Warszawa' }, { value: 'krakow', label: 'Kraków' }, { value: 'lodz', label: 'Łódź' }, { value: 'wroclaw', label: 'Wrocław' }, { value: 'poznan', label: 'Poznań' } ] }, { value: 'de', label: 'Germany', cities: [ { value: 'berlin', label: 'Berlin' }, { value: 'hamburg', label: 'Hamburg' }, { value: 'munich', label: 'Munich' }, { value: 'cologne', label: 'Cologne' }, { value: 'frankfurt', label: 'Frankfurt' } ] }, { value: 'us', label: 'United States', cities: [ { value: 'new_york', label: 'New York' }, { value: 'los_angeles', label: 'Los Angeles' }, { value: 'chicago', label: 'Chicago' }, { value: 'houston', label: 'Houston' }, { value: 'phoenix', label: 'Phoenix' } ] }, { value: 'es', label: 'Spain', cities: [ { value: 'madrid', label: 'Madrid' }, { value: 'barcelona', label: 'Barcelona' }, { value: 'valencia', label: 'Valencia' }, { value: 'seville', label: 'Seville' }, { value: 'zaragoza', label: 'Zaragoza' } ] }, { value: 'gb', label: 'United Kingdom', cities: [ { value: 'london', label: 'London' }, { value: 'birmingham', label: 'Birmingham' }, { value: 'manchester', label: 'Manchester' }, { value: 'glasgow', label: 'Glasgow' }, { value: 'liverpool', label: 'Liverpool' } ] }]
const flags = { de: cifDe, es: cifEs, gb: cifGb, pl: cifPl, us: cifUs}
return ( <CRow> <CCol md={6}> <CMultiSelect label="Select country" multiple={false} onChange={(country) => { setSelectedCountry(country.length ? country[0].value : undefined) setCities(country.length ? country[0].cities : []) }} options={countries} optionsStyle="text" optionsTemplate={ (option) => ( <div className="d-flex"><CIcon className="me-3" icon={flags[option.value]} size="xl"/> {option.label}</div> ) } /> </CCol> <CCol md={6}> <CMultiSelect label="Select city" multiple={false} onChange={(city) => setSelectedCity(city.value)} options={cities} optionsStyle="text" resetSelectionOnOptionsChange /> </CCol> </CRow>)
Disabled#
Add the disabled
boolean property to give it a grayed out appearance, remove pointer events, and prevent focusing.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', selected: true, disabled: true, }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', selected: true, }, { value: 6, label: 'Node.js', }, ], },]return <CMultiSelect disabled options={options} />
Sizing#
You may also choose from small and large multi selects to match our similarly sized text inputs.
const options = [ { value: 0, label: 'Angular', selected: true, }, { value: 1, label: 'Bootstrap', }, { value: 2, label: 'React.js', }, { value: 3, label: 'Vue.js', }, { label: 'backend', options: [ { value: 4, label: 'Django', }, { value: 5, label: 'Laravel', }, { value: 6, label: 'Node.js', }, ], },]return ( <CRow> <CCol md={6} className="mb-3"> <CMultiSelect options={options} selectionType="counter" size="lg" /> </CCol> <CCol md={6} className="mb-3"> <CMultiSelect options={options} size="lg" /> </CCol> <CCol md={6}> <CMultiSelect options={options} selectionType="counter" size="sm" /> </CCol> <CCol md={6}> <CMultiSelect options={options} size="sm" /> </CCol> </CRow>)
Virtual Scroller (1000 records)#
To enhance performance and manage large amounts of data in React Multi Select, you can generate a list of options using <CVirtualScroller />
. This component renders only the items currently visible on the screen, and as the user scrolls, new items are dynamically loaded and added to the view. This helps to reduce the data load and processing time, resulting in a more responsive UI.
To enable Virtual Scroller, simply set the virtualScroller
boolean property to true
, as shown in the example below.
const users = [ { value: "1", label: "Nanon", }, ... { value: "1000", label: "Jasper", },]return ( <CMultiSelect options={users} label="Users" text="Please select your user." virtualScroller />)
External Data#
One of the key features of React Multi Select component is the ability to load data from an external source, such as an API or a server-side script. This can be useful if you have a large amount of data that you don't want to load all at once. To load external data into a React MultiSelect, you can use the Fetch API to the data source.
Here is an example of how you might use React Multi Select with external data:
const [loading, setLoading] = useState(false)const [users, setUsers] = useState([])
const getUsers = async (name = "") => { setLoading(true) try { const response = await fetch(`https://apitest.coreui.io/demos/users?first_name=${name}`) const result = await response.json()
setUsers(result.records.map(record => ({ value: record.id, label: record.first_name, }))) } catch (error) { console.error('Error fetching users:', error) setUsers([]) // Optionally handle an error state } finally { setLoading(false) }}
// Use the debounced callback for API callsconst debouncedGetUsers = useDebouncedCallback((value) => { getUsers(value)}, 300)
return ( <CMultiSelect label="Users" loading={loading} onFilterChange={debouncedGetUsers} // Debounce the actual API call // onFilterChange={(value) => getUsers(value)} // API call without debounce onShow={getUsers} // Triggers the getUsers function when the dropdown is shown options={users} text="Please select your user." search={{ external: true, global: