@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.
Files changed (119) hide show
  1. package/package.json +93 -0
  2. package/src/README.md +32 -0
  3. package/src/components/Accordion/accordion.tsx +104 -0
  4. package/src/components/Alert/alert.tsx +188 -0
  5. package/src/components/AppShell/_demo-helpers.tsx +198 -0
  6. package/src/components/AppShell/app-shell.tsx +364 -0
  7. package/src/components/AspectRatio/aspect-ratio.tsx +58 -0
  8. package/src/components/Avatar/avatar.tsx +368 -0
  9. package/src/components/Badge/badge.tsx +104 -0
  10. package/src/components/Breadcrumb/breadcrumb.tsx +609 -0
  11. package/src/components/BulkActionBar/bulk-action-bar.tsx +156 -0
  12. package/src/components/Button/button-group.tsx +96 -0
  13. package/src/components/Button/button.tsx +539 -0
  14. package/src/components/Calendar/calendar.tsx +411 -0
  15. package/src/components/Carousel/carousel.tsx +371 -0
  16. package/src/components/Chart/chart.tsx +376 -0
  17. package/src/components/Checkbox/checkbox-group.tsx +94 -0
  18. package/src/components/Checkbox/checkbox.tsx +237 -0
  19. package/src/components/Chip/chip.tsx +359 -0
  20. package/src/components/CircularProgress/circular-progress.tsx +204 -0
  21. package/src/components/Coachmark/coachmark.tsx +255 -0
  22. package/src/components/Combobox/combobox.tsx +826 -0
  23. package/src/components/Command/command.tsx +187 -0
  24. package/src/components/DataTable/active-editor-controller.ts +72 -0
  25. package/src/components/DataTable/cell-registry.tsx +520 -0
  26. package/src/components/DataTable/column-types.ts +180 -0
  27. package/src/components/DataTable/data-table-column-visibility-panel.tsx +261 -0
  28. package/src/components/DataTable/data-table-filter-panel.tsx +813 -0
  29. package/src/components/DataTable/data-table-interaction-layer.tsx +483 -0
  30. package/src/components/DataTable/data-table-sort-manager.tsx +210 -0
  31. package/src/components/DataTable/data-table.css +165 -0
  32. package/src/components/DataTable/data-table.tsx +2924 -0
  33. package/src/components/DataTable/filter-operators.ts +225 -0
  34. package/src/components/DataTable/filter-tree.ts +313 -0
  35. package/src/components/DataTable/lib/column-meta.ts +79 -0
  36. package/src/components/DateGrid/date-grid.tsx +209 -0
  37. package/src/components/DatePicker/date-picker.tsx +1114 -0
  38. package/src/components/DescriptionList/description-list.tsx +141 -0
  39. package/src/components/Dialog/dialog.tsx +267 -0
  40. package/src/components/DropdownMenu/dropdown-menu.tsx +475 -0
  41. package/src/components/Empty/empty.tsx +108 -0
  42. package/src/components/Field/field-context.ts +136 -0
  43. package/src/components/Field/field-types.ts +52 -0
  44. package/src/components/Field/field-wrapper.tsx +348 -0
  45. package/src/components/Field/field.tsx +535 -0
  46. package/src/components/FieldControlGroup/field-control-group.tsx +136 -0
  47. package/src/components/FileItem/file-item.tsx +322 -0
  48. package/src/components/FileUpload/file-upload.tsx +326 -0
  49. package/src/components/FileViewer/file-viewer-types.ts +76 -0
  50. package/src/components/FileViewer/file-viewer.tsx +1065 -0
  51. package/src/components/FileViewer/image-renderer.tsx +256 -0
  52. package/src/components/HoverCard/hover-card.tsx +79 -0
  53. package/src/components/Input/input.tsx +233 -0
  54. package/src/components/LinkInput/link-input.tsx +304 -0
  55. package/src/components/Menu/menu-item.tsx +334 -0
  56. package/src/components/NameCard/name-card.tsx +319 -0
  57. package/src/components/Notice/notice.tsx +196 -0
  58. package/src/components/NumberInput/number-input.tsx +203 -0
  59. package/src/components/OverflowIndicator/overflow-indicator.tsx +156 -0
  60. package/src/components/PeoplePicker/avatar-stack-overflow.ts +100 -0
  61. package/src/components/PeoplePicker/people-picker-helpers.ts +76 -0
  62. package/src/components/PeoplePicker/people-picker.tsx +455 -0
  63. package/src/components/PeoplePicker/person-display.tsx +358 -0
  64. package/src/components/Popover/popover.tsx +183 -0
  65. package/src/components/ProgressBar/progress-bar.tsx +157 -0
  66. package/src/components/README.md +58 -0
  67. package/src/components/RadioGroup/radio-group.tsx +261 -0
  68. package/src/components/Rating/rating.tsx +295 -0
  69. package/src/components/ScrollArea/scroll-area.tsx +110 -0
  70. package/src/components/SegmentedControl/segmented-control.tsx +304 -0
  71. package/src/components/Select/select.tsx +658 -0
  72. package/src/components/SelectMenu/select-menu.tsx +430 -0
  73. package/src/components/SelectionControl/selection-item.tsx +261 -0
  74. package/src/components/Separator/separator.tsx +48 -0
  75. package/src/components/Sheet/sheet.tsx +240 -0
  76. package/src/components/Sidebar/sidebar.tsx +1280 -0
  77. package/src/components/Skeleton/skeleton.tsx +35 -0
  78. package/src/components/Slider/slider.tsx +158 -0
  79. package/src/components/Steps/steps.tsx +850 -0
  80. package/src/components/Switch/switch.tsx +285 -0
  81. package/src/components/Tabs/tabs.tsx +515 -0
  82. package/src/components/Tag/tag.tsx +246 -0
  83. package/src/components/Textarea/textarea.tsx +280 -0
  84. package/src/components/TimePicker/time-columns.tsx +260 -0
  85. package/src/components/TimePicker/time-picker.tsx +419 -0
  86. package/src/components/Toast/toast.tsx +129 -0
  87. package/src/components/Tooltip/tooltip.tsx +68 -0
  88. package/src/components/TreeView/tree-view.tsx +1031 -0
  89. package/src/hooks/use-controllable.ts +40 -0
  90. package/src/hooks/use-is-narrow-viewport.ts +19 -0
  91. package/src/hooks/use-is-touch-device.ts +21 -0
  92. package/src/hooks/use-overflow-items.ts +256 -0
  93. package/src/index.ts +85 -0
  94. package/src/lib/README.md +82 -0
  95. package/src/lib/drag-visual.ts +272 -0
  96. package/src/lib/i18n/README.md +60 -0
  97. package/src/lib/i18n/i18n-context.tsx +129 -0
  98. package/src/lib/multi-select-ordering.ts +61 -0
  99. package/src/lib/utils.ts +93 -0
  100. package/src/patterns/README.md +67 -0
  101. package/src/patterns/element-anatomy/item-anatomy.tsx +744 -0
  102. package/src/patterns/header-canonical/chrome-header.tsx +175 -0
  103. package/src/patterns/header-canonical/header-canonical.css +27 -0
  104. package/src/patterns/horizontal-overflow/horizontal-overflow.tsx +217 -0
  105. package/src/patterns/overlay-surface/overlay-surface.tsx +191 -0
  106. package/src/patterns/resize-handle/resize-handle.tsx +188 -0
  107. package/src/stories-helpers/anatomy/anatomy-utils.tsx +64 -0
  108. package/src/tokens/README.md +53 -0
  109. package/src/tokens/color/primitives.css +429 -0
  110. package/src/tokens/color/semantic.css +539 -0
  111. package/src/tokens/elevation/overlay-geometry.ts +13 -0
  112. package/src/tokens/layoutSpace/layoutSpace.css +36 -0
  113. package/src/tokens/motion/motion.css +30 -0
  114. package/src/tokens/motion/motion.ts +17 -0
  115. package/src/tokens/opacity/opacity.css +23 -0
  116. package/src/tokens/radius/radius.css +19 -0
  117. package/src/tokens/typography/typography.css +118 -0
  118. package/src/tokens/uiSize/icon-size.ts +52 -0
  119. package/src/tokens/uiSize/uiSize.css +125 -0
