dash-core-components 4.0.0-rc6 → 4.1.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 (55) 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 -1
  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 -2
  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 +3 -0
  21. package/lib/components/Checklist.tsx +1 -1
  22. package/lib/components/Clipboard.react.js +67 -105
  23. package/lib/components/ConfirmDialog.react.js +6 -5
  24. package/lib/components/ConfirmDialogProvider.react.js +6 -5
  25. package/lib/components/Download.react.js +3 -1
  26. package/lib/components/Graph.react.js +5 -10
  27. package/lib/components/Interval.react.js +4 -3
  28. package/lib/components/Link.react.js +13 -10
  29. package/lib/components/Location.react.js +11 -7
  30. package/lib/components/Markdown.react.js +1 -8
  31. package/lib/components/RadioItems.tsx +1 -1
  32. package/lib/components/RangeSlider.tsx +2 -0
  33. package/lib/components/Slider.tsx +2 -0
  34. package/lib/components/Store.react.js +16 -13
  35. package/lib/components/css/calendar.css +20 -3
  36. package/lib/components/css/datepickers.css +9 -2
  37. package/lib/components/css/dropdown.css +12 -0
  38. package/lib/components/css/optionslist.css +0 -1
  39. package/lib/fragments/DatePickerRange.tsx +201 -140
  40. package/lib/fragments/DatePickerSingle.tsx +132 -108
  41. package/lib/fragments/Dropdown.tsx +202 -131
  42. package/lib/fragments/Graph.react.js +43 -40
  43. package/lib/fragments/Markdown.react.js +27 -38
  44. package/lib/fragments/RangeSlider.tsx +2 -0
  45. package/lib/fragments/Upload.react.js +117 -202
  46. package/lib/types.ts +17 -0
  47. package/lib/utils/LoadingElement.js +3 -2
  48. package/lib/utils/ResizeDetector.js +5 -3
  49. package/lib/utils/calendar/Calendar.tsx +1 -3
  50. package/lib/utils/calendar/cssVariables.ts +41 -0
  51. package/lib/utils/calendar/helpers.ts +1 -0
  52. package/lib/utils/dropdownSearch.ts +36 -45
  53. package/lib/utils/optionRendering.tsx +284 -43
  54. package/lib/utils/optionTypes.ts +25 -11
  55. package/package.json +9 -8
@@ -6,7 +6,7 @@ import {
6
6
  CaretDownIcon,
7
7
  Cross1Icon,
8
8
  } from '@radix-ui/react-icons';
9
- import {addDays, subDays} from 'date-fns';
9
+ import {addDays, subDays, differenceInCalendarDays} from 'date-fns';
10
10
  import AutosizeInput from 'react-input-autosize';
11
11
  import uuid from 'uniqid';
12
12
 
@@ -19,6 +19,8 @@ import {
19
19
  isSameDay,
20
20
  strAsDate,
21
21
  } from '../utils/calendar/helpers';
22
+ import {captureCSSForPortal} from '../utils/calendar/cssVariables';
23
+ import ResizeDetector from '../utils/ResizeDetector';
22
24
  import '../components/css/datepickers.css';
23
25
 
