Demos#
Basic#
const cities = [ { value: 'oslo', title: 'Oslo', description: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', description: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', description: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', description: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', description: 'Capital of Iceland', }, ] render(<Field.MultiSelection label="Select cities" data={cities} />)
With Select all#
This example uses field width medium.
const cities = [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ] render( <Field.MultiSelection label="Select cities" data={cities} showSelectAll width="medium" /> )
With search#
render( <Field.MultiSelection label="Select fruits" data={fruits} showSearchField /> )
With confirm and cancel#
const cities = [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ] render( <Field.MultiSelection label="Select cities" data={cities} showSearchField showSelectAll showConfirmButton /> )
With nested items#
<Field.MultiSelection label="Select regions" showSelectAll data={[ { value: 'scandinavia', title: 'Scandinavia', children: [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, ], }, { value: 'nordic', title: 'Other Nordic', children: [ { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ], }, ]} />
With selected item tags#
const cities = [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ] render( <Field.MultiSelection label="Select cities" data={cities} value={['stockholm']} showSelectedTags /> )
With many selected items#
When showSelectedTags is enabled and the number of items exceeds the selectedItemsCollapsibleThreshold (default: 10), an accordion appears to collapse/expand the selected items. A "Clear all" button also appears to the right for quickly deselecting all items.
const items = Array.from( { length: 30, }, (_, i) => ({ value: `item${i + 1}`, title: `Item ${i + 1}`, description: `Description for item ${i + 1}`, }) ) render( <Field.MultiSelection label="Select items" data={items} value={items.slice(0, 25).map((item) => item.value)} showSelectedTags showSelectAll showSearchField /> )
With disabled items#
<Field.MultiSelection label="Select available languages" data={[ { value: 'nodejs', title: 'Node.js', text: 'JavaScript runtime', }, { value: 'python', title: 'Python', text: 'General purpose language', disabled: true, }, { value: 'rust', title: 'Rust', text: 'Systems programming language', }, { value: 'golang', title: 'Go', text: 'Compiled language', disabled: true, }, { value: 'java', title: 'Java', text: 'Enterprise language', }, ]} />
With search options#
Use the search prop to configure search behavior. This example uses numbers: true so account numbers can be searched by typing digits.
const accounts = [ { value: 'acc1', title: 'Brukskonto', text: '2000 12 34567', }, { value: 'acc2', title: 'Sparekonto', text: '2223 33 44425', }, { value: 'acc3', title: 'BSU', text: '1234 56 78901', }, { value: 'acc4', title: 'Felleskonto', text: '9876 54 32100', }, ] render( <Field.MultiSelection label="Select accounts" data={accounts} showSearchField search={{ numbers: true, }} /> )
Inline variant#
The inline variant renders the item list inline without a popover. This is useful when you want the options to be always visible.
const cities = [ { value: 'oslo', title: 'Oslo', }, { value: 'stockholm', title: 'Stockholm', }, { value: 'copenhagen', title: 'Copenhagen', }, { value: 'helsinki', title: 'Helsinki', }, { value: 'reykjavik', title: 'Reykjavik', }, ] render( <Field.MultiSelection label="Select cities" variant="inline" value={['stockholm']} data={cities} showSelectAll showSearchField showSelectedTags /> )
With a scrollable item list#
Set maxHeight to constrain the inline item list. Only the options scroll, while selected tags and the search field remain visible.
render( <Field.MultiSelection label="Select fruits" variant="inline" data={fruits} value={['apple', 'banana']} maxHeight="16rem" selectedItemsCollapsibleThreshold={20} showSearchField showSelectAll showSelectedTags /> )
Using dataPath from Form context#
<Form.Handler data={{ colors: [ { value: 'red', title: 'Red', text: 'Primary color', }, { value: 'green', title: 'Green', text: 'Secondary color', }, { value: 'blue', title: 'Blue', text: 'Tertiary color', }, { value: 'yellow', title: 'Yellow', text: 'Accent color', }, ], }} > <Field.MultiSelection label="Select colors" dataPath="/colors" value={['red']} help={{ title: 'Help text', content: 'Additional information about this field', }} /> </Form.Handler>
Disabled field#
const cities = [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ] render( <Field.MultiSelection label="Disabled field" disabled data={cities} /> )
With minimum required items#
const cities = [ { value: 'oslo', title: 'Oslo', text: 'Capital of Norway', }, { value: 'stockholm', title: 'Stockholm', text: 'Capital of Sweden', }, { value: 'copenhagen', title: 'Copenhagen', text: 'Capital of Denmark', }, { value: 'helsinki', title: 'Helsinki', text: 'Capital of Finland', }, { value: 'reykjavik', title: 'Reykjavik', text: 'Capital of Iceland', }, ] render( <Form.Handler> <Form.Card> <Field.MultiSelection label="Select cities" path="/cities" required minItems={2} data={cities} showSearchField showSelectedTags /> <Form.SubmitButton /> </Form.Card> </Form.Handler> )
Large data sets#
Virtualization is opt-in. Install the optional peer dependency:
yarn add @tanstack/react-virtual
Import the driver separately so the default Forms bundle does not include the virtualization dependency:
import { Field } from '@dnb/eufemia/extensions/forms'import { createMultiSelectionVirtualization } from '@dnb/eufemia/extensions/forms/Field/MultiSelection/Virtualization'const listDriver = createMultiSelectionVirtualization()<Field.MultiSelection data={items} listDriver={listDriver} />
<Field.MultiSelection label="Select from 10,000 items" data={virtualizedItems} listDriver={virtualizedMultiSelection} showSearchField showSelectAll showConfirmButton />