@krak-stack/registry 0.9.0 → 0.10.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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @krak-stack/registry
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add Time Picker with independently scrolling hour and minute columns, default 12-hour AM/PM entry and optional 24-hour display. Keep values and bounds in canonical HH:mm format, support configurable minute steps and inclusive bounds, and provide accessible keyboard navigation, validation, clearing and styling hooks. Include English/French documentation and an interactive format preview.
|
|
8
|
+
|
|
3
9
|
## 0.9.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export { TimeOnlySchema, MinuteStepSchema, getTimePickerState, parseTimeOnly, Time12HourSchema, parse12HourTime, } from "./time-picker.schema.js";
|
|
2
|
+
export type TimePickerMessages = {
|
|
3
|
+
label: string;
|
|
4
|
+
open: string;
|
|
5
|
+
clear: string;
|
|
6
|
+
hours: string;
|
|
7
|
+
minutes: string;
|
|
8
|
+
period: string;
|
|
9
|
+
done: string;
|
|
10
|
+
invalid: string;
|
|
11
|
+
invalid12: string;
|
|
12
|
+
outOfRange: string;
|
|
13
|
+
invalidRange: string;
|
|
14
|
+
offStep: string;
|
|
15
|
+
};
|
|
16
|
+
export type TimePickerProps = {
|
|
17
|
+
/** HH:mm, or an empty/incomplete draft while typing. */
|
|
18
|
+
value: string;
|
|
19
|
+
onValueChange: (value: string) => void;
|
|
20
|
+
min?: string;
|
|
21
|
+
max?: string;
|
|
22
|
+
/** Allowed minute values start at 00 in each hour; integer from 1 to 60. */
|
|
23
|
+
minuteStep?: number;
|
|
24
|
+
/** Display and entry format. Values and bounds always use 24-hour HH:mm. */
|
|
25
|
+
hourCycle?: 12 | 24;
|
|
26
|
+
label?: string;
|
|
27
|
+
name?: string;
|
|
28
|
+
required?: boolean;
|
|
29
|
+
disabled?: boolean;
|
|
30
|
+
readOnly?: boolean;
|
|
31
|
+
clearable?: boolean;
|
|
32
|
+
description?: string;
|
|
33
|
+
locale?: string;
|
|
34
|
+
messages?: Partial<Record<string, Partial<TimePickerMessages>>>;
|
|
35
|
+
className?: string;
|
|
36
|
+
inputClassName?: string;
|
|
37
|
+
contentClassName?: string;
|
|
38
|
+
columnClassName?: string;
|
|
39
|
+
optionClassName?: string;
|
|
40
|
+
};
|
|
41
|
+
export declare const TimePicker: ({ value, onValueChange, min, max, minuteStep, hourCycle, label, name, required, disabled, readOnly, clearable, description, locale: override, messages, className, inputClassName, contentClassName, columnClassName, optionClassName, }: TimePickerProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
// ../../src/components/ui/time-picker.tsx
|
|
2
|
+
import { useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { Clock, X } from "lucide-react";
|
|
4
|
+
|
|
5
|
+
// ../../src/components/ui/button.tsx
|
|
6
|
+
import { Button as ButtonPrimitive } from "@base-ui/react/button";
|
|
7
|
+
import { cva } from "class-variance-authority";
|
|
8
|
+
|
|
9
|
+
// ../../src/lib/utils.ts
|
|
10
|
+
import { cn } from "cn";
|
|
11
|
+
|
|
12
|
+
// ../../src/components/ui/button.tsx
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
var buttonVariants = cva("group/button inline-flex shrink-0 items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", {
|
|
15
|
+
variants: {
|
|
16
|
+
variant: {
|
|
17
|
+
default: "bg-primary text-primary-foreground hover:bg-primary/80",
|
|
18
|
+
outline: "border-border bg-background shadow-xs hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
|
19
|
+
secondary: "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
|
20
|
+
ghost: "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
|
21
|
+
destructive: "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
|
22
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
23
|
+
},
|
|
24
|
+
size: {
|
|
25
|
+
default: "h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
26
|
+
xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
|
27
|
+
sm: "h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",
|
|
28
|
+
lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
29
|
+
icon: "size-9",
|
|
30
|
+
"icon-xs": "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
|
|
31
|
+
"icon-sm": "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",
|
|
32
|
+
"icon-lg": "size-10"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
defaultVariants: {
|
|
36
|
+
variant: "default",
|
|
37
|
+
size: "default"
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
function Button({
|
|
41
|
+
className,
|
|
42
|
+
variant = "default",
|
|
43
|
+
size = "default",
|
|
44
|
+
...props
|
|
45
|
+
}) {
|
|
46
|
+
return /* @__PURE__ */ jsx(ButtonPrimitive, {
|
|
47
|
+
"data-slot": "button",
|
|
48
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
49
|
+
...props
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ../../src/components/ui/input.tsx
|
|
54
|
+
import { Input as InputPrimitive } from "@base-ui/react/input";
|
|
55
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
56
|
+
function Input({ className, type, ...props }) {
|
|
57
|
+
return /* @__PURE__ */ jsx2(InputPrimitive, {
|
|
58
|
+
type,
|
|
59
|
+
"data-slot": "input",
|
|
60
|
+
className: cn("h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40", className),
|
|
61
|
+
...props
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../../src/components/ui/popover.tsx
|
|
66
|
+
import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
|
|
67
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
68
|
+
function Popover({ ...props }) {
|
|
69
|
+
return /* @__PURE__ */ jsx3(PopoverPrimitive.Root, {
|
|
70
|
+
"data-slot": "popover",
|
|
71
|
+
...props
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function PopoverTrigger({ ...props }) {
|
|
75
|
+
return /* @__PURE__ */ jsx3(PopoverPrimitive.Trigger, {
|
|
76
|
+
"data-slot": "popover-trigger",
|
|
77
|
+
...props
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function PopoverContent({
|
|
81
|
+
className,
|
|
82
|
+
align = "center",
|
|
83
|
+
alignOffset = 0,
|
|
84
|
+
side = "bottom",
|
|
85
|
+
sideOffset = 4,
|
|
86
|
+
...props
|
|
87
|
+
}) {
|
|
88
|
+
return /* @__PURE__ */ jsx3(PopoverPrimitive.Portal, {
|
|
89
|
+
children: /* @__PURE__ */ jsx3(PopoverPrimitive.Positioner, {
|
|
90
|
+
align,
|
|
91
|
+
alignOffset,
|
|
92
|
+
side,
|
|
93
|
+
sideOffset,
|
|
94
|
+
className: "isolate z-50",
|
|
95
|
+
children: /* @__PURE__ */ jsx3(PopoverPrimitive.Popup, {
|
|
96
|
+
"data-slot": "popover-content",
|
|
97
|
+
className: cn("z-50 flex w-72 origin-(--transform-origin) flex-col gap-4 rounded-md bg-popover p-4 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className),
|
|
98
|
+
...props
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function PopoverTitle({ className, ...props }) {
|
|
104
|
+
return /* @__PURE__ */ jsx3(PopoverPrimitive.Title, {
|
|
105
|
+
"data-slot": "popover-title",
|
|
106
|
+
className: cn("font-medium", className),
|
|
107
|
+
...props
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ../../src/components/ui/time-picker.tsx
|
|
112
|
+
import { useKrakstackLocale } from "@krak-stack/registry/krakstack-provider";
|
|
113
|
+
|
|
114
|
+
// ../../src/components/ui/time-picker.schema.ts
|
|
115
|
+
import { Option, Schema } from "effect";
|
|
116
|
+
var TimeOnlySchema = Schema.String.check(Schema.isPattern(/^(?:[01]\d|2[0-3]):[0-5]\d$/)).annotate({ identifier: "TimeOnly" });
|
|
117
|
+
var decodeTime = Schema.decodeUnknownOption(TimeOnlySchema);
|
|
118
|
+
var Time12HourSchema = Schema.String.check(Schema.isPattern(/^(?:0?[1-9]|1[0-2]):[0-5]\d\s?(?:AM|PM)$/i)).annotate({ identifier: "Time12Hour" });
|
|
119
|
+
var decode12Hour = Schema.decodeUnknownOption(Time12HourSchema);
|
|
120
|
+
var parse12HourTime = (value) => {
|
|
121
|
+
if (Option.isNone(decode12Hour(value)))
|
|
122
|
+
return;
|
|
123
|
+
const [hourText, rest] = value.split(":");
|
|
124
|
+
const hour = Number(hourText) % 12 + (value.toUpperCase().endsWith("PM") ? 12 : 0);
|
|
125
|
+
return `${String(hour).padStart(2, "0")}:${rest.slice(0, 2)}`;
|
|
126
|
+
};
|
|
127
|
+
var parseTimeOnly = (value) => {
|
|
128
|
+
const decoded = decodeTime(value);
|
|
129
|
+
return Option.isSome(decoded) ? Number(decoded.value.slice(0, 2)) * 60 + Number(decoded.value.slice(3)) : undefined;
|
|
130
|
+
};
|
|
131
|
+
var MinuteStepSchema = Schema.Number.check(Schema.isInt(), Schema.isBetween({ minimum: 1, maximum: 60 })).annotate({ identifier: "MinuteStep" });
|
|
132
|
+
var getTimePickerState = ({
|
|
133
|
+
value,
|
|
134
|
+
min,
|
|
135
|
+
max,
|
|
136
|
+
minuteStep = 1
|
|
137
|
+
}) => {
|
|
138
|
+
if (!Schema.is(MinuteStepSchema)(minuteStep) || min !== undefined && parseTimeOnly(min) === undefined || max !== undefined && parseTimeOnly(max) === undefined || min && max && min > max)
|
|
139
|
+
return "invalid-range";
|
|
140
|
+
const minimum = parseTimeOnly(min ?? "00:00");
|
|
141
|
+
const maximum = parseTimeOnly(max ?? "23:59");
|
|
142
|
+
const hasChoice = Array.from({ length: maximum - minimum + 1 }, (_, i) => minimum + i).some((time) => time % 60 % minuteStep === 0);
|
|
143
|
+
if (!hasChoice)
|
|
144
|
+
return "invalid-range";
|
|
145
|
+
if (!value)
|
|
146
|
+
return "empty";
|
|
147
|
+
const time = parseTimeOnly(value);
|
|
148
|
+
if (time === undefined)
|
|
149
|
+
return "invalid";
|
|
150
|
+
if (time < minimum || time > maximum)
|
|
151
|
+
return "out-of-range";
|
|
152
|
+
if (time % 60 % minuteStep !== 0)
|
|
153
|
+
return "off-step";
|
|
154
|
+
return "valid";
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// ../../src/components/ui/time-picker.tsx
|
|
158
|
+
import { jsx as jsx4, jsxs } from "react/jsx-runtime";
|
|
159
|
+
"use client";
|
|
160
|
+
var labels = {
|
|
161
|
+
en: {
|
|
162
|
+
label: "Time",
|
|
163
|
+
open: "Open time picker",
|
|
164
|
+
clear: "Clear time",
|
|
165
|
+
hours: "Hours",
|
|
166
|
+
minutes: "Minutes",
|
|
167
|
+
period: "AM/PM",
|
|
168
|
+
done: "Done",
|
|
169
|
+
invalid: "Enter a valid time as HH:mm (00:00–23:59).",
|
|
170
|
+
invalid12: "Enter a valid time with AM or PM, such as 09:30 AM.",
|
|
171
|
+
outOfRange: "Choose a time within the allowed range.",
|
|
172
|
+
invalidRange: "The allowed time range or minute step is invalid.",
|
|
173
|
+
offStep: "Choose a time that matches the minute step."
|
|
174
|
+
},
|
|
175
|
+
fr: {
|
|
176
|
+
label: "Heure",
|
|
177
|
+
open: "Ouvrir le sélecteur d’heure",
|
|
178
|
+
clear: "Effacer l’heure",
|
|
179
|
+
hours: "Heures",
|
|
180
|
+
minutes: "Minutes",
|
|
181
|
+
period: "AM/PM",
|
|
182
|
+
done: "Terminer",
|
|
183
|
+
invalid: "Saisissez une heure valide au format HH:mm (00:00–23:59).",
|
|
184
|
+
invalid12: "Saisissez une heure valide avec AM ou PM, par exemple 09:30 AM.",
|
|
185
|
+
outOfRange: "Choisissez une heure dans la plage autorisée.",
|
|
186
|
+
invalidRange: "La plage horaire ou le pas des minutes est invalide.",
|
|
187
|
+
offStep: "Choisissez une heure qui respecte le pas des minutes."
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
var pad = (value) => String(value).padStart(2, "0");
|
|
191
|
+
var formatTime = (value) => `${pad(Math.floor(value / 60))}:${pad(value % 60)}`;
|
|
192
|
+
var TimeColumn = ({
|
|
193
|
+
label,
|
|
194
|
+
values,
|
|
195
|
+
selected,
|
|
196
|
+
onSelect,
|
|
197
|
+
className,
|
|
198
|
+
optionClassName,
|
|
199
|
+
format = pad
|
|
200
|
+
}) => {
|
|
201
|
+
const id = useId();
|
|
202
|
+
const focusOnSelection = useRef(false);
|
|
203
|
+
const viewport = useRef(null);
|
|
204
|
+
const selectedOption = useRef(null);
|
|
205
|
+
const choicesKey = values.join(",");
|
|
206
|
+
useLayoutEffect(() => {
|
|
207
|
+
const container = viewport.current;
|
|
208
|
+
const option = selectedOption.current;
|
|
209
|
+
if (container && option) {
|
|
210
|
+
container.scrollTop = option.offsetTop - (container.clientHeight - option.offsetHeight) / 2;
|
|
211
|
+
}
|
|
212
|
+
}, [selected, choicesKey]);
|
|
213
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
214
|
+
"data-slot": "time-picker-column",
|
|
215
|
+
className: cn("grid min-w-0 gap-2", className),
|
|
216
|
+
children: [
|
|
217
|
+
/* @__PURE__ */ jsx4("p", {
|
|
218
|
+
id,
|
|
219
|
+
"data-slot": "time-picker-column-label",
|
|
220
|
+
className: "text-muted-foreground text-center text-xs font-medium",
|
|
221
|
+
children: label
|
|
222
|
+
}),
|
|
223
|
+
/* @__PURE__ */ jsx4("div", {
|
|
224
|
+
ref: viewport,
|
|
225
|
+
role: "radiogroup",
|
|
226
|
+
"aria-labelledby": id,
|
|
227
|
+
"data-slot": "time-picker-options",
|
|
228
|
+
className: "relative flex h-56 min-w-0 flex-col gap-1 overflow-y-auto overscroll-contain p-1",
|
|
229
|
+
children: values.map((value) => /* @__PURE__ */ jsx4(Button, {
|
|
230
|
+
type: "button",
|
|
231
|
+
role: "radio",
|
|
232
|
+
"aria-checked": value === selected,
|
|
233
|
+
tabIndex: value === selected ? 0 : -1,
|
|
234
|
+
variant: value === selected ? "secondary" : "ghost",
|
|
235
|
+
"data-component": "time-picker-option",
|
|
236
|
+
className: cn("w-full min-w-0 shrink-0 px-1 tabular-nums", optionClassName),
|
|
237
|
+
ref: (element) => {
|
|
238
|
+
if (value === selected)
|
|
239
|
+
selectedOption.current = element;
|
|
240
|
+
if (element && value === selected && focusOnSelection.current) {
|
|
241
|
+
element.focus({ preventScroll: true });
|
|
242
|
+
focusOnSelection.current = false;
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
onClick: () => onSelect(value),
|
|
246
|
+
onKeyDown: (event) => {
|
|
247
|
+
const index = values.indexOf(value);
|
|
248
|
+
const next = event.key === "Home" ? 0 : event.key === "End" ? values.length - 1 : event.key === "ArrowDown" || event.key === "ArrowRight" ? (index + 1) % values.length : event.key === "ArrowUp" || event.key === "ArrowLeft" ? (index - 1 + values.length) % values.length : undefined;
|
|
249
|
+
if (next === undefined)
|
|
250
|
+
return;
|
|
251
|
+
event.preventDefault();
|
|
252
|
+
focusOnSelection.current = true;
|
|
253
|
+
onSelect(values[next]);
|
|
254
|
+
},
|
|
255
|
+
children: format(value)
|
|
256
|
+
}, value))
|
|
257
|
+
})
|
|
258
|
+
]
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
var TimePicker = ({
|
|
262
|
+
value,
|
|
263
|
+
onValueChange,
|
|
264
|
+
min,
|
|
265
|
+
max,
|
|
266
|
+
minuteStep = 1,
|
|
267
|
+
hourCycle = 12,
|
|
268
|
+
label,
|
|
269
|
+
name,
|
|
270
|
+
required,
|
|
271
|
+
disabled,
|
|
272
|
+
readOnly,
|
|
273
|
+
clearable = true,
|
|
274
|
+
description,
|
|
275
|
+
locale: override,
|
|
276
|
+
messages,
|
|
277
|
+
className,
|
|
278
|
+
inputClassName,
|
|
279
|
+
contentClassName,
|
|
280
|
+
columnClassName,
|
|
281
|
+
optionClassName
|
|
282
|
+
}) => {
|
|
283
|
+
const id = useId();
|
|
284
|
+
const language = useKrakstackLocale(override);
|
|
285
|
+
const copy = {
|
|
286
|
+
...language.startsWith("fr") ? labels.fr : labels.en,
|
|
287
|
+
...messages?.[language.split("-")[0] ?? language],
|
|
288
|
+
...messages?.[language]
|
|
289
|
+
};
|
|
290
|
+
const [open, setOpen] = useState(false);
|
|
291
|
+
const [draft, setDraft] = useState();
|
|
292
|
+
const activeDraft = draft?.value === value && draft.hourCycle === hourCycle ? draft : undefined;
|
|
293
|
+
const canonicalStatus = getTimePickerState({ value, min, max, minuteStep });
|
|
294
|
+
const status = canonicalStatus !== "invalid-range" && hourCycle === 12 && activeDraft?.text && !parse12HourTime(activeDraft.text) ? "invalid" : canonicalStatus;
|
|
295
|
+
const parsedValue = parseTimeOnly(value);
|
|
296
|
+
const displayValue = activeDraft?.text ?? (hourCycle === 12 && parsedValue !== undefined ? `${Math.floor(parsedValue / 60) % 12 || 12}:${pad(parsedValue % 60)} ${parsedValue < 720 ? "AM" : "PM"}` : value);
|
|
297
|
+
const error = status === "invalid" ? hourCycle === 12 ? copy.invalid12 : copy.invalid : status === "invalid-range" ? copy.invalidRange : status === "out-of-range" ? copy.outOfRange : status === "off-step" ? copy.offStep : undefined;
|
|
298
|
+
const locked = disabled || readOnly;
|
|
299
|
+
const times = status === "invalid-range" ? [] : Array.from({ length: 1440 }, (_, time) => time).filter((time) => {
|
|
300
|
+
const text = formatTime(time);
|
|
301
|
+
return (!min || text >= min) && (!max || text <= max) && time % 60 % minuteStep === 0;
|
|
302
|
+
});
|
|
303
|
+
const selected = status === "valid" ? parseTimeOnly(value) : times[0] ?? 0;
|
|
304
|
+
const hour = Math.floor(selected / 60);
|
|
305
|
+
const period = Math.floor(hour / 12);
|
|
306
|
+
const periods = [...new Set(times.map((time) => Math.floor(time / 720)))];
|
|
307
|
+
const hours = [
|
|
308
|
+
...new Set(times.filter((time) => hourCycle === 24 || Math.floor(time / 720) === period).map((time) => Math.floor(time / 60)))
|
|
309
|
+
].sort((left, right) => hourCycle === 12 ? (left % 12 || 12) - (right % 12 || 12) : left - right);
|
|
310
|
+
const minutes = times.filter((time) => Math.floor(time / 60) === hour).map((time) => time % 60);
|
|
311
|
+
const select = (time) => {
|
|
312
|
+
const next = formatTime(time);
|
|
313
|
+
if (!locked && getTimePickerState({ value: next, min, max, minuteStep }) === "valid") {
|
|
314
|
+
setDraft(undefined);
|
|
315
|
+
onValueChange(next);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const describedBy = [description && `${id}-description`, error && `${id}-error`].filter(Boolean).join(" ") || undefined;
|
|
319
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
320
|
+
"data-slot": "time-picker",
|
|
321
|
+
"data-state": status,
|
|
322
|
+
className: cn("grid min-w-0 gap-2", className),
|
|
323
|
+
children: [
|
|
324
|
+
/* @__PURE__ */ jsx4("label", {
|
|
325
|
+
htmlFor: id,
|
|
326
|
+
"data-slot": "time-picker-label",
|
|
327
|
+
className: "text-sm font-medium",
|
|
328
|
+
children: label ?? copy.label
|
|
329
|
+
}),
|
|
330
|
+
/* @__PURE__ */ jsxs("div", {
|
|
331
|
+
"data-slot": "time-picker-controls",
|
|
332
|
+
className: "flex min-w-0 gap-2",
|
|
333
|
+
children: [
|
|
334
|
+
/* @__PURE__ */ jsx4(Input, {
|
|
335
|
+
id,
|
|
336
|
+
value: displayValue,
|
|
337
|
+
required,
|
|
338
|
+
disabled,
|
|
339
|
+
readOnly,
|
|
340
|
+
ref: (element) => {
|
|
341
|
+
element?.setCustomValidity(error ?? "");
|
|
342
|
+
},
|
|
343
|
+
placeholder: hourCycle === 12 ? "hh:mm AM/PM" : "HH:mm",
|
|
344
|
+
autoComplete: "off",
|
|
345
|
+
spellCheck: false,
|
|
346
|
+
"aria-invalid": Boolean(error),
|
|
347
|
+
"aria-describedby": describedBy,
|
|
348
|
+
"data-component": "time-picker-input",
|
|
349
|
+
className: cn("min-w-0 flex-1 tabular-nums", inputClassName),
|
|
350
|
+
onChange: (event) => {
|
|
351
|
+
if (locked)
|
|
352
|
+
return;
|
|
353
|
+
const text = event.target.value;
|
|
354
|
+
const next = hourCycle === 12 ? parse12HourTime(text) ?? text : text;
|
|
355
|
+
setDraft({ text, value: next, hourCycle });
|
|
356
|
+
onValueChange(next);
|
|
357
|
+
},
|
|
358
|
+
onBlur: () => {
|
|
359
|
+
if (activeDraft && (hourCycle === 24 || parse12HourTime(activeDraft.text)))
|
|
360
|
+
setDraft(undefined);
|
|
361
|
+
}
|
|
362
|
+
}),
|
|
363
|
+
/* @__PURE__ */ jsx4("input", {
|
|
364
|
+
type: "hidden",
|
|
365
|
+
name,
|
|
366
|
+
value,
|
|
367
|
+
disabled
|
|
368
|
+
}),
|
|
369
|
+
/* @__PURE__ */ jsxs(Popover, {
|
|
370
|
+
open: open && !locked && status !== "invalid-range",
|
|
371
|
+
onOpenChange: setOpen,
|
|
372
|
+
children: [
|
|
373
|
+
/* @__PURE__ */ jsx4(PopoverTrigger, {
|
|
374
|
+
render: /* @__PURE__ */ jsx4(Button, {
|
|
375
|
+
type: "button",
|
|
376
|
+
variant: "outline",
|
|
377
|
+
size: "icon",
|
|
378
|
+
disabled: locked || status === "invalid-range",
|
|
379
|
+
"aria-label": copy.open,
|
|
380
|
+
"data-component": "time-picker-trigger"
|
|
381
|
+
}),
|
|
382
|
+
children: /* @__PURE__ */ jsx4(Clock, {
|
|
383
|
+
"aria-hidden": "true"
|
|
384
|
+
})
|
|
385
|
+
}),
|
|
386
|
+
/* @__PURE__ */ jsxs(PopoverContent, {
|
|
387
|
+
align: "end",
|
|
388
|
+
"data-component": "time-picker-content",
|
|
389
|
+
className: cn("max-h-[calc(100dvh-2rem)] w-72 max-w-[calc(100vw-2rem)] gap-3 overflow-y-auto", contentClassName),
|
|
390
|
+
children: [
|
|
391
|
+
/* @__PURE__ */ jsx4(PopoverTitle, {
|
|
392
|
+
className: "sr-only",
|
|
393
|
+
children: label ?? copy.label
|
|
394
|
+
}),
|
|
395
|
+
/* @__PURE__ */ jsxs("div", {
|
|
396
|
+
"data-slot": "time-picker-columns",
|
|
397
|
+
className: cn("grid gap-3", hourCycle === 12 ? "grid-cols-3" : "grid-cols-2"),
|
|
398
|
+
children: [
|
|
399
|
+
/* @__PURE__ */ jsx4(TimeColumn, {
|
|
400
|
+
label: copy.hours,
|
|
401
|
+
values: hours,
|
|
402
|
+
selected: hour,
|
|
403
|
+
className: columnClassName,
|
|
404
|
+
format: hourCycle === 12 ? (hour) => String(hour % 12 || 12) : pad,
|
|
405
|
+
optionClassName,
|
|
406
|
+
onSelect: (nextHour) => {
|
|
407
|
+
const choices = times.filter((time) => Math.floor(time / 60) === nextHour);
|
|
408
|
+
const closest = choices.reduce((best, time) => Math.abs(time % 60 - selected % 60) < Math.abs(best % 60 - selected % 60) ? time : best, choices[0]);
|
|
409
|
+
select(closest);
|
|
410
|
+
}
|
|
411
|
+
}),
|
|
412
|
+
/* @__PURE__ */ jsx4(TimeColumn, {
|
|
413
|
+
label: copy.minutes,
|
|
414
|
+
values: minutes,
|
|
415
|
+
selected: selected % 60,
|
|
416
|
+
className: columnClassName,
|
|
417
|
+
optionClassName,
|
|
418
|
+
onSelect: (minute) => select(hour * 60 + minute)
|
|
419
|
+
}),
|
|
420
|
+
hourCycle === 12 ? /* @__PURE__ */ jsx4(TimeColumn, {
|
|
421
|
+
label: copy.period,
|
|
422
|
+
values: periods,
|
|
423
|
+
selected: period,
|
|
424
|
+
format: (value) => value === 0 ? "AM" : "PM",
|
|
425
|
+
className: columnClassName,
|
|
426
|
+
optionClassName,
|
|
427
|
+
onSelect: (nextPeriod) => {
|
|
428
|
+
const desired = (hour % 12 + nextPeriod * 12) * 60 + selected % 60;
|
|
429
|
+
const choices = times.filter((time) => Math.floor(time / 720) === nextPeriod);
|
|
430
|
+
const closest = choices.reduce((best, time) => Math.abs(time - desired) < Math.abs(best - desired) ? time : best, choices[0]);
|
|
431
|
+
select(closest);
|
|
432
|
+
}
|
|
433
|
+
}) : null
|
|
434
|
+
]
|
|
435
|
+
}),
|
|
436
|
+
/* @__PURE__ */ jsx4(Button, {
|
|
437
|
+
type: "button",
|
|
438
|
+
variant: "outline",
|
|
439
|
+
"data-component": "time-picker-done",
|
|
440
|
+
onClick: () => setOpen(false),
|
|
441
|
+
children: copy.done
|
|
442
|
+
})
|
|
443
|
+
]
|
|
444
|
+
})
|
|
445
|
+
]
|
|
446
|
+
}),
|
|
447
|
+
clearable && value ? /* @__PURE__ */ jsx4(Button, {
|
|
448
|
+
type: "button",
|
|
449
|
+
variant: "ghost",
|
|
450
|
+
size: "icon",
|
|
451
|
+
"data-component": "time-picker-clear",
|
|
452
|
+
disabled: locked,
|
|
453
|
+
"aria-label": copy.clear,
|
|
454
|
+
onClick: () => {
|
|
455
|
+
setDraft(undefined);
|
|
456
|
+
onValueChange("");
|
|
457
|
+
},
|
|
458
|
+
children: /* @__PURE__ */ jsx4(X, {
|
|
459
|
+
"aria-hidden": "true"
|
|
460
|
+
})
|
|
461
|
+
}) : null
|
|
462
|
+
]
|
|
463
|
+
}),
|
|
464
|
+
description ? /* @__PURE__ */ jsx4("p", {
|
|
465
|
+
id: `${id}-description`,
|
|
466
|
+
"data-slot": "time-picker-description",
|
|
467
|
+
className: "text-muted-foreground text-xs",
|
|
468
|
+
children: description
|
|
469
|
+
}) : null,
|
|
470
|
+
error ? /* @__PURE__ */ jsx4("p", {
|
|
471
|
+
id: `${id}-error`,
|
|
472
|
+
"data-slot": "time-picker-error",
|
|
473
|
+
role: "status",
|
|
474
|
+
className: "text-destructive text-sm",
|
|
475
|
+
children: error
|
|
476
|
+
}) : null
|
|
477
|
+
]
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
export {
|
|
481
|
+
MinuteStepSchema,
|
|
482
|
+
Time12HourSchema,
|
|
483
|
+
TimeOnlySchema,
|
|
484
|
+
TimePicker,
|
|
485
|
+
getTimePickerState,
|
|
486
|
+
parse12HourTime,
|
|
487
|
+
parseTimeOnly
|
|
488
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** A wall-clock time, never a date, instant or time zone. */
|
|
3
|
+
export declare const TimeOnlySchema: Schema.String;
|
|
4
|
+
export declare const Time12HourSchema: Schema.String;
|
|
5
|
+
/** Convert display text to canonical HH:mm; 12 AM is midnight, 12 PM is noon. */
|
|
6
|
+
export declare const parse12HourTime: (value: string) => string | undefined;
|
|
7
|
+
export declare const parseTimeOnly: (value: string) => number | undefined;
|
|
8
|
+
export declare const MinuteStepSchema: Schema.Number;
|
|
9
|
+
export declare const getTimePickerState: ({ value, min, max, minuteStep, }: {
|
|
10
|
+
value: string;
|
|
11
|
+
min?: string;
|
|
12
|
+
max?: string;
|
|
13
|
+
minuteStep?: number;
|
|
14
|
+
}) => "empty" | "invalid" | "invalid-range" | "off-step" | "out-of-range" | "valid";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krak-stack/registry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Tree-shakable KrakStack components and Effect services.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"type": "module",
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"exports": {
|
|
20
|
+
"./time-picker": {
|
|
21
|
+
"types": "./dist/components/ui/time-picker.d.ts",
|
|
22
|
+
"import": "./dist/components/ui/time-picker.js"
|
|
23
|
+
},
|
|
20
24
|
"./date-picker": {
|
|
21
25
|
"types": "./dist/components/ui/date-picker.d.ts",
|
|
22
26
|
"import": "./dist/components/ui/date-picker.js"
|