Hopp til hovedinnhold

Hva leter du etter?

Prøv å søke etter…

Komponenter

Suggestion

Suggestion er en søkbar "select" med mulighet for å velge flere alternativer.

Suggestion er under utvikling. Finner du feil eller mangler, meld fra på Github eller Slack.

Bruk

Suggestion baserer seg på open-ui sitt combobox mønster (open-ui.org) og bruker u-combobox fra u-elements (github.io) for å oppfylle grunnleggende funksjonalitet fra combobox mønsteret.

Bruk klassen ds-suggestion på custom elementet <ds-suggestion>. Legg til <input class="ds-input" /> og <u-datalist> med <u-option> som barn for å få en fungerende suggestion.

For et gyldig skjemaelement anbefaler vi å bruke <ds-suggestion> i <ds-field> sammen med en <label>.

CSS variabler og data-attributter

Størrelser styres med data-size og farger med data-color. Komponenten vil arve nærmeste forelder med disse satt.

Suggestion bruker elementer fra u-elements og har derfor mange av de samme custom elementer, data-attributter og CSS variabler. Du kan lese mer om hvordan du bruker disse i u-elements sin u-combobox dokumentasjon.

CSS-variabler
NavnVerdi
--dsc-suggestion-option-background--hovervar(--ds-color-surface-hover)
--dsc-suggestion-option-background--selectedvar(--ds-color-surface-active)
--dsc-suggestion-option-border-radiusvar(--ds-border-radius-md)
--dsc-suggestion-option-checkmark-sizevar(--ds-size-6)
--dsc-suggestion-option-checkmark-urlurl("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24'%3E%3Cpath fill='currentColor' fill-rule='evenodd' d='M18.998 6.94a.75.75 0 0 1 .063 1.058l-8 9a.75.75 0 0 1-1.091.032l-5-5a.75.75 0 1 1 1.06-1.06l4.438 4.437 7.471-8.405A.75.75 0 0 1 19 6.939' clip-rule='evenodd'/%3E%3C/svg%3E")
--dsc-suggestion-option-paddingvar(--ds-size-3)
--dsc-suggestion-option-gapvar(--ds-size-2)
--dsc-suggestion-option-checkmark-bordermax(2px,0.125rem)
--dsc-suggestion-option-checkmark-border-colorvar(--ds-color-neutral-border-default)
--dsc-suggestion-clear-gapvar(--ds-size-2)
--dsc-suggestion-clear-paddingvar(--ds-size-1)
--dsc-suggestion-clear-sizevar(--ds-size-9)
--dsc-suggestion-clear-icon-urlurl("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Cpath fill='currentColor' d='M6.53 5.47a.75.75 0 0 0-1.06 1.06L10.94 12l-5.47 5.47a.75.75 0 1 0 1.06 1.06L12 13.06l5.47 5.47a.75.75 0 1 0 1.06-1.06L13.06 12l5.47-5.47a.75.75 0 0 0-1.06-1.06L12 10.94z'/%3E%3C/svg%3E")
--dsc-suggestion-clear-border-radiusvar(--ds-border-radius-md)
--dsc-suggestion-clear-background--hovervar(--ds-color-surface-hover)
--dsc-suggestion-clear-background--activevar(--ds-color-surface-active)
--dsc-suggestion-list-backgroundvar(--ds-color-neutral-surface-default)
--dsc-suggestion-list-border-radiusvar(--ds-border-radius-md)
--dsc-suggestion-list-box-shadowvar(--ds-shadow-md)
--dsc-suggestion-list-colorvar(--ds-color-text-default)
--dsc-suggestion-list-offsetvar(--ds-size-1)
--dsc-suggestion-list-gapvar(--ds-size-1)
--dsc-suggestion-list-placementbottom
--dsc-suggestion-list-paddingvar(--ds-size-3) var(--ds-size-2)
--dsc-suggestion-border-widthvar(--ds-border-width-default)
--dsc-suggestion-border-stylesolid
--dsc-suggestion-border-colorvar(--ds-color-neutral-border-subtle)
--dsc-suggestion-chip-gapvar(--ds-size-1)
--dsc-suggestion-chip-backgroundvar(--ds-color-base-default)
--dsc-suggestion-chip-background--hovervar(--ds-color-base-hover)
--dsc-suggestion-chip-border-widthvar(--ds-border-width-default)
--dsc-suggestion-chip-heightvar(--ds-size-8)
--dsc-suggestion-chip-font-sizevar(--ds-body-sm-font-size)
--dsc-suggestion-chip-border-stylesolid
--dsc-suggestion-chip-border-colortransparent
--dsc-suggestion-chip-border-radiusvar(--ds-border-radius-full)
--dsc-suggestion-chip-padding0 var(--ds-size-3)
--dsc-suggestion-chip-colorvar(--ds-color-base-contrast-default)
--dsc-suggestion-chip-icon-urlurl("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24'%3E%3Cpath fill='currentColor' d='M6.53 5.47a.75.75 0 0 0-1.06 1.06L10.94 12l-5.47 5.47a.75.75 0 1 0 1.06 1.06L12 13.06l5.47 5.47a.75.75 0 1 0 1.06-1.06L13.06 12l5.47-5.47a.75.75 0 0 0-1.06-1.06L12 10.94z'/%3E%3C/svg%3E")
--dsc-suggestion-chip-icon-sizevar(--ds-size-7)
--dsc-suggestion-chip-input-sizevar(--ds-size-5)
--dsc-suggestion-chip-spacingcalc((var(--dsc-suggestion-chip-height) - var(--dsc-suggestion-chip-input-size))/2)
--dsc-button-sizevar(--dsc-suggestion-clear-size)
Data-attributter
NavnVerdi
data-multiplefalse
data-is-floating
data-overscrollcontain
data-floatingtop, right, bottom, left
data-empty
data-activedescendant

