Web and native

Select in kanso-ui 0.59.0, for React.

import { Select } from '@kanso-labs/kanso-ui'

A labelled field that opens a list to choose from.

It is the Material Design exposed dropdown menu: the same box, label, supporting line and error as every text field, with a chevron that points down while closed and up while open, and a list as wide as the field beneath it, its options drawn as menu rows and padded 8dp from its edge. The whole box opens the list, as a native select does. A long chosen value ends in an ellipsis.

What you provide

  • label (required): what the field is for.
  • options: one ListBox.Item per option, each with an id, and ListBox.Section (with a header) to group them. The list is a ListBox and takes everything one does.

Props

PropTypeDefaultDescription
containerElement—Where to portal the list. Defaults to the container of the `ThemeScope` around it, which keeps the list on that scope's tokens, or to the end of `<body>` where there is none. A subtree themed through the `themeScope` class has no container of its own, so point this inside it, or the list renders outside and keeps the page's tokens.
descriptionstring—A hint shown under the field. Replaced by `error` when there is one, so the two never stack.
errorstring—The problem with the current value, in words. Its presence is what puts the field in its error state — the 2px `critical` border, the message after its `error` icon, and `aria-invalid` all follow from it. Outside a `Form` the message line arrives with the message, so the field grows when this does and moves what is under it. A `Form` holds that space from the start; so does a permanent `description`.
floatingLabelboolean—Whether the label floated in Material Design's text field. Kanso's label sits above the box in every state, so this changes nothing and warns in development.
label requiredstring—What the field is for. Required rather than optional: a select with no label is a box a screen reader cannot name.
leadingIconReactNode—An icon at the start of the box, before the value: Kanso's 20px icon, in `ink-muted`. An icon drawn in `em` takes that size from the slot.
optionsReactNode—The options. `ListBox.Item` for each, and `ListBox.Section` to group them — the list inside is a ListBox, and takes everything one does.
variantFieldVariant—Which of Material Design's two text fields to draw. Kanso has one, a bordered box, which both values draw, so this changes nothing and warns in development.

Also takes the props of Omit<SelectProps$1<T>, 'children' | 'defaultSelectedKey' | 'isInvalid' | 'onSelectionChange' | 'selectedKey' | 'validationBehavior'>.

Example

import { ListBox, Select } from '@kanso-labs/kanso-ui'

<Select
  label="Label"
  defaultValue="second"
  options={
    <>
      <ListBox.Item id="first">First item</ListBox.Item>
      <ListBox.Item id="second">Second item</ListBox.Item>
      <ListBox.Item id="third">Third item</ListBox.Item>
    </>
  }
/>

The design’s classes

k-field, k-field__label, k-field__control, k-field__input on the <select>, k-field__trail with k-icon k-icon--md, k-field__help, k-field--compact, k-field--error.

Select in kanso-ui and react-native-kanso-ui, restyled to this language.