@invoiceonline/ui-react 0.1.0 → 0.2.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 (48) hide show
  1. package/AGENTS.md +4 -3
  2. package/CHANGELOG.md +18 -0
  3. package/dist/components/date-picker/DatePicker.js +1 -1
  4. package/dist/components/field/Field.d.ts +2 -1
  5. package/dist/components/field/Field.js +13 -8
  6. package/dist/components/field-pair/FieldPair.d.ts +29 -13
  7. package/dist/components/field-pair/FieldPair.js +20 -6
  8. package/dist/components/text-field/TextField.d.ts +97 -2
  9. package/dist/components/text-field/TextField.js +76 -5
  10. package/dist/components/text-field/index.d.ts +2 -2
  11. package/dist/components/textarea/Textarea.js +1 -1
  12. package/dist/components/unit-field/UnitField.js +1 -1
  13. package/dist/index.d.ts +3 -5
  14. package/dist/index.js +2 -3
  15. package/dist/internal/useDateFieldTrigger.d.ts +60 -0
  16. package/dist/internal/useDateFieldTrigger.js +68 -16
  17. package/dist/internal/useFormReset.js +10 -3
  18. package/docs/Consuming.mdx +20 -3
  19. package/llms.txt +7 -13
  20. package/package.json +1 -5
  21. package/src/components/date-picker/DatePicker.mdx +18 -14
  22. package/src/components/date-picker/DatePicker.stories.tsx +1 -1
  23. package/src/components/field/Field.tsx +40 -18
  24. package/src/components/field-pair/FieldPair.mdx +13 -9
  25. package/src/components/field-pair/FieldPair.stories.tsx +11 -2
  26. package/src/components/field-pair/FieldPair.tsx +76 -27
  27. package/src/components/filter-facet/FilterFacet.tsx +1 -1
  28. package/src/components/subject-lookup-field/SubjectLookupField.tsx +3 -3
  29. package/src/components/text-field/TextField.mdx +258 -38
  30. package/src/components/text-field/TextField.stories.tsx +311 -3
  31. package/src/components/text-field/TextField.tsx +259 -5
  32. package/src/components/text-field/index.ts +3 -0
  33. package/src/components/textarea/Textarea.mdx +2 -2
  34. package/src/components/textarea/Textarea.tsx +1 -1
  35. package/src/components/unit-field/UnitField.mdx +1 -1
  36. package/src/components/unit-field/UnitField.tsx +1 -1
  37. package/src/index.ts +0 -1
  38. package/src/internal/useDateFieldTrigger.tsx +178 -38
  39. package/src/internal/useFormReset.ts +13 -3
  40. package/src/recipes/Form.stories.tsx +5 -0
  41. package/dist/components/date-field/DateField.d.ts +0 -52
  42. package/dist/components/date-field/DateField.js +0 -45
  43. package/dist/components/date-field/index.d.ts +0 -2
  44. package/dist/components/date-field/index.js +0 -1
  45. package/src/components/date-field/DateField.mdx +0 -340
  46. package/src/components/date-field/DateField.stories.tsx +0 -221
  47. package/src/components/date-field/DateField.tsx +0 -176
  48. package/src/components/date-field/index.ts +0 -1
@@ -2,7 +2,6 @@
2
2
  import * as React from 'react';
3
3
  import { FloatingPortal, FloatingFocusManager } from '@floating-ui/react';
4
4
  import { format, isAfter, isBefore, isValid, parse, startOfDay } from 'date-fns';
5
- import { cs } from 'date-fns/locale/cs';
6
5
  import type { Locale } from 'date-fns';
7
6
  import { DatePicker, type DatePickerNavLabels } from '../components/date-picker/DatePicker';
8
7
  import { popoverSurfaceVariants } from './popoverSurface';
@@ -12,8 +11,8 @@ import { useAnchoredPanel } from './useAnchoredPanel';
12
11
  // useDateFieldTrigger — the date behaviour behind a date field, without the
