Select选择器
从列表中选择一项。选项可带图标和说明,支持禁用、键盘导航与按字匹配。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add select提醒按所选时区发送。
安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add select使用
React
"use client";
import { useState } from "react";import { CodeXml, Pencil } from "lucide-react";import { KalosSelect, type KalosSelectOption } from "@/components/ui/kalos-select";
const roles: KalosSelectOption[] = [ { value: "design", label: "设计", icon: <Pencil /> }, { value: "engineering", label: "工程", icon: <CodeXml /> }, { value: "reviewer", label: "审阅者", description: "由管理员分配", disabled: true },];
export default function Example() { const [role, setRole] = useState<string | null>("design");
return <KalosSelect label="角色" name="role" options={roles} value={role} onChange={setRole} />;}API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
options / items | KalosSelectOption[] | — | 传入其中一个。每项包含 value、label,以及可选的 icon、description、disabled |
value / defaultValue | string | null | — / null | 受控值或非受控初始值,null 表示未选择 |
onChange | (value: string, option: KalosSelectOption) => void | — | 选择变化时返回值和完整选项 |
onValueChange | (value: string) => void | — | 返回选中值。启用清空时返回空字符串,受控用法需据此更新 value |
label / aria-label | ReactNode / string | — | 可见标签和控件的可访问名称。无可见标签时提供 aria-label |
placeholder | string | 请选择 | 未选择时显示 |
hint / error | ReactNode | — | 约束提示或校验错误,错误优先 |
optional | boolean | false | 有可见标签且未设置 required 时显示选填 |
size | "sm" | "md" | "lg" | md | 桌面控件高度分别为 28、32、36px |
clearable | boolean | false | 启用后,箭头区域悬停或键盘聚焦时显示清空按钮 |
name / id / required / disabled | 原生字段属性 | — | 表单提交键、触发器 ID、必填与禁用状态 |
className / ref | string / Ref<HTMLButtonElement> | — | 容器样式与触发器引用 |
键盘操作
- Enter / Space
- 展开列表或确认选项
- ↑ / ↓
- 展开列表或在可用选项间移动
- Home / End
- 移动到第一项或最后一项可用选项
- 字符键
- 按选项名称的开头匹配
- Esc
- 关闭列表并返回触发器
- Tab
- 关闭列表并移动焦点