Select

A select picks one or more values from a list. It renders one focused element with role="combobox" (a <div>, or an <input> when it searches) and a list in a panel that is put at the end of the page, so its container never cuts it off. SelectOption and SelectOptGroup are the markup form of options.

Import
import { Select, SelectOption, SelectOptGroup } from 'svelte-ant-design';
Links
src/lib/Select Report an issue Component README

When to use

  • Use a select to pick one value, or several, from a list that is too long for radio buttons or checkboxes.
  • Turn on showSearch when the list is long, and use mode="tags" when the user may add values of their own.
  • For two or three options a group of radio buttons or checkboxes is easier to read at a glance.

Examples

The city is not chosen no change yet

Basic

bind:value keeps a variable in sync with the field; options take strings, numbers and objects; onchange receives the value and the option object. A Select has no label of its own: name it (see Accessible names).

Svelte TypeScript
["Svelte","TypeScript"] nothing removed yet
Rust
Svelte Rust + more
Type, then pick several

Multiple

mode="multiple" takes an array. Each value is a tag with a remove button; Backspace in an empty search removes the last one. maxTagCount, maxTagPlaceholder, maxTagTextLength and maxCount set the limits; ondeselect runs when a value is removed.

svelte
["svelte"]
Type or paste: red, green, blue
[]

Tags

mode="tags" lets the user add values that are not in the list: Enter adds the typed text, and tokenSeparators split typed or pasted text into values.

▾
Apple Cherry

Appearance

size (large, medium, small; middle and default are aliases), variant, status, disabled, prefix, suffix, loading and allowClear, and custom icons. The field keeps its size when any of them changes.

Pick a person
"kg"

Groups and field names

An entry of options with an options array is a group; its label is not an option and the arrow keys skip it. While searching, a group with no match is hidden. fieldNames maps the keys of your own data.

Linus Torvalds

Custom content

optionRender draws an option in the list, labelRender the selected value in the field, tagRender a tag, and popupRender wraps the list with your own content (a press in it does not blur the field).

scroll events: 0
nothing yet

The list

placement, popupMatchSelectWidth, listHeight and container; bind:open with onopenchange; onpopupscroll. The list flips at the edge of the screen and follows the field when the page scrolls.

"free"
Choose, clear, tab away

Control and callbacks

A function binding can refuse a change. onselect, onclear, onfocus, onblur and oninputkeydown report what the user does; defaultActiveFirstOption decides whether the first option is highlighted when the list opens.

Colour
The colour of the fruit
Banana

Accessible names

A Select has no label of its own. Name it with an external <label for>, aria-label or aria-labelledby; add hints with aria-describedby. A <label for> also names a field that does not search. The placeholder is not a name.

🍎
Apple

Semantic parts

classNames adds a class to a part and styles adds inline styles to it: root, prefix, content, placeholder, item, clear, input, suffix, and in the list popup, list and listItem. class and style reach the root.

"b"

Options as children

