dash-core-components 4.0.0-rc2 → 4.0.0-rc4

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