13
12
  // shell it is drawn in.
14
13
  //
15
- // Two call sites need the SAME behaviour in two different chromes: `DateField`
16
- // (a bordered Text Field) and a `FieldPair` cell in `format="date"` (a
14
+ // Two call sites need the SAME behaviour in two different chromes: `TextField
15
+ // type="date"` (a bordered Text Field) and a `FieldPair` cell in `format="date"` (a
17
16
  // borderless Field Segment — the mobile substitute for Date Picker's dual-month
18
17
  // range mode, spec 04-molecules/field-pair.md § Field Segment). Everything that
19
18
  // is not the chrome lives here: draft/commit parsing, the day-granular min/max
@@ -22,7 +21,10 @@ import { useAnchoredPanel } from './useAnchoredPanel';
22
21
  //
23
22
  // The doctrine for WHY the field is the trigger (rather than a button beside
24
23
  // it), and why the input carries `role="combobox"`, is in the header of
25
- // `DateField.tsx` — read it there, it is not repeated here.
24
+ // `TextField.tsx` — read it there, it is not repeated here.
25
+ //
26
+ // The date format is the MARKET's: `dateFormat` and `locale` are required and
27
+ // have no default (text-field manifest 2.15.0) — nothing here assumes Czech.
26
28
  //
27
29
  // The panel is returned as a ready-made node on purpose: it guarantees both
28
30
  // call sites render the identical portal/focus-manager/DatePicker stack,
@@ -41,38 +43,67 @@ export interface UseDateFieldTriggerOptions {
41
43
  onValueChange?: (value: Date | undefined) => void;
42
44
  /**
43
45
  * Display + parse pattern, date-fns syntax (e.g. `dd. MM. yyyy` for CZ,
44
- * `MM/dd/yyyy` for US). Per-market values are the consumer's to supply.
46
+ * `MM/dd/yyyy` for US). Required, no default: the market's pattern comes from
47
+ * the consumer.
48
+ */
49
+ dateFormat: string;
50
+ /**
51
+ * The market's date-fns Locale — the panel's week start, weekday names and
52
+ * month caption, and the parse. Required, no default.
45
53
  */
46
- dateFormat?: string;
47
- /** Locale driving the panel's week start, weekday names and month caption. */
48
- locale?: Locale;
54
+ locale: Locale;
49
55
  /** Earliest / latest selectable date. */
50
56
  minDate?: Date;
51
57
  maxDate?: Date;
52
58
  disabled?: boolean;
53
59
  readOnly?: boolean;
54
60
  /**
55
- * Accessible name for the calendar panel (`role="dialog"`). Required and never
56
- * defaulted — the design system is locale-agnostic, and a name invisible to
57
- * sighted review is exactly the kind that ships untranslated.
61
+ * Id(s) of the visible text that names the field — its `<label>` — pointed at
62
+ * by the panel's `aria-labelledby`. A portalled `role="dialog"` inherits no
63
+ * name from its trigger, and the visible label is the name it should carry
64
+ * (overlay-behavior.md § Naming; COMPONENT-CODE-CONVENTIONS.md §4 "Any overlay
65
+ * panel"). When set it wins over `panelLabel`.
66
+ */
67
+ panelLabelledBy?: string;
68
+ /**
69
+ * Fallback accessible name for the calendar panel, used only when
70
+ * `panelLabelledBy` is absent — i.e. nothing visible names the field. Optional,
71
+ * because a labelled field never reads it; never defaulted, because the design
72
+ * system is locale-agnostic. With neither a labelled-by id nor a non-empty
73
+ * name the dialog would be anonymous, so the hook warns in development.
74
+ */
75
+ panelLabel?: string;
76
+ /**
77
+ * Called when a typed draft is rejected on commit: `'unparseable'` when it
78
+ * does not parse in `dateFormat`, `'out-of-range'` when it parses but falls
79
+ * outside `minDate` / `maxDate`. The typed text stays in the input and the
80
+ * value becomes `undefined`; this lets the consumer say why (Error on the
81
+ * Field).
58
82
  */
59
- panelLabel: string;
83
+ onInvalidInput?: (raw: string, reason: DateInputInvalidReason) => void;
60
84
  /** Accessible names for the panel's four navigation chevrons. */
61
85
  navLabels: DatePickerNavLabels;
62
- /** External ref to merge into the input (`DateField` forwards one). */
86
+ /** External ref to merge into the input (`TextField` forwards one). */
63
87
  inputRef?: React.Ref<HTMLInputElement>;
64
88
  }