SelectOption and SelectOptGroup written inside Select give the same options as options, in the order of the markup, also after {#each} and {#if} insert or move one. A non-empty options wins. The text used by search and type-ahead is the label (then the title, then the value), not the content.

maxTagCount="responsive" (drag the corner to change the width)
Ada Lovelace Grace Hopper Linus Torvalds Margaret Hamilton Dennis Ritchie + 1 ...
maxTagPlaceholder
Ada Lovelace Grace Hopper Linus Torvalds Margaret Hamilton Dennis Ritchie Barbara Liskov 1 more

Responsive tags

maxTagCount="responsive" keeps the tags in one row: as many as fit are shown and the rest collapse into one tag, so the field never grows in height. The row follows the width of the field, and typed text takes room from the tags. maxTagPlaceholder draws the last tag and gets the hidden options.

API

PropertyDescriptionTypeDefault
valueThe selected value: one value, or an array in multiple and tags. Use bind:value; a plain value is the initial one. A function binding can refuse a change. undefined and null mean nothing is selected; a value that matches no option is shown as its own text.SelectValue | SelectValue[] | null—
modemultiple and tags take an array; in tags a typed text can become a value. default (the Angular name) is the single mode.'multiple' | 'tags' | 'default''default'
optionsThe options. A string or a number is both the value and the label; an entry with an options array is a group. Values must be unique. The last type is for fieldNames: objects whose keys are not label and value.ReadonlyArray<SelectValue | SelectOptionItem | SelectOptionGroup | Record<string, unknown>>[]
fieldNamesMaps the keys of the objects in options. groupLabel follows the mapped label key unless it is set.{ label?: string; value?: string; options?: string; groupLabel?: string }—
placeholderShown while nothing is selected. It is not a label: the field still needs an accessible name.string | Snippet—
disabledNot focusable, does not open; the clear button and the icons use the disabled colors.booleanfalse
loadingA spinner takes the place of the arrow (or of a custom suffix); the width does not change. Sets aria-busy. The list stays usable.booleanfalse
sizeSize of the field: 40px, 32px or 24px high. middle and default are compatibility aliases of medium. In a SpaceCompact a control without a size takes the one of the block.'large' | 'medium' | 'middle' | 'small' | 'default''medium'
variantVisual variant. The borderless field draws a focus outline.'outlined' | 'filled' | 'borderless' | 'underlined''outlined'
statusValidation look. error also sets aria-invalid="true"; warning changes only the look.'error' | 'warning'—
prefixContent before the selection, inside the border: plain text or a snippet.string | Snippet—
suffixReplaces the arrow (the default); null hides it. A press on it opens the list like the rest of the field.string | Snippet | null—
allowClearShows a native clear button while there is a selection (not while disabled). A press keeps focus in the field and calls onchange, then onclear. The object form sets the icon, the accessible name (default Clear) and the tab index.boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number }false
removeIconReplaces the cross on a tag.string | Snippet—
menuItemSelectedIconThe mark on a selected option in the list. By default the check mark shows in multiple and tags only; with this prop a single field shows it too.string | Snippet—
loadingIconReplaces the spinner shown while loading.string | Snippet—
showSearchMakes the focused element a text input that filters the list. Default: off in the single mode, on in multiple and tags.boolean—
searchValueThe current search text. Use bind:searchValue. It is cleared when the list closes.string''
onsearchCalled on every change of the search text: an edit, a token separator, and when the component clears the text (on close, on blur, after a selection with autoClearSearchValue).(value: string) => void—
filterOptiontrue filters by optionFilterProp; a function decides per option; false leaves the list as given (server-side search).boolean | ((input: string, option: SelectOptionItem) => boolean)true
filterSortOrders the filtered options. The third argument carries the search text.(a: SelectOptionItem, b: SelectOptionItem, info: { searchValue: string }) => number—
optionFilterPropWhich fields of an option the text is matched against (case-insensitive "contains"). label is the text of the option; several fields match with OR.string | string[]'label'
autoClearSearchValuemultiple and tags: clear the search text after a selection.booleantrue
maxTagCountmultiple and tags: show this many tags, the rest collapse into one tag, + N .... 'responsive': one row with as many tags as fit.number | 'responsive'—
maxTagPlaceholderThe content of the collapsed tag; a snippet gets the omitted options.string | Snippet<[omitted: SelectOptionItem[]]>—
maxTagTextLengthA longer tag text is cut with an ellipsis (the accessible name of its remove button keeps the whole text).number—
maxCountmultiple and tags: at most this many values; while it is reached the other options are disabled. No effect in the single mode.number—
tokenSeparatorsmultiple and tags: typed or pasted text is split at these characters into values. In tags every part is a value; in multiple a part that matches no option is dropped. Values beyond maxCount are dropped.string[][]
tagRenderA custom tag. closable is false for a disabled field or option; onclose removes the value. A press on your own close control does not open the list when it is a button.Snippet<[tag: { value: SelectValue; label: string | number | Snippet; closable: boolean; onclose: () => void }]>—
optionRenderThe content of an option in the list; the field keeps the plain label.Snippet<[option: SelectOptionItem, index: number]>—
labelRenderThe content of the selected value in the field, and of the tags.Snippet<[option: SelectOptionItem]>—
notFoundContentShown in the panel when no option remains. While searching, the status line reads the same text out (it takes it from the element, so a snippet works too). The default text is No data.string | Snippet—
resultsTextThe text of the polite status line that announces the number of matches while searching: 1 result, N results by default. It is written 300 ms after the list stopped changing, and after an IME composition ends. With no match the line says the notFoundContent text instead of a number.(count: number) => string—
openThe list is shown. Use bind:open for controlled use. A disabled field never shows it.booleanfalse
onopenchangeCalled once per change of the open state caused by the user, not for a change made by the parent.(open: boolean) => void—
placementThe preferred side of the list; it flips at the edge of the screen and mirrors in right-to-left layouts.'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight''bottomLeft'
popupMatchSelectWidthtrue: the list is at least as wide as the field. A number: that width in px, never narrower than the field. false: as wide as its content.boolean | numbertrue
listHeightThe maximum height of the list in px; it scrolls beyond that. A long list renders all its options (no virtual scrolling yet).number256
containerWhere the panel is put: an element, or a function that gets the field and returns one. A Select inside another popup passes the parent of its field.HTMLElement | ((anchor: HTMLElement | null) => HTMLElement)—
popupRenderWraps the list with your own content. A press in the added content does not take focus from the field (inputs in it still work).Snippet<[list: Snippet]>—
defaultActiveFirstOptionHighlight the first option when the list opens or the filter changes. A single field highlights its selected option on opening either way.booleantrue
onchangeCalled once per user change with the new value and the option object(s); not for a change made by the parent. After a clear both are undefined (single) or []. A new tag has { value: text, label: text }.(value: SelectValue | SelectValue[] | undefined, option: SelectOptionItem | SelectOptionItem[] | undefined) => void—
onselectA value was selected. Runs before onchange.(value: SelectValue, option: SelectOptionItem) => void—
ondeselectmultiple and tags: a value was removed. Runs before onchange.(value: SelectValue, option: SelectOptionItem) => void—
onclearCalled after the clear button emptied the value (after onchange).() => void—
onfocusFocus entered the field. Moving it between the field, its clear button and its own panel does not count.(event: FocusEvent) => void—
onblurFocus left the field (and the list closes).(event: FocusEvent) => void—
onpopupscrollThe list scrolled.(event: Event) => void—
oninputkeydownA key went down in the focused element, before the field handles it.(event: KeyboardEvent) => void—
onkeydown / oninputNative handlers of the focused element; they are called before the component's own (oninput only while it searches).(event) => void—
autocompleteautocomplete of the search input. The default new-password stops browsers from offering saved values in the search box.string'new-password'
childrenSelectOption and SelectOptGroup written as markup; ignored when options has entries.Snippet—
classNamesExtra classes for the parts, appended to the component's own. The parts popup, list and listItem are in the panel.Partial<Record<'root' | 'prefix' | 'content' | 'placeholder' | 'item' | 'clear' | 'input' | 'suffix' | 'popup' | 'list' | 'listItem', string>>—
stylesInline styles for the parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). They win over the component's styles.Partial<Record<'root' | 'prefix' | 'content' | 'placeholder' | 'item' | 'clear' | 'input' | 'suffix' | 'popup' | 'list' | 'listItem', Record<string, string | number>>>—
classAn additional CSS class for the root. The field fills its container like Input; set the width with class or style.string—
styleInline style of the root; styles.root is written after it.string—
native attributesid (so <label for> works), aria-label, aria-labelledby, aria-describedby, aria-invalid, tabindex, title, data-* and the other attributes go to the focused element. The component owns role, aria-expanded, aria-controls, aria-activedescendant, aria-autocomplete and aria-busy.HTMLAttributes<HTMLElement>—
Differences from Ant Design: showSearch is a boolean and the search props are flat (filterOption, searchValue, onsearch ...), where React 6 nests them in an object; there is no defaultValue or defaultOpen, because bind: covers them; suffix replaces suffixIcon and showArrow; values are strings or numbers (no labelInValue yet); the list is not virtual yet, so a very long list renders all its options (virtual is planned); the built-in texts (No data, Clear, Remove …) are English (a locale mechanism is planned); the field fills its container like Input; the type of an option object is SelectOptionItem, because SelectOption is the component.

