Select
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.
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>
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>
API
Select
| Name | Type | Default | Description |
|---|---|---|---|
description | string | - | |
errorMessage | string | ((validation: ValidationResult) => string) | - | |
errorPosition | "top" | "bottom" | top | The position of the error message |
isSelectableAll | boolean | - | Whether to show a button to select all items. |
items | Iterable<T> | - | |
label | string | - | |
popover | InfoPopoverProps | - | An assistive text that helps the user understand the field better. Will be hidden in a popover with an info icon button. |
showTags | boolean | - | Show selected items as tags below select |
size | Size | large | Component size (large: height 48px, medium: height 40px) |
validationBehavior | "native" | "aria" | '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 | boolean | - | Whether the input is disabled. |
autoComplete | string | - | Describes the type of autocomplete functionality the input should provide if any. See MDN. |
name | string | - | The name of the input, used when submitting an HTML form. |
form | string | - | The |
selectionMode | SelectionMode | 'single' | Whether single or multiple selection is enabled. |
selectedKey | Key | null | - | The currently selected key in the collection (controlled). @deprecated |
defaultSelectedKey | Key | - | The initial selected key in the collection (uncontrolled). @deprecated |
isOpen | boolean | - | Sets the open state of the menu. |
defaultOpen | boolean | - | Sets the default open state of the menu. |
disabledKeys | Iterable<Key> | - | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
value | Key | Key[] | null | - | The current value (controlled). |
defaultValue | Key | Key[] | null | - | The default value (uncontrolled). |
isRequired | boolean | - | Whether user input is required on the input before form submission. |
isInvalid | boolean | - | Whether the input value is invalid. |
validate | ((value: ValidationType<M>) => true | ValidationError | null) | undefined | - | 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 |
placeholder | string | 'Select an item' (localized) | Temporary text that occupies the select when it is empty. |
autoFocus | boolean | - | Whether the element should receive focus on render. |
className | ClassNameOrFunction<SelectRenderProps> | - | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | StyleOrFunction<SelectRenderProps> | - | The inline style for the element. A function may be provided to compute the style based on component state. |
slot | string | null | - | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |