@dinachi/cli 1.1.0 → 1.4.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +1 -0
  3. package/dist/index.js +698 -314
  4. package/package.json +5 -1
  5. package/templates/agent/agent.tsx +35 -18
  6. package/templates/alert/alert.tsx +345 -0
  7. package/templates/alert/index.ts +16 -0
  8. package/templates/ask-bar/ask-bar.tsx +1061 -0
  9. package/templates/ask-bar/index.ts +1 -0
  10. package/templates/autocomplete/autocomplete.tsx +0 -1
  11. package/templates/breadcrumb/breadcrumb.tsx +606 -0
  12. package/templates/breadcrumb/index.ts +9 -0
  13. package/templates/calendar/calendar.tsx +1155 -0
  14. package/templates/calendar/index.ts +15 -0
  15. package/templates/combobox/combobox.tsx +0 -1
  16. package/templates/command-items/command-items.tsx +69 -0
  17. package/templates/command-items/index.ts +1 -0
  18. package/templates/command-palette/command-palette.tsx +954 -0
  19. package/templates/command-palette/index.ts +1 -0
  20. package/templates/comparison-table/comparison-table.tsx +540 -0
  21. package/templates/comparison-table/index.ts +15 -0
  22. package/templates/data-table/data-table.tsx +2077 -0
  23. package/templates/data-table/index.ts +14 -0
  24. package/templates/date-picker/date-picker.tsx +527 -0
  25. package/templates/date-picker/index.ts +2 -0
  26. package/templates/date-range-picker/date-range-picker.tsx +504 -0
  27. package/templates/date-range-picker/index.ts +2 -0
  28. package/templates/detail-list/detail-list.tsx +419 -0
  29. package/templates/detail-list/index.ts +12 -0
  30. package/templates/heat-table/heat-table.tsx +613 -0
  31. package/templates/heat-table/index.ts +9 -0
  32. package/templates/menu/menu.tsx +8 -3
  33. package/templates/month-picker/index.ts +1 -0
  34. package/templates/month-picker/month-picker.tsx +351 -0
  35. package/templates/omnibar/index.ts +1 -0
  36. package/templates/omnibar/omnibar.tsx +1009 -0
  37. package/templates/pagination/index.ts +8 -0
  38. package/templates/pagination/pagination.tsx +389 -0
  39. package/templates/table/index.ts +13 -0
  40. package/templates/table/table.tsx +265 -0
  41. package/templates/time-slots/index.ts +1 -0
  42. package/templates/time-slots/time-slots.tsx +589 -0
  43. package/templates/week-strip/index.ts +1 -0
  44. package/templates/week-strip/week-strip.tsx +329 -0
  45. package/templates/year-view/index.ts +1 -0
  46. package/templates/year-view/year-view.tsx +362 -0