SelectOption API

PropertyDescriptionTypeDefault
valueThe value of the option; required and unique within the Select.SelectValue—
labelWhat is drawn, and the text of the option for the search and the type-ahead: a string, or (a snippet) drawn only; then the title, then the value is the text.string | number | Snippet—
disabledThe option is shown, skipped by the arrow keys and cannot be selected.boolean—
titleNative title of the option; also its text when label is not a string.string—
classAn extra class of the option in the list.string—
childrenThe content of the item in the list. When label is given too, this is drawn and label is still the text.Snippet—

SelectOptGroup API

PropertyDescriptionTypeDefault
labelThe label of the group; it names the group for assistive technology. It is not an option.string | number | Snippet—
titleNative title of the group label.string—
classAn extra class of the group.string—
childrenThe SelectOptions of the group.Snippet—

Methods

Through bind:this. The list does not open on focus.

MethodDescriptionType
focusMoves focus into the field (the text input when it searches). The list does not open.(options?: { preventScroll?: boolean }) => void
blurRemoves focus from the field; the list closes.() => void

Design tokens

TokenDescriptionDefault
--ads-select-icon-sizeSize of the arrow, the clear icon and the mark of a selected option.12px
--ads-select-gapDistance between the text and the arrow or the clear button.6px
--ads-select-multiple-padding-blockVertical padding of a field with tags; with the tag height it gives the 40, 32 and 24px of the sizes for one row.3px
--ads-select-multiple-padding-inlineHorizontal padding of a field with tags.4px
--ads-select-tag-heightHeight of a tag in a medium field.24px
--ads-select-tag-height-lgHeight of a tag in a large field.32px
--ads-select-tag-height-smHeight of a tag in a small field.16px
--ads-select-tag-radiusCorner radius of a tag.4px
--ads-select-tag-gapDistance between tags, between the rows of tags, and before the remove icon.4px
--ads-select-tag-padding-startPadding of a tag at the start, and at both ends of the collapsed tag.8px
--ads-select-tag-padding-endPadding of a tag at the end, after the remove icon.4px
--ads-select-tag-remove-sizeSize of the remove icon of a tag.10px
--ads-select-tag-bgBackground of a tag.rgba(0, 0, 0, 0.06)
--ads-select-tag-borderBorder color of a tag (it shows in forced-colors modes).transparent
--ads-select-tag-color-disabledText color of a tag that cannot be removed.rgba(0, 0, 0, 0.25)
--ads-select-popup-paddingPadding of the panel around the list.4px
--ads-select-popup-radiusCorner radius of the panel.8px
--ads-select-popup-bgBackground of the panel.#ffffff
--ads-select-popup-shadowShadow of the panel.0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-select-option-radiusCorner radius of an option.4px
--ads-select-option-heightLeast height of an option.32px
--ads-select-option-font-sizeFont size of the list.14px
--ads-select-option-line-heightLine height of the list.1.5714285714285714
--ads-select-option-padding-blockVertical padding of an option.5px
--ads-select-option-padding-inlineHorizontal padding of an option and of a group label.12px
--ads-select-option-gapDistance between the options.2px
--ads-select-option-active-bgBackground of the highlighted option.rgba(0, 0, 0, 0.04)
--ads-select-option-selected-bgBackground of a selected option.#e6f4ff
--ads-select-option-selected-colorText color of a selected option.rgba(0, 0, 0, 0.88)
--ads-select-selected-font-weightFont weight of a selected option.600
--ads-select-group-font-sizeFont size of a group label.12px
--ads-select-group-label-colorText color of a group label (the accessible file darkens it).rgba(0, 0, 0, 0.45)
--ads-select-empty-colorText color of the empty-result text (the accessible file darkens it).rgba(0, 0, 0, 0.45)

