dash-core-components 4.0.0-rc1 → 4.0.0-rc3

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 (103) hide show
  1. package/dash_core_components/async-datepicker.js +1 -1
  2. package/dash_core_components/async-datepicker.js.map +1 -1
  3. package/dash_core_components/async-dropdown.js +1 -2
  4. package/dash_core_components/async-dropdown.js.map +1 -1
  5. package/dash_core_components/async-graph.js +1 -1
  6. package/dash_core_components/async-graph.js.map +1 -1
  7. package/dash_core_components/async-highlight.js +1 -1
  8. package/dash_core_components/async-highlight.js.map +1 -1
  9. package/dash_core_components/async-markdown.js +1 -1
  10. package/dash_core_components/async-markdown.js.map +1 -1
  11. package/dash_core_components/async-mathjax.js +1 -1
  12. package/dash_core_components/async-slider.js +1 -1
  13. package/dash_core_components/async-slider.js.map +1 -1
  14. package/dash_core_components/async-upload.js +1 -1
  15. package/dash_core_components/async-upload.js.map +1 -1
  16. package/dash_core_components/dash_core_components-shared.js +1 -1
  17. package/dash_core_components/dash_core_components-shared.js.map +1 -1
  18. package/dash_core_components/dash_core_components.js +1 -1
  19. package/dash_core_components/dash_core_components.js.map +1 -1
  20. package/dash_core_components/proptypes.js +209 -6
  21. package/lib/components/Checklist.tsx +67 -0
  22. package/lib/components/Clipboard.react.js +1 -1
  23. package/lib/components/DatePickerRange.tsx +70 -0
  24. package/lib/components/DatePickerSingle.tsx +72 -0
  25. package/lib/components/Dropdown.tsx +19 -3
  26. package/lib/components/Input.tsx +1 -1
  27. package/lib/components/Loading.tsx +185 -0
  28. package/lib/components/RadioItems.tsx +69 -0
  29. package/lib/components/RangeSlider.tsx +5 -1
  30. package/lib/components/Slider.tsx +12 -1
  31. package/lib/components/Tab.tsx +19 -0
  32. package/lib/components/Tabs.tsx +311 -0
  33. package/lib/components/Textarea.tsx +79 -0
  34. package/lib/components/Tooltip.tsx +66 -0
  35. package/lib/components/css/calendar.css +66 -0
  36. package/lib/components/css/checklist.css +4 -0
  37. package/lib/components/css/datepickers.css +233 -0
  38. package/lib/components/css/dcc.css +3 -0
  39. package/lib/components/css/dropdown.css +34 -50
  40. package/lib/components/css/input.css +29 -7
  41. package/lib/components/css/optionslist.css +34 -0
  42. package/lib/components/css/radioitems.css +4 -0
  43. package/lib/components/css/sliders.css +33 -7
  44. package/lib/components/css/tabs.css +113 -0
  45. package/lib/components/css/textarea.css +21 -0
  46. package/lib/components/css/tooltip.css +127 -0
  47. package/lib/fragments/DatePickerRange.tsx +444 -0
  48. package/lib/fragments/DatePickerSingle.tsx +249 -0
  49. package/lib/fragments/Dropdown.tsx +212 -272
  50. package/lib/fragments/Graph.react.js +18 -45
  51. package/lib/fragments/Loading/spinners/CircleSpinner.tsx +185 -0
  52. package/lib/fragments/Loading/spinners/CubeSpinner.tsx +193 -0
  53. package/lib/fragments/Loading/spinners/DebugTitle.tsx +11 -0
  54. package/lib/fragments/Loading/spinners/DefaultSpinner.tsx +110 -0
  55. package/lib/fragments/Loading/spinners/DotSpinner.tsx +96 -0
  56. package/lib/fragments/Loading/spinners/GraphSpinner.tsx +332 -0
  57. package/lib/fragments/Loading/types.ts +13 -0
  58. package/lib/fragments/RangeSlider.tsx +195 -63
  59. package/lib/index.ts +9 -11
  60. package/lib/types.ts +797 -132
  61. package/lib/utils/DatePickerPersistence.ts +14 -0
  62. package/lib/utils/LazyLoader/datePickerRange.ts +4 -0
  63. package/lib/utils/LazyLoader/datePickerSingle.ts +4 -0
  64. package/lib/utils/calendar/Calendar.tsx +382 -0
  65. package/lib/utils/calendar/CalendarDay.tsx +75 -0
  66. package/lib/utils/calendar/CalendarDayPadding.tsx +15 -0
  67. package/lib/utils/calendar/CalendarMonth.tsx +247 -0
  68. package/lib/utils/calendar/CalendarMonthHeader.tsx +23 -0
  69. package/lib/utils/calendar/createMonthGrid.ts +28 -0
  70. package/lib/utils/calendar/helpers.ts +272 -0
  71. package/lib/utils/computeSliderMarkers.ts +71 -116
  72. package/lib/utils/dropdownSearch.ts +4 -4
  73. package/lib/utils/optionRendering.tsx +175 -0
  74. package/lib/utils/optionTypes.ts +5 -5
  75. package/lib/utils/sliderRendering.tsx +63 -6
  76. package/package.json +16 -14
  77. package/lib/components/Checklist.react.js +0 -193
  78. package/lib/components/DatePickerRange.react.js +0 -244
  79. package/lib/components/DatePickerSingle.react.js +0 -209
  80. package/lib/components/Dropdown.react.js.old +0 -228
  81. package/lib/components/Input.react.js.old +0 -507
  82. package/lib/components/Loading.react.js +0 -254
  83. package/lib/components/RadioItems.react.js +0 -192
  84. package/lib/components/Slider.react.js.old +0 -214
  85. package/lib/components/Tab.react.js +0 -80
  86. package/lib/components/Tabs.react.js +0 -295
  87. package/lib/components/Textarea.react.js +0 -223
  88. package/lib/components/Textarea.react.js.old +0 -287
  89. package/lib/components/Tooltip.react.js +0 -120
  90. package/lib/components/css/Dropdown.css +0 -8
  91. package/lib/fragments/DatePickerRange.react.js +0 -188
  92. package/lib/fragments/DatePickerSingle.react.js +0 -132
  93. package/lib/fragments/Loading/spinners/CircleSpinner.js +0 -68
  94. package/lib/fragments/Loading/spinners/CubeSpinner.js +0 -57
  95. package/lib/fragments/Loading/spinners/DebugTitle.js +0 -15
  96. package/lib/fragments/Loading/spinners/DefaultSpinner.js +0 -54
  97. package/lib/fragments/Loading/spinners/DotSpinner.js +0 -50
  98. package/lib/fragments/Loading/spinners/GraphSpinner.js +0 -68
  99. package/lib/utils/DatePickerPersistence.js +0 -18
  100. package/lib/utils/LazyLoader/datePickerRange.js +0 -8
  101. package/lib/utils/LazyLoader/datePickerSingle.js +0 -8
  102. package/lib/utils/convertToMoment.js +0 -30
  103. package/lib/utils/optionTypes.js +0 -39
