Command palette

@yasinsensoy/web

Web · Components

Select

Select box.

10 exports

Live example

Preview

Code

import { Label, Select } from "@yasinsensoy/web";

<Label htmlFor="selection">Selection</Label>
<Select id="selection" name="selection"
  items={[
    { group: "Ekip", items: [
      { value: "owner", label: "Sahip", icon: "user", description: "Everything, including invoices" },
      { value: "editor", label: "Editor", icon: "pencil" },
    ] },
    { group: "External", items: [
      { value: "auditor", label: "Auditor", disabled: true, tooltip: "Available on the business plan" },
    ] },
  ]}
  onValueChange={(value) => …} />

Events

  1. Events appear here when you interact with the preview.

SelectField: etiket, zorunlu alan ve form

You can pick more than one.

Import

import { Select } from "@yasinsensoy/web";

Select

componentClient component

Select box. Options are given with items; clicking opens a list drawn with the theme's colors.

  • Options: plain text, a { value, label, icon, description, disabled, tooltip } object or a { group, items } group. Group headings are separated by a divider line and spacing.
  • Search: search="auto" (default) opens the search box with more than 20 options. Locale-specific characters and accents are ignored, matches are highlighted.
  • Large lists: The list is virtualized; even with 10,000 options only the visible rows are drawn, the next ones are added while scrolling.
  • Loading: items can be a Promise or function ("Loading" shows while loading, a "Try again" button appears on error). With loadItems, search runs on the server and the list loads page by page while scrolling.
  • Multiple selection: multiple selects several values; the list doesn't close on selection.
  • Keyboard: ↑/↓ or letter keys work on the closed box too; while open ↑/↓, PageUp/PageDown, Home/End navigate, Enter selects, Esc closes.
  • Mobile: Instead of the operating system's picker, a theme-matching panel opens from the bottom; rows are tall enough to tap comfortably with a thumb, and it closes by pulling down and with the back button. The search box doesn't open the keyboard by itself; it opens on tap and the panel moves above the keyboard.
  • Form: Thanks to a hidden native <select> it joins FormData with name, required works with browser validation and it returns to the initial value on form reset. Clicking <Label htmlFor> opens the list.

To use it with a label, description and error message see SelectField.

Select(props: SelectProps): JSX.Element

Examples

<Select name="city" placeholder="Choose a city" required
  items={["London", "Paris", { group: "Spain", items: ["Madrid", "Barcelona"] }]} />

<Select value={role} onValueChange={setRole} clearable
  items={[
    { value: "admin", label: "Admin", icon: "shield", description: "Accesses everything" },
    { value: "editor", label: "Editor", icon: "pencil" },
    { value: "guest", label: "Guest", disabled: true, tooltip: "Coming soon" },
  ]} />

<Select multiple name="tags" items={tags} />

<Select name="country" items={() => fetch("/api/countries").then((r) => r.json())} />

<Select name="person" loadItems={({ query, page, signal }) => searchPeople(query, page, signal)} />

SelectItem

interface

An option inside Select.

