@dashforge/tw 0.9.0-beta → 0.9.1-beta

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 (27) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/index.esm.js +629 -67
  3. package/dist/src/components/Calendar/Calendar.d.ts +14 -0
  4. package/dist/src/components/Calendar/Calendar.d.ts.map +1 -0
  5. package/dist/src/components/Calendar/calendar.types.d.ts +72 -0
  6. package/dist/src/components/Calendar/calendar.types.d.ts.map +1 -0
  7. package/dist/src/components/Calendar/calendar.variants.d.ts +99 -0
  8. package/dist/src/components/Calendar/calendar.variants.d.ts.map +1 -0
  9. package/dist/src/components/DatePicker/DatePicker.d.ts +14 -0
  10. package/dist/src/components/DatePicker/DatePicker.d.ts.map +1 -0
  11. package/dist/src/components/DatePicker/datePicker.types.d.ts +88 -0
  12. package/dist/src/components/DatePicker/datePicker.types.d.ts.map +1 -0
  13. package/dist/src/components/DatePicker/datePicker.variants.d.ts +97 -0
  14. package/dist/src/components/DatePicker/datePicker.variants.d.ts.map +1 -0
  15. package/dist/src/index.d.ts +6 -0
  16. package/dist/src/index.d.ts.map +1 -1
  17. package/package.json +2 -1
  18. package/src/components/Calendar/Calendar.test.tsx +75 -0
  19. package/src/components/Calendar/Calendar.tsx +264 -0
  20. package/src/components/Calendar/calendar.types.ts +65 -0
  21. package/src/components/Calendar/calendar.variants.ts +64 -0
  22. package/src/components/DatePicker/DatePicker.test.tsx +76 -0
  23. package/src/components/DatePicker/DatePicker.tsx +305 -0
  24. package/src/components/DatePicker/datePicker.types.ts +77 -0
  25. package/src/components/DatePicker/datePicker.variants.ts +50 -0
  26. package/src/index.ts +21 -1
  27. package/tsconfig.lib.json +7 -4