Accessibility

  • The focused element has role="combobox" with aria-expanded, aria-controls and aria-activedescendant; the list is a listbox of options and groups. DOM focus never leaves the field: the highlighted option is shown by aria-activedescendant.
  • A name is required: <label for>, aria-label or aria-labelledby. The placeholder is not a name. The list takes the name of the field.
  • Keys: Down, Up, Alt+Down and Alt+Up, Enter and Space, Home and End, PageUp and PageDown, Escape, Tab and Backspace. A field that cannot search also finds an option by typing the start of its text. Tab only closes the list and keeps the value.
  • Selected options have aria-selected="true", disabled ones aria-disabled. While searching, a polite status line says how many options match, or the empty-result text when nothing does.
  • The clear and remove buttons are native buttons named Clear and Remove …; they do not take focus from the field. The icons are hidden from assistive technology. Forced-colors modes (Windows High Contrast) are supported.
  • The default light colors are Ant Design's own and do not reach WCAG AA for every text; import tokens-accessible.css for colors that do. The dark theme keeps AA without it.
  • Support for aria-activedescendant in iOS VoiceOver has been weak, so check a Select by hand on that platform before you rely on it.
  • Known limitation: Orca announces a change of aria-selected only after the Space key. In a field that searches (the default of multiple and tags) Space types a space and Enter toggles an option, so Orca says nothing then; the field reads the selected values when it gets focus. Orca also says no values of a field with several values that does not search (showSearch={false}). NVDA, JAWS and VoiceOver have not been tried yet. An announcement of the toggled option is planned for v1.1, together with the translation of the built-in texts.