本文へ移動
日本語
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

For picking along a tree level by level. The value is the path from the first level to the chosen one. The trigger is the same filled field as the select: --ds-surface-2 at rest, white with an accent line and a soft halo while open. The panel is a floating sheet (16px radius, ring and large shadow) with one 160px column per level, separated by 4px of space instead of lines; the rows on the open path are soft accent and the chosen last level gets a check. It needs no portal, so it stays correct inside scaled or scrolling containers. On opening it measures the space around, flips above or aligns right when needed, and otherwise scrolls sideways to the newest column. Searching lists matching full paths with the matched text in accent.

向いている場面

  • Options with clear parent-child levels and few items per level, such as category / type / item or department / team.
  • A value that needs its parent to make sense (“Over-ear” under Headphones): the whole path shows and is stored.
  • Filters that may stop at an upper level: turn on changeOnSelect.
  • Many options when people know the name: turn on searchable and let them type.

向かない場面

  • Children that load from an API only after a parent opens: this needs the whole tree up front and has no lazy loading.
  • A single level of options: use a select.
  • Picking several nodes or seeing the whole tree: use a tree view with checkboxes.
  • Dozens of options per level with no search: a column shows only about seven rows.

使い方

import { useState } from 'react'
import { Cascader } from './Cascader.jsx'

const categories = [
  {
    value: 'audio',
    label: 'Audio',
    children: [
      {
        value: 'headphones',
        label: 'Headphones',
        children: [
          { value: 'over-ear', label: 'Over-ear' },
          { value: 'in-ear', label: 'In-ear' },
        ],
      },
      { value: 'speakers', label: 'Speakers', children: [{ value: 'portable', label: 'Portable' }] },
    ],
  },
  { value: 'gift-cards', label: 'Gift cards', disabled: true },
]

export function CategoryField() {
  const [category, setCategory] = useState([])
  return (
    <div>
      <label htmlFor="category">Category</label>
      <Cascader
        id="category"
        options={categories}
        value={category}
        // path is ['audio', 'headphones', 'over-ear']; nodes are the options of those levels
        onChange={(path, nodes) => setCategory(path)}
        searchable
        clearable
        name="category"
        placeholder="Type a category to search"
      />
    </div>
  )
}

プロパティ

プロパティ型既定値説明
optionsCascaderOption[]—The option tree: { value, label, disabled?, children? }, any depth. Values must be unique under one parent; no children (or an empty array) means a last level.
valuestring[]—Controlled: the selected path from the first level to the chosen one; an empty array means nothing selected.
defaultValuestring[][]Uncontrolled: the initial path.
onChange(value, selectedOptions) => void—Called on selection or clear (both arguments empty). The second argument holds the option of each level. Selecting the same path again does not fire it.
openboolean—Controlled: whether the panel is open; use with onOpenChange.
defaultOpenbooleanfalseUncontrolled: initial open state (does not take focus).
onOpenChange(open) => void—Called when the open state changes.
size'sm' | 'md' | 'lg''md'Height 36 / 44 / 52px, radius 10 / 12 / 14px and font 14 / 15 / 16px; option rows are 36 / 40 / 44px. Use lg on mobile.
placeholderstring'Select'Hint shown when nothing is selected.
disabledbooleanfalseDisables the field; an open panel closes.
invalidbooleanfalseMarks a failed validation: the field's hairline turns danger, the fill takes a faint danger tint, the focus halo turns danger, and aria-invalid="true" is set. Write the message yourself and link it with aria-describedby.
requiredbooleanfalseSets aria-required on the trigger; draw any “*” in your form item.
clearablebooleanfalseShows a clear button when there is a value (on hover or focus; always on touch). Backspace / Delete also clear.
changeOnSelectbooleanfalseSelect at any level: clicking a middle level selects it and keeps the panel open. By default only the last level selects.
searchablebooleanfalseThe trigger becomes an input; typing replaces the columns with a list of matching full paths (up to 50), matched text in bold primary colour.
displayRender(labels, selectedOptions) => ReactNode—How the selected path shows in the trigger; default joins with “ / ”. The trigger title always holds the full path.
emptyTextstring—Text when there are no options. Defaults to “No matching options” while searching and “No options” otherwise.
labelsobject—Replaces built-in text (options, clear, selected, noMatch, noOptions, moreResults). Shallow-merged over the English defaults.
namestring—Renders one hidden input of this name per level, so a native form gets the whole path from formData.getAll(name).
refRef<HTMLButtonElement | HTMLInputElement>—Forwarded to the element that takes focus in the trigger, so you can call focus().
...restHTMLAttributes<HTMLDivElement>—className, style and onBlur go to the root; id, autoFocus, aria-label, aria-labelledby and aria-describedby go to the trigger, so <label htmlFor> works. Panel z-index: --ds-cascader-z (default 30).

アクセシビリティ

  • Follows the WAI-ARIA combobox + tree pattern: role="combobox" with aria-expanded and aria-controls, rows as treeitem with aria-level, aria-expanded and aria-selected.
  • Focus stays on the trigger; the row under the keyboard is exposed with aria-activedescendant.
  • Keys: Up / Down move in the column, Right enters the next column, Left goes back, Home / End jump, Enter selects, Esc closes only the panel, Tab moves on.
  • When searchable, the selected path is read through aria-describedby; Left / Right / Home / End stay with the caret while there is text.
  • The expanded path is not shown by colour alone; option rows are 44px high on touch. With reduced motion the panel only fades.

モーション

Opening, the trigger's halo grows from 0 to 4px over 220ms and the panel scales out of the trigger's corner in 220ms ease-out (scale 0.96 → 1, 6px rise, fade); it leaves in 140ms ease-in. A column opened by the mouse slides in 8px over 220ms; keyboard moves swap it without animation. The caret on an open row nudges 2px right, and the check on the chosen item pops in with a 320ms spring. With reduced motion the panel only fades and nothing moves.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

Cascader.jsxcascader.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。