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, ListBoxItem } from '@midas-ds/components'
<Select label='Favoritfrukt'>
<ListBoxItem>Banan</ListBoxItem>
<ListBoxItem>Apelsin</ListBoxItem>
<ListBoxItem>Mango</ListBoxItem>
</Select>
Select an item

Beskrivning

Midas Select är en implementation av React Aria Components Select. Den här dokumentationen innehåller främst enklare exempel och användningsfall. För mer avancerad funktionalitet och fördjupad information, se den officiella dokumentationen.

Från och med v15.0.0 har Select uppdaterats för att överensstämma bättre med React Arias API. Det innebär vissa breaking changes. Under en övergångsperiod går det att använda den gamla versionen av komponenten, som nu heter LegacySelect och kan importeras enligt nedan.


import {LegacySelect} from '@midas-ds/components'

Flerval / Enkelval

Växla mellan flerval och enkelval med selectionMode='single|multiple'. Standardvärdet för selectionMode är single.

<Select
label='Favoritfrukt'
selectionMode='multiple'
>
<ListBoxItem>Banan</ListBoxItem>
<ListBoxItem>Apelsin</ListBoxItem>
<ListBoxItem>Mango</ListBoxItem>
</Select>
Select an item

Välj alla

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

Select an item

Visa etiketter

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

Select an item

Sektioner

Vid många val kan alternativen struktureras i sektioner.

<Select label={'Sectioned select'}>
<ListBoxSection>
<ListBoxHeader>Fruit</ListBoxHeader>
<ListBoxItem id='Apple'>Apple</ListBoxItem>
<ListBoxItem id='Banana'>Banana</ListBoxItem>
</ListBoxSection>
<ListBoxSection>
<ListBoxHeader>Vegetables</ListBoxHeader>
<ListBoxItem id='Cabbage'>Cabbage</ListBoxItem>
<ListBoxItem id='Broccoli'>Broccoli</ListBoxItem>
</ListBoxSection>
</Select>
Select an item

API

Select

NameTypeDefaultDescription
description-
errorMessage-
errorPositiontop

The position of the error message

isSelectableAll-

Whether to show a button to select all items.

items-
label-
popover-

An assistive text that helps the user understand the field better. Will be hidden in a popover with an info icon button.

showTags-

Show selected items as tags below select

sizelarge

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

validationBehavior'native'

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.

isDisabled-

Whether the input is disabled.

autoComplete-

Describes the type of autocomplete functionality the input should provide if any. See MDN.

name-

The name of the input, used when submitting an HTML form.

form-

The <form> element to associate the input with. The value of this attribute must be the id of a <form> in the same document. See MDN.

selectionMode'single'

Whether single or multiple selection is enabled.

selectedKey-

The currently selected key in the collection (controlled). @deprecated

defaultSelectedKey-

The initial selected key in the collection (uncontrolled). @deprecated

isOpen-

Sets the open state of the menu.

defaultOpen-

Sets the default open state of the menu.

disabledKeys-

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

value-

The current value (controlled).

defaultValue-

The default value (uncontrolled).

isRequired-

Whether user input is required on the input before form submission.

isInvalid-

Whether the input value is invalid.

validate-

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.

placeholder'Select an item' (localized)

Temporary text that occupies the select when it is empty.

autoFocus-

Whether the element should receive focus on render.

className-

The CSS className for the element. A function may be provided to compute the class based on component state.

style-

The inline style for the element. A function may be provided to compute the style based on component state.

slot-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.