65
89
 
66
- /** Props the hook contributes to the `<input>`; merged with the caller's. */
90
+ /** Why a typed draft was rejected — see `onInvalidInput`. */
91
+ export type DateInputInvalidReason = 'unparseable' | 'out-of-range';
92
+
93
+ /**
94
+ * Props the hook contributes to the `<input>`; merged with the caller's. The
95
+ * combobox ARIA is present only while the field can open its panel — a
96
+ * read-only (or inert) field is a plain textbox, it owns no popup.
97
+ */
67
98
  interface DateTriggerInputProps {
68
99
  value: string;
69
100
  onChange: React.ChangeEventHandler<HTMLInputElement>;
70
101
  onBlur: React.FocusEventHandler<HTMLInputElement>;
71
102
  onKeyDown: React.KeyboardEventHandler<HTMLInputElement>;
72
- role: 'combobox';
73
- 'aria-haspopup': 'dialog';
74
- 'aria-expanded': boolean;
75
- 'aria-controls': string | undefined;
103
+ role?: 'combobox';
104
+ 'aria-haspopup'?: 'dialog';
105
+ 'aria-expanded'?: boolean;
106
+ 'aria-controls'?: string;
76
107
  }
77
108
 
78
109
  export interface UseDateFieldTriggerReturn {
@@ -82,7 +113,13 @@ export interface UseDateFieldTriggerReturn {
82
113
  /** Callback ref for the `<input>` (merges the caller's `inputRef`). */
83
114
  setInputRef: (node: HTMLInputElement | null) => void;
84
115
  /**
85
- * Spread on the element that IS the trigger — `DateField`'s wrapper, or a
116
+ * Drops a typed draft without committing it, so the input shows the value
117
+ * again — for a native `form.reset()`, which must not be undone by the next
118
+ * blur committing the half-typed text.
119
+ */
120
+ clearDraft: () => void;
121
+ /**
122
+ * Spread on the element that IS the trigger — `TextField`'s date wrapper, or a
86
123
  * `FieldPair` cell. Only the anchor ref and the pointer handlers — mouse-down
87
124
  * keeps focus in the input, click opens the panel. The substrate's
88
125
  * `getReferenceProps` is deliberately NOT used, because it would put
@@ -104,17 +141,33 @@ export function useDateFieldTrigger({
104
141
  enabled = true,
105
142
  value,
106
143
  onValueChange,
107
- dateFormat = 'dd. MM. yyyy',
108
- locale = cs,
144
+ dateFormat,
145
+ locale,
109
146
  minDate,
110
147
  maxDate,
111
148
  disabled,
112
149
  readOnly,
150
+ panelLabelledBy,
113
151
  panelLabel,
152
+ onInvalidInput,
114
153
  navLabels,
115
154
  inputRef: externalRef,
116
155
  }: UseDateFieldTriggerOptions): UseDateFieldTriggerReturn {
117
- const [open, setOpen] = React.useState(false);
156
+ const [openState, setOpen] = React.useState(false);
157
+ const panelName = panelLabel?.trim() ? panelLabel : undefined;
158
+ // A nameless dialog is an a11y defect the types cannot rule out (the name may
159
+ // come from a Field label at runtime), so it is checked here, once per change
160
+ // of the name sources.
161
+ React.useEffect(() => {
162
+ if (process.env.NODE_ENV === 'production' || !enabled) return;
163
+ if (!panelLabelledBy && !panelName) {
164
+ // eslint-disable-next-line no-console
165
+ console.warn(
166
+ 'Date field: the calendar dialog has no accessible name — put the field in a labelled `Field`, '
167
+ + 'or pass `aria-label` / `aria-labelledby` or `panelLabel`.',
168
+ );
169
+ }
170
+ }, [enabled, panelLabelledBy, panelName]);
118
171
  // The trigger owns the click and the panel dismissal, so it needs a handle on
119
172
  // its own input to put focus back where a typist expects it.
120
173
  const inputRef = React.useRef<HTMLInputElement>(null);
@@ -138,6 +191,12 @@ export function useDateFieldTrigger({
138
191
  // 06-patterns/field-readonly.md). `enabled` is the same gate for a FieldPair
139
192
  // cell that is not in date mode at all.
140
193
  const canOpen = enabled && !disabled && !readOnly;
194
+ // A field locked while its panel is up (disabled / read-only flipped by the
195
+ // consumer) closes it on the same render — `aria-expanded` must never claim a
196
+ // panel a locked field cannot own. The stored flag is reset too, so unlocking
197
+ // does not resurrect a panel nobody asked for.
198
+ const open = openState && canOpen;
199
+ if (openState && !canOpen) setOpen(false);
141
200
 
142
201
  const openPanel = (byKeyboard: boolean) => {
143
202
  if (!canOpen || open) return;
@@ -149,6 +208,29 @@ export function useDateFieldTrigger({
149
208
  // back unchanged; a committed value is re-formatted from the Date so the
150
209
  // field always settles into the canonical pattern.
151
210
  const [draft, setDraft] = React.useState<string | null>(null);
211
+ // The rules a draft is judged by. A due date's `minDate` is often the issue
212
+ // date, which moves while the field holds a rejected draft.
213
+ const rulesKey = `${minDate?.getTime()}|${maxDate?.getTime()}|${dateFormat}|${locale?.code}`;
214
+ // Set while the draft on screen is one `commitDraft` rejected and the user
215
+ // has not edited since: the rules it was rejected under. Only such a draft
216
+ // outlives its commit. The verdict holds only for those rules — after they
217
+ // change, the next blur or Enter judges the same text again.
218
+ const [rejectedUnder, setRejectedUnder] = React.useState<string | null>(null);
219
+ const rejected = rejectedUnder !== null;
220
+ // A date the consumer sets after a rejection — not the `undefined` the
221
+ // commit itself reported — replaces the rejected text, or the input would
222
+ // keep showing text the value no longer matches. A draft the user is still
223
+ // typing is never touched by an outside value change. Adjusted during
224
+ // render, like `open` above; `Object.is` so an Invalid Date (NaN) settles.
225
+ const valueTime = value?.getTime();
226
+ const [prevValueTime, setPrevValueTime] = React.useState(valueTime);
227
+ if (!Object.is(valueTime, prevValueTime)) {
228
+ setPrevValueTime(valueTime);
229
+ if (valueTime !== undefined && rejected) {
230
+ setDraft(null);
231
+ setRejectedUnder(null);
232
+ }
233
+ }
152
234
  const displayValue = draft ?? (value ? format(value, dateFormat, { locale }) : '');
153
235
 
154
236
  const panel = useAnchoredPanel({
@@ -167,7 +249,7 @@ export function useDateFieldTrigger({
167
249
 
168
250
  /**
169
251
  * Anywhere in the trigger opens the calendar — the shell padding (and, in
170
- * `DateField`, the glyph) is part of the trigger, not decoration you can click
252
+ * `TextField type="date"`, the glyph) is part of the trigger, not decoration you can click
171
253
  * through. A press that missed the input keeps focus there (see
172
254
  * `handleTriggerMouseDown`), so the field stays typeable.
173
255
  *
@@ -182,7 +264,7 @@ export function useDateFieldTrigger({
182
264
 
183
265
  // A press on the glyph, the shell padding or a FieldPair cell's labels is
184
266
  // handled on mouse-down, whose default would move focus from the input to
185
- // <body>: the blur would commit — i.e. discard — a half-typed draft and drop
267
+ // <body>: the blur would commit — i.e. reject — a half-typed draft and drop
186
268
  // the floating label before the click refocuses. Same rule as Combobox's
187
269
  // `handleTriggerMouseDown`; not on pointer-down, where preventing does not
188
270
  // stop the focus move.
@@ -200,6 +282,14 @@ export function useDateFieldTrigger({
200
282
  * the second branch a keyboard user could only reach the grid by tabbing.
201
283
  */
202
284
  const handleTriggerKeyDown = (event: React.KeyboardEvent) => {
285
+ // Enter submits a form implicitly without blurring the input, so the draft
286
+ // commits here first — or the form would submit the old date while the
287
+ // input shows the typed text. The default is kept: the submit goes ahead
288
+ // with the committed (or withdrawn) value.
289
+ if (event.key === 'Enter') {
290
+ commitDraft();
291
+ return;
292
+ }
203
293
  if (event.key !== 'ArrowDown') return;
204
294
  if (!open) {
205
295
  event.preventDefault();
@@ -217,31 +307,58 @@ export function useDateFieldTrigger({
217
307
  };
218
308
 
219
309
  /**
220
- * Commit a typed draft. An unparseable draft is discarded, not coerced — and
221
- * so is one that parses but falls outside `minDate` / `maxDate`: the panel
310
+ * Commit a typed draft. An unparseable draft is never coerced — and neither
311
+ * is one that parses but falls outside `minDate` / `maxDate`: the panel
222
312
  * hard-disables those days, so typing must not be a back door around the same
223
313
  * bound. Comparison is day-granular to match the panel's own matchers, where
224
314
  * `{ before: minDate }` bounds by day and a `minDate` carrying a time
225
315
  * component must still leave its own day selectable.
316
+ *
317
+ * An invalid draft stays in the input (text-field manifest 2.15.0
318
+ * `delivery.react`): the consumer's error message belongs next to the text the
319
+ * user typed, and swapping it back for the old date would read as a false
320
+ * alarm. The old date is withdrawn — `onValueChange(undefined)` — so a form
321
+ * cannot submit a date the user no longer sees.
226
322
  */
227
323
  const commitDraft = () => {
228
- if (draft === null) return;
324
+ // A rejected draft the user has not edited was already reported; another
325
+ // commit of the same text under the same rules reports nothing new.
326
+ if (draft === null || rejectedUnder === rulesKey) return;
229
327
  const trimmed = draft.trim();
230
- setDraft(null);
231
328
  if (trimmed === '') {
329
+ setDraft(null);
232
330
  onValueChange?.(undefined);
233
331
  return;
234
332
  }
333
+ // `onValueChange` first: a form that clears its error on a value change
334
+ // must not clear the error this very rejection raises.
335
+ const reject = (reason: DateInputInvalidReason) => {
336
+ setRejectedUnder(rulesKey);
337
+ if (value !== undefined) onValueChange?.(undefined);
338
+ onInvalidInput?.(draft, reason);
339
+ };
235
340
  const parsed = parse(trimmed, dateFormat, new Date(), { locale });
236
- if (!isValid(parsed)) return;
341
+ if (!isValid(parsed)) {
342
+ reject('unparseable');
343
+ return;
344
+ }
237
345
  const day = startOfDay(parsed);
238
- if (minDate && isBefore(day, startOfDay(minDate))) return;
239
- if (maxDate && isAfter(day, startOfDay(maxDate))) return;
346
+ if (
347
+ (minDate && isBefore(day, startOfDay(minDate))) ||
348
+ (maxDate && isAfter(day, startOfDay(maxDate)))
349
+ ) {
350
+ reject('out-of-range');
351
+ return;
352
+ }
353
+ // A draft rejected under the old rules may pass under the new ones.
354
+ setDraft(null);
355
+ setRejectedUnder(null);
240
356
  onValueChange?.(parsed);
241
357
  };
242
358
 
243
359
  const handleSelect = (next: Date | undefined) => {
244
360
  setDraft(null);
361
+ setRejectedUnder(null);
245
362
  onValueChange?.(next);
246
363
  setOpen(false);
247
364
  // Focus returns to the trigger on dismissal (spec § Dismissal). The trigger
@@ -256,7 +373,9 @@ export function useDateFieldTrigger({
256
373
  // separate values, running whatever the consumer hangs off `onValueChange`
257
374
  // twice — the first time on an intermediate the user never meant to submit.
258
375
  // The draft is kept, not dropped, so paging the calendar and returning to
259
- // the input resumes typing; `handleSelect` clears it when a day wins.
376
+ // the input resumes typing; `handleSelect` clears it when a day wins. Focus
377
+ // lands inside the panel from a press on it or from `ArrowDown`; `Tab` first
378
+ // reaches the portal's focus guard, which sits outside it, so `Tab` commits.
260
379
  const intoPanel = panel.refs.floating.current?.contains(
261
380
  event.relatedTarget as Node | null,
262
381
  );
@@ -281,6 +400,7 @@ export function useDateFieldTrigger({
281
400
  value: displayValue,
282
401
  onChange: (event) => {
283
402
  setDraft(event.target.value);
403
+ setRejectedUnder(null);
284
404
  userChange?.(event);
285
405
  },
286
406
  onBlur: (event) => {
@@ -293,11 +413,19 @@ export function useDateFieldTrigger({
293
413
  },
294
414
  // A text input that owns a popup IS a combobox (APG Date Picker
295
415
  // Combobox). The role goes on the input rather than on a separate button,
296
- // because there is no button any more.
297
- role: 'combobox',
298
- 'aria-haspopup': 'dialog',
299
- 'aria-expanded': open,
300
- 'aria-controls': open ? panelId : undefined,
416
+ // because there is no button any more. A read-only field owns no popup
417
+ // (the panel never opens), so it stays a plain textbox — announcing a
418
+ // collapsed combobox that cannot expand would promise a control that is
419
+ // not there. Disabled keeps the role: a disabled combobox is still the
420
+ // same kind of control, just unavailable.
421
+ ...(enabled && !readOnly
422
+ ? {
423
+ role: 'combobox' as const,
424
+ 'aria-haspopup': 'dialog' as const,
425
+ 'aria-expanded': open,
426
+ 'aria-controls': open ? panelId : undefined,
427
+ }
428
+ : {}),
301
429
  };
302
430
  };
303
431
 
@@ -305,6 +433,10 @@ export function useDateFieldTrigger({
305
433
  open,
306
434
  displayValue,
307
435
  setInputRef,
436
+ clearDraft: () => {
437
+ setDraft(null);
438
+ setRejectedUnder(null);
439
+ },
308
440
  triggerProps: {
309
441
  ref: panel.refs.setReference,
310
442
  onMouseDown: handleTriggerMouseDown,
@@ -327,7 +459,15 @@ export function useDateFieldTrigger({
327
459
  // `z-dropdown` for the same reason as in AnchoredPanel: without it a neighbouring
328
460
  // Field's floating label (`z-4`) paints through the open calendar.
329
461
  className="z-dropdown outline-none"
330
- {...panel.getFloatingProps({ id: panelId, 'aria-label': panelLabel })}
462
+ // Named by the field's visible label when there is one
463
+ // (overlay-behavior.md § Naming); `aria-label` is the fallback for a
464
+ // field nothing visible names.
465
+ {...panel.getFloatingProps({
466
+ id: panelId,
467
+ ...(panelLabelledBy
468
+ ? { 'aria-labelledby': panelLabelledBy }
469
+ : panelName ? { 'aria-label': panelName } : {}),
470
+ })}
331
471
  >
332
472
  {/* The host surface (date-picker 1.0.0 § chrome.host): DatePicker
333
473
  paints nothing, so the Popover surface recipe draws fill, border,
@@ -10,7 +10,10 @@ type FormControl = HTMLInputElement | HTMLTextAreaElement;
10
10
  * `change`/`input`, so state an uncontrolled component mirrors from `onChange`
11
11
  * (emptiness, checked, a character count) would go stale. The `reset` event is
12
12
  * dispatched BEFORE the browser restores the values — and is cancelable — so the
13
- * value is read one task later, which also covers a reset that was prevented.
13
+ * value is read one task later. A reset that was prevented restores nothing, so
14
+ * `onReset` is skipped when every reset in that task was canceled: a callback
15
+ * that writes a default (a date field's `defaultValue`) must not run when the
16
+ * native controls kept theirs.
14
17
  *
15
18
  * `formAttr` is the control's `form` attribute: changing it re-associates the
16
19
  * control with another form, so the listener is moved along.
@@ -29,11 +32,18 @@ export function useFormReset<T extends FormControl>(
29
32
  return undefined;
30
33
  }
31
34
  let timeout: ReturnType<typeof setTimeout> | undefined;
32
- const handleReset = () => {
35
+ // Every reset since the timer was armed: one that went through restored the
36
+ // controls even when a later one in the same task was canceled.
37
+ let pending: Event[] = [];
38
+ const handleReset = (event: Event) => {
33
39
  clearTimeout(timeout);
40
+ pending.push(event);
34
41
  timeout = setTimeout(() => {
35
42
  const element = elementRef.current;
36
- if (element) {
43
+ // Read one task later: a listener after this one may still prevent it.
44
+ const restored = pending.some(e => !e.defaultPrevented);
45
+ pending = [];
46
+ if (element && restored) {
37
47
  onResetRef.current(element);
38
48
  }
39
49
  }, 0);
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import type { Meta, StoryObj } from '@storybook/react-vite';
3
3
  import { expect, fn, userEvent, waitFor, within } from 'storybook/test';
4
+ import { cs } from 'date-fns/locale/cs';
4
5
  import { Button } from '../components/button';
5
6
  import { Field } from '../components/field';
6
7
  import { FieldPair } from '../components/field-pair';
@@ -27,6 +28,8 @@ const CURRENCIES: SelectOption[] = [
27
28
  { value: 'eur', label: 'EUR' },
28
29
  ];
29
30
 
31
+ // The market's date format + locale — required by the date range, never defaulted.
32
+ const DATE_FORMAT = 'dd. MM. yyyy';
30
33
  const PANEL_LABEL = 'Kalendář';
31
34
  const NAV_LABELS = {
32
35
  previousMonth: 'Předchozí měsíc',
@@ -210,6 +213,8 @@ function ContactForm({ onSubmit }: ContactFormProps) {
210
213
  format="date"
211
214
  showUnit={false}
212
215
  label="Platnost"
216
+ dateFormat={DATE_FORMAT}
217
+ dateLocale={cs}
213
218
  panelLabel={PANEL_LABEL}
214
219
  navLabels={NAV_LABELS}
215
220
  from={{
@@ -1,52 +0,0 @@
1
- import { TextFieldSize, TextFieldState } from "../text-field/TextField.js";
2
- import { DatePickerNavLabels } from "../date-picker/DatePicker.js";
3
- import * as React from "react";
4
- import { Locale } from "date-fns";
5
- export interface DateFieldProps {
6
- /** The chosen date. Controlled. */
7
- value?: Date;
8
- onValueChange?: (value: Date | undefined) => void;
9
- /** Field height — Medium (48) standalone, Small (40) dense rows. */
10
- size?: TextFieldSize;
11
- /**
12
- * Visual state. `readonly` shows the value but never opens the calendar.
13
- * `success` is here because `Field` injects it when given a `success` message.
14
- */
15
- state?: Extract<TextFieldState, 'default' | 'focused' | 'error' | 'success' | 'disabled' | 'readonly'>;
16
- /**
17
- * Display + parse pattern, date-fns syntax (e.g. `dd. MM. yyyy` for CZ,
18
- * `MM/dd/yyyy` for US). Per-market values come from the consumer's locale config.
19
- */
20
- dateFormat?: string;
21
- /** Locale driving the panel's week start, weekday names and month caption. */
22
- locale?: Locale;
23
- /** Earliest / latest selectable date. */
24
- minDate?: Date;
25
- maxDate?: Date;
26
- placeholder?: string;
27
- id?: string;
28
- name?: string;
29
- disabled?: boolean;
30
- readOnly?: boolean;
31
- /**
32
- * Accessible name for the calendar panel (`role="dialog"`). Required and never
33
- * defaulted — the design system is locale-agnostic, and a name invisible to
34
- * sighted review is exactly the kind that ships untranslated.
35
- */
36
- panelLabel: string;
37
- /** Accessible names for the panel's four navigation chevrons. */
38
- navLabels: DatePickerNavLabels;
39
- className?: string;
40
- onFocus?: React.FocusEventHandler<HTMLInputElement>;
41
- onBlur?: React.FocusEventHandler<HTMLInputElement>;
42
- /**
43
- * Injected by `Field` when it owns the label / helper / error line. Declared
44
- * explicitly because the props interface has no index signature — without
45
- * these two the helper-text association and the error wiring would be
46
- * silently dropped by the clone.
47
- */
48
- 'aria-invalid'?: boolean;
49
- 'aria-describedby'?: string;
50
- }
51
- declare const DateField: React.ForwardRefExoticComponent<DateFieldProps & React.RefAttributes<HTMLInputElement>>;
52
- export { DateField };
@@ -1,45 +0,0 @@
1
- "use client";
2
- import { TextField } from "../text-field/TextField.js";
3
- import { useDateFieldTrigger } from "../../internal/useDateFieldTrigger.js";
4
- import { cn } from "../../internal/utils.js";
5
- import * as React from "react";
6
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
- // src/components/date-field/DateField.tsx
8
- const DateField = React.forwardRef(({ value, onValueChange, size = "md", state, dateFormat, locale, minDate, maxDate, placeholder, id, name, disabled, readOnly, panelLabel, navLabels, className, onFocus, onBlur, "aria-invalid": ariaInvalid, "aria-describedby": ariaDescribedBy }, ref) => {
9
- const isDisabled = disabled ?? state === "disabled";
10
- const isReadOnly = readOnly ?? state === "readonly";
11
- const trigger = useDateFieldTrigger({
12
- value,
13
- onValueChange,
14
- dateFormat,
15
- locale,
16
- minDate,
17
- maxDate,
18
- disabled: isDisabled,
19
- readOnly: isReadOnly,
20
- panelLabel,
21
- navLabels,
22
- inputRef: ref
23
- });
24
- return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
25
- ...trigger.triggerProps,
26
- className: cn("w-full", className),
27
- children: /* @__PURE__ */ jsx(TextField, {
28
- ref: trigger.setInputRef,
29
- id,
30
- name,
31
- size,
32
- state,
33
- placeholder,
34
- disabled: isDisabled,
35
- readOnly: isReadOnly,
36
- "aria-invalid": ariaInvalid,
37
- "aria-describedby": ariaDescribedBy,
38
- onFocus,
39
- leadingIcon: "calendar-dots",
40
- ...trigger.getInputProps({ onBlur })
41
- })
42
- }), trigger.panel] });
43
- });
44
- DateField.displayName = "DateField";
45
- export { DateField };
@@ -1,2 +0,0 @@
1
- import { DateField, DateFieldProps } from "./DateField.js";
2
- export { DateField, type DateFieldProps };
@@ -1 +0,0 @@
1
- export { DateField } from "./DateField.js";