Table表格
以行列结构展示数据,支持编辑、排序与筛选。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add tableDesign Stack
Figma | Mar 12, 2026 | ||||
Framer | May 2, 2026 | ||||
Linear | Jan 4, 2026 | ||||
Loom | Nov 18, 2026 | ||||
Vercel | Aug 21, 2026 |
A6
安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add table使用
默认示例的 Options 使用 Dropdown Menu、Dialog、Input 和 Button;同时安装这些组件,并运行 npm install sonner 以显示操作结果。已有 Toaster 时复用原有入口。
React
"use client";
import { useId, useState } from "react";import { AlignLeft, CalendarDays, Grid2X2, Link2, Copy, Download, ExternalLink, Folder, FolderInput, MoreHorizontal, Pencil, Star, Trash2 } from "lucide-react";import { KalosTable, type KalosTableColumn, type KalosTableRow } from "@/components/ui/kalos-table";import { KalosButton } from "@/components/ui/kalos-button";import { KalosDialog } from "@/components/ui/kalos-dialog";import { KalosInput } from "@/components/ui/kalos-input";import { KalosDropdownMenu } from "@/components/ui/kalos-dropdown-menu";import { tableLink } from "@/components/ui/kalos-table-model";import { FieldGroup } from "@/components/ui/field";import { toast, Toaster } from "sonner";
type StackMenuProps = { row: KalosTableRow; categories: string[]; onSave: (row: KalosTableRow) => void; onDuplicate: () => void; onDelete: () => void;};
export function StackMoreMenu({ row, categories, onSave, onDuplicate, onDelete, standalone = false }: StackMenuProps & { standalone?: boolean }) { const [starred, setStarred] = useState(false); return <KalosDropdownMenu trigger={standalone ? <><MoreHorizontal />更多菜单</> : <MoreHorizontal />} triggerProps={standalone ? undefined : { variant: "ghost", size: "icon", "aria-label": `${row.tool}的更多操作` }} align={standalone ? "start" : "end"} items={[ { value: "open", label: "打开网站", icon: <ExternalLink />, shortcut: "↵", disabled: !tableLink(String(row.website)), onSelect: () => { const url = tableLink(String(row.website)); if (url) window.open(url, "_blank", "noopener,noreferrer"); } }, { value: "duplicate", label: "创建副本", icon: <Copy />, shortcut: "⌘D", onSelect: onDuplicate }, { value: "copy", label: "复制链接", icon: <Link2 />, shortcut: "⌘L", onSelect: () => { navigator.clipboard.writeText(tableLink(String(row.website)) ?? String(row.website)).then(() => toast.success("链接已复制"), () => toast.error("复制失败,请重试")); } }, { value: "move", label: "移动到", icon: <FolderInput />, divider: true, submenu: categories.map(category => ({ value: category, label: category, icon: <Folder />, onSelect: () => { onSave({ ...row, category }); toast.success(`已移动到 ${category}`); } })) }, { value: "star", label: "收藏", icon: <Star />, type: "checkbox", checked: starred, keepOpen: true, onSelect: () => setStarred(current => !current) }, { value: "export", label: "导出记录", icon: <Download />, disabled: true }, { value: "delete", label: "删除记录", icon: <Trash2 />, divider: true, danger: true, onSelect: onDelete }, ]} />;}
export function StackRowOptions({ row, categories, onSave, onDuplicate, onDelete }: StackMenuProps) { const [open, setOpen] = useState(false); const [name, setName] = useState(String(row.tool)); const [website, setWebsite] = useState(String(row.website)); return <div className="flex items-center gap-4"> <KalosDialog trigger={<Pencil />} triggerProps={{ variant: "ghost", size: "icon", "aria-label": `编辑 ${row.tool}` }} title="编辑工具" open={open} onOpenChange={next => { if (next) { setName(String(row.tool)); setWebsite(String(row.website)); } setOpen(next); }} footer={<> <KalosButton variant="outline" onClick={() => setOpen(false)}>取消</KalosButton> <KalosButton disabled={!name.trim()} onClick={() => { onSave({ ...row, tool: name.trim(), website: website.trim() }); setOpen(false); toast.success("已保存更改"); }}>保存</KalosButton> </>}> <FieldGroup className="gap-3.5"> <KalosInput label="名称" placeholder="工具名称" value={name} onChange={event => setName(event.target.value)} /> <KalosInput label="网站" placeholder="example.com" value={website} onChange={event => setWebsite(event.target.value)} /> </FieldGroup> </KalosDialog> <StackMoreMenu row={row} categories={categories} onSave={onSave} onDuplicate={onDuplicate} onDelete={onDelete} /> </div>;}
export function StackBrandIcon({ name }: { name: string }) { const clipId = useId(); if (name === "Figma") return <svg viewBox="0 0 18 27" aria-hidden="true"><path fill="#0acf83" d="M4.5 27A4.5 4.5 0 0 0 9 22.5V18H4.5a4.5 4.5 0 0 0 0 9Z"/><path fill="#a259ff" d="M4.5 9H9v9H4.5a4.5 4.5 0 0 1 0-9Z"/><path fill="#f24e1e" d="M4.5 0H9v9H4.5a4.5 4.5 0 0 1 0-9Z"/><path fill="#ff7262" d="M9 0h4.5a4.5 4.5 0 0 1 0 9H9Z"/><circle fill="#1abcfe" cx="13.5" cy="13.5" r="4.5"/></svg>; if (name === "Framer") return <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M5 0h14v8h-7l7 8h-7v8l-7-8V8h7L5 0Z"/></svg>; if (name === "Linear") return <svg viewBox="0 0 24 24" aria-hidden="true"><defs><clipPath id={clipId}><circle cx="12" cy="12" r="11"/></clipPath></defs><g clipPath={`url(#${clipId})`}><circle cx="12" cy="12" r="11" fill="#6864df"/><path d="m-2 8 18 18M-2 12l14 14M-2 16l10 10M0 4l20 20M3 1l21 21" stroke="white" strokeWidth="1.2"/></g></svg>; if (name === "Loom") return <svg viewBox="0 0 24 24" fill="none" stroke="#6254ff" strokeWidth="2.5" aria-hidden="true"><circle cx="12" cy="12" r="4"/>{Array.from({ length: 12 }, (_, index) => <path key={index} d="M12 1v4" transform={`rotate(${index * 30} 12 12)`}/>)}</svg>; return <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="m12 2 12 21H0Z"/></svg>;}
export function StackTitleIcon() { return <svg viewBox="0 0 20 20" aria-hidden="true"><rect x="1" y="1" width="18" height="18" rx="4" fill="currentColor"/><path d="M6 6.5h8M6 10h8M6 13.5h4" stroke="white" strokeWidth="1.5" strokeLinecap="round"/></svg>;}
export const stackColumns: KalosTableColumn[] = [ { key: "tool", label: "Tool", type: "title", icon: <AlignLeft />, width: 216 }, { key: "category", label: "Category", type: "tag", icon: <Grid2X2 />, width: 188 }, { key: "website", label: "Website", type: "link", icon: <Link2 />, width: 148 }, { key: "renews", label: "Renews", type: "date", icon: <CalendarDays />, width: 128 },];
export const stackRows: KalosTableRow[] = [ { id: "figma", tool: "Figma", icon: <StackBrandIcon name="Figma" />, category: "Design", website: "figma.com", renews: "Mar 12, 2026" }, { id: "framer", tool: "Framer", icon: <StackBrandIcon name="Framer" />, category: "Website", website: "framer.com", renews: "May 2, 2026" }, { id: "linear", tool: "Linear", icon: <StackBrandIcon name="Linear" />, category: "Issue tracking", website: "linear.app", renews: "Jan 4, 2026" }, { id: "loom", tool: "Loom", icon: <StackBrandIcon name="Loom" />, category: "Video", website: "loom.com", renews: "Nov 18, 2026" }, { id: "vercel", tool: "Vercel", icon: <StackBrandIcon name="Vercel" />, category: "Hosting", website: "vercel.com", renews: "Aug 21, 2026" },];export const stackTones = { Design: "violet", Website: "pink", "Issue tracking": "blue", Video: "green", Hosting: "gray" } as const;
export function DesignStackTable({ columns = stackColumns, initialRows = stackRows, compact = false, className = "max-w-[804px]",}: { columns?: KalosTableColumn[]; initialRows?: KalosTableRow[]; compact?: boolean; className?: string;}) { const [rows, setRows] = useState(initialRows);
return ( <KalosTable title="Design Stack" icon={<StackTitleIcon />} columns={columns} rows={rows} onRowsChange={setRows} tagTones={stackTones} editable={!compact} selectable={!compact} sortable={!compact} defaultSort={compact ? undefined : { key: "tool", direction: "ascending" }} addRowLabel="Add row" rowActionsLabel="Options" rowActionsWidth={84} className={className} rowActions={row => ( <StackRowOptions row={row} categories={Object.keys(stackTones)} onSave={next => setRows(current => current.map(item => item.id === row.id ? next : item))} onDuplicate={() => { setRows(current => [...current, { ...row, id: crypto.randomUUID(), tool: `${row.tool} 副本` }]); toast.success("已创建副本"); }} onDelete={() => { setRows(current => current.filter(item => item.id !== row.id)); toast.success("记录已删除"); }} /> )} /> );}
export default function Example() { return <><Toaster /><DesignStackTable /></>;}基础表格
INV001 | 已支付 | 信用卡 | ¥250.00 |
INV002 | 待支付 | 支付宝 | ¥150.00 |
INV003 | 未支付 | 银行转账 | ¥350.00 |
分页表格
同时安装 Table 与 Pagination,通过 Table 的 pagination 插槽接入分页器。
001 | 陈晓 | contact1@example.com | 设计部 |
002 | 林舟 | contact2@example.com | 研发部 |
003 | 周宁 | contact3@example.com | 运营部 |
004 | 吴昕 | contact4@example.com | 设计部 |
005 | 张悦 | contact5@example.com | 研发部 |
006 | 李明 | contact6@example.com | 运营部 |
007 | 王珂 | contact7@example.com | 设计部 |
008 | 赵然 | contact8@example.com | 研发部 |
009 | 陈晓 | contact9@example.com | 运营部 |
010 | 林舟 | contact10@example.com | 设计部 |
011 | 周宁 | contact11@example.com | 研发部 |
012 | 吴昕 | contact12@example.com | 运营部 |
013 | 张悦 | contact13@example.com | 设计部 |
014 | 李明 | contact14@example.com | 研发部 |
015 | 王珂 | contact15@example.com | 运营部 |
016 | 赵然 | contact16@example.com | 设计部 |
017 | 陈晓 | contact17@example.com | 研发部 |
018 | 林舟 | contact18@example.com | 运营部 |
019 | 周宁 | contact19@example.com | 设计部 |
020 | 吴昕 | contact20@example.com | 研发部 |
表格线
INV001 | 已支付 | 信用卡 | ¥250.00 |
INV002 | 待支付 | 支付宝 | ¥150.00 |
INV003 | 未支付 | 银行转账 | ¥350.00 |
行操作
组件使用指南.pdf | 2.4 MB | |
联系人.csv | 128 KB | |
设计规范.fig | 8.1 MB |
可编辑数据表格
Design Stack
Figma | Mar 12, 2026 | ||||
Framer | May 2, 2026 | ||||
Linear | Jan 4, 2026 | ||||
Loom | Nov 18, 2026 | ||||
Vercel | Aug 21, 2026 |
A6
合并单元格
设计资料 | |||||
设计 | 设计规范.fig | Figma | 8.1 MB | 11 月 12 日 | 陈晓 |
组件指南与图标 · 2.4 MB | PDF / SVG | 11 月 12 日 · 林舟 | |||
备注 | 包含组件使用指南、图标源文件与导出资源 | ||||
共享文档 | |||||
联系人 | 联系人.csv | CSV | 128 KB | 11 月 11 日 | 林舟 |
会议纪要.md | Markdown | 24 KB | 11 月 10 日 · 周宁 | ||
合计 · 5 个文件 | 10.7 MB · 3 位负责人 | ||||
拖拽排序
按空格拾取行,方向键选择位置,Enter 放置,Escape 取消;也可拖动行首手柄。
组件使用指南.pdf | PDF | 2.4 MB | |
联系人.csv | CSV | 128 KB | |
设计规范.fig | Figma | 8.1 MB | |
会议纪要.md | Markdown | 24 KB |
列筛选
INV001 | 已支付 | 信用卡 | ¥250.00 |
INV002 | 待支付 | 支付宝 | ¥150.00 |
INV003 | 未支付 | 银行转账 | ¥350.00 |
API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
variant | "default" | "bordered" | default | bordered 显示行列分隔线;合并单元格保留完整的跨列边缘 |
columns | KalosTableColumn[] | — | 列的 key 唯一;包含 label、icon、width、type、editable、sortable、filterable、colSpan、options 和 format |
columns[].colSpan | number | ((row: KalosTableRow) => number) | 1 | 横向合并单元格;覆盖的列不再渲染,合并数限制在当前行剩余列范围内 |
columns[].filterable | boolean | false | 在列标题旁显示筛选按钮;按该列的值筛选,多列条件共同生效,选择全部清除当前列筛选 |
columns[].type | "title" | "text" | "tag" | "link" | "number" | "date" | text | 所有列默认左对齐;标题列显示行图标;数字使用等宽数字;日期按时间排序;链接仅允许 HTTP/HTTPS。文字超出列宽时缩略,仅实际溢出时显示完整文字提示,编辑时关闭提示 |
columns[].editable / options | boolean / string[] | true / — | 锁定列或限定标签选项;编辑、粘贴和清空均遵循列约束 |
columns[].format | (value: string | number, row: KalosTableRow) => ReactNode | — | 自定义单元格显示,排序与编辑仍使用原始值 |
rows / defaultRows | KalosTableRow[] | [] | 受控数据或挂载时的初始数据;每行必须有唯一 id,可选 icon,其他字段对应列 key;替换整份初始数据时使用 key 重新挂载 |
onRowsChange | (rows: KalosTableRow[]) => void | — | 编辑、粘贴、清空、新增、删除、撤销和重做时返回全部行;受控 rows 需要在此更新 |
title / icon | string / ReactNode | — | 表格上方的标题栏及图标;可编辑时显示撤销与重做 |
aria-label | string | title 或 表格 | 没有可见标题时为表格命名 |
editable / selectable / sortable | boolean | false | 分别开启编辑、复选框选择与列排序;连续点击排序在升序、降序、原始顺序间切换 |
reorderable | boolean | false | 开启行首拖拽手柄;支持鼠标、触控和键盘排序,完成后通过 onRowsChange 返回新顺序 |
defaultSort | { key: string; direction: "ascending" | "descending" } | — | 初始排序列与方向 |
tagTones | Record<string, "gray" | "violet" | "pink" | "blue" | "green" | "amber"> | {} | 按标签文字映射浅深主题语义颜色;未指定时为灰色 |
onSelectionChange | (ids: string[]) => void | — | 复选框变化后返回选中行的稳定 id |
footer | ReactNode[] | — | 与数据列一一对应的汇总单元格;选择列与操作列自动留空 |
pagination | ReactNode | — | 表格容器内的底部分页区,与表格共用背景、圆角和外框;可传入 KalosPagination,页码与数据由调用方控制 |
scrollAreaProps | ComponentProps<"div"> | — | 表格内容滚动区的原生属性。style.maxHeight 限制数据行及可选汇总行的可见高度,纵向滚动时表头和分页区保持可见。key 可在换页时重置滚动位置 |
rowActions | (row: KalosTableRow) => ReactNode | — | 末列的操作内容;默认示例使用编辑与更多菜单,和 Dropdown Menu 共用操作项 |
rowActionsLabel | string | 操作 | 末列的可见标题与可访问名称 |
rowActionsWidth | number | 160 | 操作列宽度,单位 px;与普通列使用相同的宽度分配规则 |
caption / addRowLabel / emptyMessage | string | — / 添加行 / 暂无数据 | 可选表格说明、新增按钮文字和空数据反馈 |
dir / className | "ltr" | "rtl" / string | — | 阅读方向与容器布局样式;窄屏在表格内横向滚动 |
键盘操作
- 拖拽手柄:Space / Enter
- 拾取或放置行;拾取后用方向键选择位置,Escape 取消
- 方向键 / Home / End
- 编辑或选择模式下在单元格间移动;Home / End 跳到当前行首尾
- Shift + 方向键
- 扩展单元格选区
- Ctrl / ⌘ + 方向键
- 跳到首末行或列
- Enter / F2 / 双击
- 编辑当前可编辑单元格;输入字符也可开始编辑
- 单击 Tag / Enter / Space
- 打开分类 Select,下拉选项保留 Tag 颜色;方向键选择,Enter 确认,Esc 取消
- Enter / Tab / Shift+Tab
- 编辑时保存并移到下方、下一格或上一格
- Esc
- 取消编辑或收起多格选区
- 点击外部 / Tab 离开
- 取消单元格聚焦;在分类下拉菜单内操作时保留聚焦
- Delete / Backspace
- 清空选区,跳过锁定列
- Ctrl / ⌘ + C / X / V
- 复制、剪切或粘贴制表符分隔数据;只读表格仅允许复制
- Ctrl / ⌘ + Z / Shift+Z
- 撤销或重做最近 100 次数据更改
- Ctrl / ⌘ + A
- 单元格聚焦时选中全部数据单元格