@carbon-labs/wc-date-picker 0.3.0 → 0.5.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 (101) hide show
  1. package/es/__stories__/{date-picker.stories.d.ts → date-picker-web-components.stories.d.ts} +5 -5
  2. package/{lib/state-machine → es}/adapters/web-component-adapter.d.ts +3 -3
  3. package/es/{state-machine/adapters → adapters}/web-component-adapter.js +1 -4
  4. package/es/adapters/web-component-adapter.js.map +1 -0
  5. package/es/components/{date-picker/calendar-renderer.js → calendar-renderer.js} +9 -9
  6. package/es/components/calendar-renderer.js.map +1 -0
  7. package/es/components/{date-picker/date-picker-input-skeleton.js → date-picker-input-skeleton.js} +2 -2
  8. package/es/components/date-picker-input-skeleton.js.map +1 -0
  9. package/es/components/{date-picker/date-picker-input.js → date-picker-input.js} +5 -5
  10. package/es/components/date-picker-input.js.map +1 -0
  11. package/es/components/{date-picker/date-picker.d.ts → date-picker.d.ts} +6 -3
  12. package/es/components/{date-picker/date-picker.js → date-picker.js} +69 -137
  13. package/es/components/date-picker.js.map +1 -0
  14. package/es/components/{date-picker/date-picker.scss.js → date-picker.scss.js} +1 -1
  15. package/{lib/components/date-picker → es/components}/defs.d.ts +1 -1
  16. package/es/components/{date-picker/defs.js → defs.js} +1 -1
  17. package/es/components/defs.js.map +1 -0
  18. package/es/index.d.ts +5 -4
  19. package/es/index.js +5 -4
  20. package/es/index.js.map +1 -1
  21. package/lib/__stories__/{date-picker.stories.d.ts → date-picker-web-components.stories.d.ts} +5 -5
  22. package/{es/state-machine → lib}/adapters/web-component-adapter.d.ts +3 -3
  23. package/lib/{state-machine/adapters → adapters}/web-component-adapter.js +24 -27
  24. package/lib/adapters/web-component-adapter.js.map +1 -0
  25. package/lib/components/{date-picker/calendar-renderer.js → calendar-renderer.js} +11 -11
  26. package/lib/components/calendar-renderer.js.map +1 -0
  27. package/lib/components/{date-picker/date-picker-input-skeleton.js → date-picker-input-skeleton.js} +2 -2
  28. package/lib/components/date-picker-input-skeleton.js.map +1 -0
  29. package/lib/components/{date-picker/date-picker-input.js → date-picker-input.js} +5 -5
  30. package/lib/components/date-picker-input.js.map +1 -0
  31. package/lib/components/{date-picker/date-picker.d.ts → date-picker.d.ts} +6 -3
  32. package/lib/components/{date-picker/date-picker.js → date-picker.js} +105 -173
  33. package/lib/components/date-picker.js.map +1 -0
  34. package/lib/components/{date-picker/date-picker.scss.js → date-picker.scss.js} +1 -1
  35. package/{es/components/date-picker → lib/components}/defs.d.ts +1 -1
  36. package/lib/components/{date-picker/defs.js → defs.js} +1 -1
  37. package/lib/components/defs.js.map +1 -0
  38. package/lib/index.d.ts +5 -4
  39. package/lib/index.js +13 -4
  40. package/lib/index.js.map +1 -1
  41. package/package.json +5 -4
  42. package/es/components/date-picker/calendar-renderer.js.map +0 -1
  43. package/es/components/date-picker/date-picker-input-skeleton.js.map +0 -1
  44. package/es/components/date-picker/date-picker-input.js.map +0 -1
  45. package/es/components/date-picker/date-picker.js.map +0 -1
  46. package/es/components/date-picker/defs.js.map +0 -1
  47. package/es/state-machine/actions.d.ts +0 -35
  48. package/es/state-machine/actions.js +0 -860
  49. package/es/state-machine/actions.js.map +0 -1
  50. package/es/state-machine/adapters/web-component-adapter.js.map +0 -1
  51. package/es/state-machine/effects.d.ts +0 -35
  52. package/es/state-machine/effects.js +0 -92
  53. package/es/state-machine/effects.js.map +0 -1
  54. package/es/state-machine/guards.d.ts +0 -41
  55. package/es/state-machine/guards.js +0 -143
  56. package/es/state-machine/guards.js.map +0 -1
  57. package/es/state-machine/index.d.ts +0 -12
  58. package/es/state-machine/machine.d.ts +0 -92
  59. package/es/state-machine/machine.js +0 -272
  60. package/es/state-machine/machine.js.map +0 -1
  61. package/es/state-machine/states.d.ts +0 -89
  62. package/es/state-machine/states.js +0 -105
  63. package/es/state-machine/states.js.map +0 -1
  64. package/es/state-machine/temporal-utils.d.ts +0 -203
  65. package/es/state-machine/temporal-utils.js +0 -128
  66. package/es/state-machine/temporal-utils.js.map +0 -1
  67. package/es/state-machine/types.d.ts +0 -163
  68. package/lib/components/date-picker/calendar-renderer.js.map +0 -1
  69. package/lib/components/date-picker/date-picker-input-skeleton.js.map +0 -1
  70. package/lib/components/date-picker/date-picker-input.js.map +0 -1
  71. package/lib/components/date-picker/date-picker.js.map +0 -1
  72. package/lib/components/date-picker/defs.js.map +0 -1
  73. package/lib/state-machine/actions.d.ts +0 -35
  74. package/lib/state-machine/actions.js +0 -864
  75. package/lib/state-machine/actions.js.map +0 -1
  76. package/lib/state-machine/adapters/web-component-adapter.js.map +0 -1
  77. package/lib/state-machine/effects.d.ts +0 -35
  78. package/lib/state-machine/effects.js +0 -96
  79. package/lib/state-machine/effects.js.map +0 -1
  80. package/lib/state-machine/guards.d.ts +0 -41
  81. package/lib/state-machine/guards.js +0 -147
  82. package/lib/state-machine/guards.js.map +0 -1
  83. package/lib/state-machine/index.d.ts +0 -12
  84. package/lib/state-machine/machine.d.ts +0 -92
  85. package/lib/state-machine/machine.js +0 -274
  86. package/lib/state-machine/machine.js.map +0 -1
  87. package/lib/state-machine/states.d.ts +0 -89
  88. package/lib/state-machine/states.js +0 -105
  89. package/lib/state-machine/states.js.map +0 -1
  90. package/lib/state-machine/temporal-utils.d.ts +0 -203
  91. package/lib/state-machine/temporal-utils.js +0 -136
  92. package/lib/state-machine/temporal-utils.js.map +0 -1
  93. package/lib/state-machine/types.d.ts +0 -163
  94. /package/es/components/{date-picker/calendar-renderer.d.ts → calendar-renderer.d.ts} +0 -0
  95. /package/es/components/{date-picker/date-picker-input-skeleton.d.ts → date-picker-input-skeleton.d.ts} +0 -0
  96. /package/es/components/{date-picker/date-picker-input.d.ts → date-picker-input.d.ts} +0 -0
  97. /package/es/components/{date-picker/date-picker.scss.js.map → date-picker.scss.js.map} +0 -0
  98. /package/lib/components/{date-picker/calendar-renderer.d.ts → calendar-renderer.d.ts} +0 -0
  99. /package/lib/components/{date-picker/date-picker-input-skeleton.d.ts → date-picker-input-skeleton.d.ts} +0 -0
  100. /package/lib/components/{date-picker/date-picker-input.d.ts → date-picker-input.d.ts} +0 -0
  101. /package/lib/components/{date-picker/date-picker.scss.js.map → date-picker.scss.js.map} +0 -0
