@qijenchen/design-system 0.1.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +93 -0
- package/src/README.md +32 -0
- package/src/components/Accordion/accordion.tsx +104 -0
- package/src/components/Alert/alert.tsx +188 -0
- package/src/components/AppShell/_demo-helpers.tsx +198 -0
- package/src/components/AppShell/app-shell.tsx +364 -0
- package/src/components/AspectRatio/aspect-ratio.tsx +58 -0
- package/src/components/Avatar/avatar.tsx +368 -0
- package/src/components/Badge/badge.tsx +104 -0
- package/src/components/Breadcrumb/breadcrumb.tsx +609 -0
- package/src/components/BulkActionBar/bulk-action-bar.tsx +156 -0
- package/src/components/Button/button-group.tsx +96 -0
- package/src/components/Button/button.tsx +539 -0
- package/src/components/Calendar/calendar.tsx +411 -0
- package/src/components/Carousel/carousel.tsx +371 -0
- package/src/components/Chart/chart.tsx +376 -0
- package/src/components/Checkbox/checkbox-group.tsx +94 -0
- package/src/components/Checkbox/checkbox.tsx +237 -0
- package/src/components/Chip/chip.tsx +359 -0
- package/src/components/CircularProgress/circular-progress.tsx +204 -0
- package/src/components/Coachmark/coachmark.tsx +255 -0
- package/src/components/Combobox/combobox.tsx +826 -0
- package/src/components/Command/command.tsx +187 -0
- package/src/components/DataTable/active-editor-controller.ts +72 -0
- package/src/components/DataTable/cell-registry.tsx +520 -0
- package/src/components/DataTable/column-types.ts +180 -0
- package/src/components/DataTable/data-table-column-visibility-panel.tsx +261 -0
- package/src/components/DataTable/data-table-filter-panel.tsx +813 -0
- package/src/components/DataTable/data-table-interaction-layer.tsx +483 -0
- package/src/components/DataTable/data-table-sort-manager.tsx +210 -0
- package/src/components/DataTable/data-table.css +165 -0
- package/src/components/DataTable/data-table.tsx +2924 -0
- package/src/components/DataTable/filter-operators.ts +225 -0
- package/src/components/DataTable/filter-tree.ts +313 -0
- package/src/components/DataTable/lib/column-meta.ts +79 -0
- package/src/components/DateGrid/date-grid.tsx +209 -0
- package/src/components/DatePicker/date-picker.tsx +1114 -0
- package/src/components/DescriptionList/description-list.tsx +141 -0
- package/src/components/Dialog/dialog.tsx +267 -0
- package/src/components/DropdownMenu/dropdown-menu.tsx +475 -0
- package/src/components/Empty/empty.tsx +108 -0
- package/src/components/Field/field-context.ts +136 -0
- package/src/components/Field/field-types.ts +52 -0
- package/src/components/Field/field-wrapper.tsx +348 -0
- package/src/components/Field/field.tsx +535 -0
- package/src/components/FieldControlGroup/field-control-group.tsx +136 -0
- package/src/components/FileItem/file-item.tsx +322 -0
- package/src/components/FileUpload/file-upload.tsx +326 -0
- package/src/components/FileViewer/file-viewer-types.ts +76 -0
- package/src/components/FileViewer/file-viewer.tsx +1065 -0
- package/src/components/FileViewer/image-renderer.tsx +256 -0
- package/src/components/HoverCard/hover-card.tsx +79 -0
- package/src/components/Input/input.tsx +233 -0
- package/src/components/LinkInput/link-input.tsx +304 -0
- package/src/components/Menu/menu-item.tsx +334 -0
- package/src/components/NameCard/name-card.tsx +319 -0
- package/src/components/Notice/notice.tsx +196 -0
- package/src/components/NumberInput/number-input.tsx +203 -0
- package/src/components/OverflowIndicator/overflow-indicator.tsx +156 -0
- package/src/components/PeoplePicker/avatar-stack-overflow.ts +100 -0
- package/src/components/PeoplePicker/people-picker-helpers.ts +76 -0
- package/src/components/PeoplePicker/people-picker.tsx +455 -0
- package/src/components/PeoplePicker/person-display.tsx +358 -0
- package/src/components/Popover/popover.tsx +183 -0
- package/src/components/ProgressBar/progress-bar.tsx +157 -0
- package/src/components/README.md +58 -0
- package/src/components/RadioGroup/radio-group.tsx +261 -0
- package/src/components/Rating/rating.tsx +295 -0
- package/src/components/ScrollArea/scroll-area.tsx +110 -0
- package/src/components/SegmentedControl/segmented-control.tsx +304 -0
- package/src/components/Select/select.tsx +658 -0
- package/src/components/SelectMenu/select-menu.tsx +430 -0
- package/src/components/SelectionControl/selection-item.tsx +261 -0
- package/src/components/Separator/separator.tsx +48 -0
- package/src/components/Sheet/sheet.tsx +240 -0
- package/src/components/Sidebar/sidebar.tsx +1280 -0
- package/src/components/Skeleton/skeleton.tsx +35 -0
- package/src/components/Slider/slider.tsx +158 -0
- package/src/components/Steps/steps.tsx +850 -0
- package/src/components/Switch/switch.tsx +285 -0
- package/src/components/Tabs/tabs.tsx +515 -0
- package/src/components/Tag/tag.tsx +246 -0
- package/src/components/Textarea/textarea.tsx +280 -0
- package/src/components/TimePicker/time-columns.tsx +260 -0
- package/src/components/TimePicker/time-picker.tsx +419 -0
- package/src/components/Toast/toast.tsx +129 -0
- package/src/components/Tooltip/tooltip.tsx +68 -0
- package/src/components/TreeView/tree-view.tsx +1031 -0
- package/src/hooks/use-controllable.ts +40 -0
- package/src/hooks/use-is-narrow-viewport.ts +19 -0
- package/src/hooks/use-is-touch-device.ts +21 -0
- package/src/hooks/use-overflow-items.ts +256 -0
- package/src/index.ts +85 -0
- package/src/lib/README.md +82 -0
- package/src/lib/drag-visual.ts +272 -0
- package/src/lib/i18n/README.md +60 -0
- package/src/lib/i18n/i18n-context.tsx +129 -0
- package/src/lib/multi-select-ordering.ts +61 -0
- package/src/lib/utils.ts +93 -0
- package/src/patterns/README.md +67 -0
- package/src/patterns/element-anatomy/item-anatomy.tsx +744 -0
- package/src/patterns/header-canonical/chrome-header.tsx +175 -0
- package/src/patterns/header-canonical/header-canonical.css +27 -0
- package/src/patterns/horizontal-overflow/horizontal-overflow.tsx +217 -0
- package/src/patterns/overlay-surface/overlay-surface.tsx +191 -0
- package/src/patterns/resize-handle/resize-handle.tsx +188 -0
- package/src/stories-helpers/anatomy/anatomy-utils.tsx +64 -0
- package/src/tokens/README.md +53 -0
- package/src/tokens/color/primitives.css +429 -0
- package/src/tokens/color/semantic.css +539 -0
- package/src/tokens/elevation/overlay-geometry.ts +13 -0
- package/src/tokens/layoutSpace/layoutSpace.css +36 -0
- package/src/tokens/motion/motion.css +30 -0
- package/src/tokens/motion/motion.ts +17 -0
- package/src/tokens/opacity/opacity.css +23 -0
- package/src/tokens/radius/radius.css +19 -0
- package/src/tokens/typography/typography.css +118 -0
- package/src/tokens/uiSize/icon-size.ts +52 -0
- package/src/tokens/uiSize/uiSize.css +125 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
// @benchmark-unverified-blanket: file-level retraction per M22 (d) — claims herein not individually URL-cited; treat as unverified visual/usage rumor unless retrofit per-claim. Hook escape preserved.
|
|
2
|
+
// code-quality-allow: file-size — Cell Registry 含 10 cell-type components(string/number/date/time/select/multiSelect/person/multiPerson/boolean/url)+ shared helpers,split-into-files 會破壞 type-keyed registry SSOT canonical
|
|
3
|
+
// DataTable Cell Registry — type-keyed SSOT for cell rendering(Phase C 2026-05-05)
|
|
4
|
+
//
|
|
5
|
+
// 對齊 M17 SSOT consolidation + audit recommendation:
|
|
6
|
+
// 原 `renderTypedValue` switch + `EditableCellContent` switch 兩條平行 type-switch 已 collapse
|
|
7
|
+
// 為**一張 type → cell component** registry。每個 cell component 同時處理 display / edit mode,
|
|
8
|
+
// 靠底層 Field control 的 `mode` prop 切換。
|
|
9
|
+
//
|
|
10
|
+
// 設計原則:
|
|
11
|
+
// - 每個 cell component 接同一組 props(`CellComponentProps`)
|
|
12
|
+
// - 用 `variant="naked"` — DataTable cell-as-input substrate(對齊 Field B1 chrome=bare)
|
|
13
|
+
// - 消費 full Field 家族 primitive(無 stub)
|
|
14
|
+
// - 不再用 `meta._editable` 私有 flag — `isEditable` 直接顯式入參(消除 M1 hack)
|
|
15
|
+
//
|
|
16
|
+
// World-class 對照(@benchmark-unverified):AG Grid cellRendererSelector / Material X-Grid
|
|
17
|
+
// `valueGetter + renderCell` / Notion property type registry。
|
|
18
|
+
|
|
19
|
+
import * as React from 'react'
|
|
20
|
+
import type { ComponentType } from 'react'
|
|
21
|
+
import { Pencil } from 'lucide-react'
|
|
22
|
+
import { cn } from '@/lib/utils'
|
|
23
|
+
import type { ColumnType } from './column-types'
|
|
24
|
+
import { Input } from '@/design-system/components/Input/input'
|
|
25
|
+
import { Textarea } from '@/design-system/components/Textarea/textarea'
|
|
26
|
+
import { NumberInput } from '@/design-system/components/NumberInput/number-input'
|
|
27
|
+
import { Select } from '@/design-system/components/Select/select'
|
|
28
|
+
import { Combobox } from '@/design-system/components/Combobox/combobox'
|
|
29
|
+
import { DatePicker } from '@/design-system/components/DatePicker/date-picker'
|
|
30
|
+
import { TimePicker } from '@/design-system/components/TimePicker/time-picker'
|
|
31
|
+
import { PeoplePicker } from '@/design-system/components/PeoplePicker/people-picker'
|
|
32
|
+
import { LinkInput } from '@/design-system/components/LinkInput/link-input'
|
|
33
|
+
import { Checkbox } from '@/design-system/components/Checkbox/checkbox'
|
|
34
|
+
import { Button } from '@/design-system/components/Button/button'
|
|
35
|
+
import type { PersonValue } from '@/design-system/components/PeoplePicker/person-display'
|
|
36
|
+
import { FieldSurfaceProvider } from '@/design-system/components/Field/field-context'
|
|
37
|
+
|
|
38
|
+
// ── Types ────────────────────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
export type CellMode = 'display' | 'edit'
|
|
41
|
+
export type CellSize = 'sm' | 'md' | 'lg'
|
|
42
|
+
|
|
43
|
+
export interface CellComponentProps {
|
|
44
|
+
// any-allow: free-form column value(consumer-defined,跨 type 共用 signature)
|
|
45
|
+
value: any
|
|
46
|
+
// any-allow: free-form consumer meta bag(prefix / options / formatOptions / locale / linkLabel 等)
|
|
47
|
+
meta: Record<string, any>
|
|
48
|
+
mode: CellMode
|
|
49
|
+
size: CellSize
|
|
50
|
+
autoRowHeight: boolean
|
|
51
|
+
/** 該 cell 是否可編。replaces 舊 `meta._editable` 私有 flag(Phase C M1 hack 移除)。 */
|
|
52
|
+
isEditable?: boolean
|
|
53
|
+
/** 2026-05-13:cell 是否 disabled(state overlay,orthogonal to display/edit lifecycle)。
|
|
54
|
+
* per codex Q3 verdict:不擴 CellMode='disabled',加 prop。各 Cell function 收 true 時
|
|
55
|
+
* 傳 `mode='disabled'` 給 inner Field control,各 Field 內部走具體 disabled token(非 wrapper opacity)。 */
|
|
56
|
+
isDisabled?: boolean
|
|
57
|
+
/** Cell 進 edit mode → 提交新值(blur / Enter / option select 都觸發)— 提交後**自動 exit edit**。
|
|
58
|
+
* 適用 single-shot commit:string / number / select(single)/ person(single)/ date / time / boolean / url。 */
|
|
59
|
+
onCommit?: (next: unknown) => void
|
|
60
|
+
/** Live commit — 提交新值但 **不 exit edit**(popover 持續開)。
|
|
61
|
+
* 適用 multi-select 類:multiSelect / multiPerson — user 連續勾選,直到點外面才關。
|
|
62
|
+
* 對齊 Notion / Linear / Airtable canonical:multi-pick popover 不在每次 toggle 後關閉。 */
|
|
63
|
+
onCommitLive?: (next: unknown) => void
|
|
64
|
+
/** Esc 取消編輯,不 commit。 */
|
|
65
|
+
onCancel?: () => void
|
|
66
|
+
/** URL cell 專用:hover 顯示的 Pencil 鈕 → 進 edit mode(read mode 保留 link click 語意)。 */
|
|
67
|
+
onRequestEdit?: () => void
|
|
68
|
+
/** Per-keystroke draft propagation(2026-05-10 Phase 7 D.3 portal Field virtualizer unmount preserve draft):
|
|
69
|
+
* Edit mode 內部 input onChange/onValueChange 每 keystroke 呼叫 onDraft,讓 lifted draft state(in
|
|
70
|
+
* data-table.tsx)持有 user 編輯中字。Cell DOM unmount(virtualizer scroll out)時 draft 在
|
|
71
|
+
* parent state 不丟;mount-back 時 portal Field value 從 draft 取,user 字保留。
|
|
72
|
+
* 非 portal mode(inline edit)不傳此 prop,各 Cell 走原 uncontrolled defaultValue 路徑。 */
|
|
73
|
+
onDraft?: (next: unknown) => void
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
/** 鍵盤 commit / cancel — string / number cell edit mode 共用 */
|
|
79
|
+
function makeKeyHandler(
|
|
80
|
+
onCommit?: (v: unknown) => void,
|
|
81
|
+
onCancel?: () => void,
|
|
82
|
+
parseValue?: (raw: string) => unknown,
|
|
83
|
+
) {
|
|
84
|
+
return (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
85
|
+
if (e.key === 'Escape') { e.preventDefault(); onCancel?.() }
|
|
86
|
+
if (e.key === 'Enter') {
|
|
87
|
+
e.preventDefault()
|
|
88
|
+
const raw = (e.target as HTMLInputElement).value
|
|
89
|
+
onCommit?.(parseValue ? parseValue(raw) : raw)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const sizeForInput = (size: CellSize): CellSize => size
|
|
95
|
+
|
|
96
|
+
/** 2026-05-13 Q3 helper(per codex Q3 verdict):cell display + isDisabled → Field mode='disabled'。
|
|
97
|
+
* Cell display lifecycle 不擴 CellMode='disabled',而是各 Cell 在 display branch 翻譯 isDisabled
|
|
98
|
+
* → Field mode='disabled' prop。inner Field 內部走具體 disabled token(text-fg-disabled / bg-disabled 等),
|
|
99
|
+
* 非 wrapper blanket opacity-disabled 逃生艙(per color.spec.md:729)。 */
|
|
100
|
+
const displayOrDisabled = (isDisabled?: boolean): 'display' | 'disabled' =>
|
|
101
|
+
isDisabled ? 'disabled' : 'display'
|
|
102
|
+
|
|
103
|
+
// ── Cell Components ──────────────────────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
function StringCell({ value, meta, mode, size, isDisabled, autoRowHeight, onCommit, onCancel, onDraft }: CellComponentProps) {
|
|
106
|
+
// 2026-05-14 I9 fix(per codex+Layer A 共識):meta.maxLines opt-in line-clamp。
|
|
107
|
+
// display autoRow 用 Tailwind arbitrary line-clamp 支援 N rows;edit textarea field-sizing
|
|
108
|
+
// 已 auto-grow to content,natural match clamp。
|
|
109
|
+
// 2026-05-16 Round 5 audit Dim 27 fix:narrow type 取代 `as any` cast。
|
|
110
|
+
const maxLines: number | undefined = (meta as { maxLines?: number } | undefined)?.maxLines
|
|
111
|
+
const clampClass = maxLines && autoRowHeight ? `line-clamp-[${maxLines}]` : undefined
|
|
112
|
+
// string type canonical(2026-05-05 v2 user 校正:input space ≥ display space):
|
|
113
|
+
// - autoRowHeight: Textarea(display + edit)— display wrap text 撐高 row,edit textarea
|
|
114
|
+
// 多行輸入、`!h-full` 填 cell。對齊 Notion long-text cell canonical。
|
|
115
|
+
// - fixed: Input(display + edit)— 單行 truncate display,單行 input edit;Field naked intrinsic
|
|
116
|
+
// 高 = cell 高 = h-field-md,文字位置 display↔edit 完全一致。對齊 AG Grid / Material X-Grid。
|
|
117
|
+
// - autoRowHeight 是 table 框架決定(consumer 不需 per-column 設 meta.wrap)。
|
|
118
|
+
// - 互動(Textarea):Esc cancel / Cmd|Ctrl+Enter commit / blur commit;Enter 保留換行
|
|
119
|
+
// - 互動(Input):Esc cancel / Enter commit / blur commit
|
|
120
|
+
const v = value != null ? String(value) : ''
|
|
121
|
+
if (mode === 'display') {
|
|
122
|
+
return autoRowHeight
|
|
123
|
+
? <Textarea variant="naked" mode={displayOrDisabled(isDisabled)} value={v} className={clampClass} />
|
|
124
|
+
: <Input variant="naked" mode={displayOrDisabled(isDisabled)} value={v} />
|
|
125
|
+
}
|
|
126
|
+
if (autoRowHeight) {
|
|
127
|
+
// 2026-05-14 I8 fix(per codex verdict + user 抓「edit cell shrink」):
|
|
128
|
+
// 原 `wrapRows = value.length / 40` 字元估算不準(對應實際 column width 不同
|
|
129
|
+
// → cell 進 edit shrink)。改 CSS `field-sizing: content`(Chrome 123+ / FF 122+ /
|
|
130
|
+
// Safari 17+)讓 textarea 自動 grow to content,匹配 display wrap 真實高度。
|
|
131
|
+
// Fallback rows 仍保留給舊 browser(rows attr 在 field-sizing 支援時被覆蓋)。
|
|
132
|
+
const newlineRows = (v.match(/\n/g) || []).length + 1
|
|
133
|
+
const wrapRows = Math.ceil(v.length / 40)
|
|
134
|
+
const estimateRows = Math.min(10, Math.max(1, newlineRows, wrapRows))
|
|
135
|
+
return (
|
|
136
|
+
<Textarea
|
|
137
|
+
autoFocus
|
|
138
|
+
variant="naked"
|
|
139
|
+
size={sizeForInput(size)}
|
|
140
|
+
rows={estimateRows}
|
|
141
|
+
defaultValue={v}
|
|
142
|
+
// any-allow: CSS `field-sizing` 屬性 Chrome 123+/FF 122+/Safari 17+ 支援但 TypeScript lib.dom
|
|
143
|
+
// 尚未加 type;narrow 到 CSSProperties 仍需 cast,保留 single-site any 較 type aug 簡潔。
|
|
144
|
+
style={{ fieldSizing: 'content' } as React.CSSProperties}
|
|
145
|
+
onChange={(e) => onDraft?.((e.target as HTMLTextAreaElement).value)}
|
|
146
|
+
onBlur={(e) => onCommit?.((e.target as HTMLTextAreaElement).value)}
|
|
147
|
+
onKeyDown={(e) => {
|
|
148
|
+
if (e.key === 'Escape') { e.preventDefault(); onCancel?.() }
|
|
149
|
+
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
|
|
150
|
+
e.preventDefault()
|
|
151
|
+
onCommit?.((e.target as HTMLTextAreaElement).value)
|
|
152
|
+
}
|
|
153
|
+
}}
|
|
154
|
+
/>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
return (
|
|
158
|
+
<Input
|
|
159
|
+
autoFocus
|
|
160
|
+
variant="naked"
|
|
161
|
+
size={sizeForInput(size)}
|
|
162
|
+
defaultValue={v}
|
|
163
|
+
onChange={(e) => onDraft?.(e.target.value)}
|
|
164
|
+
onBlur={(e) => onCommit?.(e.target.value)}
|
|
165
|
+
onKeyDown={makeKeyHandler(onCommit, onCancel)}
|
|
166
|
+
/>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function NumberCell({ value, meta, mode, size, isDisabled, onCommit, onCancel, onDraft }: CellComponentProps) {
|
|
171
|
+
// currency 透過 columnType-aware prefix:type='currency' → 預設 '$'(可 override)
|
|
172
|
+
const isCurrency = meta?.type === 'currency'
|
|
173
|
+
const prefix = isCurrency ? (meta?.prefix ?? '$') : meta?.prefix
|
|
174
|
+
if (mode === 'display') {
|
|
175
|
+
return (
|
|
176
|
+
<NumberInput
|
|
177
|
+
variant="naked"
|
|
178
|
+
mode={displayOrDisabled(isDisabled)}
|
|
179
|
+
value={value as number | null}
|
|
180
|
+
prefix={prefix}
|
|
181
|
+
suffix={meta?.suffix}
|
|
182
|
+
precision={meta?.precision}
|
|
183
|
+
locale={meta?.locale}
|
|
184
|
+
/>
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
// Edit mode value pre-fill canonical(2026-05-05):NumberInput edit 強制 controlled
|
|
188
|
+
// (`value={value ?? ''}`)— 若 NumberCell 以 `defaultValue` 傳入,NumberInput value=undefined → ''
|
|
189
|
+
// empty。對齊 cell-as-input「edit mode 自動帶入 display 值」(對齊 Notion / Airtable 共識),
|
|
190
|
+
// 改用 local state controlled。User typing → setLocalValue;blur/Enter → onCommit(localValue)。
|
|
191
|
+
const initial = typeof value === 'number' ? value : null
|
|
192
|
+
const [localValue, setLocalValue] = React.useState<number | null>(initial)
|
|
193
|
+
return (
|
|
194
|
+
<NumberInput
|
|
195
|
+
autoFocus
|
|
196
|
+
variant="naked"
|
|
197
|
+
size={sizeForInput(size)}
|
|
198
|
+
value={localValue}
|
|
199
|
+
onChange={(v) => { setLocalValue(v); onDraft?.(v) }}
|
|
200
|
+
prefix={prefix}
|
|
201
|
+
suffix={meta?.suffix}
|
|
202
|
+
precision={meta?.precision}
|
|
203
|
+
onBlur={() => onCommit?.(localValue)}
|
|
204
|
+
onKeyDown={(e) => {
|
|
205
|
+
if (e.key === 'Escape') { e.preventDefault(); onCancel?.() }
|
|
206
|
+
if (e.key === 'Enter') { e.preventDefault(); onCommit?.(localValue) }
|
|
207
|
+
}}
|
|
208
|
+
/>
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Cell-as-input dismiss canonical(2026-05-05):defaultOpen=true 開始 → user click 外 popover 關
|
|
213
|
+
// → 元件 fire onOpenChange(false) → cell call onCancel exit edit。否則 cell 卡 edit mode 不可 re-trigger
|
|
214
|
+
// (對齊 Airtable / Notion canonical:click 外即關)。
|
|
215
|
+
const dismissOnClose = (onCancel?: () => void) => (open: boolean) => { if (!open) onCancel?.() }
|
|
216
|
+
|
|
217
|
+
// Mode-keyed remount canonical(2026-05-05):display↔edit 切換時,因 React reconciliation 同 type 同
|
|
218
|
+
// position 會重用 instance,導致 `useState(defaultOpen)` 只在首次 mount 跑(那時 mode='display'
|
|
219
|
+
// defaultOpen 沒給→預設 false)。後續 mode='edit' 即使傳 defaultOpen=true 也無效。
|
|
220
|
+
// Fix:`key={mode}` 強制 React unmount + remount,每次切 mode 都重跑 useState init。
|
|
221
|
+
// 對齊 Notion / Airtable cell-as-input「display 跟 edit 是不同 mount cycle」語義。
|
|
222
|
+
|
|
223
|
+
function DateCell({ value, meta, mode, size, isDisabled, isEditable, onCommit, onCancel }: CellComponentProps) {
|
|
224
|
+
if (mode === 'display') {
|
|
225
|
+
return (
|
|
226
|
+
<DatePicker
|
|
227
|
+
key="display"
|
|
228
|
+
variant="naked"
|
|
229
|
+
mode={displayOrDisabled(isDisabled)}
|
|
230
|
+
value={value as string | null}
|
|
231
|
+
size={size}
|
|
232
|
+
formatOptions={meta?.formatOptions}
|
|
233
|
+
locale={meta?.locale}
|
|
234
|
+
// Indicator(calendar icon)= editable affordance(2026-05-10 user 糾正)。
|
|
235
|
+
// Non-editable cell 不該顯 picker indicator(誤導 read-only 為 editable)。
|
|
236
|
+
// 對齊 UrlCell L394 / BooleanCell L368 既有 isEditable conditional pattern。
|
|
237
|
+
showDisplayEndIcon={isEditable === true}
|
|
238
|
+
/>
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
return (
|
|
242
|
+
<DatePicker
|
|
243
|
+
key="edit"
|
|
244
|
+
autoFocus
|
|
245
|
+
variant="naked"
|
|
246
|
+
size={sizeForInput(size)}
|
|
247
|
+
value={typeof value === 'string' ? value : null}
|
|
248
|
+
showTime={meta?.includeTime === true}
|
|
249
|
+
onChange={(v) => onCommit?.(v)}
|
|
250
|
+
defaultOpen
|
|
251
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
252
|
+
/>
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function TimeCell({ value, meta, mode, size, isDisabled, isEditable, onCommit, onCancel }: CellComponentProps) {
|
|
257
|
+
if (mode === 'display') {
|
|
258
|
+
return (
|
|
259
|
+
<TimePicker
|
|
260
|
+
key="display"
|
|
261
|
+
variant="naked"
|
|
262
|
+
mode={displayOrDisabled(isDisabled)}
|
|
263
|
+
value={value as string | null}
|
|
264
|
+
size={size}
|
|
265
|
+
formatOptions={meta?.formatOptions}
|
|
266
|
+
locale={meta?.locale}
|
|
267
|
+
showDisplayEndIcon={isEditable === true} // 2026-05-10:non-editable 不顯 indicator
|
|
268
|
+
/>
|
|
269
|
+
)
|
|
270
|
+
}
|
|
271
|
+
return (
|
|
272
|
+
<TimePicker
|
|
273
|
+
key="edit"
|
|
274
|
+
variant="naked"
|
|
275
|
+
size={sizeForInput(size)}
|
|
276
|
+
value={typeof value === 'string' ? value : null}
|
|
277
|
+
showSeconds={meta?.showSeconds === true}
|
|
278
|
+
minuteStep={meta?.minuteStep}
|
|
279
|
+
secondStep={meta?.secondStep}
|
|
280
|
+
onChange={(v) => onCommit?.(v)}
|
|
281
|
+
defaultOpen
|
|
282
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
283
|
+
/>
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function SelectCell({ value, meta, mode, size, isDisabled, isEditable, onCommit, onCancel }: CellComponentProps) {
|
|
288
|
+
// Display canonical(2026-05-05):cell IS variant,default plain text(no Tag pill 疊在 cell border 內)。
|
|
289
|
+
// Consumer 可在 column meta.display='tag' opt-in 內容導向的 Tag 視覺(category 含色彩標籤等)。
|
|
290
|
+
// 對齊 JTable / AG Grid「renderer/editor 視覺一致」canonical。
|
|
291
|
+
const displayMode = (meta?.display as 'plain' | 'tag' | undefined) ?? 'plain'
|
|
292
|
+
if (mode === 'display') {
|
|
293
|
+
return (
|
|
294
|
+
<Select
|
|
295
|
+
key="display"
|
|
296
|
+
variant="naked"
|
|
297
|
+
mode={displayOrDisabled(isDisabled)}
|
|
298
|
+
value={value as string | null}
|
|
299
|
+
options={meta?.options ?? []}
|
|
300
|
+
size={size}
|
|
301
|
+
display={displayMode}
|
|
302
|
+
showDisplayEndIcon={isEditable === true} // 2026-05-10:non-editable 不顯 chevron indicator
|
|
303
|
+
/>
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
return (
|
|
307
|
+
<Select
|
|
308
|
+
key="edit"
|
|
309
|
+
autoFocus
|
|
310
|
+
variant="naked"
|
|
311
|
+
size={sizeForInput(size)}
|
|
312
|
+
options={meta?.options ?? []}
|
|
313
|
+
value={value as string | null | undefined}
|
|
314
|
+
onChange={(v) => onCommit?.(v)}
|
|
315
|
+
// B7(2026-05-05):cell 編輯時支援 inline search,沿用 Select.searchable 機制(對齊 cell-as-input
|
|
316
|
+
// 「沿用既有輸入框互動」原則)。Default false,consumer 在 meta.searchable 開啟。
|
|
317
|
+
searchable={meta?.searchable === true}
|
|
318
|
+
display={displayMode}
|
|
319
|
+
defaultOpen
|
|
320
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
321
|
+
/>
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function MultiSelectCell({ value, meta, mode, size, isDisabled, autoRowHeight, isEditable, onCommitLive, onCancel }: CellComponentProps) {
|
|
326
|
+
const wrap = autoRowHeight && meta?.wrap === true
|
|
327
|
+
if (mode === 'display') {
|
|
328
|
+
return (
|
|
329
|
+
<Combobox
|
|
330
|
+
key="display"
|
|
331
|
+
variant="naked"
|
|
332
|
+
mode={displayOrDisabled(isDisabled)}
|
|
333
|
+
value={(value as string[] | null) ?? []}
|
|
334
|
+
options={meta?.options ?? []}
|
|
335
|
+
wrap={wrap}
|
|
336
|
+
size={size}
|
|
337
|
+
showDisplayEndIcon={isEditable === true} // 2026-05-10:non-editable 不顯 chevron indicator
|
|
338
|
+
/>
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
// Multi 用 onCommitLive(commit 但不 exit edit)— 每勾一項即時生效,popover 持續開
|
|
342
|
+
// 直到點外面;onOpenChange(false) → onCancel exit edit。對齊 Notion / Linear / Airtable canonical。
|
|
343
|
+
return (
|
|
344
|
+
<Combobox
|
|
345
|
+
key="edit"
|
|
346
|
+
variant="naked"
|
|
347
|
+
size={sizeForInput(size)}
|
|
348
|
+
options={meta?.options ?? []}
|
|
349
|
+
value={Array.isArray(value) ? (value as string[]) : []}
|
|
350
|
+
onChange={(v) => onCommitLive?.(v)}
|
|
351
|
+
defaultOpen
|
|
352
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
353
|
+
/>
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function PersonCell({ value, mode, size, isDisabled, isEditable, onCommit, onCancel, meta }: CellComponentProps) {
|
|
358
|
+
if (mode === 'display') {
|
|
359
|
+
// 2026-05-10:non-editable 不顯 chevron indicator(對齊 UrlCell isEditable conditional pattern)
|
|
360
|
+
return <PeoplePicker key="display" variant="naked" mode={displayOrDisabled(isDisabled)} value={value as PersonValue | null} size={size} showDisplayEndIcon={isEditable === true} />
|
|
361
|
+
}
|
|
362
|
+
return (
|
|
363
|
+
<PeoplePicker
|
|
364
|
+
key="edit"
|
|
365
|
+
variant="naked"
|
|
366
|
+
size={sizeForInput(size)}
|
|
367
|
+
value={value as PersonValue | null}
|
|
368
|
+
people={meta?.people ?? []}
|
|
369
|
+
// PeoplePicker onChange 永遠 emit array(API contract);single mode commit 取首位
|
|
370
|
+
onChange={(next) => onCommit?.(next[0] ?? null)}
|
|
371
|
+
defaultOpen
|
|
372
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
373
|
+
/>
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function MultiPersonCell({ value, mode, size, isDisabled, isEditable, onCommitLive, onCancel, meta }: CellComponentProps) {
|
|
378
|
+
if (mode === 'display') {
|
|
379
|
+
// 2026-05-10:non-editable 不顯 chevron indicator
|
|
380
|
+
return <PeoplePicker key="display" variant="naked" mode={displayOrDisabled(isDisabled)} value={(value as PersonValue[]) ?? []} size={size} showDisplayEndIcon={isEditable === true} />
|
|
381
|
+
}
|
|
382
|
+
// Multi 用 onCommitLive(commit 但不 exit edit)— 每勾一人即時生效,popover 持續開
|
|
383
|
+
// 直到點外面;onOpenChange(false) → onCancel exit edit。對齊 multiSelect canonical。
|
|
384
|
+
return (
|
|
385
|
+
<PeoplePicker
|
|
386
|
+
key="edit"
|
|
387
|
+
variant="naked"
|
|
388
|
+
size={sizeForInput(size)}
|
|
389
|
+
value={Array.isArray(value) ? (value as PersonValue[]) : []}
|
|
390
|
+
people={meta?.people ?? []}
|
|
391
|
+
onChange={(next) => onCommitLive?.(next)}
|
|
392
|
+
defaultOpen
|
|
393
|
+
onOpenChange={dismissOnClose(onCancel)}
|
|
394
|
+
/>
|
|
395
|
+
)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function BooleanCell({ value, mode, meta, size, isEditable, isDisabled, onCommit }: CellComponentProps) {
|
|
399
|
+
// boolean 不分 read/edit mode — display 渲 mode='display' 純展示;editable 時直接 toggle Checkbox
|
|
400
|
+
// 2026-05-13 codex V1 fix:editable=true + disabled=true 之前 fall through to live Checkbox,
|
|
401
|
+
// onCheckedChange 仍 fire(violate disabled contract)。Fix:`!isEditable || isDisabled` →
|
|
402
|
+
// 走 display branch,Checkbox 拿 disabled mode + 不接 onCheckedChange。
|
|
403
|
+
if (mode === 'display' && (!isEditable || isDisabled)) {
|
|
404
|
+
return <Checkbox variant="naked" mode={displayOrDisabled(isDisabled)} checked={value === true} />
|
|
405
|
+
}
|
|
406
|
+
return (
|
|
407
|
+
<Checkbox
|
|
408
|
+
size={size === 'lg' ? 'lg' : 'md'}
|
|
409
|
+
checked={value === true}
|
|
410
|
+
onCheckedChange={(checked) => onCommit?.(checked === true)}
|
|
411
|
+
aria-label={meta?.ariaLabel ?? '切換'}
|
|
412
|
+
/>
|
|
413
|
+
)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* UrlCell — Phase C drift fix:
|
|
418
|
+
* 舊 EditableCellContent edit mode 對 url 走 plain `<Input>`(失去 URL 驗證 + auto-link)。
|
|
419
|
+
* 現改用 `<LinkInput>` edit mode → 保留 URL parse / hostname 顯示一致性 + 鍵盤 commit / cancel。
|
|
420
|
+
* read mode 仍 `<LinkInput mode={displayOrDisabled(isDisabled)}>` = 一致 SSOT。
|
|
421
|
+
* editable 互動:hover 時右側出 Pencil 鈕 → 進 edit(保留 link click 語意,對齊原 spec)。
|
|
422
|
+
*/
|
|
423
|
+
function UrlCell({ value, meta, mode, size, isDisabled, isEditable, onRequestEdit, onCommit, onCancel }: CellComponentProps) {
|
|
424
|
+
if (mode === 'display') {
|
|
425
|
+
// showDisplayEndIcon ← D path Phase 2(2026-05-08):Field naked wrapper 包 anchor,與 Input edit 同 chrome
|
|
426
|
+
const display = (
|
|
427
|
+
<LinkInput variant="naked" mode={displayOrDisabled(isDisabled)} value={value as string | null} label={meta?.linkLabel} size={size} showDisplayEndIcon />
|
|
428
|
+
)
|
|
429
|
+
// 2026-05-13 codex V1 fix:disabled URL 不顯 Pencil affordance(parent onRequestEdit 已被攔但 UI 仍誤導)
|
|
430
|
+
if (!isEditable || isDisabled) return display
|
|
431
|
+
// editable read mode:hover Pencil 鈕(對齊 spec 第十二段「url:read = 連結 + Pencil」)
|
|
432
|
+
return (
|
|
433
|
+
<span className="group/cell relative flex items-center w-full"> {/* @naked-row-mode-allow: URL hover-Pencil 是 inline action 不是 value content,items-center 鎖 Pencil 對齊行高第一行(autoRow 跟 fixed 皆同視覺正確) */}
|
|
434
|
+
<span className="flex-1 min-w-0">{display}</span>
|
|
435
|
+
<Button
|
|
436
|
+
variant="tertiary"
|
|
437
|
+
size="xs"
|
|
438
|
+
iconOnly
|
|
439
|
+
startIcon={Pencil}
|
|
440
|
+
aria-label="編輯連結"
|
|
441
|
+
className={cn('ml-1 opacity-0 group-hover/cell:opacity-100 transition-opacity')}
|
|
442
|
+
onClick={(e) => {
|
|
443
|
+
e.stopPropagation()
|
|
444
|
+
onRequestEdit?.()
|
|
445
|
+
}}
|
|
446
|
+
/>
|
|
447
|
+
</span>
|
|
448
|
+
)
|
|
449
|
+
}
|
|
450
|
+
// edit mode value pre-fill canonical(2026-05-05):LinkInput edit `value` prop 強制 controlled
|
|
451
|
+
// (line 113 `useState(value ?? '')`)+ `showLink = !editing && hasValidValue` 預設顯 link 不顯 input
|
|
452
|
+
// → cell-as-input editing 場景需要 input 直接 focus 編輯。改用 plain `<Input>`(uncontrolled
|
|
453
|
+
// `defaultValue` 正確 pre-fill,Input.tsx `value={value}` 是 undefined → uncontrolled 走 defaultValue)。
|
|
454
|
+
// URL 驗證等 deferred 到 commit phase(consumer 可在 onCommit 時 validate)。
|
|
455
|
+
return (
|
|
456
|
+
<Input
|
|
457
|
+
autoFocus
|
|
458
|
+
variant="naked"
|
|
459
|
+
size={sizeForInput(size)}
|
|
460
|
+
defaultValue={value != null ? String(value) : ''}
|
|
461
|
+
onBlur={(e) => onCommit?.(e.target.value)}
|
|
462
|
+
onKeyDown={makeKeyHandler(onCommit, onCancel)}
|
|
463
|
+
/>
|
|
464
|
+
)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// ── Registry ────────────────────────────────────────────────────────────────
|
|
468
|
+
//
|
|
469
|
+
// type → cell component。新增 columnType 必同步註冊一條(否則 fallback 到 string)。
|
|
470
|
+
|
|
471
|
+
export const cellRegistry: Record<ColumnType, ComponentType<CellComponentProps>> = {
|
|
472
|
+
string: StringCell,
|
|
473
|
+
number: NumberCell,
|
|
474
|
+
currency: NumberCell, // 共用 NumberCell — currency-ness 走 meta.type 判 prefix='$'
|
|
475
|
+
date: DateCell,
|
|
476
|
+
time: TimeCell,
|
|
477
|
+
select: SelectCell,
|
|
478
|
+
multiSelect: MultiSelectCell,
|
|
479
|
+
person: PersonCell,
|
|
480
|
+
multiPerson: MultiPersonCell,
|
|
481
|
+
boolean: BooleanCell,
|
|
482
|
+
url: UrlCell,
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** Resolve cell component by type;default = StringCell(consumer 沒設 type 的 fallback)。
|
|
486
|
+
* 2026-05-12 Stream C Cluster B fix:wrap with FieldSurfaceProvider `surface='table-cell'`
|
|
487
|
+
* 讓所有 Cell 內的 Field family controls 透過 `useFieldSurface()` 取得「我在 cell 裡」context,
|
|
488
|
+
* 取代散落的 `variant === 'naked'` cell-detection heuristic + per-prop hardcoded padding。
|
|
489
|
+
*
|
|
490
|
+
* **2026-05-13 (a) perf fix(user 拍板 + codex V1 verdict + Layer A grep root cause)**:
|
|
491
|
+
* 原 factory pattern 每次 call 在 function body 內宣告新 `CellWithSurface` FC closure → 每 scroll
|
|
492
|
+
* × 每 visible cell 都 return 新 FC reference → React 認 component type 變,**整 subtree mount/
|
|
493
|
+
* unmount cascade**(Field + ItemPrefix/Suffix + Avatar / Tag / PersonDisplay)。
|
|
494
|
+
* Fix:每 ColumnType **module-level 預建** wrapped FC + `React.memo`,resolve 走 cached map,
|
|
495
|
+
* identity stable across scroll → memo 真生效 + subtree 不 mount/unmount。
|
|
496
|
+
* Cite world-class:AG Grid「cell renderer per-type stable reference」/ MUI X DataGrid「memoized
|
|
497
|
+
* subcomponents」/ Glide Data Grid「DOM virtualization 加解掛 = bottleneck」。 */
|
|
498
|
+
const cellWithSurfaceCache = new Map<ColumnType | '_default_', ComponentType<CellComponentProps>>()
|
|
499
|
+
|
|
500
|
+
function buildCellWithSurface(Inner: ComponentType<CellComponentProps>, key: string): ComponentType<CellComponentProps> {
|
|
501
|
+
const CellWithSurface = React.memo(function CellWithSurface(props: CellComponentProps) {
|
|
502
|
+
return (
|
|
503
|
+
<FieldSurfaceProvider surface="table-cell">
|
|
504
|
+
<Inner {...props} />
|
|
505
|
+
</FieldSurfaceProvider>
|
|
506
|
+
)
|
|
507
|
+
})
|
|
508
|
+
;(CellWithSurface as { displayName?: string }).displayName = `CellWithSurface(${key})`
|
|
509
|
+
return CellWithSurface as ComponentType<CellComponentProps>
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Pre-build per-type cached wrapped components(module-level,one-time init)
|
|
513
|
+
for (const type of Object.keys(cellRegistry) as ColumnType[]) {
|
|
514
|
+
cellWithSurfaceCache.set(type, buildCellWithSurface(cellRegistry[type], type))
|
|
515
|
+
}
|
|
516
|
+
cellWithSurfaceCache.set('_default_', buildCellWithSurface(StringCell, 'StringCell-fallback'))
|
|
517
|
+
|
|
518
|
+
export function resolveCellComponent(type?: ColumnType): ComponentType<CellComponentProps> {
|
|
519
|
+
return cellWithSurfaceCache.get(type ?? '_default_') ?? cellWithSurfaceCache.get('_default_')!
|
|
520
|
+
}
|