选择框

基于 React Aria 构建的下拉选择 - 触发器与 Figma 表面 1:1 匹配,弹出窗口是非模态的,因此页面不断滚动,并且项目接受自由格式的内容,例如状态点和头像。

安装

您可以使用我们的 CLI 或手动添加此选择组件:

已支持深色模式,此 组件 会自动使用 BoardUI 的语义设计变量。 设置主题切换.

CLI 将组件源(及其内部依赖项)复制到您的项目中并安装任何所需的 npm 包。

npx boardui@latest add select

使用共享 UI 包或自定义导入别名? 配置您的安装路径 在运行 CLI 之前。复制粘贴示例使用默认导入。

紧凑与宽松

菜单默认使用 BoardUI 的紧凑下拉菜单:14px 圆角、4px 内边距的面板,行距紧凑,宽度与触发器一致。长列表在顶部柔和模糊下滚动,并在打开时定位到已选中的行。传入 variant="comfortable" 可使用更宽松的 266px 菜单。

紧凑型
宽松
const years = Array.from({ length: 41 }, (_, index) => 2000 + index); {/* Compact is the default menu: no prop needed */}<Select label="Compact" defaultSelectedKey="2026" className="w-[120px]"> {years.map((year) => ( <SelectItem key={year} id={String(year)}>{year}</SelectItem> ))}</Select> {/* The roomier 266px menu */}<Select label="Comfortable" variant="comfortable" defaultSelectedKey="2026" className="w-[120px]"> {years.map((year) => ( <SelectItem key={year} id={String(year)}>{year}</SelectItem> ))}</Select>

标签和占位文字

传入 label 可显示字段标签:React Aria 会把它渲染为选择框的标签,并用它为触发按钮命名,因此屏幕阅读器会同时读出两者。placeholder 会一直显示,直到选中某一项。像上面的筛选器这样没有可见标签的选择框,请改用 aria-label。

角色
{/* label names the select; placeholder shows until something is picked */}<Select label="Role" placeholder="Select a role" className="w-[240px]"> <SelectItem id="admin" textValue="Admin">Admin</SelectItem> <SelectItem id="editor" textValue="Editor">Editor</SelectItem> <SelectItem id="viewer" textValue="Viewer">Viewer</SelectItem></Select>

带状态点

项目在标签旁边呈现任意子项 - 始终通过 textValue 因此,提前输入和触发器标签保持正确。

import { StatusDot } from "@/components/base/badges/status-dot"; {/* Items take free-form content - dots, avatars, icons */}<Select aria-label="Purchase status" defaultSelectedKey="completed" className="w-[142px]"> <SelectItem id="completed" textValue="Completed"> <StatusDot color="green" /> Completed </SelectItem> <SelectItem id="waiting" textValue="Waiting"> <StatusDot color="yellow" /> Waiting </SelectItem> <SelectItem id="processing" textValue="Processing"> <StatusDot color="indigo" /> Processing </SelectItem></Select>