Hoppa till huvudinnehåll

Select

Flerval, väljare, dropdown, rullgardin

Inmatningsfält som används för att välja ett eller flera fördefinierade alternativ.

import { Select } from '@midas-ds/components'
<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='single'
options={[
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]}
/>
Välj vilken du vill

Riktlinjer

Om det är färre alternativ än fem bör Radio användas istället.

Beskrivning

Midas Select är en variant av React Aria Select med möjlighet till flerval.

Enkelval

Standardvärdet för selectionMode är single, om du inte anger selectionMode kan användaren bara välja ett värde.

Kontrollerat val

Användarens val kan kontrolleras med hjälp av attributet selectedKeys tillsammans med eventet onSelectionChange. Värdet för id skickas tillbaka i callbacken när användaren justerar sitt val, så kan du använda det för att uppdatera ditt state.

För att komponenten också stödjer flerval benämns selectedKeys i plural och värdet för id kommer tillbaka som en Selection. Eftersom att vi vet att alternativet "all" inte kommer ges så kan vi skicka setSelectedFruit direkt till onSelectionChange till skillnad från exemplet för flerval.

import React from 'react'
import { Selection } from 'react-aria-components'

const options = [
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]

const [selectedFruit, setSelectedFruit] = React.useState<Selection>(new Set())

return (
<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
options={options}
selectedKeys={selectedFruit}
onSelectionChange={setSelectedFruit}
/>
)
Välj vilken du vill
Selected fruit: 

Flerval

Använd egenskapen selectionMode="multiple" för att slå på flervalsläget.

<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='multiple'
options={[
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]}
/>
Välj vilken du vill

Välj alla

Egenskapen isSelectableAll kan användas för att lägga till en "Välj alla"-knapp.

<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='multiple'
isSelectableAll
options={[
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]}
/>
Välj vilken du vill

Visa etiketter

Egenskapen showTags kan användas för att visa valen som etiketter under rullgardinen.

<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='multiple'
showTags
options={[
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]}
/>
Välj vilken du vill

Förvalda alternativ (okontrollerat)

Använd egenskapen defaultSelectedKeys för att sätta ett initialt värde till komponenten.

<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='multiple'
defaultSelectedKeys={new Set(['banan', 'dadel'])}
options={[
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]}
/>
Välj vilken du vill
2 valda

Kontrollerade val

Se kontrollerat val vid enkelval för en beskrivning av typerna för selectedKeys och onSelectionChange.

Notera den manuella hantering som görs om användaren klickar på "Välj alla"

import React from 'react'
import { Selection } from 'react-aria-components'

const options = [
{ id: 'apelsin', name: 'Apelsin' },
{ id: 'banan', name: 'Banan' },
{ id: 'citron', name: 'Citron' },
{ id: 'dadel', name: 'Dadel' },
{ id: 'fikon', name: 'Fikon' },
]

const [selectedFruit, setSelectedFruit] = React.useState<Selection>(new Set())

const handleSelectionChange = (keys: Selection) => {
if (keys === 'all') {
return setSelectedFruit(new Set(options.map(({ id }) => id)))
}
return setSelectedFruit(keys)
}

return (
<Select
label='Favoritfrukt'
description='Välj vilken du vill'
placeholder='Välj en frukt'
selectionMode='multiple'
selectedKeys={selectedFruit}
onSelectionChange={handleSelectionChange}
isSelectableAll
options={options}
/>
)
Välj vilken du vill
Selected fruit: 

Sektioner

Vid många val kan alternativen struktureras i sektioner genom att dela in dem enligt följande:

const options = [
{
name: 'Frukter',
children: [
{ id: 'kiwi', name: 'Kiwi' },
{ id: 'banana', name: 'Banan' },
{ id: 'apple', name: 'Äpple' },
],
},
{
name: 'Grönsaker',
children: [
{ id: 'carrot', name: 'Morot' },
{ id: 'broccoli', name: 'Broccoli' },
],
},
]

return (
<Select
label='Favoritfrukt eller grönsak'
description='Välj vilken du vill'
placeholder='Välj en frukt eller grönsak'
selectionMode='multiple'
options={options}
/>
)
Välj vilken du vill

API

Select

NameTypeDefaultDescription
label *string-

The content to display as the label.

isDisabled-

Whether the field is disabled.

autoFocus-

Whether the element should receive focus on render.

classNamestring-

Sets the CSS className for the element.

namestring-

Name of the field, for uncontrolled use

placeholderstring-

Placeholder value

size'large'

Component size (large: height 48px, medium: height 40px)

isRequired-

Whether the field is required.

isInvalid-

The selection is valid or not

validationState-

@deprecated Use isInvalid instead.

validationBehavior'aria'

Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.

validate(value: boolean) => true | ValidationError-

A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.

excludeFromTabOrder-
defaultOpen-

Sets the default open state of the field (uncontrolled).

isClearabletrue

Whether the field can be emptied.

isSelectableAll-

Whether to show a button to select all items.

isOpen-

Sets the open state of the field (controlled).

descriptionstring-

Optional description

showTags-

Show selected items as tags below select

selectionMode'single'

The type of selection that is allowed in the collection.

errorMessagestring-

Error message to be displayed in case of invalid state

errorPosition"top"

The position of the error message

shouldFliptrue

Whether the menu should automatically flip direction when space is limited.

disabledKeysIterable<Key>-

The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.

disallowEmptySelection-

Whether the collection allows empty selection.

selectedKeys-

The currently selected keys in the collection (controlled).

defaultSelectedKeys-

The initial selected keys in the collection (uncontrolled).

isLoading-

Whether the items are currently loading.

options *ListBoxOption[]-

Item objects in the collection.