@@ -0,0 +1,305 @@
1
+ import { useCallback, useContext, useEffect, useId, useRef, useState } from 'react';
2
+ import { DashFormContext, useEngineVisibility } from '@dashforge/ui-core';
3
+ import type { DashFormBridge, FieldRegistration } from '@dashforge/ui-core';
4
+ import { useDashFieldMeta } from '@dashforge/forms';
5
+ import { parseISODate } from '@dashforge/calendar-core';
6
+ import type { ISODate } from '@dashforge/calendar-core';
7
+ import { cn } from '../../utils/cn.js';
8
+ import { useAccessState } from '../../hooks/useAccessState.js';
9
+ import { resolveValidationState } from '../_shared/resolveValidationState.js';
10
+ import { Popover } from '../Popover/Popover.js';
11
+ import { Calendar } from '../Calendar/Calendar.js';
12
+ import { datePickerVariants } from './datePicker.variants.js';
13
+ import type { DatePickerProps } from './datePicker.types.js';
14
+
15
+ // Inline 16×16 stroke calendar glyph — no icon dependency (tw convention).
16
+ function CalendarIcon() {
17
+ return (
18
+ <svg
19
+ width="1em"
20
+ height="1em"
21
+ viewBox="0 0 16 16"
22
+ fill="none"
23
+ aria-hidden="true"
24
+ >
25
+ <rect
26
+ x="2.5"
27
+ y="3.5"
28
+ width="11"
29
+ height="10"
30
+ rx="1.5"
31
+ stroke="currentColor"
32
+ strokeWidth="1.5"
33
+ />
34
+ <path
35
+ d="M2.5 6.5h11M5.5 2v3M10.5 2v3"
36
+ stroke="currentColor"
37
+ strokeWidth="1.5"
38
+ strokeLinecap="round"
39
+ />
40
+ </svg>
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Formats a stored ISO date for the trigger display, localized to a medium
46
+ * date style (e.g. `"May 20, 2026"`). Formatting is done in UTC so the
47
+ * displayed day always matches the stored `YYYY-MM-DD`.
48
+ */
49
+ function formatDisplayDate(
50
+ iso: ISODate | null | undefined,
51
+ locale: string,
52
+ ): string {
53
+ if (iso == null || iso === '') {
54
+ return '';
55
+ }
56
+ const parts = parseISODate(iso);
57
+ if (parts === null) {
58
+ return iso;
59
+ }
60
+ return new Intl.DateTimeFormat(locale, {
61
+ dateStyle: 'medium',
62
+ timeZone: 'UTC',
63
+ }).format(Date.UTC(parts.year, parts.month - 1, parts.day));
64
+ }
65
+
66
+ /**
67
+ * Dashforge TW `<DatePicker>` — a form-bound single-date field.
68
+ *
69
+ * A read-only trigger button paired with a `<Calendar>` popover (Radix
70
+ * Popover — toggle / Escape / outside-click / focus-trap are Radix-managed).
71
+ * Integrates with the Dashforge form bridge + RBAC, mirroring the other
72
+ * `@dashforge/tw` form fields.
73
+ *
74
+ * Storage contract: an ISO `YYYY-MM-DD` string, or `null` — shared with the
75
+ * MUI `@dashforge/ui` `DatePicker`.
76
+ */
77
+ export function DatePicker(props: DatePickerProps) {
78
+ const {
79
+ name,
80
+ rules,
81
+ label,
82
+ helperText,
83
+ error,
84
+ required,
85
+ disabled,
86
+ placeholder,
87
+ layout = 'stacked',
88
+ visibleWhen,
89
+ access,
90
+ value: explicitValue,
91
+ defaultValue,
92
+ onChange,
93
+ minDate,
94
+ maxDate,
95
+ disabledDates,
96
+ isDateDisabled,
97
+ weekStartDay,
98
+ locale,
99
+ fullWidth,
100
+ sx,
101
+ slotProps,
102
+ testId,
103
+ } = props;
104
+
105
+ // ───── Hooks (unconditional, before any early return) ─────
106
+ const bridge = useContext(DashFormContext) as DashFormBridge | null;
107
+ const isVisible = useEngineVisibility(bridge?.engine, visibleWhen);
108
+ const fieldMeta = useDashFieldMeta(name);
109
+ const accessState = useAccessState(access);
110
+
111
+ const fieldId = useId();
112
+ const helperId = `${fieldId}-help`;
113
+
114
+ const [isOpen, setIsOpen] = useState(false);
115
+ const [internalValue, setInternalValue] = useState<ISODate | null>(
116
+ defaultValue ?? null,
117
+ );
118
+
119
+ // StrictMode-safe unregister-on-unmount.
120
+ const unregisterRef = useRef({ bridge, name });
121
+ unregisterRef.current = { bridge, name };
122
+ const isMountedRef = useRef(false);
123
+ useEffect(() => {
124
+ isMountedRef.current = true;
125
+ return () => {
126
+ isMountedRef.current = false;
127
+ const { bridge: cap, name: capName } = unregisterRef.current;
128
+ queueMicrotask(() => {
129
+ if (!isMountedRef.current) cap?.unregister?.(capName);
130
+ });
131
+ };
132
+ }, []);
133
+
134
+ // ───── Derived ─────
135
+ const effectiveDisabled = Boolean(disabled) || accessState.disabled;
136
+ const effectiveReadOnly = accessState.readonly;
137
+ const isInteractive = !effectiveDisabled && !effectiveReadOnly;
138
+ const isFormMode = Boolean(bridge?.register);
139
+
140
+ let resolvedError = error;
141
+ let resolvedHelperText: typeof helperText = helperText;
142
+ let registration: FieldRegistration | null = null;
143
+ let resolvedValue: ISODate | null;
144
+
145
+ if (isFormMode && bridge) {
146
+ registration = bridge.register(name, rules);
147
+ const validation = resolveValidationState(name, bridge, error, helperText);
148
+ resolvedError = validation.error;
149
+ resolvedHelperText = validation.helperText;
150
+ if (explicitValue !== undefined) {
151
+ resolvedValue = explicitValue;
152
+ } else {
153
+ const bv = fieldMeta.value;
154
+ resolvedValue = bv == null ? null : String(bv);
155
+ }
156
+ } else if (explicitValue !== undefined) {
157
+ resolvedValue = explicitValue;
158
+ } else {
159
+ resolvedValue = internalValue;
160
+ }
161
+
162
+ // RHF threads its own callback ref through here in form mode.
163
+ const registrationRefFn = registration?.ref;
164
+ const triggerRef = useCallback(
165
+ (instance: HTMLButtonElement | null) => {
166
+ if (typeof registrationRefFn === 'function') {
167
+ registrationRefFn(instance);
168
+ }
169
+ },
170
+ [registrationRefFn],
171
+ );
172
+
173
+ // ───── Render-time guards (after all hooks) ─────
174
+ if (!isVisible) return null;
175
+ if (!accessState.visible) return null;
176
+
177
+ const v = datePickerVariants({
178
+ layout,
179
+ error: resolvedError,
180
+ fullWidth,
181
+ });
182
+ const resolvedLocale = locale ?? 'en-US';
183
+ const displayValue = formatDisplayDate(resolvedValue, resolvedLocale);
184
+
185
+ const commitValue = (next: ISODate | null) => {
186
+ if (isFormMode && bridge) {
187
+ bridge.setValue?.(name, next);
188
+ void registration?.onChange?.({
189
+ target: { name, value: next ?? '' },
190
+ type: 'change',
191
+ });
192
+ } else if (explicitValue === undefined) {
193
+ setInternalValue(next);
194
+ }
195
+ onChange?.(next);
196
+ };
197
+
198
+ // Mark the field touched on popover close. Reads the freshest committed
199
+ // value straight from the bridge so the touched-blur never reverts a
200
+ // value just chosen in the calendar.
201
+ const markTouched = () => {
202
+ if (isFormMode && bridge) {
203
+ const committed = bridge.getValue(name);
204
+ void registration?.onBlur?.({
205
+ target: { name, value: committed == null ? '' : String(committed) },
206
+ type: 'blur',
207
+ });
208
+ }
209
+ };
210
+
211
+ const handleOpenChange = (next: boolean) => {
212
+ if (next && !isInteractive) {
213
+ return;
214
+ }
215
+ setIsOpen(next);
216
+ if (!next) {
217
+ markTouched();
218
+ }
219
+ };
220
+
221
+ const handleCalendarChange = (next: ISODate) => {
222
+ commitValue(next);
223
+ setIsOpen(false);
224
+ markTouched();
225
+ };
226
+
227
+ return (
228
+ <div
229
+ data-testid={testId}
230
+ className={cn(v.root(), sx, slotProps?.root?.className)}
231
+ >
232
+ {label && (
233
+ <label
234
+ htmlFor={fieldId}
235
+ className={cn(v.label(), slotProps?.label?.className)}
236
+ >
237
+ {label}
238
+ {required && (
239
+ <span
240
+ aria-hidden="true"
241
+ className={cn(
242
+ v.requiredMark(),
243
+ slotProps?.requiredMark?.className,
244
+ )}
245
+ >
246
+ *
247
+ </span>
248
+ )}
249
+ </label>
250
+ )}
251
+
252
+ <Popover
253
+ open={isOpen}
254
+ onOpenChange={handleOpenChange}
255
+ side="bottom"
256
+ align="start"
257
+ content={
258
+ <Calendar
259
+ value={resolvedValue}
260
+ onChange={handleCalendarChange}
261
+ minDate={minDate}
262
+ maxDate={maxDate}
263
+ disabledDates={disabledDates}
264
+ isDateDisabled={isDateDisabled}
265
+ weekStartDay={weekStartDay}
266
+ locale={locale}
267
+ aria-label="Choose date"
268
+ sx="border-0 bg-transparent p-0"
269
+ />
270
+ }
271
+ >
272
+ <button
273
+ type="button"
274
+ id={fieldId}
275
+ ref={triggerRef}
276
+ disabled={effectiveDisabled}
277
+ aria-invalid={resolvedError ? true : undefined}
278
+ aria-describedby={resolvedHelperText ? helperId : undefined}
279
+ className={cn(v.trigger(), slotProps?.trigger?.className)}
280
+ >
281
+ <span className={displayValue ? v.value() : v.placeholder()}>
282
+ {displayValue || placeholder || 'Select a date'}
283
+ </span>
284
+ <span className={v.icon()}>
285
+ <CalendarIcon />
286
+ </span>
287
+ </button>
288
+ </Popover>
289
+
290
+ {resolvedHelperText && (
291
+ <p
292
+ id={helperId}
293
+ className={cn(
294
+ resolvedError ? v.errorText() : v.helperText(),
295
+ resolvedError
296
+ ? slotProps?.errorText?.className
297
+ : slotProps?.helperText?.className,
298
+ )}
299
+ >
300
+ {resolvedHelperText}
301
+ </p>
302
+ )}
303
+ </div>
304
+ );
305
+ }
@@ -0,0 +1,77 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { Engine } from '@dashforge/ui-core';
3
+ import type { AccessRequirement } from '@dashforge/rbac';
4
+ import type { ISODate, WeekDay } from '@dashforge/calendar-core';
5
+
6
+ /** Per-slot `className` overrides for `<DatePicker>`. */
7
+ export interface DatePickerSlotProps {
8
+ root?: { className?: string };
9
+ label?: { className?: string };
10
+ requiredMark?: { className?: string };
11
+ trigger?: { className?: string };
12
+ helperText?: { className?: string };
13
+ errorText?: { className?: string };
14
+ }
15
+
16
+ /**
17
+ * Props for the `<DatePicker>` form field (Tailwind skin).
18
+ *
19
+ * A read-only trigger button paired with a `<Calendar>` popover, integrated
20
+ * with the Dashforge form bridge + RBAC. The stored value is a plain ISO
21
+ * calendar date — `YYYY-MM-DD` — or `null`. (A pure date carries no time or
22
+ * timezone, which removes the whole class of DST round-trip hazards.)
23
+ *
24
+ * The prop surface mirrors the MUI `@dashforge/ui` `DatePicker` so the API
25
+ * is consistent across both ecosystems; only the styling escape hatches
26
+ * (`sx` / `slotProps`) follow the Tailwind conventions.
27
+ */
28
+ export interface DatePickerProps {
29
+ /** Field name — the bridge registration key. Required. */
30
+ name: string;
31
+ /** Validation rules forwarded to the form bridge. */
32
+ rules?: unknown;
33
+ /** Field label. */
34
+ label?: ReactNode;
35
+ /** Helper text below the control (overrides a bridge error message). */
36
+ helperText?: ReactNode;
37
+ /** Explicit error state (overrides the bridge's auto error). */
38
+ error?: boolean;
39
+ /** Marks the field required (adds the label asterisk). */
40
+ required?: boolean;
41
+ /** Disables the field. */
42
+ disabled?: boolean;
43
+ /** Placeholder shown when no date is selected. */
44
+ placeholder?: string;
45
+ /** Label/control layout. */
46
+ layout?: 'stacked' | 'inline';
47
+ /** Reactive visibility predicate evaluated against the form engine. */
48
+ visibleWhen?: (engine: Engine) => boolean;
49
+ /** RBAC access requirement. */
50
+ access?: AccessRequirement;
51
+ /** Controlled value — ISO `YYYY-MM-DD` or `null`. */
52
+ value?: ISODate | null;
53
+ /** Uncontrolled initial value. */
54
+ defaultValue?: ISODate | null;
55
+ /** Fired with the new ISO date (or `null` when cleared). */
56
+ onChange?: (value: ISODate | null) => void;
57
+ /** Earliest selectable date (inclusive). */
58
+ minDate?: ISODate;
59
+ /** Latest selectable date (inclusive). */
60
+ maxDate?: ISODate;
61
+ /** Explicit list of disabled dates. */
62
+ disabledDates?: readonly ISODate[];
63
+ /** Predicate marking arbitrary dates disabled. */
64
+ isDateDisabled?: (date: ISODate) => boolean;
65
+ /** Weekday of the calendar's first column (`0` = Sunday). */
66
+ weekStartDay?: WeekDay;
67
+ /** BCP-47 locale for the calendar and the display format. */
68
+ locale?: string;
69
+ /** Stretches the field to its container width. */
70
+ fullWidth?: boolean;
71
+ /** Root-level Tailwind class override. */
72
+ sx?: string;
73
+ /** Per-slot `className` overrides. */
74
+ slotProps?: DatePickerSlotProps;
75
+ /** Test id applied to the field root. */
76
+ testId?: string;
77
+ }
@@ -0,0 +1,50 @@
1
+ import { tv } from 'tailwind-variants';
2
+
3
+ /**
4
+ * Slots for `<DatePicker>` — a bridge-integrated date field whose trigger
5
+ * is a `<button>` styled as an input, opening a `<Calendar>` popover.
6
+ *
7
+ * Theme identity: bare neutral / brand token-backed classes only — no
8
+ * `dark:` variants on the neutral palette (the preset CSS-var swap
9
+ * auto-inverts). See `_shared/themeIdentity.test.ts`.
10
+ */
11
+ export const datePickerVariants = tv({
12
+ slots: {
13
+ root: 'flex flex-col',
14
+ label: 'block text-sm font-medium text-neutral-900 mb-1',
15
+ requiredMark: 'text-danger-500 ml-0.5',
16
+ trigger:
17
+ 'flex items-center gap-2 h-10 min-w-[12rem] rounded-md border border-neutral-300 bg-neutral-50 px-3 text-base text-neutral-900 cursor-pointer transition-colors hover:border-neutral-400 focus-visible:outline-none focus-visible:border-primary-500 focus-visible:ring-2 focus-visible:ring-primary-500/30 disabled:cursor-not-allowed disabled:opacity-60 disabled:bg-neutral-200',
18
+ value: 'flex-1 truncate text-left',
19
+ placeholder: 'flex-1 truncate text-left text-neutral-400',
20
+ icon: 'shrink-0 text-[1rem] text-neutral-500',
21
+ helperText: 'mt-1 text-sm text-neutral-600',
22
+ errorText: 'mt-1 text-sm text-danger-600',
23
+ },
24
+ variants: {
25
+ layout: {
26
+ stacked: {},
27
+ inline: {
28
+ root: 'flex-row items-start gap-3',
29
+ label: 'mb-0 pt-2.5 whitespace-nowrap shrink-0',
30
+ },
31
+ },
32
+ error: {
33
+ true: {
34
+ trigger:
35
+ 'border-danger-500 hover:border-danger-500 focus-visible:border-danger-500 focus-visible:ring-danger-500/30',
36
+ },
37
+ },
38
+ fullWidth: {
39
+ true: {
40
+ root: 'w-full',
41
+ trigger: 'w-full min-w-0',
42
+ },
43
+ },
44
+ },
45
+ defaultVariants: {
46
+ layout: 'stacked',
47
+ error: false,
48
+ fullWidth: false,
49
+ },
50
+ });
package/src/index.ts CHANGED
@@ -108,6 +108,26 @@ export type {
108
108
  } from './components/DateTimePicker/dateTimePicker.types.js';
109
109
  export { dateTimePickerVariants } from './components/DateTimePicker/dateTimePicker.variants.js';
110
110
 
111
+ // Custom calendar suite — Calendar primitive + bridge-integrated DatePicker,
112
+ // both built on the shared headless `@dashforge/calendar-core` engine.
113
+
114
+ export { Calendar } from './components/Calendar/Calendar.js';
115
+ export type {
116
+ CalendarProps,
117
+ CalendarSlotProps,
118
+ } from './components/Calendar/calendar.types.js';
119
+ export {
120
+ calendarVariants,
121
+ calendarDayVariants,
122
+ } from './components/Calendar/calendar.variants.js';
123
+
124
+ export { DatePicker } from './components/DatePicker/DatePicker.js';
125
+ export type {
126
+ DatePickerProps,
127
+ DatePickerSlotProps,
128
+ } from './components/DatePicker/datePicker.types.js';
129
+ export { datePickerVariants } from './components/DatePicker/datePicker.variants.js';
130
+
111
131
  // F6 layout: router-agnostic navigation primitives.
112
132
 
113
133
  export { Breadcrumbs } from './components/Breadcrumbs/Breadcrumbs.js';
@@ -353,4 +373,4 @@ export type { VariantProps } from 'tailwind-variants';
353
373
  /**
354
374
  * Package version (synced with `package.json` at publish time).
355
375
  */
356
- export const VERSION = '0.7.0-beta';
376
+ export const VERSION = '0.9.1-beta';
package/tsconfig.lib.json CHANGED
@@ -40,16 +40,19 @@
40
40
  "path": "../ui-core/tsconfig.lib.json"
41
41
  },
42
42
  {
43
- "path": "../tw-tokens/tsconfig.lib.json"
43
+ "path": "../rbac/tsconfig.lib.json"
44
44
  },
45
45
  {
46
- "path": "../tw-theme/tsconfig.lib.json"
46
+ "path": "../forms/tsconfig.lib.json"
47
47
  },
48
48
  {
49
- "path": "../rbac/tsconfig.lib.json"
49
+ "path": "../calendar-core/tsconfig.lib.json"
50
50
  },
51
51
  {
52
- "path": "../forms/tsconfig.lib.json"
52
+ "path": "../tw-tokens/tsconfig.lib.json"
53
+ },
54
+ {
55
+ "path": "../tw-theme/tsconfig.lib.json"
53
56
  }
54
57
  ]
55
58
  }