24
26
  const DatePickerRange = ({
@@ -103,9 +105,17 @@ const DatePickerRange = ({
103
105
  const containerRef = useRef<HTMLDivElement>(null);
104
106
  const startInputRef = useRef<HTMLInputElement | null>(null);
105
107
  const endInputRef = useRef<HTMLInputElement | null>(null);
108
+ const startAutosizeRef = useRef<any>(null);
109
+ const endAutosizeRef = useRef<any>(null);
106
110
  const calendarRef = useRef<CalendarHandle>(null);
111
+ const isNewRangeRef = useRef(false);
107
112
  const hasPortal = with_portal || with_full_screen_portal;
108
113
 
114
+ // Capture CSS variables for portal mode
115
+ const portalStyle = useMemo(() => {
116
+ return hasPortal ? captureCSSForPortal(containerRef) : undefined;
117
+ }, [hasPortal, isCalendarOpen]);
118
+
109
119
  useEffect(() => {
110
120
  setInternalStartDate(strAsDate(start_date));
111
121
  }, [start_date]);
@@ -122,6 +132,19 @@ const DatePickerRange = ({
122
132
  setEndInputValue(formatDate(internalEndDate, display_format));
123
133
  }, [internalEndDate, display_format]);
124
134
 
135
+ const handleResize = useCallback(() => {
136
+ startAutosizeRef.current?.updateInputWidth?.();
137
+ endAutosizeRef.current?.updateInputWidth?.();
138
+ }, []);
139
+
140
+ useEffect(() => {
141
+ startAutosizeRef.current?.updateInputWidth?.();
142
+ }, [startInputValue]);
143
+
144
+ useEffect(() => {
145
+ endAutosizeRef.current?.updateInputWidth?.();
146
+ }, [endInputValue]);
147
+
125
148
  useEffect(() => {
126
149
  // Controls when setProps is called. Basically, whenever internal state
127
150
  // diverges from props (i.e., user interaction)
@@ -139,11 +162,17 @@ const DatePickerRange = ({
139
162
  end_date: dateAsStr(internalEndDate),
140
163
  });
141
164
  } else if (!internalStartDate && !internalEndDate) {
142
- // Both dates cleared - send undefined for both
165
+ // Both dates cleared - send both
143
166
  setProps({
144
167
  start_date: dateAsStr(internalStartDate),
145
168
  end_date: dateAsStr(internalEndDate),
146
169
  });
170
+ } else if (endChanged && !internalEndDate) {
171
+ // End date was cleared (user started a new range).
172
+ setProps({
173
+ start_date: dateAsStr(internalStartDate) ?? null,
174
+ end_date: null,
175
+ });
147
176
  } else if (updatemode === 'singledate' && internalStartDate) {
148
177
  // Only start changed - send just that one
149
178
  setProps({start_date: dateAsStr(internalStartDate)});
@@ -289,6 +318,22 @@ const DatePickerRange = ({
289
318
  setInternalStartDate(start);
290
319
  setInternalEndDate(undefined);
291
320
  } else {
321
+ // Skip the mouseUp from the same click that started this range
322
+ if (isNewRangeRef.current && isSameDay(start, end)) {
323
+ isNewRangeRef.current = false;
324
+ return;
325
+ }
326
+ isNewRangeRef.current = !!(start && !end);
327
+
328
+ if (start && end && minimum_nights) {
329
+ const numNights = Math.abs(
330
+ differenceInCalendarDays(end, start)
331
+ );
332
+ if (numNights < minimum_nights) {
333
+ return;
334
+ }
335
+ }
336
+
292
337
  // Normalize dates: ensure start <= end
293
338
  if (start && end && start > end) {
294
339
  setInternalStartDate(end);
@@ -303,152 +348,168 @@ const DatePickerRange = ({
303
348
  }
304
349
  }
305
350
  },
306
- [internalStartDate, internalEndDate, stay_open_on_select]
351
+ [
352
+ internalStartDate,
353
+ internalEndDate,
354
+ stay_open_on_select,
355
+ minimum_nights,
356
+ ]
307
357
  );
308
358
 
309
359
  return (
310
- <div className="dash-datepicker" ref={containerRef}>
311
- <Popover.Root
312
- open={!disabled && isCalendarOpen}
313
- onOpenChange={disabled ? undefined : setIsCalendarOpen}
314
- >
315
- <Popover.Trigger asChild disabled={disabled}>
316
- <div
317
- id={accessibleId + '-wrapper'}
318
- className={classNames}
319
- style={style}
320
- aria-labelledby={`${accessibleId} ${accessibleId}-end-date ${start_date_id} ${end_date_id}`}
321
- aria-haspopup="dialog"
322
- aria-expanded={isCalendarOpen}
323
- aria-disabled={disabled}
324
- onClick={e => {
325
- e.preventDefault();
326
- if (!isCalendarOpen && !disabled) {
327
- setIsCalendarOpen(true);
328
- }
329
- }}
330
- >
331
- <AutosizeInput
332
- inputRef={node => {
333
- startInputRef.current = node;
334
- }}
335
- type="text"
336
- id={start_date_id || accessibleId}
337
- inputClassName="dash-datepicker-input dash-datepicker-start-date"
338
- value={startInputValue}
339
- onChange={e => setStartInputValue(e.target?.value)}
340
- onKeyDown={handleStartInputKeyDown}
341
- onFocus={() => {
342
- if (isCalendarOpen) {
343
- sendStartInputAsDate();
360
+ <ResizeDetector onResize={handleResize} targets={[containerRef]}>
361
+ <div className="dash-datepicker" ref={containerRef}>
362
+ <Popover.Root
363
+ open={!disabled && isCalendarOpen}
364
+ onOpenChange={disabled ? undefined : setIsCalendarOpen}
365
+ >
366
+ <Popover.Trigger asChild disabled={disabled}>
367
+ <div
368
+ id={accessibleId + '-wrapper'}
369
+ className={classNames}
370
+ style={style}
371
+ aria-labelledby={`${accessibleId} ${accessibleId}-end-date ${start_date_id} ${end_date_id}`}
372
+ aria-haspopup="dialog"
373
+ aria-expanded={isCalendarOpen}
374
+ aria-disabled={disabled}
375
+ onClick={e => {
376
+ e.preventDefault();
377
+ if (!isCalendarOpen && !disabled) {
378
+ setIsCalendarOpen(true);
344
379
  }
345
380
  }}
346
- placeholder={start_date_placeholder_text}
347
- disabled={disabled}
348
- dir={direction}
349
- aria-label={start_date_placeholder_text}
350
- />
351
- <ArrowIcon className="dash-datepicker-range-arrow" />
352
- <AutosizeInput
353
- inputRef={node => {
354
- endInputRef.current = node;
355
- }}
356
- type="text"
357
- id={end_date_id || accessibleId + '-end-date'}
358
- inputClassName="dash-datepicker-input dash-datepicker-end-date"
359
- value={endInputValue}
360
- onChange={e => setEndInputValue(e.target?.value)}
361
- onKeyDown={handleEndInputKeyDown}
362
- onFocus={() => {
363
- if (isCalendarOpen) {
364
- sendEndInputAsDate();
381
+ >
382
+ <AutosizeInput
383
+ ref={startAutosizeRef}
384
+ inputRef={node => {
385
+ startInputRef.current = node;
386
+ }}
387
+ type="text"
388
+ id={start_date_id || accessibleId}
389
+ inputClassName="dash-datepicker-input dash-datepicker-start-date"
390
+ value={startInputValue}
391
+ onChange={e =>
392
+ setStartInputValue(e.target?.value)
365
393
  }
366
- }}
367
- placeholder={end_date_placeholder_text}
368
- disabled={disabled}
369
- dir={direction}
370
- aria-label={end_date_placeholder_text}
371
- />
372
- {clearable && !disabled && (
373
- <a
374
- className="dash-datepicker-clear"
375
- onClick={clearSelection}
376
- aria-label="Clear Dates"
377
- >
378
- <Cross1Icon />
379
- </a>
380
- )}
381
- <CaretDownIcon className="dash-datepicker-caret-icon" />
382
- </div>
383
- </Popover.Trigger>
384
-
385
- <Popover.Portal container={containerRef.current}>
386
- <Popover.Content
387
- className={`dash-datepicker-content${
388
- hasPortal ? ' dash-datepicker-portal' : ''
389
- }${
390
- with_full_screen_portal
391
- ? ' dash-datepicker-fullscreen'
392
- : ''
393
- }`}
394
- align={hasPortal ? 'center' : 'start'}
395
- sideOffset={hasPortal ? 0 : 5}
396
- avoidCollisions={!hasPortal}
397
- onInteractOutside={
398
- with_full_screen_portal
399
- ? e => e.preventDefault()
400
- : undefined
401
- }
402
- onOpenAutoFocus={e => e.preventDefault()}
403
- onCloseAutoFocus={e => {
404
- e.preventDefault();
405
- // Only focus if focus is not already on one of the inputs
406
- const inputs: (Element | null)[] = [
407
- startInputRef.current,
408
- endInputRef.current,
409
- ];
410
- if (inputs.includes(document.activeElement)) {
411
- return;
412
- }
394
+ onKeyDown={handleStartInputKeyDown}
395
+ onFocus={() => {
396
+ if (isCalendarOpen) {
397
+ sendStartInputAsDate();
398
+ }
399
+ }}
400
+ placeholder={start_date_placeholder_text}
401
+ disabled={disabled}
402
+ dir={direction}
403
+ aria-label={start_date_placeholder_text}
404
+ />
405
+ <ArrowIcon className="dash-datepicker-range-arrow" />
406
+ <AutosizeInput
407
+ ref={endAutosizeRef}
408
+ inputRef={node => {
409
+ endInputRef.current = node;
410
+ }}
411
+ type="text"
412
+ id={end_date_id || accessibleId + '-end-date'}
413
+ inputClassName="dash-datepicker-input dash-datepicker-end-date"
414
+ value={endInputValue}
415
+ onChange={e =>
416
+ setEndInputValue(e.target?.value)
417
+ }
418
+ onKeyDown={handleEndInputKeyDown}
419
+ onFocus={() => {
420
+ if (isCalendarOpen) {
421
+ sendEndInputAsDate();
422
+ }
423
+ }}
424
+ placeholder={end_date_placeholder_text}
425
+ disabled={disabled}
426
+ dir={direction}
427
+ aria-label={end_date_placeholder_text}
428
+ />
429
+ {clearable && !disabled && (
430
+ <a
431
+ className="dash-datepicker-clear"
432
+ onClick={clearSelection}
433
+ aria-label="Clear Dates"
434
+ >
435
+ <Cross1Icon />
436
+ </a>
437
+ )}
438
+ <CaretDownIcon className="dash-datepicker-caret-icon" />
439
+ </div>
440
+ </Popover.Trigger>
413
441
 
414
- // Keeps focus on the component when the calendar closes
415
- if (!startInputValue) {
416
- startInputRef.current?.focus();
417
- } else {
418
- endInputRef.current?.focus();
419
- }
420
- }}
442
+ <Popover.Portal
443
+ container={hasPortal ? undefined : containerRef.current}
421
444
  >
422
- {with_full_screen_portal && (
423
- <button
424
- className="dash-datepicker-close-button"
425
- onClick={() => setIsCalendarOpen(false)}
426
- aria-label="Close calendar"
427
- >
428
- <Cross1Icon />
429
- </button>
430
- )}
431
- <Calendar
432
- ref={calendarRef}
433
- initialVisibleDate={initialCalendarDate}
434
- selectionStart={internalStartDate}
435
- selectionEnd={internalEndDate}
436
- minDateAllowed={minDate}
437
- maxDateAllowed={maxDate}
438
- disabledDates={disabledDates}
439
- firstDayOfWeek={first_day_of_week}
440
- showOutsideDays={show_outside_days}
441
- monthFormat={month_format}
442
- numberOfMonthsShown={number_of_months_shown}
443
- calendarOrientation={calendar_orientation}
444
- daySize={day_size}
445
- direction={direction}
446
- onSelectionChange={handleSelectionChange}
447
- />
448
- </Popover.Content>
449
- </Popover.Portal>
450
- </Popover.Root>
451
- </div>
445
+ <Popover.Content
446
+ className={`dash-datepicker-content${
447
+ hasPortal ? ' dash-datepicker-portal' : ''
448
+ }${
449
+ with_full_screen_portal
450
+ ? ' dash-datepicker-fullscreen'
451
+ : ''
452
+ }`}
453
+ style={portalStyle}
454
+ align={hasPortal ? 'center' : 'start'}
455
+ sideOffset={hasPortal ? 0 : 5}
456
+ avoidCollisions={!hasPortal}
457
+ onInteractOutside={
458
+ with_full_screen_portal
459
+ ? e => e.preventDefault()
460
+ : undefined
461
+ }
462
+ onOpenAutoFocus={e => e.preventDefault()}
463
+ onCloseAutoFocus={e => {
464
+ e.preventDefault();
465
+ // Only focus if focus is not already on one of the inputs
466
+ const inputs: (Element | null)[] = [
467
+ startInputRef.current,
468
+ endInputRef.current,
469
+ ];
470
+ if (inputs.includes(document.activeElement)) {
471
+ return;
472
+ }
473
+
474
+ // Keeps focus on the component when the calendar closes
475
+ if (!startInputValue) {
476
+ startInputRef.current?.focus();
477
+ } else {
478
+ endInputRef.current?.focus();
479
+ }
480
+ }}
481
+ >
482
+ {with_full_screen_portal && (
483
+ <button
484
+ className="dash-datepicker-close-button"
485
+ onClick={() => setIsCalendarOpen(false)}
486
+ aria-label="Close calendar"
487
+ >
488
+ <Cross1Icon />
489
+ </button>
490
+ )}
491
+ <Calendar
492
+ ref={calendarRef}
493
+ initialVisibleDate={initialCalendarDate}
494
+ selectionStart={internalStartDate}
495
+ selectionEnd={internalEndDate}
496
+ minDateAllowed={minDate}
497
+ maxDateAllowed={maxDate}
498
+ disabledDates={disabledDates}
499
+ firstDayOfWeek={first_day_of_week}
500
+ showOutsideDays={show_outside_days}
501
+ monthFormat={month_format}
502
+ numberOfMonthsShown={number_of_months_shown}
503
+ calendarOrientation={calendar_orientation}
504
+ daySize={day_size}
505
+ direction={direction}
506
+ onSelectionChange={handleSelectionChange}
507
+ />
508
+ </Popover.Content>
509
+ </Popover.Portal>
510
+ </Popover.Root>
511
+ </div>
512
+ </ResizeDetector>
452
513
  );
453
514
  };
454
515