@@ -0,0 +1,351 @@
1
+ "use client"
2
+
3
+ import * as React from "react";
4
+ import { format, type Locale } from "date-fns";
5
+ import { ChevronLeft, ChevronRight } from "lucide-react";
6
+ import { cn } from "@/lib/utils";
7
+ import { changeAsAgent, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent";
8
+ import { type CalendarChangeDetails, type DateRange, addMonths, fromISODate, monthsBetween, parseDate } from "@/components/ui/calendar";
9
+
10
+ /** A month is a Date on its first day, at local midnight. Forms carry it as YYYY-MM. */
11
+ const toISOMonth = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
12
+ const monthOf = (d: Date) => new Date(d.getFullYear(), d.getMonth(), 1);
13
+ const sameMonth = (a: Date | null | undefined, b: Date | null | undefined) =>
14
+ !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth();
15
+
16
+ function changeDetails(reason: string, event?: Event): CalendarChangeDetails {
17
+ const details: CalendarChangeDetails = {
18
+ reason,
19
+ event,
20
+ isCanceled: false,
21
+ cancel() {
22
+ details.isCanceled = true;
23
+ },
24
+ };
25
+ return details;
26
+ }
27
+
28
+ const EMPTY: DateRange = { from: null, to: null };
29
+
30
+ interface MonthPickerBaseProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange" | "dir"> {
31
+ /** The earliest month that can be picked. */
32
+ min?: Date;
33
+ /** The latest month that can be picked. */
34
+ max?: Date;
35
+ /** Months that can't be picked, besides those outside `min` and `max`. Called with the first of the month. */
36
+ isMonthDisabled?: (month: Date) => boolean;
37
+ disabled?: boolean;
38
+ /** A date-fns locale, for month names. */
39
+ locale?: Locale;
40
+ dir?: "ltr" | "rtl";
41
+ /** Defaults to the current day. Its month carries a ring. */
42
+ today?: Date;
43
+ /** Submits YYYY-MM, or YYYY-MM/YYYY-MM for a range. */
44
+ name?: string;
45
+ required?: boolean;
46
+ }
47
+
48
+ export type MonthPickerProps = MonthPickerBaseProps &
49
+ (
50
+ | {
51
+ mode?: "single";
52
+ value?: Date | null;
53
+ defaultValue?: Date | null;
54
+ onValueChange?: (value: Date | null, details: CalendarChangeDetails) => void;
55
+ }
56
+ | {
57
+ mode: "range";
58
+ value?: DateRange;
59
+ defaultValue?: DateRange;
60
+ onValueChange?: (value: DateRange, details: CalendarChangeDetails) => void;
61
+ }
62
+ );
63
+
64
+ type Value = Date | null | DateRange;
65
+
66
+ const KEYS: Record<string, number> = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -3, ArrowDown: 3, PageUp: -12, PageDown: 12 };
67
+
68
+ /**
69
+ * A year of months in three columns, for billing and reporting periods where days are
70
+ * noise. In range mode two picks make a span, banded the way Calendar bands days.
71
+ */
72
+ const MonthPicker = React.forwardRef<HTMLDivElement, MonthPickerProps>((props, ref) => {
73
+ const {
74
+ mode = "single",
75
+ value: valueProp,
76
+ defaultValue,
77
+ onValueChange,
78
+ min: minProp,
79
+ max: maxProp,
80
+ isMonthDisabled,
81
+ disabled,
82
+ locale,
83
+ dir,
84
+ today: todayProp,
85
+ name,
86
+ required,
87
+ className,
88
+ "aria-label": ariaLabel = mode === "range" ? "Months" : "Month",
89
+ ...rest
90
+ } = props;
91
+ const range = mode === "range";
92
+ const hasAgent = useHasAgent();
93
+
94
+ const thisMonth = monthOf(todayProp ?? new Date());
95
+ const min = minProp && monthOf(minProp);
96
+ const max = maxProp && monthOf(maxProp);
97
+ const [value, change] = useAgentState<Value, CalendarChangeDetails>(
98
+ valueProp as Value | undefined,
99
+ (defaultValue as Value | undefined) ?? (range ? EMPTY : null),
100
+ onValueChange as ((v: Value, d: CalendarChangeDetails) => void) | undefined
101
+ );
102
+ const from = range ? (value as DateRange).from && monthOf((value as DateRange).from!) : value && monthOf(value as Date);
103
+ const to = range ? (value as DateRange).to && monthOf((value as DateRange).to!) : from;
104
+
105
+ const outside = (m: Date) => (!!min && m < min) || (!!max && m > max);
106
+ const blocked = (m: Date) => !!disabled || outside(m) || !!isMonthDisabled?.(m);
107
+ const clamp = (m: Date) => (min && m < min ? min : max && m > max ? max : m);
108
+
109
+ /* ---- which month has focus; its year is the one on screen ---- */
110
+ const anchor = () => clamp(from ?? thisMonth);
111
+ const [focus, setFocus] = React.useState(anchor);
112
+ const key = `${from ? toISOMonth(from) : ""}/${to ? toISOMonth(to) : ""}`;
113
+ const [seen, setSeen] = React.useState(key);
114
+ if (seen !== key) {
115
+ setSeen(key);
116
+ // A value set from outside brings its year into view, unless one end is already on screen.
117
+ if (from && ![from, to].some((m) => m?.getFullYear() === focus.getFullYear())) setFocus(anchor());
118
+ }
119
+ const year = focus.getFullYear();
120
+
121
+ const root = React.useRef<HTMLDivElement>(null);
122
+ const moved = React.useRef(false);
123
+ React.useEffect(() => {
124
+ if (!moved.current) return;
125
+ moved.current = false;
126
+ root.current?.querySelector<HTMLElement>(`[data-month="${toISOMonth(focus)}"]`)?.focus();
127
+ }, [focus.getTime()]);
128
+
129
+ const commit = (next: Value, details: CalendarChangeDetails) => {
130
+ const eq = (a: Date | null | undefined, b: Date | null | undefined) => (!a && !b) || sameMonth(a, b);
131
+ const same = range ? eq((next as DateRange).from, from) && eq((next as DateRange).to, to) : eq(next as Date | null, from);
132
+ if (!same) change(next, details);
133
+ };
134
+
135
+ const pick = (m: Date, event?: Event) => {
136
+ if (blocked(m)) return;
137
+ setFocus(m);
138
+ if (!range) return commit(m, changeDetails("month-press", event));
139
+ const r = value as DateRange;
140
+ // A third pick, or one before the start, starts a new span.
141
+ const next = !r.from || r.to || m < from! ? { from: m, to: null } : { from: r.from, to: m };
142
+ commit(next, changeDetails("month-press", event));
143
+ };
144
+
145
+ /* ---- agent ---- */
146
+ const monthNames = React.useMemo(
147
+ () => Array.from({ length: 12 }, (_, i) => [format(new Date(2000, i, 1), "MMMM", { locale }).toLowerCase(), "january february march april may june july august september october november december".split(" ")[i]]),
148
+ [locale]
149
+ );
150
+ const readMonth = (item: unknown): Date | null => {
151
+ if (item instanceof Date) return isNaN(item.getTime()) ? null : monthOf(item);
152
+ if (typeof item !== "string") return null;
153
+ const t = item.trim().toLowerCase();
154
+ const iso = /^(\d{4})-(\d{1,2})$/.exec(t);
155
+ if (iso && +iso[2] >= 1 && +iso[2] <= 12) return new Date(+iso[1], +iso[2] - 1, 1);
156
+ const named = /^([^\d\s.,]+)\.?,?\s*(\d{4})?$/.exec(t);
157
+ if (named && named[1].length >= 3) {
158
+ const i = monthNames.findIndex((names) => names.some((n) => n.startsWith(named[1])));
159
+ if (i >= 0) return new Date(named[2] ? +named[2] : thisMonth.getFullYear(), i, 1);
160
+ }
161
+ const day = fromISODate(t) ?? parseDate(t, { today: todayProp ?? new Date() });
162
+ return day && monthOf(day);
163
+ };
164
+ const refusal = (m: Date): string | null => {
165
+ if (min && m < min) return `The earliest is ${toISOMonth(min)}.`;
166
+ if (max && m > max) return `The latest is ${toISOMonth(max)}.`;
167
+ if (isMonthDisabled?.(m)) return `${toISOMonth(m)} can't be picked.`;
168
+ return null;
169
+ };
170
+
171
+ /** A value set from outside that names a month that can't be picked. */
172
+ const wrong = (m: Date | null | undefined) => !!m && !!refusal(m);
173
+ const problem = [from, to].map((m) => m && refusal(m)).find(Boolean) ?? null;
174
+
175
+ useAgentControl(() => ({
176
+ kind: "date",
177
+ element: root.current,
178
+ invalid: !!problem || undefined,
179
+ errors: problem ? [problem] : undefined,
180
+ value: range ? (from ? [toISOMonth(from), ...((value as DateRange).to ? [toISOMonth(to!)] : [])] : undefined) : from ? toISOMonth(from) : undefined,
181
+ multiple: range,
182
+ disabled,
183
+ required,
184
+ actions: {
185
+ setValue: (wanted: unknown) => {
186
+ if (wanted === null || wanted === "") return changeAsAgent(commit, range ? EMPTY : null);
187
+ const parts = !range
188
+ ? [wanted]
189
+ : Array.isArray(wanted)
190
+ ? wanted
191
+ : typeof wanted === "string"
192
+ ? // "jun to nov 2026": a month with no year takes the one after it.
193
+ wanted.split(/\s*(?:\/|,| to )\s*/i).map((p, _, all) => (/\d/.test(p) ? p : `${p} ${/\d{4}$/.exec(all[all.length - 1])?.[0] ?? ""}`))
194
+ : [wanted];
195
+ const months = parts.map(readMonth);
196
+ const bad = parts.findIndex((_, i) => !months[i]);
197
+ if (bad >= 0) return `"${String(parts[bad])}" is not a month. Send YYYY-MM, or a name such as jun 2026.`;
198
+ const why = months.map((m) => refusal(m!)).find(Boolean);
199
+ if (why) return why;
200
+ if (!range) return changeAsAgent(commit, months[0]);
201
+ if (months.length !== 2) return "Send two months, from and to.";
202
+ if (months[1]! < months[0]!) return "The span must end on or after its start.";
203
+ return changeAsAgent(commit, { from: months[0], to: months[1] });
204
+ },
205
+ clear: () => changeAsAgent(commit, range ? EMPTY : null),
206
+ },
207
+ }));
208
+
209
+ /* ---- render ---- */
210
+ const months = Array.from({ length: 12 }, (_, i) => new Date(year, i, 1));
211
+ const done = range && from && (value as DateRange).to;
212
+ const count = done ? monthsBetween(from!, to!) + 1 : 0;
213
+ const short = (d: Date) => format(d, "MMM", { locale });
214
+ const submitted = range ? (done ? `${toISOMonth(from!)}/${toISOMonth(to!)}` : "") : from ? toISOMonth(from) : "";
215
+ const step = (n: number) => setFocus((f) => clamp(new Date(f.getFullYear() + n, f.getMonth(), 1)));
216
+
217
+ const navButton = (n: -1 | 1) => (
218
+ <button
219
+ type="button"
220
+ aria-label={n < 0 ? "Previous year" : "Next year"}
221
+ data-agent-part={hasAgent ? "" : undefined}
222
+ disabled={disabled || (n < 0 ? !!min && year <= min.getFullYear() : !!max && year >= max.getFullYear())}
223
+ onClick={() => step(n)}
224
+ className={cn(
225
+ "inline-flex size-8 items-center justify-center rounded-md text-muted-foreground",
226
+ "hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
227
+ "disabled:pointer-events-none disabled:opacity-40"
228
+ )}
229
+ >
230
+ {n < 0 ? <ChevronLeft className="size-4 rtl:rotate-180" aria-hidden /> : <ChevronRight className="size-4 rtl:rotate-180" aria-hidden />}
231
+ </button>
232
+ );
233
+
234
+ return (
235
+ <div
236
+ {...rest}
237
+ ref={useMergedRef(ref, root)}
238
+ role="group"
239
+ aria-label={ariaLabel}
240
+ aria-disabled={disabled || undefined}
241
+ dir={dir}
242
+ data-slot="month-picker"
243
+ data-invalid={problem ? "" : undefined}
244
+ className={cn("relative w-full max-w-80", className)}
245
+ >
246
+ <div data-slot="month-picker-header" className="mb-2.5 flex items-center">
247
+ {navButton(-1)}
248
+ <div data-slot="month-picker-year" aria-live="polite" className="flex-1 text-center text-sm font-semibold tabular-nums">
249
+ {year}
250
+ </div>
251
+ {navButton(1)}
252
+ </div>
253
+ <div
254
+ data-slot="month-picker-grid"
255
+ className="grid grid-cols-3 gap-y-1.5"
256
+ onKeyDown={(e) => {
257
+ let n = KEYS[e.key];
258
+ if (e.key === "Home") n = -focus.getMonth();
259
+ if (e.key === "End") n = 11 - focus.getMonth();
260
+ if (n === undefined) return;
261
+ if ((e.key === "ArrowLeft" || e.key === "ArrowRight") && root.current?.closest("[dir]")?.getAttribute("dir") === "rtl") n = -n;
262
+ e.preventDefault();
263
+ const home = e.key === "Home" || e.key === "End";
264
+ const next = home ? clamp(addMonths(focus, n)) : addMonths(focus, n);
265
+ if (outside(next)) return;
266
+ moved.current = true;
267
+ setFocus(next);
268
+ }}
269
+ >
270
+ {months.map((m) => {
271
+ const ends = sameMonth(m, from) || sameMonth(m, to);
272
+ const within = !!done && m >= from! && m <= to!;
273
+ const col = m.getMonth() % 3;
274
+ const bad = ends && wrong(m);
275
+ return (
276
+ <span
277
+ key={m.getMonth()}
278
+ data-range={done && within ? (sameMonth(m, from) ? "start" : sameMonth(m, to) ? "end" : "middle") : undefined}
279
+ className={cn(
280
+ "grid h-11 place-items-center",
281
+ done && within && from! < to! && "bg-secondary",
282
+ done && within && (sameMonth(m, from) || col === 0) && "rounded-s-xl",
283
+ done && within && (sameMonth(m, to) || col === 2) && "rounded-e-xl"
284
+ )}
285
+ >
286
+ <button
287
+ type="button"
288
+ data-slot="month-picker-month"
289
+ data-month={toISOMonth(m)}
290
+ data-agent-part={hasAgent ? "" : undefined}
291
+ data-selected={(ends && !bad) || undefined}
292
+ data-invalid={bad || undefined}
293
+ data-current={sameMonth(m, thisMonth) || undefined}
294
+ tabIndex={sameMonth(m, focus) ? 0 : -1}
295
+ aria-label={`${format(m, "MMMM yyyy", { locale })}${bad ? ", can't be picked" : ""}`}
296
+ aria-current={sameMonth(m, thisMonth) ? "date" : undefined}
297
+ aria-pressed={ends || within}
298
+ aria-disabled={blocked(m) || undefined}
299
+ onClick={(e) => pick(m, e.nativeEvent)}
300
+ className={cn(
301
+ "h-10 w-full max-w-[5.25rem] rounded-[0.625rem] border border-transparent text-[0.8125rem] outline-none transition-colors duration-150 motion-reduce:transition-none",
302
+ "hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
303
+ // This month is ringed with a border, so the focus ring stays free for focus.
304
+ "data-[current]:border-muted-foreground",
305
+ "data-[selected]:border-transparent data-[selected]:bg-primary data-[selected]:font-medium data-[selected]:text-primary-foreground",
306
+ "aria-disabled:pointer-events-none",
307
+ bad ? "border-destructive bg-destructive/10 font-medium text-destructive" : "aria-disabled:opacity-30"
308
+ )}
309
+ >
310
+ {short(m)}
311
+ </button>
312
+ </span>
313
+ );
314
+ })}
315
+ </div>
316
+ {range && (
317
+ <p data-slot="month-picker-readout" aria-live="polite" className="mt-3 min-h-5 border-t pt-3 text-[0.8125rem]">
318
+ {done ? (
319
+ <>
320
+ {short(from!)}
321
+ {from!.getFullYear() !== to!.getFullYear() && ` ${from!.getFullYear()}`} to {short(to!)} {to!.getFullYear()}{" "}
322
+ <span className="text-muted-foreground">
323
+ · {count} {count === 1 ? "month" : "months"}
324
+ </span>
325
+ </>
326
+ ) : from ? (
327
+ <>
328
+ {short(from)} {from.getFullYear()} <span className="text-muted-foreground">to ...</span>
329
+ </>
330
+ ) : null}
331
+ </p>
332
+ )}
333
+ {(name || required) && (
334
+ <input
335
+ aria-hidden
336
+ tabIndex={-1}
337
+ name={name}
338
+ required={required}
339
+ disabled={disabled}
340
+ value={submitted}
341
+ onChange={() => {}}
342
+ onFocus={() => root.current?.querySelector<HTMLElement>("[data-month][tabindex='0']")?.focus()}
343
+ className="pointer-events-none absolute bottom-0 start-0 size-px opacity-0"
344
+ />
345
+ )}
346
+ </div>
347
+ );
348
+ });
349
+ MonthPicker.displayName = "MonthPicker";
350
+
351
+ export { MonthPicker };
@@ -0,0 +1 @@
1
+ export * from "./omnibar"