@@ -1,860 +0,0 @@
1
- import { DatePickerState } from './states.js';
2
- import { comparePlainDates, plainDateToISOString } from './temporal-utils.js';
3
-
4
- /**
5
- * Copyright IBM Corp. 2026
6
- *
7
- * This source code is licensed under the Apache-2.0 license found in the
8
- * LICENSE file in the root directory of this source tree.
9
- */
10
- /**
11
- * Actions for state transitions
12
- */
13
- const actions = {
14
- [DatePickerState.IDLE]: {
15
- /**
16
- * Action for CALENDAR_ICON_CLICK event
17
- *
18
- * @param {DatePickerContext} context - Current context
19
- * @returns {Partial<DatePickerContext>} Updated context
20
- */
21
- CALENDAR_ICON_CLICK: (context) => {
22
- // Initialize viewDate to today if not set
23
- const viewDate = context.viewDate || context.startDate || Temporal.Now.plainDateISO();
24
- // Initialize focusedDate to selected date or today if no date is selected
25
- const focusedDate = context.startDate || Temporal.Now.plainDateISO();
26
- return {
27
- isOpen: true,
28
- viewDate,
29
- focusedDate,
30
- };
31
- },
32
- /** Action for INPUT_BLUR event */
33
- INPUT_BLUR: () => ({
34
- isFocused: false,
35
- }),
36
- /** Action for OUTSIDE_CLICK event */
37
- OUTSIDE_CLICK: () => ({
38
- isOpen: false,
39
- isFocused: false,
40
- }),
41
- /** Action for CALENDAR_CLOSE event */
42
- CALENDAR_CLOSE: () => ({
43
- isOpen: false,
44
- }),
45
- },
46
- [DatePickerState.FOCUSED]: {
47
- /**
48
- * Action for INPUT_FOCUS event
49
- *
50
- * @param {DatePickerContext} _context - The current context (unused)
51
- * @param {DatePickerEvent} event - The event
52
- * @returns {Partial<DatePickerContext>} Updated context
53
- */
54
- INPUT_FOCUS: (_context, event) => {
55
- const payload = event.payload;
56
- return {
57
- isFocused: true,
58
- lastFocusedInput: (payload === null || payload === void 0 ? void 0 : payload.inputType) || 'from',
59
- };
60
- },
61
- /**
62
- * Action for CALENDAR_OPEN event from FOCUSED state
63
- *
64
- * @param {DatePickerContext} context - Current context
65
- * @returns {Partial<DatePickerContext>} Updated context
66
- */
67
- CALENDAR_OPEN: (context) => {
68
- // Initialize viewDate to today if not set
69
- const viewDate = context.viewDate || context.startDate || Temporal.Now.plainDateISO();
70
- // Initialize focusedDate to selected date or today if no date is selected
71
- const focusedDate = context.startDate || Temporal.Now.plainDateISO();
72
- return {
73
- isOpen: true,
74
- viewDate,
75
- focusedDate,
76
- };
77
- },
78
- },
79
- [DatePickerState.CALENDAR_OPEN]: {
80
- /**
81
- * Action for CALENDAR_OPEN event
82
- *
83
- * @param {DatePickerContext} context - Current context
84
- * @returns {Partial<DatePickerContext>} Updated context
85
- */
86
- CALENDAR_OPEN: (context) => {
87
- // Initialize viewDate to today if not set
88
- const viewDate = context.viewDate || context.startDate || Temporal.Now.plainDateISO();
89
- // Initialize focusedDate for keyboard navigation
90
- const focusedDate = context.startDate || viewDate;
91
- return {
92
- isOpen: true,
93
- viewDate,
94
- focusedDate,
95
- };
96
- },
97
- /**
98
- * Action for CALENDAR_ICON_CLICK event
99
- *
100
- * @param {DatePickerContext} context - Current context
101
- * @returns {Partial<DatePickerContext>} Updated context
102
- */
103
- CALENDAR_ICON_CLICK: (context) => {
104
- // Initialize viewDate to today if not set
105
- const viewDate = context.viewDate || context.startDate || Temporal.Now.plainDateISO();
106
- // Initialize focusedDate for keyboard navigation
107
- const focusedDate = context.startDate || viewDate;
108
- return {
109
- isOpen: true,
110
- viewDate,
111
- focusedDate,
112
- };
113
- },
114
- /**
115
- * Action for RANGE_START_SELECT event
116
- *
117
- * @param {DatePickerContext} _context - Current context
118
- * @param {DatePickerEvent} event - The event
119
- * @returns {Partial<DatePickerContext>} Updated context
120
- */
121
- RANGE_START_SELECT: (_context, event) => {
122
- const payload = event.payload;
123
- const startDate = payload === null || payload === void 0 ? void 0 : payload.date;
124
- if (!startDate) {
125
- return {};
126
- }
127
- return {
128
- startDate,
129
- endDate: null, // Reset end date when selecting a new start
130
- value: plainDateToISOString(startDate),
131
- viewDate: startDate, // Set view date to show the selected month
132
- focusedDate: startDate, // Set focused date to the selected start date
133
- isOpen: true, // Keep calendar open for selecting end date
134
- };
135
- },
136
- /**
137
- * Action for DATE_SELECT event (single mode)
138
- *
139
- * @param {DatePickerContext} _context - Current context
140
- * @param {DatePickerEvent} event - The event
141
- * @returns {Partial<DatePickerContext>} Updated context
142
- */
143
- DATE_SELECT: (_context, event) => {
144
- const payload = event.payload;
145
- const startDate = payload === null || payload === void 0 ? void 0 : payload.date;
146
- if (!startDate) {
147
- return {};
148
- }
149
- return {
150
- startDate,
151
- value: plainDateToISOString(startDate),
152
- isOpen: false, // Close calendar after selecting date in single mode
153
- };
154
- },
155
- /**
156
- * Action for PREV_MONTH event
157
- *
158
- * @param {DatePickerContext} context - Current context
159
- * @returns {Partial<DatePickerContext>} Updated context
160
- */
161
- PREV_MONTH: (context) => {
162
- if (!context.viewDate) {
163
- return {};
164
- }
165
- return {
166
- viewDate: context.viewDate.add({ months: -1 }),
167
- };
168
- },
169
- /**
170
- * Action for NEXT_MONTH event
171
- *
172
- * @param {DatePickerContext} context - Current context
173
- * @returns {Partial<DatePickerContext>} Updated context
174
- */
175
- NEXT_MONTH: (context) => {
176
- if (!context.viewDate) {
177
- return {};
178
- }
179
- return {
180
- viewDate: context.viewDate.add({ months: 1 }),
181
- };
182
- },
183
- /**
184
- * Action for PREV_YEAR event
185
- *
186
- * @param {DatePickerContext} context - Current context
187
- * @returns {Partial<DatePickerContext>} Updated context
188
- */
189
- PREV_YEAR: (context) => {
190
- if (!context.viewDate) {
191
- return {};
192
- }
193
- return {
194
- viewDate: context.viewDate.add({ years: -1 }),
195
- };
196
- },
197
- /**
198
- * Action for NEXT_YEAR event
199
- *
200
- * @param {DatePickerContext} context - Current context
201
- * @returns {Partial<DatePickerContext>} Updated context
202
- */
203
- NEXT_YEAR: (context) => {
204
- if (!context.viewDate) {
205
- return {};
206
- }
207
- return {
208
- viewDate: context.viewDate.add({ years: 1 }),
209
- };
210
- },
211
- /** Action for GO_TO_TODAY event */
212
- GO_TO_TODAY: () => ({
213
- viewDate: Temporal.Now.plainDateISO(),
214
- }),
215
- /**
216
- * Action for ESCAPE_KEY event - close calendar
217
- *
218
- * @returns {Partial<DatePickerContext>} Updated context
219
- */
220
- ESCAPE_KEY: () => ({
221
- isOpen: false,
222
- }),
223
- /**
224
- * Action for TAB_KEY event - close calendar
225
- *
226
- * @returns {Partial<DatePickerContext>} Updated context
227
- */
228
- TAB_KEY: () => ({
229
- isOpen: false,
230
- }),
231
- /**
232
- * Action for ENTER_KEY event - select focused date
233
- *
234
- * @param {DatePickerContext} context - Current context
235
- * @returns {Partial<DatePickerContext>} Updated context
236
- */
237
- ENTER_KEY: (context) => {
238
- if (!context.focusedDate) {
239
- return {};
240
- }
241
- // Select the focused date (same logic as DATE_SELECT)
242
- return {
243
- startDate: context.focusedDate,
244
- value: plainDateToISOString(context.focusedDate),
245
- isOpen: context.closeOnSelect ? false : context.isOpen,
246
- };
247
- },
248
- /**
249
- * Action for ARROW_UP event - move focus up one week
250
- *
251
- * @param {DatePickerContext} context - Current context
252
- * @returns {Partial<DatePickerContext>} Updated context
253
- */
254
- ARROW_UP: (context) => {
255
- const focusedDate = context.focusedDate ||
256
- context.startDate ||
257
- context.viewDate ||
258
- Temporal.Now.plainDateISO();
259
- const newFocusedDate = focusedDate.add({ days: -7 });
260
- // If we moved to a different month, update viewDate
261
- const viewDate = newFocusedDate.month !== focusedDate.month
262
- ? newFocusedDate
263
- : context.viewDate;
264
- return {
265
- focusedDate: newFocusedDate,
266
- viewDate,
267
- };
268
- },
269
- /**
270
- * Action for ARROW_DOWN event - move focus down one week
271
- *
272
- * @param {DatePickerContext} context - Current context
273
- * @returns {Partial<DatePickerContext>} Updated context
274
- */
275
- ARROW_DOWN: (context) => {
276
- const focusedDate = context.focusedDate ||
277
- context.startDate ||
278
- context.viewDate ||
279
- Temporal.Now.plainDateISO();
280
- const newFocusedDate = focusedDate.add({ days: 7 });
281
- // If we moved to a different month, update viewDate
282
- const viewDate = newFocusedDate.month !== focusedDate.month
283
- ? newFocusedDate
284
- : context.viewDate;
285
- return {
286
- focusedDate: newFocusedDate,
287
- viewDate,
288
- };
289
- },
290
- /**
291
- * Action for ARROW_LEFT event - move focus left one day
292
- *
293
- * @param {DatePickerContext} context - Current context
294
- * @returns {Partial<DatePickerContext>} Updated context
295
- */
296
- ARROW_LEFT: (context) => {
297
- const focusedDate = context.focusedDate ||
298
- context.startDate ||
299
- context.viewDate ||
300
- Temporal.Now.plainDateISO();
301
- const newFocusedDate = focusedDate.add({ days: -1 });
302
- // If we moved to a different month, update viewDate
303
- const viewDate = newFocusedDate.month !== focusedDate.month
304
- ? newFocusedDate
305
- : context.viewDate;
306
- return {
307
- focusedDate: newFocusedDate,
308
- viewDate,
309
- };
310
- },
311
- /**
312
- * Action for ARROW_RIGHT event - move focus right one day
313
- *
314
- * @param {DatePickerContext} context - Current context
315
- * @returns {Partial<DatePickerContext>} Updated context
316
- */
317
- ARROW_RIGHT: (context) => {
318
- const focusedDate = context.focusedDate ||
319
- context.startDate ||
320
- context.viewDate ||
321
- Temporal.Now.plainDateISO();
322
- const newFocusedDate = focusedDate.add({ days: 1 });
323
- // If we moved to a different month, update viewDate
324
- const viewDate = newFocusedDate.month !== focusedDate.month
325
- ? newFocusedDate
326
- : context.viewDate;
327
- return {
328
- focusedDate: newFocusedDate,
329
- viewDate,
330
- };
331
- },
332
- /**
333
- * Action for PAGE_UP event - move to previous month
334
- *
335
- * @param {DatePickerContext} context - Current context
336
- * @returns {Partial<DatePickerContext>} Updated context
337
- */
338
- PAGE_UP: (context) => {
339
- if (!context.viewDate) {
340
- return {};
341
- }
342
- const newViewDate = context.viewDate.add({ months: -1 });
343
- const focusedDate = context.focusedDate
344
- ? context.focusedDate.add({ months: -1 })
345
- : null;
346
- return {
347
- viewDate: newViewDate,
348
- focusedDate,
349
- };
350
- },
351
- /**
352
- * Action for PAGE_DOWN event - move to next month
353
- *
354
- * @param {DatePickerContext} context - Current context
355
- * @returns {Partial<DatePickerContext>} Updated context
356
- */
357
- PAGE_DOWN: (context) => {
358
- if (!context.viewDate) {
359
- return {};
360
- }
361
- const newViewDate = context.viewDate.add({ months: 1 });
362
- const focusedDate = context.focusedDate
363
- ? context.focusedDate.add({ months: 1 })
364
- : null;
365
- return {
366
- viewDate: newViewDate,
367
- focusedDate,
368
- };
369
- },
370
- /**
371
- * Action for HOME_KEY event - move to start of week
372
- *
373
- * @param {DatePickerContext} context - Current context
374
- * @returns {Partial<DatePickerContext>} Updated context
375
- */
376
- HOME_KEY: (context) => {
377
- const focusedDate = context.focusedDate ||
378
- context.startDate ||
379
- context.viewDate ||
380
- Temporal.Now.plainDateISO();
381
- // Convert to JS Date to get day of week (0 = Sunday, 6 = Saturday)
382
- const jsDate = new Date(focusedDate.year, focusedDate.month - 1, focusedDate.day);
383
- const dayOfWeek = jsDate.getDay();
384
- // Move to Sunday (start of week in US calendar)
385
- const daysToSubtract = dayOfWeek;
386
- const newFocusedDate = focusedDate.add({ days: -daysToSubtract });
387
- // If we moved to a different month, update viewDate
388
- const viewDate = newFocusedDate.month !== focusedDate.month
389
- ? newFocusedDate
390
- : context.viewDate;
391
- return {
392
- focusedDate: newFocusedDate,
393
- viewDate,
394
- };
395
- },
396
- /**
397
- * Action for END_KEY event - move to end of week
398
- *
399
- * @param {DatePickerContext} context - Current context
400
- * @returns {Partial<DatePickerContext>} Updated context
401
- */
402
- END_KEY: (context) => {
403
- const focusedDate = context.focusedDate ||
404
- context.startDate ||
405
- context.viewDate ||
406
- Temporal.Now.plainDateISO();
407
- // Convert to JS Date to get day of week (0 = Sunday, 6 = Saturday)
408
- const jsDate = new Date(focusedDate.year, focusedDate.month - 1, focusedDate.day);
409
- const dayOfWeek = jsDate.getDay();
410
- // Move to Saturday (end of week in US calendar)
411
- const daysToAdd = 6 - dayOfWeek;
412
- const newFocusedDate = focusedDate.add({ days: daysToAdd });
413
- // If we moved to a different month, update viewDate
414
- const viewDate = newFocusedDate.month !== focusedDate.month
415
- ? newFocusedDate
416
- : context.viewDate;
417
- return {
418
- focusedDate: newFocusedDate,
419
- viewDate,
420
- };
421
- },
422
- },
423
- [DatePickerState.SELECTING_START]: {
424
- /**
425
- * Action for CALENDAR_OPEN event
426
- *
427
- * @param {DatePickerContext} context - Current context
428
- * @returns {Partial<DatePickerContext>} Updated context
429
- */
430
- CALENDAR_OPEN: (context) => {
431
- if (context.mode === 'range') {
432
- return {
433
- isOpen: true,
434
- startDate: null,
435
- endDate: null,
436
- };
437
- }
438
- return { isOpen: true };
439
- },
440
- },
441
- [DatePickerState.SELECTING_END]: {
442
- /**
443
- * Action for RANGE_START_SELECT event
444
- *
445
- * @param {DatePickerContext} _context - Current context
446
- * @param {DatePickerEvent} event - The event
447
- * @returns {Partial<DatePickerContext>} Updated context
448
- */
449
- RANGE_START_SELECT: (_context, event) => {
450
- const payload = event.payload;
451
- const startDate = payload === null || payload === void 0 ? void 0 : payload.date;
452
- if (!startDate) {
453
- return {};
454
- }
455
- return {
456
- startDate,
457
- endDate: null, // Reset end date when selecting a new start
458
- value: plainDateToISOString(startDate),
459
- };
460
- },
461
- /**
462
- * Action for RANGE_END_SELECT event
463
- *
464
- * @param {DatePickerContext} context - Current context
465
- * @param {DatePickerEvent} event - The event
466
- * @returns {Partial<DatePickerContext>} Updated context
467
- */
468
- RANGE_END_SELECT: (context, event) => {
469
- const payload = event.payload;
470
- const endDate = payload === null || payload === void 0 ? void 0 : payload.date;
471
- const { startDate } = context;
472
- if (!endDate || !startDate) {
473
- return {};
474
- }
475
- // Ensure end date is after start date, swap if needed
476
- let finalStartDate = startDate;
477
- let finalEndDate = endDate;
478
- if (comparePlainDates(endDate, startDate) < 0) {
479
- finalStartDate = endDate;
480
- finalEndDate = startDate;
481
- }
482
- return {
483
- startDate: finalStartDate,
484
- endDate: finalEndDate,
485
- value: `${plainDateToISOString(finalStartDate)}/${plainDateToISOString(finalEndDate)}`,
486
- isOpen: false, // Always close after selecting end date in range mode
487
- lastFocusedInput: 'to', // Set to 'to' so the end date input gets updated
488
- };
489
- },
490
- /**
491
- * Action for ESCAPE_KEY event - close calendar
492
- *
493
- * @returns {Partial<DatePickerContext>} Updated context
494
- */
495
- ESCAPE_KEY: () => ({
496
- isOpen: false,
497
- }),
498
- /**
499
- * Action for TAB_KEY event - close calendar
500
- *
501
- * @returns {Partial<DatePickerContext>} Updated context
502
- */
503
- TAB_KEY: () => ({
504
- isOpen: false,
505
- }),
506
- /**
507
- * Action for ARROW_UP event - move focus up one week
508
- *
509
- * @param {DatePickerContext} context - Current context
510
- * @returns {Partial<DatePickerContext>} Updated context
511
- */
512
- ARROW_UP: (context) => {
513
- const focusedDate = context.focusedDate ||
514
- context.startDate ||
515
- context.viewDate ||
516
- Temporal.Now.plainDateISO();
517
- const newFocusedDate = focusedDate.add({ days: -7 });
518
- // If we moved to a different month, update viewDate
519
- const viewDate = newFocusedDate.month !== focusedDate.month
520
- ? newFocusedDate
521
- : context.viewDate;
522
- return {
523
- focusedDate: newFocusedDate,
524
- viewDate,
525
- };
526
- },
527
- /**
528
- * Action for ARROW_DOWN event - move focus down one week
529
- *
530
- * @param {DatePickerContext} context - Current context
531
- * @returns {Partial<DatePickerContext>} Updated context
532
- */
533
- ARROW_DOWN: (context) => {
534
- const focusedDate = context.focusedDate ||
535
- context.startDate ||
536
- context.viewDate ||
537
- Temporal.Now.plainDateISO();
538
- const newFocusedDate = focusedDate.add({ days: 7 });
539
- // If we moved to a different month, update viewDate
540
- const viewDate = newFocusedDate.month !== focusedDate.month
541
- ? newFocusedDate
542
- : context.viewDate;
543
- return {
544
- focusedDate: newFocusedDate,
545
- viewDate,
546
- };
547
- },
548
- /**
549
- * Action for ARROW_LEFT event - move focus left one day
550
- *
551
- * @param {DatePickerContext} context - Current context
552
- * @returns {Partial<DatePickerContext>} Updated context
553
- */
554
- ARROW_LEFT: (context) => {
555
- const focusedDate = context.focusedDate ||
556
- context.startDate ||
557
- context.viewDate ||
558
- Temporal.Now.plainDateISO();
559
- const newFocusedDate = focusedDate.add({ days: -1 });
560
- // If we moved to a different month, update viewDate
561
- const viewDate = newFocusedDate.month !== focusedDate.month
562
- ? newFocusedDate
563
- : context.viewDate;
564
- return {
565
- focusedDate: newFocusedDate,
566
- viewDate,
567
- };
568
- },
569
- /**
570
- * Action for ARROW_RIGHT event - move focus right one day
571
- *
572
- * @param {DatePickerContext} context - Current context
573
- * @returns {Partial<DatePickerContext>} Updated context
574
- */
575
- ARROW_RIGHT: (context) => {
576
- const focusedDate = context.focusedDate ||
577
- context.startDate ||
578
- context.viewDate ||
579
- Temporal.Now.plainDateISO();
580
- const newFocusedDate = focusedDate.add({ days: 1 });
581
- // If we moved to a different month, update viewDate
582
- const viewDate = newFocusedDate.month !== focusedDate.month
583
- ? newFocusedDate
584
- : context.viewDate;
585
- return {
586
- focusedDate: newFocusedDate,
587
- viewDate,
588
- };
589
- },
590
- /**
591
- * Action for PAGE_UP event - move to previous month
592
- *
593
- * @param {DatePickerContext} context - Current context
594
- * @returns {Partial<DatePickerContext>} Updated context
595
- */
596
- PAGE_UP: (context) => {
597
- const focusedDate = context.focusedDate ||
598
- context.startDate ||
599
- context.viewDate ||
600
- Temporal.Now.plainDateISO();
601
- const newFocusedDate = focusedDate.add({ months: -1 });
602
- return {
603
- focusedDate: newFocusedDate,
604
- viewDate: newFocusedDate,
605
- };
606
- },
607
- /**
608
- * Action for PAGE_DOWN event - move to next month
609
- *
610
- * @param {DatePickerContext} context - Current context
611
- * @returns {Partial<DatePickerContext>} Updated context
612
- */
613
- PAGE_DOWN: (context) => {
614
- const focusedDate = context.focusedDate ||
615
- context.startDate ||
616
- context.viewDate ||
617
- Temporal.Now.plainDateISO();
618
- const newFocusedDate = focusedDate.add({ months: 1 });
619
- return {
620
- focusedDate: newFocusedDate,
621
- viewDate: newFocusedDate,
622
- };
623
- },
624
- /**
625
- * Action for HOME_KEY event - move to start of week
626
- *
627
- * @param {DatePickerContext} context - Current context
628
- * @returns {Partial<DatePickerContext>} Updated context
629
- */
630
- HOME_KEY: (context) => {
631
- const focusedDate = context.focusedDate ||
632
- context.startDate ||
633
- context.viewDate ||
634
- Temporal.Now.plainDateISO();
635
- const dayOfWeek = focusedDate.dayOfWeek(); // 1 = Monday, 7 = Sunday
636
- const daysToSubtract = dayOfWeek === 7 ? 0 : dayOfWeek; // Move to Sunday
637
- const newFocusedDate = focusedDate.add({ days: -daysToSubtract });
638
- // If we moved to a different month, update viewDate
639
- const viewDate = newFocusedDate.month !== focusedDate.month
640
- ? newFocusedDate
641
- : context.viewDate;
642
- return {
643
- focusedDate: newFocusedDate,
644
- viewDate,
645
- };
646
- },
647
- /**
648
- * Action for END_KEY event - move to end of week
649
- *
650
- * @param {DatePickerContext} context - Current context
651
- * @returns {Partial<DatePickerContext>} Updated context
652
- */
653
- END_KEY: (context) => {
654
- const focusedDate = context.focusedDate ||
655
- context.startDate ||
656
- context.viewDate ||
657
- Temporal.Now.plainDateISO();
658
- const dayOfWeek = focusedDate.dayOfWeek(); // 1 = Monday, 7 = Sunday
659
- const daysToAdd = dayOfWeek === 7 ? 0 : 7 - dayOfWeek; // Move to Saturday
660
- const newFocusedDate = focusedDate.add({ days: daysToAdd });
661
- // If we moved to a different month, update viewDate
662
- const viewDate = newFocusedDate.month !== focusedDate.month
663
- ? newFocusedDate
664
- : context.viewDate;
665
- return {
666
- focusedDate: newFocusedDate,
667
- viewDate,
668
- };
669
- },
670
- /**
671
- * Action for ENTER_KEY event - select focused date as end date
672
- *
673
- * @param {DatePickerContext} context - Current context
674
- * @returns {Partial<DatePickerContext>} Updated context
675
- */
676
- ENTER_KEY: (context) => {
677
- if (!context.focusedDate || !context.startDate) {
678
- return {};
679
- }
680
- const endDate = context.focusedDate;
681
- const { startDate } = context;
682
- // Ensure end date is after start date, swap if needed
683
- let finalStartDate = startDate;
684
- let finalEndDate = endDate;
685
- if (comparePlainDates(endDate, startDate) < 0) {
686
- finalStartDate = endDate;
687
- finalEndDate = startDate;
688
- }
689
- return {
690
- startDate: finalStartDate,
691
- endDate: finalEndDate,
692
- value: `${plainDateToISOString(finalStartDate)}/${plainDateToISOString(finalEndDate)}`,
693
- isOpen: false, // Always close after selecting end date in range mode
694
- };
695
- },
696
- /**
697
- * Action for PREV_MONTH event
698
- *
699
- * @param {DatePickerContext} context - Current context
700
- * @returns {Partial<DatePickerContext>} Updated context
701
- */
702
- PREV_MONTH: (context) => {
703
- if (!context.viewDate) {
704
- return {};
705
- }
706
- return {
707
- viewDate: context.viewDate.add({ months: -1 }),
708
- };
709
- },
710
- /**
711
- * Action for NEXT_MONTH event
712
- *
713
- * @param {DatePickerContext} context - Current context
714
- * @returns {Partial<DatePickerContext>} Updated context
715
- */
716
- NEXT_MONTH: (context) => {
717
- if (!context.viewDate) {
718
- return {};
719
- }
720
- return {
721
- viewDate: context.viewDate.add({ months: 1 }),
722
- };
723
- },
724
- },
725
- [DatePickerState.DATE_SELECTED]: {
726
- /**
727
- * Action for DATE_SELECT event
728
- *
729
- * @param {DatePickerContext} context - Current context
730
- * @param {DatePickerEvent} event - The event
731
- * @returns {Partial<DatePickerContext>} Updated context
732
- */
733
- DATE_SELECT: (context, event) => {
734
- const payload = event.payload;
735
- const date = payload === null || payload === void 0 ? void 0 : payload.date;
736
- if (!date) {
737
- return {};
738
- }
739
- if (context.mode === 'single') {
740
- return {
741
- startDate: date,
742
- value: plainDateToISOString(date),
743
- isOpen: context.closeOnSelect ? false : context.isOpen,
744
- };
745
- }
746
- return {};
747
- },
748
- /**
749
- * Action for RANGE_END_SELECT event
750
- *
751
- * @param {DatePickerContext} context - Current context
752
- * @param {DatePickerEvent} event - The event
753
- * @returns {Partial<DatePickerContext>} Updated context
754
- */
755
- RANGE_END_SELECT: (context, event) => {
756
- const payload = event.payload;
757
- const endDate = payload === null || payload === void 0 ? void 0 : payload.date;
758
- const { startDate } = context;
759
- if (!endDate || !startDate) {
760
- return {};
761
- }
762
- // Ensure end date is after start date, swap if needed
763
- let finalStartDate = startDate;
764
- let finalEndDate = endDate;
765
- if (comparePlainDates(endDate, startDate) < 0) {
766
- finalStartDate = endDate;
767
- finalEndDate = startDate;
768
- }
769
- return {
770
- startDate: finalStartDate,
771
- endDate: finalEndDate,
772
- value: `${plainDateToISOString(finalStartDate)}/${plainDateToISOString(finalEndDate)}`,
773
- isOpen: context.closeOnSelect ? false : context.isOpen,
774
- };
775
- },
776
- /** Action for CALENDAR_CLOSE event */
777
- CALENDAR_CLOSE: () => ({
778
- isOpen: false,
779
- }),
780
- },
781
- [DatePickerState.DISABLED]: {
782
- /** Action for a DISABLE event */
783
- DISABLE: () => ({
784
- isDisabled: true,
785
- isOpen: false,
786
- }),
787
- /** Action for ENABLE event */
788
- ENABLE: () => ({
789
- isDisabled: false,
790
- }),
791
- },
792
- [DatePickerState.READONLY]: {
793
- /** Action for SET_READONLY event */
794
- SET_READONLY: () => ({
795
- isReadonly: true,
796
- isOpen: false,
797
- }),
798
- /** Action for UNSET_READONLY event */
799
- UNSET_READONLY: () => ({
800
- isReadonly: false,
801
- }),
802
- },
803
- [DatePickerState.ERROR]: {
804
- /**
805
- * Action for VALIDATION_ERROR event
806
- *
807
- * @param {DatePickerContext} _context - Current context (unused)
808
- * @param {DatePickerEvent} event - The event
809
- * @returns {Partial<DatePickerContext>} Updated context
810
- */
811
- VALIDATION_ERROR: (_context, event) => {
812
- const payload = event.payload;
813
- return {
814
- isInvalid: true,
815
- errorMessage: (payload === null || payload === void 0 ? void 0 : payload.message) || 'Invalid date',
816
- };
817
- },
818
- /** Action for CLEAR_ERROR event */
819
- CLEAR_ERROR: () => ({
820
- isInvalid: false,
821
- errorMessage: undefined,
822
- }),
823
- /** Action for VALUE_CHANGE event */
824
- VALUE_CHANGE: () => ({
825
- isInvalid: false,
826
- errorMessage: undefined,
827
- }),
828
- },
829
- };
830
- /**
831
- * Get action for a specific state and event
832
- *
833
- * @param {DatePickerState} state - The current state
834
- * @param {string} eventType - The event type
835
- * @returns {StateAction | undefined} The action function or undefined
836
- */
837
- function getAction(state, eventType) {
838
- var _a;
839
- return (_a = actions[state]) === null || _a === void 0 ? void 0 : _a[eventType];
840
- }
841
- /**
842
- * Execute an action and return context updates
843
- *
844
- * @param {DatePickerState} state - The current state
845
- * @param {string} eventType - The event type
846
- * @param {DatePickerContext} context - The current context
847
- * @param {DatePickerEvent} event - The event
848
- * @returns {Partial<DatePickerContext>} Partial context updates
849
- */
850
- function executeAction(state, eventType, context, event) {
851
- const action = getAction(state, eventType);
852
- if (!action) {
853
- return {};
854
- }
855
- return action(context, event);
856
- }
857
- // Made with Bob
858
-
859
- export { actions, executeAction, getAction };
860
- //# sourceMappingURL=actions.js.map