@@ -0,0 +1,444 @@
1
+ import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
2
+ import * as Popover from '@radix-ui/react-popover';
3
+ import {
4
+ ArrowLeftIcon,
5
+ ArrowRightIcon,
6
+ CalendarIcon,
7
+ CaretDownIcon,
8
+ Cross1Icon,
9
+ } from '@radix-ui/react-icons';
10
+ import {addDays, subDays} from 'date-fns';
11
+ import AutosizeInput from 'react-input-autosize';
12
+ import uuid from 'uniqid';
13
+
14
+ import {CalendarDirection, DatePickerRangeProps} from '../types';
15
+ import Calendar, {CalendarHandle} from '../utils/calendar/Calendar';
16
+ import {
17
+ dateAsStr,
18
+ formatDate,
19
+ isDateDisabled,
20
+ isSameDay,
21
+ strAsDate,
22
+ } from '../utils/calendar/helpers';
23
+ import '../components/css/datepickers.css';
24
+
25
+ const DatePickerRange = ({
26
+ id,
27
+ className,
28
+ start_date,
29
+ end_date,
30
+ min_date_allowed,
31
+ max_date_allowed,
32
+ initial_visible_month = start_date ?? min_date_allowed ?? max_date_allowed,
33
+ disabled_days,
34
+ minimum_nights,
35
+ first_day_of_week,
36
+ show_outside_days,
37
+ clearable,
38
+ reopen_calendar_on_clear,
39
+ disabled,
40
+ display_format,
41
+ month_format = 'MMMM YYYY',
42
+ stay_open_on_select,
43
+ is_RTL = false,
44
+ setProps,
45
+ style,
46
+ // eslint-disable-next-line no-magic-numbers
47
+ day_size = 34,
48
+ number_of_months_shown = 1,
49
+ calendar_orientation,
50
+ updatemode,
51
+ start_date_id,
52
+ end_date_id,
53
+ start_date_placeholder_text = 'Start Date',
54
+ end_date_placeholder_text = 'End Date',
55
+ }: DatePickerRangeProps) => {
56
+ const [internalStartDate, setInternalStartDate] = useState(
57
+ strAsDate(start_date)
58
+ );
59
+ const [internalEndDate, setInternalEndDate] = useState(strAsDate(end_date));
60
+ const direction = is_RTL
61
+ ? CalendarDirection.RightToLeft
62
+ : CalendarDirection.LeftToRight;
63
+ const initialCalendarDate =
64
+ strAsDate(initial_visible_month) ||
65
+ internalStartDate ||
66
+ internalEndDate;
67
+
68
+ const minDate = strAsDate(min_date_allowed);
69
+ const maxDate = strAsDate(max_date_allowed);
70
+ const disabledDates = useMemo(() => {
71
+ const baseDates =
72
+ disabled_days
73
+ ?.map(d => strAsDate(d))
74
+ .filter((d): d is Date => d !== undefined) || [];
75
+
76
+ // Add minimum_nights constraint: disable dates within the minimum nights range
77
+ if (
78
+ internalStartDate &&
79
+ minimum_nights &&
80
+ minimum_nights > 0 &&
81
+ !internalEndDate
82
+ ) {
83
+ const minimumNightsDates: Date[] = [];
84
+ for (let i = 1; i < minimum_nights; i++) {
85
+ minimumNightsDates.push(addDays(internalStartDate, i));
86
+ minimumNightsDates.push(subDays(internalStartDate, i));
87
+ }
88
+ return [...baseDates, ...minimumNightsDates];
89
+ }
90
+
91
+ return baseDates;
92
+ }, [disabled_days, internalStartDate, internalEndDate, minimum_nights]);
93
+
94
+ const [isCalendarOpen, setIsCalendarOpen] = useState(false);
95
+ const [startInputValue, setStartInputValue] = useState(
96
+ formatDate(internalStartDate, display_format)
97
+ );
98
+ const [endInputValue, setEndInputValue] = useState(
99
+ formatDate(internalEndDate, display_format)
100
+ );
101
+
102
+ const containerRef = useRef<HTMLDivElement>(null);
103
+ const startInputRef = useRef<HTMLInputElement | null>(null);
104
+ const endInputRef = useRef<HTMLInputElement | null>(null);
105
+ const calendarRef = useRef<CalendarHandle>(null);
106
+
107
+ useEffect(() => {
108
+ setInternalStartDate(strAsDate(start_date));
109
+ }, [start_date]);
110
+
111
+ useEffect(() => {
112
+ setInternalEndDate(strAsDate(end_date));
113
+ }, [end_date]);
114
+
115
+ useEffect(() => {
116
+ setStartInputValue(formatDate(internalStartDate, display_format));
117
+ }, [internalStartDate, display_format]);
118
+
119
+ useEffect(() => {
120
+ setEndInputValue(formatDate(internalEndDate, display_format));
121
+ }, [internalEndDate, display_format]);
122
+
123
+ useEffect(() => {
124
+ // Controls when setProps is called. Basically, whenever internal state
125
+ // diverges from props (i.e., user interaction)
126
+ const startChanged = !isSameDay(start_date, internalStartDate);
127
+ const endChanged = !isSameDay(end_date, internalEndDate);
128
+
129
+ if (!startChanged && !endChanged) {
130
+ return;
131
+ }
132
+
133
+ if (internalStartDate && internalEndDate) {
134
+ // Both dates are set - send both
135
+ setProps({
136
+ start_date: dateAsStr(internalStartDate),
137
+ end_date: dateAsStr(internalEndDate),
138
+ });
139
+ } else if (updatemode === 'singledate' && internalStartDate) {
140
+ // Only start changed - send just that one
141
+ setProps({start_date: dateAsStr(internalStartDate)});
142
+ } else if (updatemode === 'singledate' && internalEndDate) {
143
+ // Only end changed - send just that one
144
+ setProps({end_date: dateAsStr(internalEndDate)});
145
+ }
146
+ }, [internalStartDate, internalEndDate, updatemode]);
147
+
148
+ const sendStartInputAsDate = useCallback(
149
+ (focusCalendar = false) => {
150
+ if (startInputValue) {
151
+ setInternalStartDate(undefined);
152
+ }
153
+ const parsed = strAsDate(startInputValue, display_format);
154
+ const isValid =
155
+ parsed &&
156
+ !isDateDisabled(parsed, minDate, maxDate, disabledDates);
157
+
158
+ if (isValid) {
159
+ setInternalStartDate(parsed);
160
+ if (focusCalendar) {
161
+ calendarRef.current?.focusDate(parsed);
162
+ } else {
163
+ calendarRef.current?.setVisibleDate(parsed);
164
+ }
165
+ } else {
166
+ // Invalid or disabled input: revert to previous valid date with proper formatting
167
+ const previousDate = strAsDate(start_date);
168
+ setStartInputValue(
169
+ previousDate ? formatDate(previousDate, display_format) : ''
170
+ );
171
+ if (focusCalendar) {
172
+ calendarRef.current?.focusDate(previousDate);
173
+ }
174
+ }
175
+ },
176
+ [
177
+ startInputValue,
178
+ display_format,
179
+ start_date,
180
+ minDate,
181
+ maxDate,
182
+ disabledDates,
183
+ ]
184
+ );
185
+
186
+ const sendEndInputAsDate = useCallback(
187
+ (focusCalendar = false) => {
188
+ if (endInputValue === '') {
189
+ setInternalEndDate(undefined);
190
+ }
191
+ const parsed = strAsDate(endInputValue, display_format);
192
+ const isValid =
193
+ parsed &&
194
+ !isDateDisabled(parsed, minDate, maxDate, disabledDates);
195
+
196
+ if (isValid) {
197
+ setInternalEndDate(parsed);
198
+ if (focusCalendar) {
199
+ calendarRef.current?.focusDate(parsed);
200
+ } else {
201
+ calendarRef.current?.setVisibleDate(parsed);
202
+ }
203
+ } else {
204
+ // Invalid or disabled input: revert to previous valid date with proper formatting
205
+ const previousDate = strAsDate(end_date);
206
+ setEndInputValue(
207
+ previousDate ? formatDate(previousDate, display_format) : ''
208
+ );
209
+ if (focusCalendar) {
210
+ calendarRef.current?.focusDate(previousDate);
211
+ }
212
+ }
213
+ },
214
+ [
215
+ endInputValue,
216
+ display_format,
217
+ end_date,
218
+ minDate,
219
+ maxDate,
220
+ disabledDates,
221
+ ]
222
+ );
223
+
224
+ const clearSelection = useCallback(
225
+ e => {
226
+ setInternalStartDate(undefined);
227
+ setInternalEndDate(undefined);
228
+ startInputRef.current?.focus();
229
+ e.preventDefault();
230
+ e.stopPropagation();
231
+ if (reopen_calendar_on_clear) {
232
+ setIsCalendarOpen(true);
233
+ }
234
+ },
235
+ [reopen_calendar_on_clear]
236
+ );
237
+
238
+ const handleStartInputKeyDown = useCallback(
239
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
240
+ if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
241
+ e.preventDefault();
242
+ sendStartInputAsDate(true);
243
+ if (!isCalendarOpen) {
244
+ // open the calendar after resolving prop changes, so that
245
+ // it opens with the correct date showing
246
+ setTimeout(() => setIsCalendarOpen(true), 0);
247
+ }
248
+ } else if (['Enter', 'Tab'].includes(e.key)) {
249
+ sendStartInputAsDate();
250
+ }
251
+ },
252
+ [isCalendarOpen, sendStartInputAsDate]
253
+ );
254
+
255
+ const handleEndInputKeyDown = useCallback(
256
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
257
+ if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
258
+ e.preventDefault();
259
+ sendEndInputAsDate(true);
260
+ if (!isCalendarOpen) {
261
+ // open the calendar after resolving prop changes, so that
262
+ // it opens with the correct date showing
263
+ setTimeout(() => setIsCalendarOpen(true), 0);
264
+ }
265
+ } else if (['Enter', 'Tab'].includes(e.key)) {
266
+ sendEndInputAsDate();
267
+ }
268
+ },
269
+ [isCalendarOpen, sendEndInputAsDate]
270
+ );
271
+
272
+ const accessibleId = id ?? uuid();
273
+ let classNames = 'dash-datepicker-input-wrapper';
274
+ if (disabled) {
275
+ classNames += ' dash-datepicker-input-wrapper-disabled';
276
+ }
277
+ if (className) {
278
+ classNames += ' ' + className;
279
+ }
280
+
281
+ const ArrowIcon =
282
+ direction === CalendarDirection.LeftToRight
283
+ ? ArrowRightIcon
284
+ : ArrowLeftIcon;
285
+
286
+ const handleSelectionChange = useCallback(
287
+ (start?: Date, end?: Date) => {
288
+ const isNewSelection =
289
+ isSameDay(start, end) &&
290
+ ((!internalStartDate && !internalEndDate) ||
291
+ (internalStartDate && internalEndDate));
292
+
293
+ if (isNewSelection) {
294
+ setInternalStartDate(start);
295
+ setInternalEndDate(undefined);
296
+ } else {
297
+ // Normalize dates: ensure start <= end
298
+ if (start && end && start > end) {
299
+ setInternalStartDate(end);
300
+ setInternalEndDate(start);
301
+ } else {
302
+ setInternalStartDate(start);
303
+ setInternalEndDate(end);
304
+ }
305
+
306
+ if (end && !stay_open_on_select) {
307
+ setIsCalendarOpen(false);
308
+ }
309
+ }
310
+ },
311
+ [internalStartDate, internalEndDate, stay_open_on_select]
312
+ );
313
+
314
+ return (
315
+ <div className="dash-datepicker" ref={containerRef}>
316
+ <Popover.Root
317
+ open={!disabled && isCalendarOpen}
318
+ onOpenChange={disabled ? undefined : setIsCalendarOpen}
319
+ >
320
+ <Popover.Trigger asChild disabled={disabled}>
321
+ <div
322
+ id={accessibleId + '-wrapper'}
323
+ className={classNames}
324
+ style={style}
325
+ aria-labelledby={`${accessibleId} ${accessibleId}-end-date ${start_date_id} ${end_date_id}`}
326
+ aria-haspopup="dialog"
327
+ aria-expanded={isCalendarOpen}
328
+ aria-disabled={disabled}
329
+ onClick={e => {
330
+ e.preventDefault();
331
+ if (!isCalendarOpen && !disabled) {
332
+ setIsCalendarOpen(true);
333
+ }
334
+ }}
335
+ >
336
+ <CalendarIcon className="dash-datepicker-trigger-icon" />
337
+ <AutosizeInput
338
+ inputRef={node => {
339
+ startInputRef.current = node;
340
+ }}
341
+ type="text"
342
+ id={start_date_id || accessibleId}
343
+ inputClassName="dash-datepicker-input dash-datepicker-start-date"
344
+ value={startInputValue}
345
+ onChange={e => setStartInputValue(e.target.value)}
346
+ onKeyDown={handleStartInputKeyDown}
347
+ onFocus={() => {
348
+ if (internalStartDate) {
349
+ calendarRef.current?.setVisibleDate(
350
+ internalStartDate
351
+ );
352
+ }
353
+ }}
354
+ placeholder={start_date_placeholder_text}
355
+ disabled={disabled}
356
+ dir={direction}
357
+ aria-label={start_date_placeholder_text}
358
+ />
359
+ <ArrowIcon />
360
+ <AutosizeInput
361
+ inputRef={node => {
362
+ endInputRef.current = node;
363
+ }}
364
+ type="text"
365
+ id={end_date_id || accessibleId + '-end-date'}
366
+ inputClassName="dash-datepicker-input dash-datepicker-end-date"
367
+ value={endInputValue}
368
+ onChange={e => setEndInputValue(e.target.value)}
369
+ onKeyDown={handleEndInputKeyDown}
370
+ onFocus={() => {
371
+ if (internalEndDate) {
372
+ calendarRef.current?.setVisibleDate(
373
+ internalEndDate
374
+ );
375
+ }
376
+ }}
377
+ placeholder={end_date_placeholder_text}
378
+ disabled={disabled}
379
+ dir={direction}
380
+ aria-label={end_date_placeholder_text}
381
+ />
382
+ {clearable && !disabled && (
383
+ <a
384
+ className="dash-datepicker-clear"
385
+ onClick={clearSelection}
386
+ aria-label="Clear Dates"
387
+ >
388
+ <Cross1Icon />
389
+ </a>
390
+ )}
391
+ <CaretDownIcon className="dash-datepicker-caret-icon" />
392
+ </div>
393
+ </Popover.Trigger>
394
+
395
+ <Popover.Portal container={containerRef.current}>
396
+ <Popover.Content
397
+ className="dash-datepicker-content"
398
+ align="start"
399
+ sideOffset={5}
400
+ onOpenAutoFocus={e => e.preventDefault()}
401
+ onCloseAutoFocus={e => {
402
+ e.preventDefault();
403
+ // Only focus if focus is not already on one of the inputs
404
+ const inputs: (Element | null)[] = [
405
+ startInputRef.current,
406
+ endInputRef.current,
407
+ ];
408
+ if (inputs.includes(document.activeElement)) {
409
+ return;
410
+ }
411
+
412
+ // Keeps focus on the component when the calendar closes
413
+ if (!startInputValue) {
414
+ startInputRef.current?.focus();
415
+ } else {
416
+ endInputRef.current?.focus();
417
+ }
418
+ }}
419
+ >
420
+ <Calendar
421
+ ref={calendarRef}
422
+ initialVisibleDate={initialCalendarDate}
423
+ selectionStart={internalStartDate}
424
+ selectionEnd={internalEndDate}
425
+ minDateAllowed={minDate}
426
+ maxDateAllowed={maxDate}
427
+ disabledDates={disabledDates}
428
+ firstDayOfWeek={first_day_of_week}
429
+ showOutsideDays={show_outside_days}
430
+ monthFormat={month_format}
431
+ numberOfMonthsShown={number_of_months_shown}
432
+ calendarOrientation={calendar_orientation}
433
+ daySize={day_size}
434
+ direction={direction}
435
+ onSelectionChange={handleSelectionChange}
436
+ />
437
+ </Popover.Content>
438
+ </Popover.Portal>
439
+ </Popover.Root>
440
+ </div>
441
+ );
442
+ };
443
+
444
+ export default DatePickerRange;
@@ -0,0 +1,249 @@
1
+ import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
2
+ import * as Popover from '@radix-ui/react-popover';
3
+ import {CalendarIcon, CaretDownIcon, Cross1Icon} from '@radix-ui/react-icons';
4
+ import AutosizeInput from 'react-input-autosize';
5
+ import uuid from 'uniqid';
6
+
7
+ import {CalendarDirection, DatePickerSingleProps} from '../types';
8
+ import Calendar, {CalendarHandle} from '../utils/calendar/Calendar';
9
+ import {
10
+ dateAsStr,
11
+ formatDate,
12
+ isDateDisabled,
13
+ isSameDay,
14
+ strAsDate,
15
+ } from '../utils/calendar/helpers';
16
+ import '../components/css/datepickers.css';
17
+
18
+ const DatePickerSingle = ({
19
+ id,
20
+ className,
21
+ date,
22
+ min_date_allowed,
23
+ max_date_allowed,
24
+ initial_visible_month = date ?? min_date_allowed,
25
+ disabled_days,
26
+ first_day_of_week,
27
+ show_outside_days,
28
+ placeholder = 'Select Date',
29
+ clearable,
30
+ reopen_calendar_on_clear,
31
+ disabled,
32
+ display_format,
33
+ month_format = 'MMMM YYYY',
34
+ stay_open_on_select,
35
+ is_RTL = false,
36
+ setProps,
37
+ style,
38
+ // eslint-disable-next-line no-magic-numbers
39
+ day_size = 34,
40
+ number_of_months_shown = 1,
41
+ calendar_orientation,
42
+ }: DatePickerSingleProps) => {
43
+ const [internalDate, setInternalDate] = useState(strAsDate(date));
44
+ const direction = is_RTL
45
+ ? CalendarDirection.RightToLeft
46
+ : CalendarDirection.LeftToRight;
47
+ const initialMonth = strAsDate(initial_visible_month) || internalDate;
48
+ const minDate = strAsDate(min_date_allowed);
49
+ const maxDate = strAsDate(max_date_allowed);
50
+ const disabledDates = useMemo(() => {
51
+ return disabled_days
52
+ ?.map(d => strAsDate(d))
53
+ .filter((d): d is Date => d !== undefined);
54
+ }, [disabled_days]);
55
+
56
+ const [isCalendarOpen, setIsCalendarOpen] = useState(false);
57
+ const [inputValue, setInputValue] = useState<string>(
58
+ (internalDate && formatDate(internalDate, display_format)) ?? ''
59
+ );
60
+
61
+ const containerRef = useRef<HTMLDivElement>(null);
62
+ const inputRef = useRef<HTMLInputElement | null>(null);
63
+ const calendarRef = useRef<CalendarHandle>(null);
64
+
65
+ useEffect(() => {
66
+ setInternalDate(strAsDate(date));
67
+ }, [date]);
68
+
69
+ useEffect(() => {
70
+ setInputValue(formatDate(internalDate, display_format));
71
+ }, [internalDate, display_format]);
72
+
73
+ useEffect(() => {
74
+ const dateChanged = !(date && isSameDay(date, internalDate));
75
+
76
+ if (dateChanged) {
77
+ setProps({date: dateAsStr(internalDate)});
78
+ }
79
+ }, [internalDate]);
80
+
81
+ const parseUserInput = useCallback(
82
+ (focusCalendar = false) => {
83
+ if (inputValue === '') {
84
+ setInternalDate(undefined);
85
+ }
86
+ const parsed = strAsDate(inputValue, display_format);
87
+ const isValid =
88
+ parsed &&
89
+ !isDateDisabled(parsed, minDate, maxDate, disabledDates);
90
+
91
+ if (isValid) {
92
+ setInternalDate(parsed);
93
+ if (focusCalendar) {
94
+ calendarRef.current?.focusDate(parsed);
95
+ } else {
96
+ calendarRef.current?.setVisibleDate(parsed);
97
+ }
98
+ } else {
99
+ // Invalid or disabled input: revert to previous valid date with proper formatting
100
+ const previousDate = strAsDate(date);
101
+ setInputValue(
102
+ previousDate ? formatDate(previousDate, display_format) : ''
103
+ );
104
+ if (focusCalendar) {
105
+ calendarRef.current?.focusDate(previousDate);
106
+ }
107
+ }
108
+ },
109
+ [inputValue, display_format, date, minDate, maxDate, disabledDates]
110
+ );
111
+
112
+ const clearSelection = useCallback(
113
+ (e: React.MouseEvent<HTMLAnchorElement>) => {
114
+ setInternalDate(undefined);
115
+ inputRef.current?.focus();
116
+ e.preventDefault();
117
+ e.stopPropagation();
118
+ if (reopen_calendar_on_clear) {
119
+ setIsCalendarOpen(true);
120
+ }
121
+ },
122
+ [reopen_calendar_on_clear]
123
+ );
124
+
125
+ const handleInputKeyDown = useCallback(
126
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
127
+ if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
128
+ e.preventDefault();
129
+ parseUserInput(true);
130
+ if (!isCalendarOpen) {
131
+ // open the calendar after resolving prop changes, so that
132
+ // it opens with the correct date showing
133
+ setTimeout(() => setIsCalendarOpen(true), 0);
134
+ }
135
+ } else if (['Enter', 'Tab'].includes(e.key)) {
136
+ parseUserInput();
137
+ }
138
+ },
139
+ [isCalendarOpen, parseUserInput]
140
+ );
141
+
142
+ const accessibleId = id ?? uuid();
143
+ let classNames = 'dash-datepicker-input-wrapper';
144
+ if (disabled) {
145
+ classNames += ' dash-datepicker-input-wrapper-disabled';
146
+ }
147
+ if (className) {
148
+ classNames += ' ' + className;
149
+ }
150
+
151
+ return (
152
+ <div className="dash-datepicker" ref={containerRef}>
153
+ <Popover.Root
154
+ open={!disabled && isCalendarOpen}
155
+ onOpenChange={disabled ? undefined : setIsCalendarOpen}
156
+ >
157
+ <Popover.Trigger asChild disabled={disabled}>
158
+ <div
159
+ id={accessibleId + '-wrapper'}
160
+ className={classNames}
161
+ style={style}
162
+ aria-labelledby={`${accessibleId}`}
163
+ aria-haspopup="dialog"
164
+ aria-expanded={isCalendarOpen}
165
+ aria-disabled={disabled}
166
+ onClick={e => {
167
+ e.preventDefault();
168
+ if (!isCalendarOpen && !disabled) {
169
+ setIsCalendarOpen(true);
170
+ }
171
+ }}
172
+ >
173
+ <CalendarIcon className="dash-datepicker-trigger-icon" />
174
+ <AutosizeInput
175
+ inputRef={node => {
176
+ inputRef.current = node;
177
+ }}
178
+ type="text"
179
+ id={accessibleId}
180
+ inputClassName="dash-datepicker-input dash-datepicker-end-date"
181
+ value={inputValue}
182
+ onChange={e => setInputValue(e.target.value)}
183
+ onKeyDown={handleInputKeyDown}
184
+ placeholder={placeholder}
185
+ disabled={disabled}
186
+ dir={direction}
187
+ aria-label={placeholder}
188
+ />
189
+ {clearable && !disabled && !!date && (
190
+ <a
191
+ className="dash-datepicker-clear"
192
+ onClick={clearSelection}
193
+ aria-label="Clear date"
194
+ >
195
+ <Cross1Icon />
196
+ </a>
197
+ )}
198
+
199
+ <CaretDownIcon className="dash-datepicker-caret-icon" />
200
+ </div>
201
+ </Popover.Trigger>
202
+
203
+ <Popover.Portal container={containerRef.current}>
204
+ <Popover.Content
205
+ className="dash-datepicker-content"
206
+ align="start"
207
+ sideOffset={5}
208
+ onOpenAutoFocus={e => e.preventDefault()}
209
+ onCloseAutoFocus={e => {
210
+ e.preventDefault();
211
+ // Only focus if focus is not already on the input
212
+ if (document.activeElement !== inputRef.current) {
213
+ inputRef.current?.focus();
214
+ }
215
+ }}
216
+ >
217
+ <Calendar
218
+ ref={calendarRef}
219
+ initialVisibleDate={initialMonth}
220
+ selectionStart={internalDate}
221
+ selectionEnd={internalDate}
222
+ minDateAllowed={minDate}
223
+ maxDateAllowed={maxDate}
224
+ disabledDates={disabledDates}
225
+ firstDayOfWeek={first_day_of_week}
226
+ showOutsideDays={show_outside_days}
227
+ monthFormat={month_format}
228
+ numberOfMonthsShown={number_of_months_shown}
229
+ calendarOrientation={calendar_orientation}
230
+ daySize={day_size}
231
+ direction={direction}
232
+ onSelectionChange={(_, selection) => {
233
+ if (!selection) {
234
+ return;
235
+ }
236
+ setInternalDate(selection);
237
+ if (!stay_open_on_select) {
238
+ setIsCalendarOpen(false);
239
+ }
240
+ }}
241
+ />
242
+ </Popover.Content>
243
+ </Popover.Portal>
244
+ </Popover.Root>
245
+ </div>
246
+ );
247
+ };
248
+
249
+ export default DatePickerSingle;