@behavioral-agentic-design-system/core 0.14.0 → 0.15.0
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 +1 -1
- package/src/app/preview/components/page.tsx +745 -6
- package/src/components/ui/color-picker.tsx +219 -0
- package/src/components/ui/date-range-picker.tsx +174 -0
- package/src/components/ui/index.ts +3 -0
- package/src/components/ui/password-input.tsx +150 -0
- package/src/components/ui/time-picker.tsx +363 -0
- package/src/components/ui/tree-view.tsx +405 -0
- package/src/data/docs-index.ts +1 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { XIcon } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
|
|
7
|
+
export interface TimePickerProps {
|
|
8
|
+
value?: string
|
|
9
|
+
onValueChange?: (value: string | undefined) => void
|
|
10
|
+
placeholder?: string
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
clearable?: boolean
|
|
13
|
+
size?: "sm" | "default" | "lg"
|
|
14
|
+
hours12?: boolean
|
|
15
|
+
showSeconds?: boolean
|
|
16
|
+
className?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const sizeMap = {
|
|
20
|
+
sm: { height: "h-8", px: "px-2.5", text: "text-sm", gap: "gap-1", icon: 14 },
|
|
21
|
+
default: { height: "h-9", px: "px-3", text: "text-sm", gap: "gap-1.5", icon: 16 },
|
|
22
|
+
lg: { height: "h-10", px: "px-4", text: "text-base", gap: "gap-2", icon: 16 },
|
|
23
|
+
} as const
|
|
24
|
+
|
|
25
|
+
function pad2(n: number) {
|
|
26
|
+
return String(n).padStart(2, "0")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parseTime(value?: string): { h: number; m: number; s: number } | null {
|
|
30
|
+
if (!value) return null
|
|
31
|
+
const parts = value.split(":").map(Number)
|
|
32
|
+
if (parts.length < 2 || parts.some(isNaN)) return null
|
|
33
|
+
return { h: parts[0], m: parts[1], s: parts[2] ?? 0 }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatTime(h: number, m: number, s: number, showSeconds: boolean): string {
|
|
37
|
+
return showSeconds ? `${pad2(h)}:${pad2(m)}:${pad2(s)}` : `${pad2(h)}:${pad2(m)}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Segment = "h" | "m" | "s" | "ampm"
|
|
41
|
+
|
|
42
|
+
function TimePicker({
|
|
43
|
+
value,
|
|
44
|
+
onValueChange,
|
|
45
|
+
placeholder = "--:--",
|
|
46
|
+
disabled = false,
|
|
47
|
+
clearable = false,
|
|
48
|
+
size = "default",
|
|
49
|
+
hours12 = false,
|
|
50
|
+
showSeconds = false,
|
|
51
|
+
className,
|
|
52
|
+
}: TimePickerProps) {
|
|
53
|
+
const isControlled = value !== undefined || onValueChange !== undefined
|
|
54
|
+
const [internalValue, setInternalValue] = React.useState<string | undefined>(undefined)
|
|
55
|
+
const resolvedValue = isControlled ? value : internalValue
|
|
56
|
+
|
|
57
|
+
const parsed = parseTime(resolvedValue)
|
|
58
|
+
const [h, setH] = React.useState(parsed?.h ?? 0)
|
|
59
|
+
const [m, setM] = React.useState(parsed?.m ?? 0)
|
|
60
|
+
const [s, setS] = React.useState(parsed?.s ?? 0)
|
|
61
|
+
const [active, setActive] = React.useState<Segment | null>(null)
|
|
62
|
+
const [digitBuffer, setDigitBuffer] = React.useState("")
|
|
63
|
+
const [hasValue, setHasValue] = React.useState(!!parsed)
|
|
64
|
+
|
|
65
|
+
React.useEffect(() => {
|
|
66
|
+
const p = parseTime(resolvedValue)
|
|
67
|
+
if (p) {
|
|
68
|
+
setH(p.h)
|
|
69
|
+
setM(p.m)
|
|
70
|
+
setS(p.s)
|
|
71
|
+
setHasValue(true)
|
|
72
|
+
} else if (resolvedValue === undefined || resolvedValue === "") {
|
|
73
|
+
setHasValue(false)
|
|
74
|
+
}
|
|
75
|
+
}, [resolvedValue])
|
|
76
|
+
|
|
77
|
+
function commit(nh: number, nm: number, ns: number) {
|
|
78
|
+
const str = formatTime(nh, nm, ns, showSeconds)
|
|
79
|
+
setHasValue(true)
|
|
80
|
+
if (!isControlled) setInternalValue(str)
|
|
81
|
+
onValueChange?.(str)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function handleClear(e: React.MouseEvent) {
|
|
85
|
+
e.stopPropagation()
|
|
86
|
+
setHasValue(false)
|
|
87
|
+
setH(0)
|
|
88
|
+
setM(0)
|
|
89
|
+
setS(0)
|
|
90
|
+
if (!isControlled) setInternalValue(undefined)
|
|
91
|
+
onValueChange?.(undefined)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const isPM = h >= 12
|
|
95
|
+
const displayH = hours12 ? (h === 0 ? 12 : h > 12 ? h - 12 : h) : h
|
|
96
|
+
|
|
97
|
+
function adjustH(delta: number) {
|
|
98
|
+
const nh = (h + delta + 24) % 24
|
|
99
|
+
setH(nh)
|
|
100
|
+
commit(nh, m, s)
|
|
101
|
+
}
|
|
102
|
+
function adjustM(delta: number) {
|
|
103
|
+
const nm = (m + delta + 60) % 60
|
|
104
|
+
setM(nm)
|
|
105
|
+
commit(h, nm, s)
|
|
106
|
+
}
|
|
107
|
+
function adjustS(delta: number) {
|
|
108
|
+
const ns = (s + delta + 60) % 60
|
|
109
|
+
setS(ns)
|
|
110
|
+
commit(h, m, ns)
|
|
111
|
+
}
|
|
112
|
+
function toggleAMPM() {
|
|
113
|
+
const nh = h >= 12 ? h - 12 : h + 12
|
|
114
|
+
setH(nh)
|
|
115
|
+
commit(nh, m, s)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function handleSegmentKeyDown(seg: Segment, e: React.KeyboardEvent) {
|
|
119
|
+
if (e.key === "ArrowUp") {
|
|
120
|
+
e.preventDefault()
|
|
121
|
+
if (seg === "h") adjustH(1)
|
|
122
|
+
else if (seg === "m") adjustM(1)
|
|
123
|
+
else if (seg === "s") adjustS(1)
|
|
124
|
+
else if (seg === "ampm") toggleAMPM()
|
|
125
|
+
} else if (e.key === "ArrowDown") {
|
|
126
|
+
e.preventDefault()
|
|
127
|
+
if (seg === "h") adjustH(-1)
|
|
128
|
+
else if (seg === "m") adjustM(-1)
|
|
129
|
+
else if (seg === "s") adjustS(-1)
|
|
130
|
+
else if (seg === "ampm") toggleAMPM()
|
|
131
|
+
} else if (e.key >= "0" && e.key <= "9" && seg !== "ampm") {
|
|
132
|
+
e.preventDefault()
|
|
133
|
+
const digit = Number(e.key)
|
|
134
|
+
const newBuf = digitBuffer + String(digit)
|
|
135
|
+
const val = parseInt(newBuf, 10)
|
|
136
|
+
if (seg === "h") {
|
|
137
|
+
if (hours12) {
|
|
138
|
+
if (newBuf.length === 1 && digit > 1) {
|
|
139
|
+
const nh = isPM ? digit + 12 : digit
|
|
140
|
+
setH(nh)
|
|
141
|
+
commit(nh, m, s)
|
|
142
|
+
setDigitBuffer("")
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
const clamped = Math.min(val, 12)
|
|
146
|
+
const nh =
|
|
147
|
+
clamped === 0
|
|
148
|
+
? isPM ? 12 : 0
|
|
149
|
+
: isPM
|
|
150
|
+
? clamped === 12
|
|
151
|
+
? 12
|
|
152
|
+
: clamped + 12
|
|
153
|
+
: clamped === 12
|
|
154
|
+
? 0
|
|
155
|
+
: clamped
|
|
156
|
+
if (newBuf.length >= 2) {
|
|
157
|
+
setH(nh)
|
|
158
|
+
commit(nh, m, s)
|
|
159
|
+
setDigitBuffer("")
|
|
160
|
+
} else {
|
|
161
|
+
setDigitBuffer(newBuf)
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
if (val > 2 || newBuf.length >= 2) {
|
|
165
|
+
const nh = Math.min(val, 23)
|
|
166
|
+
setH(nh)
|
|
167
|
+
commit(nh, m, s)
|
|
168
|
+
setDigitBuffer("")
|
|
169
|
+
} else {
|
|
170
|
+
setDigitBuffer(newBuf)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} else if (seg === "m" || seg === "s") {
|
|
174
|
+
if (digit > 5 || newBuf.length >= 2) {
|
|
175
|
+
const nv = Math.min(val, 59)
|
|
176
|
+
if (seg === "m") {
|
|
177
|
+
setM(nv)
|
|
178
|
+
commit(h, nv, s)
|
|
179
|
+
} else {
|
|
180
|
+
setS(nv)
|
|
181
|
+
commit(h, m, nv)
|
|
182
|
+
}
|
|
183
|
+
setDigitBuffer("")
|
|
184
|
+
} else {
|
|
185
|
+
setDigitBuffer(newBuf)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
setDigitBuffer("")
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const { height, px, text, gap, icon: iconSize } = sizeMap[size]
|
|
194
|
+
const segClass = cn(
|
|
195
|
+
"inline-flex items-center justify-center rounded px-0.5 outline-none select-none tabular-nums",
|
|
196
|
+
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:bg-accent",
|
|
197
|
+
"transition-colors",
|
|
198
|
+
text
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
return (
|
|
202
|
+
<div
|
|
203
|
+
data-slot="time-picker"
|
|
204
|
+
data-size={size}
|
|
205
|
+
role="group"
|
|
206
|
+
aria-label="Time input"
|
|
207
|
+
aria-disabled={disabled}
|
|
208
|
+
className={cn(
|
|
209
|
+
"inline-flex w-full min-w-0 items-center rounded-md border border-input bg-transparent shadow-xs",
|
|
210
|
+
"focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
211
|
+
disabled && "pointer-events-none cursor-not-allowed opacity-50",
|
|
212
|
+
height,
|
|
213
|
+
px,
|
|
214
|
+
gap,
|
|
215
|
+
className
|
|
216
|
+
)}
|
|
217
|
+
>
|
|
218
|
+
{!hasValue ? (
|
|
219
|
+
<span
|
|
220
|
+
tabIndex={disabled ? -1 : 0}
|
|
221
|
+
className={cn(
|
|
222
|
+
"flex-1 text-muted-foreground cursor-text outline-none",
|
|
223
|
+
text
|
|
224
|
+
)}
|
|
225
|
+
onFocus={() => {
|
|
226
|
+
setActive("h")
|
|
227
|
+
setHasValue(true)
|
|
228
|
+
}}
|
|
229
|
+
onClick={() => {
|
|
230
|
+
setActive("h")
|
|
231
|
+
setHasValue(true)
|
|
232
|
+
}}
|
|
233
|
+
>
|
|
234
|
+
{placeholder}
|
|
235
|
+
</span>
|
|
236
|
+
) : (
|
|
237
|
+
<span className={cn("inline-flex items-center", gap)}>
|
|
238
|
+
<span
|
|
239
|
+
role="spinbutton"
|
|
240
|
+
aria-label="Hours"
|
|
241
|
+
aria-valuenow={displayH}
|
|
242
|
+
aria-valuemin={hours12 ? 1 : 0}
|
|
243
|
+
aria-valuemax={hours12 ? 12 : 23}
|
|
244
|
+
aria-valuetext={pad2(displayH)}
|
|
245
|
+
tabIndex={disabled ? -1 : 0}
|
|
246
|
+
className={cn(segClass, active === "h" && "bg-accent")}
|
|
247
|
+
onFocus={() => setActive("h")}
|
|
248
|
+
onBlur={() => {
|
|
249
|
+
setActive(null)
|
|
250
|
+
setDigitBuffer("")
|
|
251
|
+
}}
|
|
252
|
+
onKeyDown={(e) => handleSegmentKeyDown("h", e)}
|
|
253
|
+
style={{
|
|
254
|
+
transitionDuration: "var(--duration-fast)",
|
|
255
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
256
|
+
}}
|
|
257
|
+
>
|
|
258
|
+
{pad2(displayH)}
|
|
259
|
+
</span>
|
|
260
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
261
|
+
:
|
|
262
|
+
</span>
|
|
263
|
+
<span
|
|
264
|
+
role="spinbutton"
|
|
265
|
+
aria-label="Minutes"
|
|
266
|
+
aria-valuenow={m}
|
|
267
|
+
aria-valuemin={0}
|
|
268
|
+
aria-valuemax={59}
|
|
269
|
+
aria-valuetext={pad2(m)}
|
|
270
|
+
tabIndex={disabled ? -1 : 0}
|
|
271
|
+
className={cn(segClass, active === "m" && "bg-accent")}
|
|
272
|
+
onFocus={() => setActive("m")}
|
|
273
|
+
onBlur={() => {
|
|
274
|
+
setActive(null)
|
|
275
|
+
setDigitBuffer("")
|
|
276
|
+
}}
|
|
277
|
+
onKeyDown={(e) => handleSegmentKeyDown("m", e)}
|
|
278
|
+
style={{
|
|
279
|
+
transitionDuration: "var(--duration-fast)",
|
|
280
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
{pad2(m)}
|
|
284
|
+
</span>
|
|
285
|
+
{showSeconds && (
|
|
286
|
+
<>
|
|
287
|
+
<span aria-hidden className={cn("text-muted-foreground", text)}>
|
|
288
|
+
:
|
|
289
|
+
</span>
|
|
290
|
+
<span
|
|
291
|
+
role="spinbutton"
|
|
292
|
+
aria-label="Seconds"
|
|
293
|
+
aria-valuenow={s}
|
|
294
|
+
aria-valuemin={0}
|
|
295
|
+
aria-valuemax={59}
|
|
296
|
+
aria-valuetext={pad2(s)}
|
|
297
|
+
tabIndex={disabled ? -1 : 0}
|
|
298
|
+
className={cn(segClass, active === "s" && "bg-accent")}
|
|
299
|
+
onFocus={() => setActive("s")}
|
|
300
|
+
onBlur={() => {
|
|
301
|
+
setActive(null)
|
|
302
|
+
setDigitBuffer("")
|
|
303
|
+
}}
|
|
304
|
+
onKeyDown={(e) => handleSegmentKeyDown("s", e)}
|
|
305
|
+
style={{
|
|
306
|
+
transitionDuration: "var(--duration-fast)",
|
|
307
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
308
|
+
}}
|
|
309
|
+
>
|
|
310
|
+
{pad2(s)}
|
|
311
|
+
</span>
|
|
312
|
+
</>
|
|
313
|
+
)}
|
|
314
|
+
{hours12 && (
|
|
315
|
+
<button
|
|
316
|
+
type="button"
|
|
317
|
+
role="spinbutton"
|
|
318
|
+
aria-label="AM/PM"
|
|
319
|
+
aria-valuenow={isPM ? 1 : 0}
|
|
320
|
+
aria-valuemin={0}
|
|
321
|
+
aria-valuemax={1}
|
|
322
|
+
aria-valuetext={isPM ? "PM" : "AM"}
|
|
323
|
+
tabIndex={disabled ? -1 : 0}
|
|
324
|
+
disabled={disabled}
|
|
325
|
+
onClick={toggleAMPM}
|
|
326
|
+
onFocus={() => setActive("ampm")}
|
|
327
|
+
onBlur={() => setActive(null)}
|
|
328
|
+
onKeyDown={(e) => handleSegmentKeyDown("ampm", e)}
|
|
329
|
+
className={cn(
|
|
330
|
+
segClass,
|
|
331
|
+
"cursor-pointer hover:bg-accent",
|
|
332
|
+
active === "ampm" && "bg-accent"
|
|
333
|
+
)}
|
|
334
|
+
style={{
|
|
335
|
+
transitionDuration: "var(--duration-fast)",
|
|
336
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
337
|
+
}}
|
|
338
|
+
>
|
|
339
|
+
{isPM ? "PM" : "AM"}
|
|
340
|
+
</button>
|
|
341
|
+
)}
|
|
342
|
+
</span>
|
|
343
|
+
)}
|
|
344
|
+
{clearable && hasValue && (
|
|
345
|
+
<button
|
|
346
|
+
type="button"
|
|
347
|
+
aria-label="Clear time"
|
|
348
|
+
onClick={handleClear}
|
|
349
|
+
disabled={disabled}
|
|
350
|
+
className="shrink-0 -mr-1 rounded p-0.5 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
351
|
+
style={{
|
|
352
|
+
transitionDuration: "var(--duration-fast)",
|
|
353
|
+
transitionTimingFunction: "var(--ease-out)",
|
|
354
|
+
}}
|
|
355
|
+
>
|
|
356
|
+
<XIcon size={iconSize - 2} strokeWidth={1.5} />
|
|
357
|
+
</button>
|
|
358
|
+
)}
|
|
359
|
+
</div>
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export { TimePicker }
|