@@ -0,0 +1,1114 @@
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 — foundational composite(DatePicker single + Range + showTime + format/ISO helpers + TimePickerSidePanel sub-components),拆 sub-file 會 (a) 增 cross-file context binding 複雜度 (b) M21 過度抽象(每 helper 1 consumer)。等 inline filter UI 真接入第 2 consumer 再拆。
3
+ import * as React from 'react'
4
+ import { X, Calendar as CalendarIcon, ArrowRight } from 'lucide-react'
5
+ import { cn } from '@/lib/utils'
6
+ import type { FieldMode, FieldVariant } from '@/design-system/components/Field/field-types'
7
+ import { fieldWrapperStyles, bareInputStyles, EMPTY_DISPLAY, nakedCellRowModeAlign, fieldDisplayTextClass } from '@/design-system/components/Field/field-wrapper'
8
+ import { ItemInlineAction, ItemSuffix } from '@/design-system/patterns/element-anatomy/item-anatomy'
9
+ import { Popover, PopoverTrigger, PopoverAnchor, PopoverContent } from '@/design-system/components/Popover/popover'
10
+ import { DateGrid } from '@/design-system/components/DateGrid/date-grid'
11
+ import { Button } from '@/design-system/components/Button/button'
12
+ import { SurfaceFooter } from '@/design-system/patterns/overlay-surface/overlay-surface'
13
+ import { useFieldContext } from '@/design-system/components/Field/field-context'
14
+ import {
15
+ TimeColumns,
16
+ isoToTimeParts,
17
+ timePartsToString,
18
+ type TimeParts,
19
+ type TimeStep,
20
+ } from '@/design-system/components/TimePicker/time-columns'
21
+ import { ICON_SIZE } from '@/design-system/tokens/uiSize/icon-size'
22
+
23
+ // ── Format ──────────────────────────────────────────────────────────────────
24
+
25
+ export interface DateFormatOptions {
26
+ /** Intl.DateTimeFormat options(預設 { year: 'numeric', month: '2-digit', day: '2-digit' }) */
27
+ formatOptions?: Intl.DateTimeFormatOptions
28
+ /** locale(預設 'en-US') */
29
+ locale?: string
30
+ }
31
+
32
+ /**
33
+ * Default format:**YYYY/MM/DD**(對齊 Ant Design 順序,year-first ISO-like)。
34
+ * 棄 `en-US` `MM/DD/YYYY`(month-first 美式)— 美式順序在 international DS 反直覺
35
+ * (跟 ISO date 視覺對不上,跟 sort 順序也對不上)。Ant / Material X / Apple HIG
36
+ * 一致 year-first。Consumer 想自訂可傳 `formatOptions` + `locale`。
37
+ */
38
+ function formatDate(
39
+ value: string | number | Date,
40
+ options: DateFormatOptions = {},
41
+ ): string {
42
+ const date = value instanceof Date ? value : new Date(value)
43
+ if (Number.isNaN(date.getTime())) return String(value)
44
+ // 若 consumer 顯式傳 formatOptions / locale → 走 Intl.DateTimeFormat
45
+ if (options.formatOptions || options.locale) {
46
+ return new Intl.DateTimeFormat(options.locale ?? 'en-US', options.formatOptions ?? { year: 'numeric', month: '2-digit', day: '2-digit' }).format(date)
47
+ }
48
+ // 預設:YYYY/MM/DD(直接組,locale-independent + 視覺穩定)
49
+ const y = date.getFullYear()
50
+ const m = String(date.getMonth() + 1).padStart(2, '0')
51
+ const d = String(date.getDate()).padStart(2, '0')
52
+ return `${y}/${m}/${d}`
53
+ }
54
+
55
+ /** 顯示用:date 或 datetime,根據 showTime / showSeconds 切換 */
56
+ function formatDateOrDateTime(
57
+ iso: string | null | undefined,
58
+ showTime: boolean,
59
+ showSeconds: boolean,
60
+ options: DateFormatOptions = {},
61
+ ): string {
62
+ if (!iso) return ''
63
+ const dateText = formatDate(iso, options)
64
+ if (!showTime) return dateText
65
+ const time = isoToTimeParts(iso)
66
+ if (!time) return dateText
67
+ return `${dateText} ${timePartsToString(time, showSeconds)}`
68
+ }
69
+
70
+ // ── ISO <-> Date conversion ─────────────────────────────────────────────────
71
+ // date-only:'YYYY-MM-DD'(local-time 語意,不帶時區)
72
+ // datetime :'YYYY-MM-DDTHH:MM:SS'(同 local-time 語意)
73
+
74
+ function isoToDate(iso: string | null | undefined): Date | undefined {
75
+ if (!iso) return undefined
76
+ const datePart = iso.slice(0, 10)
77
+ const [y, m, d] = datePart.split('-').map(Number)
78
+ if (!y || !m || !d) return undefined
79
+ const date = new Date(y, m - 1, d)
80
+ const time = isoToTimeParts(iso)
81
+ if (time) {
82
+ date.setHours(time.hours, time.minutes, time.seconds)
83
+ }
84
+ return date
85
+ }
86
+
87
+ /**
88
+ * Issue 10 typed input parser(2026-05-10):接 user 自由輸入字串,parse 出 ISO date(YYYY-MM-DD)
89
+ * 或 ISO datetime(YYYY-MM-DDTHH:MM[:SS])。
90
+ *
91
+ * 支援 format(per Material X DatePicker / Ant DatePicker typed input idiom):
92
+ * - `YYYY-MM-DD` / `YYYY/MM/DD` / `YYYY.MM.DD`(ISO + 慣例 separator)
93
+ * - `MM/DD/YYYY` / `MM-DD-YYYY`(US locale)
94
+ * - `DD/MM/YYYY` / `DD-MM-YYYY`(EU locale)
95
+ * - `YYYY-MM-DD HH:MM[:SS]` / `YYYY-MM-DDTHH:MM[:SS]`(datetime,showTime 才接)
96
+ * - native `Date.parse()` fallback(handle 'Mar 12 2025' 等英文 RFC)
97
+ *
98
+ * Return:`{ iso, valid }`。Invalid → `valid=false` + `iso=null`,UI 顯 aria-invalid。
99
+ * Partial input(打到一半)→ valid=false 但不顯 error UI(consumer 用 onBlur / Enter 才檢驗)。
100
+ *
101
+ * 對齊 Material X-DatePicker `formats` parser + Ant DatePicker `format` array + dayjs.parse。
102
+ */
103
+ function parseDateInput(input: string, opts: { allowTime: boolean }): { iso: string | null; valid: boolean } {
104
+ const trimmed = input.trim()
105
+ if (trimmed === '') return { iso: null, valid: true } // empty = no value, OK
106
+ // 1. ISO date YYYY-MM-DD or with separators / . /(可選 time YYYY-MM-DD[T| ]HH:MM[:SS])
107
+ const isoDateMatch = trimmed.match(/^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})(?:[T\s](\d{1,2}):(\d{1,2})(?::(\d{1,2}))?)?$/)
108
+ if (isoDateMatch) {
109
+ const [, y, m, d, hh, mm, ss] = isoDateMatch
110
+ const date = new Date(Number(y), Number(m) - 1, Number(d), Number(hh ?? 0), Number(mm ?? 0), Number(ss ?? 0))
111
+ if (isNaN(date.getTime()) || date.getMonth() !== Number(m) - 1) return { iso: null, valid: false }
112
+ const datePart = dateToIso(date)
113
+ if (hh != null && opts.allowTime) {
114
+ const h2 = String(Number(hh)).padStart(2, '0')
115
+ const m2 = String(Number(mm)).padStart(2, '0')
116
+ const s2 = String(Number(ss ?? 0)).padStart(2, '0')
117
+ return { iso: `${datePart}T${h2}:${m2}:${s2}`, valid: true }
118
+ }
119
+ return { iso: datePart, valid: true }
120
+ }
121
+ // 2. US / EU locale MM/DD/YYYY or DD/MM/YYYY — ambiguous;v1 fallback Date.parse 並接受結果
122
+ // 3. native Date.parse fallback(RFC-style 'Mar 12 2025')
123
+ const parsed = new Date(trimmed)
124
+ if (!isNaN(parsed.getTime())) {
125
+ const datePart = dateToIso(parsed)
126
+ if (opts.allowTime && (trimmed.includes(':') || trimmed.includes('T'))) {
127
+ const h2 = String(parsed.getHours()).padStart(2, '0')
128
+ const m2 = String(parsed.getMinutes()).padStart(2, '0')
129
+ const s2 = String(parsed.getSeconds()).padStart(2, '0')
130
+ return { iso: `${datePart}T${h2}:${m2}:${s2}`, valid: true }
131
+ }
132
+ return { iso: datePart, valid: true }
133
+ }
134
+ return { iso: null, valid: false }
135
+ }
136
+
137
+ function dateToIso(date: Date | undefined): string {
138
+ if (!date) return ''
139
+ const y = date.getFullYear()
140
+ const m = String(date.getMonth() + 1).padStart(2, '0')
141
+ const d = String(date.getDate()).padStart(2, '0')
142
+ return `${y}-${m}-${d}`
143
+ }
144
+
145
+ function combineDateAndTime(date: Date, time: TimeParts): string {
146
+ const datePart = dateToIso(date)
147
+ const hh = String(time.hours).padStart(2, '0')
148
+ const mi = String(time.minutes).padStart(2, '0')
149
+ const ss = String(time.seconds).padStart(2, '0')
150
+ return `${datePart}T${hh}:${mi}:${ss}`
151
+ }
152
+
153
+ function nowIsoDateTime(): string {
154
+ const d = new Date()
155
+ return combineDateAndTime(d, {
156
+ hours: d.getHours(),
157
+ minutes: d.getMinutes(),
158
+ seconds: d.getSeconds(),
159
+ })
160
+ }
161
+
162
+ function addDays(date: Date, n: number): Date {
163
+ const next = new Date(date)
164
+ next.setDate(next.getDate() + n)
165
+ return next
166
+ }
167
+
168
+ // ── TimePickerSidePanel ────────────────────────────────────────────────
169
+ //
170
+ // DatePicker showTime / Range showTime 共用的右側時間 panel(canonical 2026-05-03 v8)。
171
+ //
172
+ // ── Caption row alignment canonical(永遠跟 calendar 年月對齊)──
173
+ // 結構必須符合 DateGrid month_caption 同樣的 pt-3 + h-field-xs + mb-3 規格,讓 title
174
+ // 跟 calendar 「April 2026」字 baseline 在同一 Y 座標(垂直對齊)。
175
+ // Y 座標推導:
176
+ // - panel root pt-3 = 12px top 對齊 DateGrid p-3 top
177
+ // - h-field-xs = 24px header,title 純 flex items-center justify-center → 真正水平+垂直置中
178
+ // - mb-3 = 12px gap 對齊 DateGrid month_caption mb-3
179
+ // → title text center Y = 12 + 12 = 24px(from CalendarTimeContainer top)
180
+ // → calendar caption text center Y = 12(p-3 top)+ 12(caption row half)= 24px ✓ 同一 Y
181
+ // ⚠️ 若改 DateGrid p-3(例如 p-2)→ 必同步改 TimePicker pt-3,否則 caption 行錯位。
182
+ // 兩處共識在 spec.md「Spacing canonical」段 + 本 comment 雙鎖。
183
+ //
184
+ // ── Header divider canonical(無 border-b)──
185
+ // Header 下方無 divider,對齊 DateGrid month_caption(無 border-b,只 mb-3 gap)。
186
+ // DS internal canonical(M23)優先於 Ant time-picker header divider 慣例 — 兩 panel
187
+ // 同層級 caption 視覺對稱,引入 divider 會破對稱。
188
+ //
189
+ // ── Bottom padding canonical(0)──
190
+ // Root 用 pt-3 而非 py-3:bottom = 0,讓 columns 連續延伸到 SurfaceFooter border-t。
191
+ // Ant / Material time picker idiom — time list 視覺感「continuous scroll」延伸到 footer
192
+ // divider,bottom padding 12px 反而讓 list 看起來「截斷」。
193
+ // 此處與 DateGrid p-3(bottom 12)有意 asymmetric:Calendar cells 不該撞 footer divider
194
+ // (cells 是離散 grid),time list 是 scroll list 撞 divider 反而合理。
195
+
196
+ interface TimePickerSidePanelProps {
197
+ value?: TimeParts
198
+ onChange: (next: TimeParts) => void
199
+ showSeconds?: boolean
200
+ minuteStep?: TimeStep
201
+ secondStep?: TimeStep
202
+ }
203
+
204
+ function TimePickerSidePanel({
205
+ value,
206
+ onChange,
207
+ showSeconds = false,
208
+ minuteStep = 1,
209
+ secondStep = 1,
210
+ className,
211
+ }: TimePickerSidePanelProps & { className?: string }) {
212
+ // Dynamic header text — 顯示當前選擇的 HH:MM(對齊 user Q4 + Ant idiom)
213
+ const headerText = value
214
+ ? timePartsToString(value, showSeconds)
215
+ : (showSeconds ? '--:--:--' : '--:--')
216
+
217
+ return (
218
+ <div className={cn('flex flex-col h-full pt-3', className)}>
219
+ {/* Header 純結構:h-field-xs (24px) + flex 水平+垂直置中 + mb-3 (12px gap) */}
220
+ <div className="h-field-xs flex items-center justify-center mb-3">
221
+ <span className="text-body font-medium tabular-nums">{headerText}</span>
222
+ </div>
223
+ {/* Columns:flex-1 填滿剩餘 height,無 horizontal padding(填滿容器寬度) */}
224
+ <div className="flex-1 min-h-0 flex">
225
+ <TimeColumns
226
+ value={value}
227
+ onChange={onChange}
228
+ showSeconds={showSeconds}
229
+ minuteStep={minuteStep}
230
+ secondStep={secondStep}
231
+ />
232
+ </div>
233
+ </div>
234
+ )
235
+ }
236
+
237
+ /**
238
+ * showTime panel container — 包 DateGrid + TimePicker side panel,DateGrid 主導 row 高度,
239
+ * TimePicker absolute 撐滿同高,不影響 layout。Spacer div 留 layout 寬度給 absolute panel。
240
+ */
241
+ const TIME_PANEL_WIDTH = (showSeconds: boolean) => showSeconds ? 'w-60' : 'w-40'
242
+
243
+ interface CalendarTimeContainerProps {
244
+ showTime: boolean
245
+ showSeconds: boolean
246
+ calendar: React.ReactNode
247
+ timePanel?: React.ReactNode
248
+ }
249
+
250
+ function CalendarTimeContainer({ showTime, showSeconds, calendar, timePanel }: CalendarTimeContainerProps) {
251
+ if (!showTime) return <>{calendar}</>
252
+ return (
253
+ <div className="relative">
254
+ <div className="flex flex-row">
255
+ {calendar}
256
+ {/* Spacer 佔 layout 寬度給 absolute TimePicker;border-l 在這層,不在 absolute 層
257
+ (避免 stacking + border 雙繪) */}
258
+ <div className={cn('shrink-0 border-l border-divider', TIME_PANEL_WIDTH(showSeconds))} />
259
+ </div>
260
+ {/* TimePicker absolute 撐滿 DateGrid 高度(top-0 bottom-0),right-0 對齊 spacer */}
261
+ <div className={cn('absolute top-0 right-0 bottom-0', TIME_PANEL_WIDTH(showSeconds))}>
262
+ {timePanel}
263
+ </div>
264
+ </div>
265
+ )
266
+ }
267
+
268
+ // ── DatePicker(single)──────────────────────────────────────────────────
269
+
270
+ export interface DatePickerProps
271
+ extends DateFormatOptions,
272
+ Omit<
273
+ React.HTMLAttributes<HTMLDivElement>,
274
+ 'value' | 'onChange' | 'placeholder' | 'defaultValue'
275
+ > {
276
+ mode?: FieldMode
277
+ /** Field chrome variant. Default = context.variant ?? 'default'. Per-prop override. */
278
+ variant?: FieldVariant
279
+ error?: boolean
280
+ size?: 'sm' | 'md' | 'lg'
281
+ /** ISO date(YYYY-MM-DD)或 ISO datetime(YYYY-MM-DDTHH:MM:SS,當 showTime=true) */
282
+ value?: string | null
283
+ onChange?: (value: string) => void
284
+ placeholder?: string
285
+ className?: string
286
+ disabled?: boolean
287
+ /** 允許清空已選值 */
288
+ clearable?: boolean
289
+ /** 啟用時間欄位(時 / 分 [/ 秒]),Ant idiom — value 變 ISO datetime */
290
+ showTime?: boolean
291
+ /** showTime 時是否顯示秒 */
292
+ showSeconds?: boolean
293
+ /** showTime 分鐘步進(會議常用 15) */
294
+ minuteStep?: TimeStep
295
+ /** showTime 秒鐘步進 */
296
+ secondStep?: TimeStep
297
+ /**
298
+ * 是否需 OK 確認才提交,預設 showTime=true 時為 true(對齊 Ant DatePicker showTime)
299
+ * — datetime picker user 習慣編完才 commit,避免 calendar 點到就關。
300
+ */
301
+ needConfirm?: boolean
302
+ /**
303
+ * Display 是否渲 Calendar icon + Field naked wrapper(D-path opt-in,2026-05-08)
304
+ * — DataTable cell display↔edit 像素級對齊用。預設 false(裸 span,backward compat)。
305
+ * 設 true 時 display 走 fieldWrapperStyles(naked variant)+ ItemSuffix CalendarIcon,
306
+ * 與 edit 同 DOM 結構,消除 Layer-B padding mismatch。
307
+ */
308
+ showDisplayEndIcon?: boolean
309
+ /** Initial open state(uncontrolled)— DataTable cell-as-input 1-step open canonical */
310
+ defaultOpen?: boolean
311
+ /** open state 變更 callback。DataTable cell-as-input 用:open=false → cell exit edit */
312
+ onOpenChange?: (open: boolean) => void
313
+ /**
314
+ * Issue 10 typed input(2026-05-10):trigger 內渲 real `<input>` 接 user 鍵盤輸入,
315
+ * 同時保持 Calendar icon trigger 開啟 calendar 選擇(對齊 Material X DatePicker / Ant
316
+ * DatePicker / Notion typed-date idiom)。
317
+ *
318
+ * 預設 false(backward-compat)— trigger 仍是 `<div role="combobox">` + `<span>` 文字。
319
+ *
320
+ * Behavior(opt-in `typeable=true`):
321
+ * - `<input>` 取代 `<span>` displayValue,user 可直接打字
322
+ * - Partial input(打到一半,如 "2025-")allow,**不**即時驗證
323
+ * - `Enter` / `Blur` → `parseDateInput` 解析 → 合法 commit `onChange`;不合法 set
324
+ * aria-invalid + keep draft visible(user 可繼續修)
325
+ * - `Esc` → reset draft 回 committed value
326
+ * - IME composition 期間不觸發驗證(中日韓輸入法 onCompositionStart/End 攔截)
327
+ * - Calendar pick → 同步 input draft + commit(走原 path)
328
+ * - Calendar icon 仍 click 開 popover(Material/Ant idiom)
329
+ *
330
+ * **v1 limits**:
331
+ * - format detection ISO YYYY-MM-DD / YYYY/MM/DD / Date.parse fallback(`parseDateInput`)
332
+ * - US `MM/DD/YYYY` vs EU `DD/MM/YYYY` ambiguous → fallback native parser
333
+ * - 未支援 locale-aware format prop(v2 加 `dateFormat?: string`)
334
+ *
335
+ * 對齊 Material X-DatePicker `format` prop / Ant DatePicker `format` array / Notion typed-input。
336
+ */
337
+ typeable?: boolean
338
+ }
339
+
340
+ // Trigger uses `<div role="combobox" tabIndex={...}>` instead of `<button>` —
341
+ // 對齊 Combobox / Select / TimePicker 同 pattern,避免 ItemInlineAction(內部 button)
342
+ // 構成 nested-interactive(axe serious)。Radix Popover asChild 仍處理 Enter/Space 鍵盤觸發。
343
+ // code-quality-allow: long-function — foundational composite main body — 拆 sub-fn 會複雜化 local state / ref / context binding
344
+ const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>(
345
+ (
346
+ {
347
+ mode = 'edit',
348
+ variant: variantProp,
349
+ error: errorProp = false,
350
+ size = 'md',
351
+ value,
352
+ onChange,
353
+ placeholder,
354
+ className,
355
+ disabled: disabledProp,
356
+ clearable = false,
357
+ formatOptions,
358
+ locale,
359
+ showTime = false,
360
+ showSeconds = false,
361
+ minuteStep = 1,
362
+ secondStep = 1,
363
+ needConfirm: needConfirmProp,
364
+ showDisplayEndIcon = false,
365
+ defaultOpen = false,
366
+ onOpenChange,
367
+ typeable = false,
368
+ id: idProp,
369
+ 'aria-label': ariaLabelProp,
370
+ 'aria-labelledby': ariaLabelledByProp,
371
+ 'aria-describedby': ariaDescribedByProp,
372
+ 'aria-errormessage': ariaErrorMessageProp,
373
+ ...props
374
+ },
375
+ ref
376
+ ) => {
377
+ const fieldCtx = useFieldContext()
378
+ const error = errorProp || (fieldCtx?.invalid ?? false)
379
+ const disabled = disabledProp ?? fieldCtx?.disabled
380
+ const resolvedMode = disabled ? 'disabled' : mode
381
+ const variant: FieldVariant = variantProp ?? fieldCtx?.variant ?? 'default'
382
+ const isEditable = resolvedMode === 'edit'
383
+ // 2026-05-18 改 import ICON_SIZE SSOT(per user『做完』approval,消除 M17 違反 7+ 重複 ternary)
384
+ const iconSize = ICON_SIZE[size as 'sm' | 'md' | 'lg']
385
+ const needConfirm = needConfirmProp ?? showTime // datetime 預設需確認
386
+ const [open, setOpenState] = React.useState(defaultOpen)
387
+ const setOpen = React.useCallback((next: boolean) => { setOpenState(next); onOpenChange?.(next) }, [onOpenChange])
388
+ const [draft, setDraft] = React.useState<string | null>(value ?? null)
389
+ const resolvedPlaceholder = placeholder ?? (showTime ? 'YYYY/MM/DD HH:MM' : 'YYYY/MM/DD')
390
+ // a11y:role="combobox" 必須有 accessible name(aria-label / labelledby / fieldCtx label)
391
+ const accessibleName = ariaLabelProp ?? (ariaLabelledByProp ? undefined : (fieldCtx?.id ? undefined : resolvedPlaceholder))
392
+
393
+ // Sync draft from value ONLY on open false→true(避免 popover 開啟期間 value 改變
394
+ // clobber user 的編輯。Popover 關閉後下次再開時自動同步最新 value。)
395
+ const lastOpenRef = React.useRef(open)
396
+ React.useEffect(() => {
397
+ if (!lastOpenRef.current && open) setDraft(value ?? null)
398
+ lastOpenRef.current = open
399
+ }, [open, value])
400
+
401
+ // Display value canonical(2026-05-02 fix):
402
+ // needConfirm=true(showTime 預設)→ trigger 讀 draft,user 點 calendar 看到 input 即時更新
403
+ // needConfirm=false → trigger 讀 value(committed,符合非確認流程)
404
+ const displayValue = needConfirm ? draft : (value ?? null)
405
+ const displayDate = React.useMemo(() => isoToDate(displayValue), [displayValue])
406
+ const draftDate = React.useMemo(() => isoToDate(draft), [draft])
407
+ const draftTime = isoToTimeParts(draft) ?? { hours: 0, minutes: 0, seconds: 0 }
408
+ const showClear = clearable && (needConfirm ? draft : value) && isEditable
409
+
410
+ const displayCommitted = formatDateOrDateTime(value, showTime, showSeconds, { formatOptions, locale })
411
+ const displayLive = formatDateOrDateTime(displayValue, showTime, showSeconds, { formatOptions, locale })
412
+
413
+ // Issue 10 typed input(2026-05-10):draft string + invalid flag + IME composition guard。
414
+ const [inputDraft, setInputDraft] = React.useState<string>(displayLive)
415
+ const [inputInvalid, setInputInvalid] = React.useState(false)
416
+ const composingRef = React.useRef(false)
417
+ // Sync input draft from committed displayLive(value change from outside)— 不要在 user
418
+ // 打字期間覆寫。透過 ref 比較:committed vs current draft 是否從相同 source。
419
+ const lastDisplayLiveRef = React.useRef(displayLive)
420
+ React.useEffect(() => {
421
+ if (lastDisplayLiveRef.current !== displayLive) {
422
+ setInputDraft(displayLive)
423
+ setInputInvalid(false)
424
+ lastDisplayLiveRef.current = displayLive
425
+ }
426
+ }, [displayLive])
427
+ const handleInputCommit = React.useCallback((raw: string) => {
428
+ const { iso, valid } = parseDateInput(raw, { allowTime: showTime })
429
+ if (!valid) { setInputInvalid(true); return }
430
+ setInputInvalid(false)
431
+ if (iso === null) {
432
+ // empty input = clear value
433
+ onChange?.('')
434
+ setDraft(null)
435
+ } else {
436
+ onChange?.(iso)
437
+ setDraft(iso)
438
+ }
439
+ }, [onChange, showTime])
440
+
441
+ // mode='display'(Phase B2 2026-05-05):純內容輸出 — 對齊原 DatePickerDisplay sub-component(retired)。
442
+ // Default(showDisplayEndIcon=false):無 Field wrapper / 無 Calendar icon — backward compat 裸 span。
443
+ // Opt-in(showDisplayEndIcon=true,2026-05-08 D-path):Field naked wrapper + ItemSuffix Calendar,
444
+ // 與 edit 同結構消除 cell display↔edit 像素偏移(Layer-B padding mismatch)。
445
+ if (resolvedMode === 'display') {
446
+ if (!showDisplayEndIcon) {
447
+ // 2026-05-14 I2 fix(spec contract (e) display typography canonical):bare span 套
448
+ // `fieldDisplayTextClass(size)`(sm/md→text-body,lg→text-body-lg)— 對齊 Field family 統一。
449
+ if (!value) return <span className={cn(fieldDisplayTextClass(size), 'text-fg-muted', className)}>{EMPTY_DISPLAY}</span>
450
+ return <span className={cn(fieldDisplayTextClass(size), 'truncate', className)}>{displayCommitted}</span>
451
+ }
452
+ return (
453
+ <div
454
+ className={cn(fieldWrapperStyles({ mode: 'display', variant, size }), className)}
455
+ data-field-mode="display"
456
+ >
457
+ <span className={cn(bareInputStyles, 'flex-1 min-w-0 truncate', !value && 'text-fg-muted')}>
458
+ {value ? displayCommitted : EMPTY_DISPLAY}
459
+ </span>
460
+ <ItemSuffix className="pointer-events-none">
461
+ <CalendarIcon size={iconSize} className="text-fg-muted" aria-hidden />
462
+ </ItemSuffix>
463
+ </div>
464
+ )
465
+ }
466
+
467
+ // readonly / disabled
468
+ if (!isEditable) {
469
+ return (
470
+ <div
471
+ className={cn(fieldWrapperStyles({ mode: resolvedMode, variant: variant, size }), className)}
472
+ data-field-mode={resolvedMode}
473
+ {...(props as React.HTMLAttributes<HTMLDivElement>)}
474
+ >
475
+ <span className={cn('flex-1 min-w-0', resolvedMode === 'disabled' && 'text-fg-disabled')}>
476
+ {value
477
+ ? displayCommitted
478
+ : <span className="text-fg-muted">{EMPTY_DISPLAY}</span>
479
+ }
480
+ </span>
481
+ <ItemSuffix className="pointer-events-none">
482
+ <CalendarIcon size={iconSize} className="text-fg-muted" aria-hidden />
483
+ </ItemSuffix>
484
+ </div>
485
+ )
486
+ }
487
+
488
+ const triggerText = displayValue
489
+ ? displayLive
490
+ : <span className="text-fg-muted">{resolvedPlaceholder}</span>
491
+
492
+ const commitDraft = (next: string | null) => {
493
+ if (needConfirm) setDraft(next)
494
+ else onChange?.(next ?? '')
495
+ }
496
+ const handleConfirm = () => { onChange?.(draft ?? ''); setOpen(false) }
497
+ const handleNow = () => {
498
+ const now = showTime ? nowIsoDateTime() : dateToIso(new Date())
499
+ commitDraft(now)
500
+ }
501
+
502
+ return (
503
+ <Popover open={open} onOpenChange={setOpen}>
504
+ <PopoverTrigger asChild>
505
+ <div
506
+ ref={ref}
507
+ id={idProp ?? fieldCtx?.id}
508
+ role="combobox"
509
+ tabIndex={disabled ? -1 : 0}
510
+ aria-disabled={disabled || undefined}
511
+ aria-label={accessibleName}
512
+ aria-labelledby={ariaLabelledByProp ?? fieldCtx?.labelId}
513
+ aria-invalid={error || undefined}
514
+ aria-required={fieldCtx?.required || undefined}
515
+ aria-describedby={ariaDescribedByProp ?? fieldCtx?.descriptionId}
516
+ aria-errormessage={ariaErrorMessageProp ?? (error ? fieldCtx?.errorId : undefined)}
517
+ aria-haspopup="dialog"
518
+ aria-expanded={open}
519
+ data-field-mode="edit"
520
+ data-error={error ? '' : undefined}
521
+ className={cn(
522
+ fieldWrapperStyles({ mode: 'edit', variant: variant, size }),
523
+ 'text-left cursor-pointer',
524
+ 'focus-visible:outline-none',
525
+ error && [
526
+ 'border-error hover:border-error-hover',
527
+ 'focus-within:border-error focus-within:hover:border-error',
528
+ ],
529
+ className,
530
+ )}
531
+ {...props}
532
+ >
533
+ {typeable ? (
534
+ // Issue 10 typed input(2026-05-10):real `<input>` 接 user 鍵盤打字。
535
+ // Click 在 input 上不 propagate 給外層 popover trigger(避免每次打字都開 popover)。
536
+ // Calendar icon `<ItemSuffix>` 點才開 popover(Material/Ant typed-date idiom)。
537
+ <input
538
+ type="text"
539
+ className={cn(bareInputStyles, 'truncate', !inputDraft && 'placeholder:text-fg-muted')}
540
+ value={inputDraft}
541
+ placeholder={resolvedPlaceholder}
542
+ aria-invalid={inputInvalid || error || undefined}
543
+ onChange={(e) => { setInputDraft(e.target.value); setInputInvalid(false) }}
544
+ onCompositionStart={() => { composingRef.current = true }}
545
+ onCompositionEnd={() => { composingRef.current = false }}
546
+ onKeyDown={(e) => {
547
+ if (composingRef.current) return
548
+ if (e.key === 'Enter') { e.preventDefault(); handleInputCommit(inputDraft) }
549
+ if (e.key === 'Escape') { setInputDraft(displayLive); setInputInvalid(false); e.preventDefault() }
550
+ }}
551
+ onBlur={() => { if (!composingRef.current) handleInputCommit(inputDraft) }}
552
+ onClick={(e) => e.stopPropagation()}
553
+ />
554
+ ) : (
555
+ <span className={cn(bareInputStyles, 'truncate', !displayValue && 'text-fg-muted')}>
556
+ {triggerText}
557
+ </span>
558
+ )}
559
+ {showClear && (
560
+ <ItemInlineAction
561
+ size={size ?? 'md'}
562
+ action={{
563
+ icon: X,
564
+ label: '清除日期', // i18n-allow: DS default inline-action label
565
+ // Clear = 立刻 commit + 同步 draft(對齊 user 體感 / Ant trigger X 慣例)
566
+ // 不走 needConfirm「等確定」語義 — X 在 trigger 上是 standard clear affordance,
567
+ // 應立刻清空。dual-state 必同步:value('') + draft(null),否則 popover 開
568
+ // 著時 displayValue=draft 仍顯示舊值(see line 318: displayValue = needConfirm ? draft : value)。
569
+ onClick: (e) => {
570
+ e?.stopPropagation()
571
+ onChange?.('')
572
+ setDraft(null)
573
+ },
574
+ }}
575
+ />
576
+ )}
577
+ <ItemSuffix className="pointer-events-none">
578
+ <CalendarIcon size={iconSize} className="text-fg-muted" aria-hidden />
579
+ </ItemSuffix>
580
+ </div>
581
+ </PopoverTrigger>
582
+ <PopoverContent className="w-auto p-0" align="start">
583
+ {/* role="dialog" 為 flex item of PopoverContent(flex flex-col overflow-hidden)。
584
+ 2026-05-06 v9.1:加 `flex flex-col flex-1 min-h-0` 完成 M25 chain — viewport
585
+ 壓縮時 dialog 縮 + 內 calendar/footer 排序;原無 chain 致 calendar 末行被
586
+ overflow-hidden 切掉、footer 推出 popover(user 報「位置改變就壞掉」根因)。 */}
587
+ <div role="dialog" className="flex flex-col flex-1 min-h-0">
588
+ {/* Calendar 區包 overflow-y-auto:viewport 壓縮時 calendar 內滾(Material / Carbon
589
+ date picker idiom)。footer 永遠 in-view(SurfaceFooter shrink-0)。 */}
590
+ <div className="flex-1 min-h-0 overflow-y-auto">
591
+ <CalendarTimeContainer
592
+ showTime={showTime}
593
+ showSeconds={showSeconds}
594
+ calendar={
595
+ <DateGrid
596
+ mode="single"
597
+ selected={displayDate}
598
+ onSelect={(date) => {
599
+ if (!date) return
600
+ if (showTime) {
601
+ commitDraft(combineDateAndTime(date, draftTime))
602
+ } else {
603
+ commitDraft(dateToIso(date))
604
+ if (!needConfirm) setOpen(false)
605
+ }
606
+ }}
607
+ defaultMonth={displayDate ?? undefined}
608
+ autoFocus
609
+ />
610
+ }
611
+ timePanel={
612
+ <TimePickerSidePanel
613
+ value={draftTime}
614
+ onChange={(time) => {
615
+ const target = draftDate ?? new Date()
616
+ commitDraft(combineDateAndTime(target, time))
617
+ }}
618
+ showSeconds={showSeconds}
619
+ minuteStep={minuteStep}
620
+ secondStep={secondStep}
621
+ />
622
+ }
623
+ />
624
+ </div>
625
+ {showTime && (
626
+ // Footer:消費 SurfaceFooter SSOT(border-t + canonical px-loose py-tight padding,
627
+ // 不再 hand-coded p-2 / Separator / ml-auto wrapper 三層垃圾)。
628
+ // 「此刻」加 mr-auto 把後面 button 推右(對齊 Ant `marginInlineStart: auto` on OK)。
629
+ <SurfaceFooter>
630
+ <Button variant="tertiary" size="sm" onClick={handleNow} className="mr-auto">此刻</Button>
631
+ {needConfirm ? (
632
+ <Button variant="primary" size="sm" onClick={handleConfirm} disabled={!draft}>確定</Button>
633
+ ) : (
634
+ <Button variant="tertiary" size="sm" onClick={() => setOpen(false)}>關閉</Button>
635
+ )}
636
+ </SurfaceFooter>
637
+ )}
638
+ </div>
639
+ </PopoverContent>
640
+ </Popover>
641
+ )
642
+ }
643
+ )
644
+ DatePicker.displayName = 'DatePicker'
645
+
646
+ // ── DatePickerRange ─────────────────────────────────────────────────────────
647
+ //
648
+ // Canonical 2026-05-02 v4 — 全對齊 Ant Design RangePicker(WebFetch 實證):
649
+ //
650
+ // **showTime Range**(rc-picker `multiplePanel = false` 證實):
651
+ // - **1 calendar + 1 time panel**(等同 single DateTimePicker layout)
652
+ // - 沒 range track 視覺(計算上跟 single 一樣)
653
+ // - footer **無「此刻」按鈕**(rc-picker `showNow={multiple ? false : showNow}` 證實)
654
+ // - Click flow:click input → open popup for activeEnd → 編 → 點「確定」commit activeEnd
655
+ // → if start: switch activeEnd='end' + popup 維持 open;if end: close popup
656
+ // - Cell disable(rc-picker useRangeDisabledDate 證實):
657
+ // activeEnd='end' + start 已選 → date < start disabled
658
+ // activeEnd='start' + end 已選 → date > end disabled
659
+ //
660
+ // **date-only Range**(rc-picker `multiplePanel = true`):
661
+ // - **2 calendars 並列**(showTime=false 時)
662
+ // - Full range track 視覺(start / middle / end)
663
+ // - 走原 RDP mode='range' 配對 click 邏輯 + auto-swap
664
+ //
665
+ // **Trigger**:2 input button,active end blue underline 標示
666
+
667
+ export interface DatePickerRangeProps
668
+ extends DateFormatOptions,
669
+ Omit<
670
+ React.HTMLAttributes<HTMLDivElement>,
671
+ 'value' | 'onChange' | 'placeholder' | 'defaultValue'
672
+ > {
673
+ mode?: FieldMode
674
+ /** Field chrome variant. Default = context.variant ?? 'default'. Per-prop override. */
675
+ variant?: FieldVariant
676
+ error?: boolean
677
+ size?: 'sm' | 'md' | 'lg'
678
+ /** 區間值:[start ISO, end ISO]。任一 null 代表尚未選。 */
679
+ value?: [string | null, string | null] | null
680
+ onChange?: (value: [string | null, string | null]) => void
681
+ /** Placeholder:[start placeholder, end placeholder] */
682
+ placeholder?: [string, string]
683
+ className?: string
684
+ disabled?: boolean
685
+ clearable?: boolean
686
+ /** 啟用時間欄位 — value 兩端皆變 ISO datetime */
687
+ showTime?: boolean
688
+ showSeconds?: boolean
689
+ minuteStep?: TimeStep
690
+ secondStep?: TimeStep
691
+ needConfirm?: boolean
692
+ }
693
+
694
+ // code-quality-allow: long-function — foundational composite main body — 拆 sub-fn 會複雜化 local state / ref / context binding
695
+ const DatePickerRange = React.forwardRef<HTMLDivElement, DatePickerRangeProps>(
696
+ (
697
+ {
698
+ mode = 'edit',
699
+ variant: variantProp,
700
+ error: errorProp = false,
701
+ size = 'md',
702
+ value,
703
+ onChange,
704
+ placeholder,
705
+ className,
706
+ disabled: disabledProp,
707
+ clearable = false,
708
+ formatOptions,
709
+ locale,
710
+ showTime = false,
711
+ showSeconds = false,
712
+ minuteStep = 1,
713
+ secondStep = 1,
714
+ needConfirm: needConfirmProp,
715
+ id: idProp,
716
+ 'aria-describedby': ariaDescribedByProp,
717
+ 'aria-errormessage': ariaErrorMessageProp,
718
+ ...props
719
+ },
720
+ ref,
721
+ ) => {
722
+ const fieldCtx = useFieldContext()
723
+ const error = errorProp || (fieldCtx?.invalid ?? false)
724
+ const disabled = disabledProp ?? fieldCtx?.disabled
725
+ const resolvedMode = disabled ? 'disabled' : mode
726
+ const variant: FieldVariant = variantProp ?? fieldCtx?.variant ?? 'default'
727
+ const isEditable = resolvedMode === 'edit'
728
+ // 2026-05-18 改 import ICON_SIZE SSOT(per user『做完』approval,消除 M17 違反 7+ 重複 ternary)
729
+ const iconSize = ICON_SIZE[size as 'sm' | 'md' | 'lg']
730
+ const needConfirm = needConfirmProp ?? showTime
731
+ const resolvedPlaceholder: [string, string] = placeholder ?? (
732
+ showTime ? ['Start date time', 'End date time'] : ['Start date', 'End date']
733
+ )
734
+
735
+ const [open, setOpen] = React.useState(false)
736
+ const [draft, setDraft] = React.useState<[string | null, string | null]>(value ?? [null, null])
737
+ const [activeEnd, setActiveEnd] = React.useState<'start' | 'end'>('start')
738
+
739
+ // Sync draft from value ONLY on open false→true(canonical 2026-05-02 v3):
740
+ // 之前用 `[value, open]` 雙 dep,popover 開啟期間 value 任何 reference 變更 → useEffect
741
+ // 觸發 → 直接 clobber user 的 draft 編輯。改成只在 open 從 false→true 同步。
742
+ const lastOpenRef = React.useRef(open)
743
+ React.useEffect(() => {
744
+ if (!lastOpenRef.current && open) setDraft(value ?? [null, null])
745
+ lastOpenRef.current = open
746
+ }, [open, value])
747
+
748
+ const startIso = (needConfirm ? draft[0] : value?.[0]) ?? null
749
+ const endIso = (needConfirm ? draft[1] : value?.[1]) ?? null
750
+ const startDate = React.useMemo(() => isoToDate(startIso), [startIso])
751
+ const endDate = React.useMemo(() => isoToDate(endIso), [endIso])
752
+ const hasValue = !!(value?.[0] || value?.[1])
753
+ const showClear = clearable && hasValue && isEditable
754
+
755
+ const startText = startIso
756
+ ? formatDateOrDateTime(startIso, showTime, showSeconds, { formatOptions, locale })
757
+ : resolvedPlaceholder[0]
758
+ const endText = endIso
759
+ ? formatDateOrDateTime(endIso, showTime, showSeconds, { formatOptions, locale })
760
+ : resolvedPlaceholder[1]
761
+
762
+ const activeIso = activeEnd === 'start' ? startIso : endIso
763
+ const activeDate = activeEnd === 'start' ? startDate : endDate
764
+ const activeTime = isoToTimeParts(activeIso) ?? { hours: 0, minutes: 0, seconds: 0 }
765
+
766
+ // Range visual modifiers(自管,不靠 RDP mode='range'):
767
+ // rangeStart:start 那天 → 圓底白字
768
+ // rangeEnd:end 那天 → 圓底白字
769
+ // rangeMiddle:start+1 ~ end-1 之間的所有天 → 灰底矩形 track
770
+ const rangeModifiers = React.useMemo(() => {
771
+ const mods: Record<string, Date | { from: Date; to: Date } | undefined> = {}
772
+ if (startDate) mods.rangeStart = startDate
773
+ if (endDate) mods.rangeEnd = endDate
774
+ if (startDate && endDate) {
775
+ const middleStart = addDays(startDate, 1)
776
+ const middleEnd = addDays(endDate, -1)
777
+ if (middleEnd >= middleStart) {
778
+ mods.rangeMiddle = { from: middleStart, to: middleEnd }
779
+ }
780
+ }
781
+ return mods
782
+ }, [startDate, endDate])
783
+
784
+ const commitRange = (next: [string | null, string | null]) => {
785
+ if (needConfirm) setDraft(next)
786
+ else { onChange?.(next); setDraft(next) }
787
+ }
788
+ const setActive = (iso: string | null) => {
789
+ const nextDraft = activeEnd === 'start'
790
+ ? ([iso, draft[1]] as [string | null, string | null])
791
+ : ([draft[0], iso] as [string | null, string | null])
792
+ commitRange(nextDraft)
793
+ }
794
+ /**
795
+ * Click「確定」canonical(2026-05-02 v4,對齊 Ant Design 序列流程):
796
+ * showTime Range:
797
+ * - activeEnd='start' → commit start to draft + switch activeEnd='end' + popup 維持 open
798
+ * - activeEnd='end' → commit final draft to value + close popup
799
+ * date-only Range(沒有 footer,不會走這 path):— N/A
800
+ */
801
+ const handleConfirm = () => {
802
+ if (showTime && activeEnd === 'start' && draft[0]) {
803
+ // Start 已填 → switch to end,popup 維持 open
804
+ setActiveEnd('end')
805
+ } else {
806
+ // End 也填好(or non-showTime needConfirm)→ final commit + close
807
+ onChange?.(draft)
808
+ setOpen(false)
809
+ }
810
+ }
811
+ const handleNow = () => {
812
+ setActive(showTime ? nowIsoDateTime() : dateToIso(new Date()))
813
+ }
814
+ const handleClearRange = (e?: React.MouseEvent) => {
815
+ e?.stopPropagation()
816
+ // Clear = 立刻 commit + 同步 draft(對齊 single mode + user 體感)
817
+ // dual-state 同步,否則 popover 開著時 displayValue=draft 仍顯示舊 [start, end]
818
+ onChange?.([null, null])
819
+ setDraft([null, null])
820
+ }
821
+ const openWithActive = (which: 'start' | 'end') => {
822
+ setActiveEnd(which)
823
+ setOpen(true)
824
+ }
825
+ /**
826
+ * Cell disable(對齊 Ant rc-picker `useRangeDisabledDate`):
827
+ * activeEnd='end' + start 已選 → date < start 被 disable(同日 OK)
828
+ * activeEnd='start' + end 已選 → date > end 被 disable(同日 OK)
829
+ * 防 user 點下違反順序的日期(start > end / end < start)。
830
+ */
831
+ const isOutOfRangeOrder = React.useCallback((date: Date): boolean => {
832
+ // ⚠️ 必先 clone(new Date(...)),否則 setHours 會 mutate useMemo'd date 物件
833
+ if (activeEnd === 'end' && startDate) {
834
+ const startMidnight = new Date(startDate.getTime())
835
+ startMidnight.setHours(0, 0, 0, 0)
836
+ return date.getTime() < startMidnight.getTime()
837
+ }
838
+ if (activeEnd === 'start' && endDate) {
839
+ const endEndOfDay = new Date(endDate.getTime())
840
+ endEndOfDay.setHours(23, 59, 59, 999)
841
+ return date.getTime() > endEndOfDay.getTime()
842
+ }
843
+ return false
844
+ }, [activeEnd, startDate, endDate])
845
+
846
+ // mode='display'(Phase B2 2026-05-05):純內容輸出 — 無 Field wrapper / 無 Calendar icon。
847
+ if (resolvedMode === 'display') {
848
+ const hasAny = !!(startIso || endIso)
849
+ if (!hasAny) return <span className={cn('text-fg-muted', className)}>{EMPTY_DISPLAY}</span>
850
+ return (
851
+ <span className={cn('inline-flex items-center min-w-0', nakedCellRowModeAlign, className)}>
852
+ <span className={cn('truncate', !startIso && 'text-fg-muted')}>
853
+ {startIso ? formatDateOrDateTime(startIso, showTime, showSeconds, { formatOptions, locale }) : resolvedPlaceholder[0]}
854
+ </span>
855
+ <ArrowRight size={iconSize} className="shrink-0 text-fg-muted mx-2" aria-hidden />
856
+ <span className={cn('truncate', !endIso && 'text-fg-muted')}>
857
+ {endIso ? formatDateOrDateTime(endIso, showTime, showSeconds, { formatOptions, locale }) : resolvedPlaceholder[1]}
858
+ </span>
859
+ </span>
860
+ )
861
+ }
862
+
863
+ // readonly / disabled view — plain wrapper,no popover
864
+ if (!isEditable) {
865
+ return (
866
+ <div
867
+ ref={ref}
868
+ className={cn(fieldWrapperStyles({ mode: resolvedMode, variant: variant, size }), className)}
869
+ data-field-mode={resolvedMode}
870
+ {...props}
871
+ >
872
+ <span className={cn('flex-1 min-w-0 truncate', !startIso && 'text-fg-muted', resolvedMode === 'disabled' && 'text-fg-disabled')}>
873
+ {startIso ? formatDateOrDateTime(startIso, showTime, showSeconds, { formatOptions, locale }) : resolvedPlaceholder[0]}
874
+ </span>
875
+ <ArrowRight size={iconSize} className="shrink-0 text-fg-muted mx-2" aria-hidden />
876
+ <span className={cn('flex-1 min-w-0 truncate', !endIso && 'text-fg-muted', resolvedMode === 'disabled' && 'text-fg-disabled')}>
877
+ {endIso ? formatDateOrDateTime(endIso, showTime, showSeconds, { formatOptions, locale }) : resolvedPlaceholder[1]}
878
+ </span>
879
+ <ItemSuffix className="pointer-events-none">
880
+ <CalendarIcon size={iconSize} className="text-fg-muted" aria-hidden />
881
+ </ItemSuffix>
882
+ </div>
883
+ )
884
+ }
885
+
886
+ return (
887
+ <Popover open={open} onOpenChange={setOpen}>
888
+ <PopoverAnchor asChild>
889
+ <div
890
+ ref={ref}
891
+ id={idProp ?? fieldCtx?.id}
892
+ aria-invalid={error || undefined}
893
+ aria-required={fieldCtx?.required || undefined}
894
+ aria-describedby={ariaDescribedByProp ?? fieldCtx?.descriptionId}
895
+ aria-errormessage={ariaErrorMessageProp ?? (error ? fieldCtx?.errorId : undefined)}
896
+ data-field-mode="edit"
897
+ data-error={error ? '' : undefined}
898
+ data-state={open ? 'open' : 'closed'}
899
+ className={cn(
900
+ fieldWrapperStyles({ mode: 'edit', size }),
901
+ 'cursor-text',
902
+ error && [
903
+ 'border-error hover:border-error-hover',
904
+ 'focus-within:border-error focus-within:hover:border-error',
905
+ ],
906
+ className,
907
+ )}
908
+ {...props}
909
+ >
910
+ <button
911
+ type="button"
912
+ onClick={() => openWithActive('start')}
913
+ data-active-end={open && activeEnd === 'start' ? 'true' : undefined}
914
+ aria-label={resolvedPlaceholder[0]}
915
+ aria-haspopup="dialog"
916
+ aria-expanded={open && activeEnd === 'start'}
917
+ className={cn(
918
+ bareInputStyles,
919
+ 'truncate text-left cursor-pointer focus-visible:outline-none',
920
+ 'data-[active-end=true]:underline decoration-primary underline-offset-4 decoration-2',
921
+ !startIso && 'text-fg-muted',
922
+ )}
923
+ >
924
+ {startText}
925
+ </button>
926
+ <ArrowRight size={iconSize} className="shrink-0 text-fg-muted mx-2" aria-hidden />
927
+ <button
928
+ type="button"
929
+ onClick={() => openWithActive('end')}
930
+ data-active-end={open && activeEnd === 'end' ? 'true' : undefined}
931
+ aria-label={resolvedPlaceholder[1]}
932
+ aria-haspopup="dialog"
933
+ aria-expanded={open && activeEnd === 'end'}
934
+ className={cn(
935
+ bareInputStyles,
936
+ 'truncate text-left cursor-pointer focus-visible:outline-none',
937
+ 'data-[active-end=true]:underline decoration-primary underline-offset-4 decoration-2',
938
+ !endIso && 'text-fg-muted',
939
+ )}
940
+ >
941
+ {endText}
942
+ </button>
943
+ {showClear && (
944
+ <ItemInlineAction
945
+ size={size ?? 'md'}
946
+ action={{
947
+ icon: X,
948
+ label: '清除日期區間', // i18n-allow: DS default inline-action label
949
+ onClick: handleClearRange,
950
+ }}
951
+ />
952
+ )}
953
+ <ItemSuffix className="pointer-events-none">
954
+ <CalendarIcon size={iconSize} className="text-fg-muted" aria-hidden />
955
+ </ItemSuffix>
956
+ </div>
957
+ </PopoverAnchor>
958
+ <PopoverContent className="w-auto p-0" align="start">
959
+ {/* 2026-05-06 v9.1 M25 chain — 同 single DatePicker 修法,viewport 壓縮 calendar 內滾 + footer 永遠 in-view */}
960
+ <div role="dialog" aria-label="日期區間選擇" className="flex flex-col flex-1 min-h-0">
961
+ <div className="flex-1 min-h-0 overflow-y-auto">
962
+ <CalendarTimeContainer
963
+ showTime={showTime}
964
+ showSeconds={showSeconds}
965
+ calendar={
966
+ <DateGrid
967
+ // mode='single' + manual modifiers(canonical 2026-05-02 v3):
968
+ // 不用 RDP 內建 mode='range'(它的 click 配對邏輯跟我們的 activeEnd 衝突,
969
+ // 造成「點一次沒反應 / 要點兩次」bug)。改自管 modifiers 控視覺。
970
+ // showTime Range:rangeModifiers 為空(不顯示 range track,對齊 Ant)
971
+ mode="single"
972
+ selected={activeDate}
973
+ onSelect={(date) => {
974
+ if (!date) return
975
+ if (isOutOfRangeOrder(date)) return // 防護:disable 邏輯內 click 已被 RDP 擋,但雙保險
976
+ const preservedTime = isoToTimeParts(activeEnd === 'start' ? draft[0] : draft[1]) ?? activeTime
977
+ const nextIso = showTime
978
+ ? combineDateAndTime(date, preservedTime)
979
+ : dateToIso(date)
980
+ const nextDraft: [string | null, string | null] = activeEnd === 'start'
981
+ ? [nextIso, draft[1]]
982
+ : [draft[0], nextIso]
983
+ commitRange(nextDraft)
984
+ // Auto-advance / close logic:
985
+ if (!showTime) {
986
+ // date-only Range:選完 start 自動切 end;兩端皆填 + 不需確認 → 關閉
987
+ if (activeEnd === 'start') {
988
+ setActiveEnd('end')
989
+ if (!needConfirm && nextDraft[0] && nextDraft[1]) setOpen(false)
990
+ } else if (!needConfirm && nextDraft[0] && nextDraft[1]) {
991
+ setOpen(false)
992
+ }
993
+ }
994
+ // showTime Range:不 auto-advance,讓 user 編 time 後手動按確定 commit
995
+ // (對齊 Ant 序列流程 — 確定 button 切 activeEnd)
996
+ }}
997
+ // showTime Range:不渲 range visualization(對齊 Ant — 整個 popup 等同 single
998
+ // DateTimePicker,沒 range 視覺概念);date-only Range 才顯示
999
+ modifiers={showTime ? {} : rangeModifiers}
1000
+ modifiersClassNames={{
1001
+ // ── Range visual canonical(2026-05-03 v8 stadium pattern)──
1002
+ // v5 修「白色破圖」用 pseudo 蓋全 cell 矩形,但新副作用:button 圓比矩形小,
1003
+ // 4 corner triangle 區域 grey 凸出圓外(user 2026-05-03 抓到「凸出去」)。
1004
+ // v8 對齊 Ant `cell-range-start::before { border-radius: 9999px 0 0 9999px }`:
1005
+ // rangeStart pseudo 加 `rounded-l-full` → pseudo 變「左半圓 + 右矩形」stadium
1006
+ // 左半圓 EXACTLY OVERLAY button 圓的左半弧(同 center 同 radius 14)→ 無縫
1007
+ // 右側矩形 bridge 2px to middle → 跟 middle pseudo 連續
1008
+ // Cell 的 top-left + bottom-left corner triangle:pseudo 不蓋 + button 不蓋 →
1009
+ // popover white 顯露(乾淨 breathing)
1010
+ rangeStart: cn(
1011
+ '[&>button]:!bg-primary [&>button]:!text-on-emphasis [&>button]:hover:!ring-0',
1012
+ "before:content-[''] before:absolute before:inset-y-0",
1013
+ 'before:left-0 before:-right-[2px]',
1014
+ 'before:bg-neutral-selected before:pointer-events-none',
1015
+ 'before:rounded-l-full', // ← stadium 左半圓 matches button 圓的左半弧
1016
+ ),
1017
+ rangeEnd: cn(
1018
+ '[&>button]:!bg-primary [&>button]:!text-on-emphasis [&>button]:hover:!ring-0',
1019
+ "before:content-[''] before:absolute before:inset-y-0",
1020
+ 'before:-left-[2px] before:right-0',
1021
+ 'before:bg-neutral-selected before:pointer-events-none',
1022
+ 'before:rounded-r-full', // ← 鏡像
1023
+ ),
1024
+ rangeMiddle: cn(
1025
+ "before:content-[''] before:absolute before:inset-y-0 before:-inset-x-[2px]",
1026
+ 'before:bg-neutral-selected before:pointer-events-none',
1027
+ '[&>button]:!bg-transparent [&>button]:!text-foreground',
1028
+ ),
1029
+ }}
1030
+ // Cell disable:防 user 點下違反順序的日期(對齊 Ant useRangeDisabledDate)
1031
+ disabled={isOutOfRangeOrder}
1032
+ // showTime → 1 cal(對齊 Ant `multiplePanel=false`);date-only → 2 cal(`multiplePanel=true`)
1033
+ numberOfMonths={showTime ? 1 : 2}
1034
+ defaultMonth={activeDate ?? startDate ?? endDate ?? undefined}
1035
+ autoFocus
1036
+ />
1037
+ }
1038
+ timePanel={
1039
+ <TimePickerSidePanel
1040
+ value={activeTime}
1041
+ onChange={(time) => {
1042
+ const target = activeDate ?? new Date()
1043
+ setActive(combineDateAndTime(target, time))
1044
+ }}
1045
+ showSeconds={showSeconds}
1046
+ minuteStep={minuteStep}
1047
+ secondStep={secondStep}
1048
+ />
1049
+ }
1050
+ />
1051
+ </div>
1052
+ {(showTime || needConfirm) && (
1053
+ // Footer 消費 SurfaceFooter SSOT(border-t + canonical px-loose py-tight)。
1054
+ // showTime Range 無「此刻」(對齊 Ant `showNow={multiple ? false : showNow}`)→ 只有 確定 走 justify-end。
1055
+ // date-only Range needConfirm:左 此刻(mr-auto)+ 右 確定。
1056
+ <SurfaceFooter>
1057
+ {!showTime && (
1058
+ <Button variant="tertiary" size="sm" onClick={handleNow} className="mr-auto">此刻</Button>
1059
+ )}
1060
+ {needConfirm ? (
1061
+ <Button
1062
+ variant="primary"
1063
+ size="sm"
1064
+ onClick={handleConfirm}
1065
+ // showTime Range serial flow:start mode 只需 start filled;end mode 兩端皆 filled
1066
+ disabled={
1067
+ showTime
1068
+ ? (activeEnd === 'start' ? !draft[0] : !draft[0] || !draft[1])
1069
+ : !draft[0] || !draft[1]
1070
+ }
1071
+ >
1072
+ 確定
1073
+ </Button>
1074
+ ) : (
1075
+ <Button variant="tertiary" size="sm" onClick={() => setOpen(false)}>關閉</Button>
1076
+ )}
1077
+ </SurfaceFooter>
1078
+ )}
1079
+ </div>
1080
+ </PopoverContent>
1081
+ </Popover>
1082
+ )
1083
+ },
1084
+ )
1085
+ DatePickerRange.displayName = 'DatePickerRange'
1086
+
1087
+ // Attach Range as namespace:consumer 用 <DatePicker.Range ...>(Ant-style)
1088
+ // 走 Object.assign 確保 TS 型別帶上 Range 屬性,而非只做 runtime 附掛
1089
+ const DatePickerWithRange = Object.assign(DatePicker, { Range: DatePickerRange })
1090
+
1091
+ // Story auto-compile metadata — Phase 1 mechanical migration(2026-04-24)
1092
+ // Phase 2 fill needed: purpose descriptions + when rationale + world-class refs
1093
+ export const datePickerMeta = {
1094
+ component: 'DatePicker',
1095
+ family: 4,
1096
+ variants: {
1097
+
1098
+ },
1099
+ sizes: {
1100
+
1101
+ },
1102
+ states: ['default', 'hover', 'active', 'focus-visible', 'disabled'],
1103
+ tokens: {
1104
+ bg: [],
1105
+ fg: ['text-fg-disabled', 'text-fg-muted'],
1106
+ ring: [],
1107
+ },
1108
+ } as const
1109
+
1110
+ export {
1111
+ DatePickerWithRange as DatePicker,
1112
+ DatePickerRange,
1113
+ formatDate,
1114
+ }