Components
Suggestion
Suggestion is a searchable "select" with support for multiple selections.
Suggestion is under development. If you find any errors or bugs, please report them on Github or Slack.
<ds-field class="ds-field">
<label class="ds-label">Select a destination</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">No results found</u-option>
<u-option label="Sogndal" value="Sogndal">
Sogndal
<div>Municipality</div>
</u-option>
<u-option label="Oslo" value="Oslo">
Oslo
<div>Municipality</div>
</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">
Brønnøysund
<div>Municipality</div>
</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const PreviewEn = () => { const DATA_PLACES = ['Sogndal', 'Oslo', 'Brønnøysund']; return ( <Field> <Label>Select a destination</Label> <EXPERIMENTAL_Suggestion> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> No results found </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place} label={place} value={place} > {place} <div>Municipality</div> </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<PreviewEn />)
Usage
Suggestion is based on open-ui's combobox pattern (open-ui.org) and uses u-combobox from u-elements (github.io) to provide core combobox functionality.
Use the class ds-suggestion on the custom element <ds-suggestion>.
Add <input class="ds-input" /> and <u-datalist> with <u-option> children to get a working suggestion.
<ds-suggestion class="ds-suggestion">
<input class="ds-input" />
<u-datalist>
<u-option label="Sogndal" value="Sogndal">Sogndal</u-option>
<u-option label="Oslo" value="Oslo">Oslo</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">Brønnøysund</u-option>
</u-datalist>
</ds-suggestion>
For a valid form control, we recommend using <ds-suggestion> inside <ds-field> together with a <label>.
<ds-field>
<label class="ds-label">Suggestion</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" />
<button type="reset" aria-label="Clear" />
<u-datalist>
<u-option label="Sogndal" value="Sogndal">Sogndal</u-option>
<u-option label="Oslo" value="Oslo">Oslo</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">Brønnøysund</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
Suggestion uses elements from u-elements and therefore supports many of the same custom elements, data attributes, and CSS variables.
You can read more about how to use these in the u-elements u-combobox documentation.
| Name | Value |
|---|---|
| --dsc-button-size | var(--dsc-suggestion-clear-size) |
| --dsc-search-clear-icon-url | var(--dsc-suggestion-clear-icon-url) |
| --dsc-search-clear-padding | var(--dsc-suggestion-clear-padding) |
| --dsc-search-clear-size | var(--dsc-suggestion-clear-size) |
| --dsc-search-magnifying-icon-size | var(--dsc-suggestion-search-icon-size) |
| --dsc-search-magnifying-icon-url | var(--dsc-suggestion-search-icon-url) |
| --dsc-search-padding-inline | var(--dsc-suggestion-clear-gap) |
| --dsc-suggestion-border-color | var(--ds-color-neutral-border-subtle) |
| --dsc-suggestion-border-style | solid |
| --dsc-suggestion-border-width | var(--ds-border-width-default) |
| --dsc-suggestion-chip-background | var(--ds-color-base-default) |
| --dsc-suggestion-chip-background--hover | var(--ds-color-base-hover) |
| --dsc-suggestion-chip-border-color | transparent |
| --dsc-suggestion-chip-border-radius | var(--ds-border-radius-full) |
| --dsc-suggestion-chip-border-style | solid |
| --dsc-suggestion-chip-border-width | var(--ds-border-width-default) |
| --dsc-suggestion-chip-color | var(--ds-color-base-contrast-default) |
| --dsc-suggestion-chip-font-size | var(--ds-body-sm-font-size) |
| --dsc-suggestion-chip-gap | var(--ds-size-1) |
| --dsc-suggestion-chip-height | var(--ds-size-8) |
| --dsc-suggestion-chip-icon-size | var(--ds-icon-size) |
| --dsc-suggestion-chip-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24' focusable='false' role='img'%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/path%3E%3C/svg%3E") |
| --dsc-suggestion-chip-input-size | var(--ds-size-5) |
| --dsc-suggestion-chip-padding | 0 var(--ds-size-3) |
| --dsc-suggestion-chip-spacing | calc(.5 * (var(--dsc-suggestion-chip-height) - var(--dsc-suggestion-chip-input-size))) |
| --dsc-suggestion-clear-background--active | var(--ds-color-surface-active) |
| --dsc-suggestion-clear-background--hover | var(--ds-color-surface-hover) |
| --dsc-suggestion-clear-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-clear-gap | var(--ds-size-2) |
| --dsc-suggestion-clear-icon-url | url("data:image/svg+xml,%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-padding | var(--ds-size-1) |
| --dsc-suggestion-clear-size | var(--ds-size-9) |
| --dsc-suggestion-create-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M12.75 5.5a.75.75 0 0 0-1.5 0v5.75H5.5a.75.75 0 0 0 0 1.5h5.75v5.75a.75.75 0 0 0 1.5 0v-5.75h5.75a.75.75 0 0 0 0-1.5h-5.75z'/%3E%3C/svg%3E") |
| --dsc-suggestion-create-text | 'Legg til "{value}"' |
| --dsc-suggestion-empty-text | "Tomt" |
| --dsc-suggestion-list-background | var(--ds-color-neutral-surface-default) |
| --dsc-suggestion-list-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-list-box-shadow | var(--ds-shadow-md) |
| --dsc-suggestion-list-color | var(--ds-color-text-default) |
| --dsc-suggestion-list-gap | var(--ds-size-1) |
| --dsc-suggestion-list-offset | var(--ds-size-1) |
| --dsc-suggestion-list-padding | var(--ds-size-3) var(--ds-size-2) |
| --dsc-suggestion-list-placement | bottom |
| --dsc-suggestion-option-background--hover | var(--ds-color-surface-hover) |
| --dsc-suggestion-option-background--selected | var(--ds-color-surface-active) |
| --dsc-suggestion-option-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-option-checkmark-border | calc(max(2px, .125rem)) |
| --dsc-suggestion-option-checkmark-border-color | var(--ds-color-neutral-border-default) |
| --dsc-suggestion-option-checkmark-size | var(--ds-size-6) |
| --dsc-suggestion-option-checkmark-url | url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20fill%3D%22none%22%20viewBox%3D%220%200%2024%2024%22%20focusable%3D%22false%22%20role%3D%22img%22%3E%3Cpath%20fill%3D%22currentColor%22%20fill-rule%3D%22evenodd%22%20d%3D%22M18.998%206.94a.75.75%200%200%201%20.063%201.058l-8%209a.75.75%200%200%201-1.091.032l-5-5a.75.75%200%201%201%201.06-1.06l4.438%204.437%207.471-8.405A.75.75%200%200%201%2019%206.939%22%20clip-rule%3D%22evenodd%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E") |
| --dsc-suggestion-option-gap | var(--ds-size-2) |
| --dsc-suggestion-option-padding | var(--ds-size-3) |
| --dsc-suggestion-search-icon-size | var(--ds-icon-size) |
| --dsc-suggestion-search-icon-spacing | var(--ds-size-3) |
| --dsc-suggestion-search-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Cpath d='M10.5 3.25a7.25 7.25 0 1 0 4.57 12.88l5.41 5.41a.75.75 0 1 0 1.06-1.06l-5.41-5.41A7.25 7.25 0 0 0 10.5 3.25M4.75 10.5a5.75 5.75 0 1 1 11.5 0 5.75 5.75 0 0 1-11.5 0'/%3E%3C/svg%3E") |
| --dsc-suggestion-sr-added | "La til" |
| --dsc-suggestion-sr-clear | "Fjern tekst" |
| --dsc-suggestion-sr-empty | "Ingen valgte" |
| --dsc-suggestion-sr-found | "Naviger til venstre for å finne %d valgte" |
| --dsc-suggestion-sr-invalid | "Ugyldig valg" |
| --dsc-suggestion-sr-items | "Valgte" |
| --dsc-suggestion-sr-of | "av" |
| --dsc-suggestion-sr-plural | "%d forslag" |
| --dsc-suggestion-sr-remove | "Trykk for å fjerne" |
| --dsc-suggestion-sr-removed | "Fjernet" |
| --dsc-suggestion-sr-singular | "%d forslag" |
| --dsc-suggestion-sr-toggle | "Forslag" |
| --dsc-suggestion-toggle-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5.97 9.47a.75.75 0 0 1 1.06 0L12 14.44l4.97-4.97a.75.75 0 1 1 1.06 1.06l-5.5 5.5a.75.75 0 0 1-1.06 0l-5.5-5.5a.75.75 0 0 1 0-1.06'/%3E%3C/svg%3E") |
| Name | Value |
|---|---|
| data-creatable | |
| data-empty | |
| data-multiple | |
| data-overscroll | contain |
Examples
Check the u-combobox documentation for more examples of how you can use suggestion.
Multiple choice
To allow users to select multiple options, use data-multiple on suggestion.
<ds-field class="ds-field">
<label class="ds-label">Select a destination</label>
<ds-suggestion data-multiple="" class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">No results found</u-option>
<u-option>Sogndal</u-option>
<u-option>Oslo</u-option>
<u-option>Brønnøysund</u-option>
<u-option>Stavanger</u-option>
<u-option>Trondheim</u-option>
<u-option>Bergen</u-option>
<u-option>Lillestrøm</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const MultipleEn = () => { const DATA_PLACES = [ 'Sogndal', 'Oslo', 'Brønnøysund', 'Stavanger', 'Trondheim', 'Bergen', 'Lillestrøm', ]; return ( <Field> <Label>Select a destination</Label> <EXPERIMENTAL_Suggestion multiple> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> No results found </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place}> {place} </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<MultipleEn />)
Adding new options
With data-creatable on suggestion, a user can create new selected options by entering text in the input and pressing Enter.
<ds-field class="ds-field">
<label class="ds-label">Select destination</label>
<ds-suggestion
data-multiple=""
data-creatable=""
class="ds-suggestion"
>
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="Add "%s""></u-option>
<u-option>Sogndal</u-option>
<u-option>Oslo</u-option>
<u-option>Brønnøysund</u-option>
<u-option>Stavanger</u-option>
<u-option>Trondheim</u-option>
<u-option>Bergen</u-option>
<u-option>Lillestrøm</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const CreatableEn = () => { const DATA_PLACES = [ 'Sogndal', 'Oslo', 'Brønnøysund', 'Stavanger', 'Trondheim', 'Bergen', 'Lillestrøm', ]; return ( <Field> <Label>Select destination</Label> <EXPERIMENTAL_Suggestion creatable multiple> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty data-empty='Add "%s"' /> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place}> {place} </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<CreatableEn />)
Clear selection
Add a button with type="reset" and aria-label to allow users to clear the selection.
<ds-field class="ds-field">
<label class="ds-label">Select a destination</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">No results found</u-option>
<u-option label="Sogndal" value="Sogndal">
Sogndal
<div>Municipality</div>
</u-option>
<u-option label="Oslo" value="Oslo">
Oslo
<div>Municipality</div>
</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">
Brønnøysund
<div>Municipality</div>
</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const PreviewEn = () => { const DATA_PLACES = ['Sogndal', 'Oslo', 'Brønnøysund']; return ( <Field> <Label>Select a destination</Label> <EXPERIMENTAL_Suggestion> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> No results found </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place} label={place} value={place} > {place} <div>Municipality</div> </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<PreviewEn />)
Toggle list
Add a button with type="button" aria-expanded="false" and aria-label to allow users to toggle the list.
<ds-field class="ds-field">
<label class="ds-label">Select a destination</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">No results found</u-option>
<u-option label="Sogndal" value="Sogndal">
Sogndal
<div>Municipality</div>
</u-option>
<u-option label="Oslo" value="Oslo">
Oslo
<div>Municipality</div>
</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">
Brønnøysund
<div>Municipality</div>
</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const PreviewEn = () => { const DATA_PLACES = ['Sogndal', 'Oslo', 'Brønnøysund']; return ( <Field> <Label>Select a destination</Label> <EXPERIMENTAL_Suggestion> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> No results found </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place} label={place} value={place} > {place} <div>Municipality</div> </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<PreviewEn />)
With external data
<ds-field class="ds-field" lang="en">
<label class="ds-label">Search for recipes (in English)</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="search" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">Search for recipes</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const FetchExternal = () => { const empty = 'Search for recipes'; const [options, setOptions] = useState<string[] | string>(empty); // Store results const timer = useRef<ReturnType<typeof setTimeout> | number>(0); const latestValue = useRef(''); const getRecipes = async (value: string) => { if (!value) return setOptions(empty); const api = `https://dummyjson.com/recipes/search?q=${value}`; const data = await (await fetch(api)).json(); if (latestValue.current !== value) return; setOptions( Array.isArray(data.recipes) ? data.recipes.map(({ name }: { name: string }) => name) : [], ); }; const handleInput = (event: React.InputEvent<HTMLInputElement>) => { const value = encodeURIComponent(event.currentTarget.value.trim()); latestValue.current = value; setOptions(value ? 'Loading...' : empty); clearTimeout(timer.current); timer.current = setTimeout(getRecipes, 500, value); // Debounce API call }; return ( <Field lang='en'> <Label>Search for recipes (in English)</Label> <EXPERIMENTAL_Suggestion filter={false}> <EXPERIMENTAL_Suggestion.Input type='search' onInput={handleInput} // Note: using onInput, not onChange /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> {Array.isArray(options) ? ( options.map((option) => ( <EXPERIMENTAL_Suggestion.Option key={option}> {option} </EXPERIMENTAL_Suggestion.Option> )) ) : ( <EXPERIMENTAL_Suggestion.Empty> {options} </EXPERIMENTAL_Suggestion.Empty> )} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<FetchExternal />)
With dynamic suggestions
<ds-field class="ds-field">
<label class="ds-label">Enter your email</label>
<ds-suggestion data-creatable="" class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty=""></u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const EmailSuggestionsEn = () => { const [options, setOptions] = useState<string[]>([]); // Store results const vendora = ['gmail.com', 'yahoo.com', 'outlook.com', 'icloud.com']; const handleInput = (event: React.InputEvent<HTMLInputElement>) => { const value = event.currentTarget.value.trim(); const name = value.split('@')[0]; setOptions(value ? vendora.map((vendor) => `${name}@${vendor}`) : []); }; return ( <Field> <Label>Enter your email</Label> <EXPERIMENTAL_Suggestion creatable> <EXPERIMENTAL_Suggestion.Input onInput={handleInput} // Note: using onInput, not onChange /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty /> {options.map((option) => ( <EXPERIMENTAL_Suggestion.Option key={option} value={option}> {option} </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<EmailSuggestionsEn />)
Click to navigate
<ds-field class="ds-field">
<label class="ds-label">Click to navigate</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option value="https://www.designsystemet.no/">Designsystemet</u-option>
<u-option value="https://www.digdir.no/">Digdir</u-option>
<u-option value="https://www.uutilsynet.no/">UU-tilsynet</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const ClickToNavigateEn = () => { const handleSelect = (item: SuggestionItem | null) => { const href = item?.value; if (href) window.location.href = href; }; return ( <Field> <Label>Click to navigate</Label> <EXPERIMENTAL_Suggestion onSelectedChange={handleSelect}> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Option value='https://www.designsystemet.no/'> Designsystemet </EXPERIMENTAL_Suggestion.Option> <EXPERIMENTAL_Suggestion.Option value='https://www.digdir.no/'> Digdir </EXPERIMENTAL_Suggestion.Option> <EXPERIMENTAL_Suggestion.Option value='https://www.uutilsynet.no/'> UU-tilsynet </EXPERIMENTAL_Suggestion.Option> </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<ClickToNavigateEn />)
React
import { EXPERIMENTAL_Suggestion as Suggestion } from "@digdir/designsystemet-react";
<Suggestion>
<Suggestion.Input />
<Suggestion.Toggle />
<Suggestion.Clear />
<Suggestion.List>
<Suggestion.Empty>Empty</Suggestion.Empty>
<Suggestion.Option label="Sogndal" value="sogndal">
Sogndal
</Suggestion.Option>
<Suggestion.Option label="Oslo" value="oslo">
Oslo
</Suggestion.Option>
</Suggestion.List>
</Suggestion>;
Suggestion
- 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)
| Name | Type | Default | Description |
|---|---|---|---|
| filter | boolean | Filter | true | Filter options; boolean or a custom callback. See {@link Filter} for the callback signature. |
| creatable | boolean | false | Allows the user to create new items |
| onBeforeMatch | ((event: EventBeforeMatch) => void) | - | Callback when matching input value against options |
| name | string | undefined | 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`. |
| multiple | boolean | false | Allows the user to select multiple items |
| selected | string | 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`. |
| defaultSelected | string | SuggestionItem | (string | SuggestionItem)[] | - | Default selected item when uncontrolled |
| onSelectedChange | ((value: SuggestionItem | null) => void) | ((value: SuggestionItem[]) => void) | - | Callback when selected items changes |
SuggestionClear
- aria-label
- Description
Aria label for the clear button @deprecated Please use `--dsc-suggestion-sr-clear` or `data-sr-clear` on Suggestion to set label.
- Type
string
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | - | Aria label for the clear button @deprecated Please use `--dsc-suggestion-sr-clear` or `data-sr-clear` on Suggestion to set label. |
SuggestionEmpty
- data-empty
- Description
Optionally the text to display when the suggestion list is empty. If <Suggestion> has `data-creatable`, use `%s` to replace with the current input value.
- Type
string
| Name | Type | Default | Description |
|---|---|---|---|
| data-empty | string | - | Optionally the text to display when the suggestion list is empty. If <Suggestion> has `data-creatable`, use `%s` to replace with the current input value. |
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
| Name | Type | Default | Description |
|---|---|---|---|
| type | "number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week" | 'text' | Supported `input` types |
| disabled | boolean | - | Disables element @note Avoid using if possible for accessibility purposes |
| readOnly | boolean | - | Toggle `readOnly` |
| size | number | - | 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 @deprecated Please use `data-sr-singular` on `<Suggestion>` instead
- Type
string
- plural
- Description
The screen reader announcement for plural Suggestions, where %d is the number of Suggestions @deprecated Please use `data-sr-plural` on `<Suggestion>` instead
- Type
string
- autoPlacement
- Description
Whether to enable auto placement.
- Type
boolean- Default
true
| Name | Type | Default | Description |
|---|---|---|---|
| singular | string | - | The screen reader announcement for singular Suggestion, where %d is the number of Suggestions @deprecated Please use `data-sr-singular` on `<Suggestion>` instead |
| plural | string | - | The screen reader announcement for plural Suggestions, where %d is the number of Suggestions @deprecated Please use `data-sr-plural` on `<Suggestion>` instead |
| autoPlacement | boolean | true | Whether to enable auto placement. |
SuggestionToggle
- aria-label
- Description
Aria label for the toggle button
- Type
string- Default
Valg
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | Valg | Aria label for the toggle button |
Controlled multiple choice example
If you need to control which options are selected, you can use selected and onSelectedChange on Suggestion.
<ds-field class="ds-field">
<label class="ds-label">Select destinations</label>
<ds-suggestion data-multiple="" class="ds-suggestion">
<data value="Oslo">Oslo</data>
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">No results found</u-option>
<u-option label="Sogndal" value="Sogndal">
Sogndal
<div>Municipality</div>
</u-option>
<u-option label="Oslo" value="Oslo">
Oslo
<div>Municipality</div>
</u-option>
<u-option label="Brønnøysund" value="Brønnøysund">
Brønnøysund
<div>Municipality</div>
</u-option>
<u-option label="Stavanger" value="Stavanger">
Stavanger
<div>Municipality</div>
</u-option>
<u-option label="Trondheim" value="Trondheim">
Trondheim
<div>Municipality</div>
</u-option>
<u-option label="Bergen" value="Bergen">
Bergen
<div>Municipality</div>
</u-option>
<u-option label="Lillestrøm" value="Lillestrøm">
Lillestrøm
<div>Municipality</div>
</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>
<hr
aria-hidden="true"
class="ds-divider"
style="margin-top:var(--ds-size-4)"
/>
<p class="ds-paragraph" style="margin:var(--ds-size-2) 0">
Selected destinations:
<!-- -->
Oslo
</p>
<button class="ds-button" type="button">Set destinations to Sogndal, Stavanger</button>const ControlledMultipleEn = () => { const DATA_PLACES = [ 'Sogndal', 'Oslo', 'Brønnøysund', 'Stavanger', 'Trondheim', 'Bergen', 'Lillestrøm', ]; const [selected, setSelected] = useState<string[]>(['Oslo']); return ( <> <Field> <Label>Select destinations</Label> <EXPERIMENTAL_Suggestion multiple selected={selected} onSelectedChange={(items) => setSelected(items.map((item) => item.value)) } > <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> No results found </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((place) => ( <EXPERIMENTAL_Suggestion.Option key={place} label={place} value={place} > {place} <div>Municipality</div> </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> <Divider style={{ marginTop: 'var(--ds-size-4)' }} /> <Paragraph style={{ margin: 'var(--ds-size-2) 0' }}> Selected destinations: {selected.join(', ')} </Paragraph> <Button onClick={() => { setSelected(['Sogndal', 'Stavanger']); }} > Set destinations to Sogndal, Stavanger </Button> </> ); }; render(<ControlledMultipleEn />)
Filter
Filter is enabled by default, and the filter searches based on the text in the input. You can pass your own filter. The example below shows how to turn the filter off entirely.
<ds-field class="ds-field">
<label class="ds-label">Skriv inn et tall mellom 1-6</label>
<ds-suggestion class="ds-suggestion">
<input class="ds-input" type="text" placeholder=""/>
<button aria-expanded="false" aria-label="Valg" type="button"></button>
<button hidden="" type="reset"></button>
<u-datalist data-nofilter="" popover="manual" role="listbox">
<u-option data-empty="">Ingen treff</u-option>
<u-option value="sogndal">Sogndal</u-option>
<u-option value="oslo">Oslo</u-option>
<u-option value="brønnøysund">Brønnøysund</u-option>
<u-option value="stavanger">Stavanger</u-option>
<u-option value="trondheim">Trondheim</u-option>
<u-option value="bergen">Bergen</u-option>
<u-option value="lillestrøm">Lillestrøm</u-option>
</u-datalist>
</ds-suggestion>
</ds-field>const Filter = () => { const DATA_PLACES = [ 'Sogndal', 'Oslo', 'Brønnøysund', 'Stavanger', 'Trondheim', 'Bergen', 'Lillestrøm', ]; return ( <Field> <Label>Skriv inn et tall mellom 1-6</Label> <EXPERIMENTAL_Suggestion filter={false}> <EXPERIMENTAL_Suggestion.Input /> <EXPERIMENTAL_Suggestion.Toggle /> <EXPERIMENTAL_Suggestion.Clear /> <EXPERIMENTAL_Suggestion.List> <EXPERIMENTAL_Suggestion.Empty> Ingen treff </EXPERIMENTAL_Suggestion.Empty> {DATA_PLACES.map((label) => ( <EXPERIMENTAL_Suggestion.Option key={label} value={label.toLowerCase()} > {label} </EXPERIMENTAL_Suggestion.Option> ))} </EXPERIMENTAL_Suggestion.List> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<Filter />)