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>

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.

Statiskt och dynamiskt innehåll​

Select använder internt ListBox-komponenten som följer React Arias mönster för Collections. En collection kan vara statisk:

<Select label='Favoritfrukt'>
<ListBoxItem>Banan</ListBoxItem>
<ListBoxItem>Apelsin</ListBoxItem>
<ListBoxItem>Mango</ListBoxItem>
</Select>

eller dynamisk:

import { ListBoxItem, Select } from '@midas-ds/components'

function Example() {
const items = [
{ id: 0, name: 'Banan' },
{ id: 1, name: 'Apelsin' },
{ id: 2, name: 'Mango' },
]

return (
<Select
label='Favoritfrukt'
items={items}
>
{item => <ListBoxItem id={item.id}>{item.name}</ListBoxItem>}
</Select>
)
}

Vänligen läs React Arias dokumentation för ListBox för mer information

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>

Välj alla​

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

Visa etiketter​

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

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>

Anpassa popover​

Det inbyggda popover-fönstret kan konfigureras via popoverProps, som skickas vidare till den underliggande Popover-komponenten. Ett användbart exempel är isNonModal, som gör att ett klick utanför popover-fönstret inte konsumeras — användbart när flera select-komponenter finns sida vid sida och man vill kunna växla direkt mellan dem med ett klick:

<Select
label='Land'
popoverProps={{ isNonModal: true }}
>
...
</Select>

API​

Select​

NameTypeDefaultDescription
descriptionstring-
errorMessage-
errorPositiontop

The position of the error message

isSelectableAll-

Whether to show a button to select all items.

itemsIterable<T>-
labelstring-
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)

popoverProps-

Props passed to the internal Popover element.

listBoxProps-

Props passed to the internal ListBox element.

isDisabled-

Whether the input is disabled.

className'react-aria-Select'

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

namestring-

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

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.

style-

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

renderDOMRenderFunction<"div", SelectRenderProps>-

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.

Requirements:

  • You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>).
  • Only a single root DOM element can be rendered (no fragments).
  • You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
formstring-

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.

disabledKeysIterable<Key>-

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

autoFocus-

Whether the element should receive focus on render.

value-

The current value (controlled).

selectionMode'single'

Whether single or multiple selection is enabled.

isInvalid-

Whether the input value is invalid.

defaultValue-

The default value (uncontrolled).

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.

autoCompletestring-

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

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.

shouldCloseOnSelect-

Whether the Select should close when an item is selected. Defaults to true if selectionMode is single, false otherwise.

allowsEmptyCollection-

Whether the select should be allowed to be open when the collection is empty.

isRequired-

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

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.

placeholderstring'Select an item' (localized)

Temporary text that occupies the select when it is empty.