NameTypeDescription
valuestringValue submitted to the form and used with value/onValueChange.
label?ReactNodeVisible text. If omitted, value is shown.
icon?IconPropIcon before the text: name ("user") or element.
description?ReactNodeSmall description below the text (single line; truncated if it doesn't fit).
disabled?booleanCan't be selected.
keywords?readonly string[]Extra words to match in search (e.g. a plate code, an old name). Search already looks at the label text and the value.
tooltip?TooltipPropTooltip shown when hovering the item. Good for explaining why a disabled item can't be selected. See TooltipProp.

SelectGroup

interface

Option group with a heading.

NameTypeDescription
groupReactNodeHeading of the group.
itemsreadonly (string | SelectItem)[]Options in the group.

An element of the Select items array: plain text (value and label are the same), a { value, label, icon… } object or a { group, items } group.

type SelectEntry = string | SelectItem | SelectGroup

SelectPage

interface

One page of response from the Select loadItems function.

NameTypeDescription
itemsreadonly SelectEntry[]Items on this page.
hasMore?booleanWhether there are more pages. If true, the next page is requested when the list scrolls to the end.

Info given to the Select loadItems function.

NameTypeDescription
querystringText in the search box (can be empty).
pagenumberRequested page; the first page is 0. Starts from 0 when the search changes.
signalAbortSignalCancels the old request when the search changes or the list closes; pass it with fetch(url, { signal }).

Classes that can be passed to the inner parts of Select.

NameTypeDescription
trigger?stringOpen button (the visible box of the field).
content?stringOpened panel (e.g. w-80 for width).
search?stringSearch box.
item?stringEach option.

Single-select Select.

NameTypeDefaultDescription
multiple?falsefalseIf true, several options can be selected.
value?stringSelected value in controlled use; "" for none.
defaultValue?stringInitial value in uncontrolled use.
onValueChange?(value: string, item: SelectItem | undefined) => voidCalled with the new value and the selected item when the selection changes ("" when cleared).
renderValue?(item: SelectItem) => ReactNodeHow the selected item looks in the box; the list keeps label. E.g. in a country code picker the list shows "Türkiye" and the box only "+90".
variant?FieldVariant | CustomVariant<"field">Look: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, otherwise outline. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
items?SelectItemsSourceOptions. Can be given in three forms: - Array: plain text ("London"), a { value, label, icon, description, disabled } object or a { group, items } group. See SelectEntry. - Promise: e.g. a request started in a server component. "Loading" is shown until it arrives. - Function: () => fetch(...). Called once on first open (or right away if there is a selected value); change key to reload. Search runs over these items in the browser. For server search and pagination use loadItems (SelectProps.loadItems).
loadItems?SelectLoaderFunction that searches the server for options and fetches them page by page. When the list opens the first page is requested, while typing (with a 250 ms debounce) the first page of that search, and as the list scrolls to the end the next page. Old requests are cancelled with signal. Returns an array or { items, hasMore }. To show the label of the selected value, the first page is requested even before opening if a value is given; selected items are remembered.
search?boolean | "auto""auto"Search box above the list. - "auto": visible with more than 20 options, hidden with fewer. Always visible with loadItems (search runs on the server). - true / false: always visible / never visible. Search ignores case, locale-specific characters and accents ("cankaya" → "Çankaya") and highlights the matching part.
searchPlaceholder?string"Ara…"` (`messages.search`)Placeholder of the search box.
placeholder?ReactNode"Seçiniz"` (`messages.select`)Text or content shown while nothing is selected (e.g. an item with an icon).
emptyMessage?ReactNodeContent shown when the list is empty or the search finds nothing.
icon?IconPropIcon at the start of the box (if the selected item has its own icon, that is shown).
clearable?booleanfalseWhether to show a × button that clears the selection. For optional fields, so the user can undo their selection.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (if outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button; <Label htmlFor> links to it.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the list is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown when hovering the box. In labeled fields (SelectField) the tooltip is shown on the ⓘ icon next to the label. See TooltipProp.
className?stringClasses added to the container (e.g. w-56 for width).
classNames?SelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name if there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box. SelectField passes it itself.
aria-invalid?boolean | "true" | "false"Show the field as invalid (red border). SelectField passes it itself.

Multi-select Select.

NameTypeDefaultDescription
multipletrueSeveral options can be selected; the list doesn't close on selection.
value?readonly string[]Selected values in controlled use.
defaultValue?readonly string[]Initial values in uncontrolled use.
onValueChange?(value: string[], items: SelectItem[]) => voidCalled with the new values and their items when the selection changes.
variant?FieldVariant | CustomVariant<"field">Look: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, otherwise outline. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
items?SelectItemsSourceOptions. Can be given in three forms: - Array: plain text ("London"), a { value, label, icon, description, disabled } object or a { group, items } group. See SelectEntry. - Promise: e.g. a request started in a server component. "Loading" is shown until it arrives. - Function: () => fetch(...). Called once on first open (or right away if there is a selected value); change key to reload. Search runs over these items in the browser. For server search and pagination use loadItems (SelectProps.loadItems).
loadItems?SelectLoaderFunction that searches the server for options and fetches them page by page. When the list opens the first page is requested, while typing (with a 250 ms debounce) the first page of that search, and as the list scrolls to the end the next page. Old requests are cancelled with signal. Returns an array or { items, hasMore }. To show the label of the selected value, the first page is requested even before opening if a value is given; selected items are remembered.
search?boolean | "auto""auto"Search box above the list. - "auto": visible with more than 20 options, hidden with fewer. Always visible with loadItems (search runs on the server). - true / false: always visible / never visible. Search ignores case, locale-specific characters and accents ("cankaya" → "Çankaya") and highlights the matching part.
searchPlaceholder?string"Ara…"` (`messages.search`)Placeholder of the search box.
placeholder?ReactNode"Seçiniz"` (`messages.select`)Text or content shown while nothing is selected (e.g. an item with an icon).
emptyMessage?ReactNodeContent shown when the list is empty or the search finds nothing.
icon?IconPropIcon at the start of the box (if the selected item has its own icon, that is shown).
clearable?booleanfalseWhether to show a × button that clears the selection. For optional fields, so the user can undo their selection.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (if outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button; <Label htmlFor> links to it.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the list is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown when hovering the box. In labeled fields (SelectField) the tooltip is shown on the ⓘ icon next to the label. See TooltipProp.
className?stringClasses added to the container (e.g. w-56 for width).
classNames?SelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name if there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box. SelectField passes it itself.
aria-invalid?boolean | "true" | "false"Show the field as invalid (red border). SelectField passes it itself.

Props of the Select component.

type SelectProps = SingleSelectProps | MultiSelectProps
NameTypeDefaultDescription
multiple?false | truefalseIf true, several options can be selected.
value?string | readonly string[]Selected value in controlled use; "" for none.
defaultValue?string | readonly string[]Initial value in uncontrolled use.
onValueChange?(value: string, item: SelectItem | undefined) => void | (value: string[], items: SelectItem[]) => voidCalled with the new value and the selected item when the selection changes ("" when cleared).
renderValue?(item: SelectItem) => ReactNodeHow the selected item looks in the box; the list keeps label. E.g. in a country code picker the list shows "Türkiye" and the box only "+90".(SingleSelectProps)
variant?FieldVariant | CustomVariant<"field">Look: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, otherwise outline. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
items?SelectItemsSourceOptions. Can be given in three forms: - Array: plain text ("London"), a { value, label, icon, description, disabled } object or a { group, items } group. See SelectEntry. - Promise: e.g. a request started in a server component. "Loading" is shown until it arrives. - Function: () => fetch(...). Called once on first open (or right away if there is a selected value); change key to reload. Search runs over these items in the browser. For server search and pagination use loadItems (SelectProps.loadItems).
loadItems?SelectLoaderFunction that searches the server for options and fetches them page by page. When the list opens the first page is requested, while typing (with a 250 ms debounce) the first page of that search, and as the list scrolls to the end the next page. Old requests are cancelled with signal. Returns an array or { items, hasMore }. To show the label of the selected value, the first page is requested even before opening if a value is given; selected items are remembered.
search?boolean | "auto""auto"Search box above the list. - "auto": visible with more than 20 options, hidden with fewer. Always visible with loadItems (search runs on the server). - true / false: always visible / never visible. Search ignores case, locale-specific characters and accents ("cankaya" → "Çankaya") and highlights the matching part.
searchPlaceholder?string"Ara…"` (`messages.search`)Placeholder of the search box.
placeholder?ReactNode"Seçiniz"` (`messages.select`)Text or content shown while nothing is selected (e.g. an item with an icon).
emptyMessage?ReactNodeContent shown when the list is empty or the search finds nothing.
icon?IconPropIcon at the start of the box (if the selected item has its own icon, that is shown).
clearable?booleanfalseWhether to show a × button that clears the selection. For optional fields, so the user can undo their selection.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (if outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the open button; <Label htmlFor> links to it.
autoFocus?booleanFocuses when the page opens.
open?booleanWhether the list is open in controlled use.
defaultOpen?booleanfalseWhether it starts open in uncontrolled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown when hovering the box. In labeled fields (SelectField) the tooltip is shown on the ⓘ icon next to the label. See TooltipProp.
className?stringClasses added to the container (e.g. w-56 for width).
classNames?SelectClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name if there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box. SelectField passes it itself.
aria-invalid?boolean | "true" | "false"Show the field as invalid (red border). SelectField passes it itself.