Eksempel

Sjekk ut u-combobox sin dokumentasjon for flere eksempler på hvordan du kan bruke suggestion.

Flervalg

For å tillate at brukeren kan velge flere alternativer, bruk data-multiple på suggestion.

Legge til nye alternativer

Med data-creatable på suggestion kan en bruker opprette nye valgte alternativer ved å skrive inn tekst i input og trykke Enter.

Tøm valget

Legg til en knapp med type="reset" og aria-label for å la brukeren tømme valget.

React

filter
Description

Filter options; boolean or a custom callback. See {@link Filter} for the callback signature.

Type
boolean | Filter
Default
true
creatable
Description

Allows the user to create new items

Type
boolean
Default
false
onBeforeMatch
Description

Callback when matching input value against options

Type
((event: EventBeforeMatch) => void)
name
Description

The name of the associated form control

Type
string
Default
undefined
renderSelected
Description

Change how the selected options are rendered inside the `Chip`.

Type
((args: { label: string; value: string; }) => ReactNode)
Default
({ label }) => label
multiple
Description

Allows the user to select multiple items

Type
boolean
Default
false
selected
Description

The selected item of the Suggestion. If `label` and `value` are the same, each item can be a `string`. Otherwise, each item must be a `SuggestionItem`. Using this makes the component controlled and it must be used in combination with `onSelectedChange`.

Type
string | SuggestionItem | (string | SuggestionItem)[] | null
defaultSelected
Description

Default selected item when uncontrolled

Type
string | SuggestionItem | (string | SuggestionItem)[]
onSelectedChange
Description

Callback when selected items changes

Type
((value: SuggestionItem | null) => void) | ((value: SuggestionItem[]) => void)
NameTypeDefaultDescription
filterboolean | Filtertrue

Filter options; boolean or a custom callback. See {@link Filter} for the callback signature.

creatablebooleanfalse

Allows the user to create new items

onBeforeMatch((event: EventBeforeMatch) => void)-

Callback when matching input value against options

namestringundefined

The name of the associated form control

renderSelected((args: { label: string; value: string; }) => ReactNode)({ label }) => label

Change how the selected options are rendered inside the `Chip`.

multiplebooleanfalse

Allows the user to select multiple items

selectedstring | SuggestionItem | (string | SuggestionItem)[] | null-

The selected item of the Suggestion. If `label` and `value` are the same, each item can be a `string`. Otherwise, each item must be a `SuggestionItem`. Using this makes the component controlled and it must be used in combination with `onSelectedChange`.

defaultSelectedstring | SuggestionItem | (string | SuggestionItem)[]-

Default selected item when uncontrolled

onSelectedChange((value: SuggestionItem | null) => void) | ((value: SuggestionItem[]) => void)-

Callback when selected items changes

SuggestionClear

command
Type
string
commandfor
Type
string
aria-label
Description

Aria label for the clear button

Type
string
Default
Tøm
NameTypeDefaultDescription
commandstring-

-

commandforstring-

-

aria-labelstringTøm

Aria label for the clear button

SuggestionInput

type
Description

Supported `input` types

Type
"number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week"
Default
'text'
disabled
Description

Disables element @note Avoid using if possible for accessibility purposes

Type
boolean
readOnly
Description

Toggle `readOnly`

Type
boolean
size
Description

Defines the width of `Input` in count of characters.

Type
number
NameTypeDefaultDescription
type"number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week"'text'

Supported `input` types

disabledboolean-

Disables element @note Avoid using if possible for accessibility purposes

readOnlyboolean-

Toggle `readOnly`

sizenumber-

Defines the width of `Input` in count of characters.

SuggestionList

singular
Description

The screen reader announcement for singular Suggestion, where %d is the number of Suggestions

Type
string
Default
%d forslag
plural
Description

The screen reader announcement for plural Suggestions, where %d is the number of Suggestions

Type
string
Default
%d forslag
autoPlacement
Description

Whether to enable auto placement.

Type
boolean
Default
true
NameTypeDefaultDescription
singularstring%d forslag

The screen reader announcement for singular Suggestion, where %d is the number of Suggestions

pluralstring%d forslag

The screen reader announcement for plural Suggestions, where %d is the number of Suggestions

autoPlacementbooleantrue

Whether to enable auto placement.

Eksempel på kontrollert flervalg

Har du behov for å kontrollere hvilke alternativer som er valgt, kan du bruke selected og onSelectedChangeSuggestion.

Filter

Filter er som standard på, og filteret ser etter teksten i input. Du kan sende inn ditt eget filter. Under vises et eksempel som skrur av filteret helt.

Rediger denne siden på github.com (åpnes i ny fane)