dash-core-components 4.0.0-rc1 → 4.0.0-rc2

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-dropdown.js +1 -1
  2. package/dash_core_components/async-dropdown.js.map +1 -1
  3. package/dash_core_components/async-slider.js +1 -1
  4. package/dash_core_components/async-slider.js.map +1 -1
  5. package/dash_core_components/dash_core_components.js +1 -1
  6. package/dash_core_components/dash_core_components.js.map +1 -1
  7. package/dash_core_components/proptypes.js +102 -3
  8. package/lib/components/Checklist.tsx +67 -0
  9. package/lib/components/Dropdown.tsx +19 -3
  10. package/lib/components/Loading.tsx +185 -0
  11. package/lib/components/RadioItems.tsx +69 -0
  12. package/lib/components/RangeSlider.tsx +5 -1
  13. package/lib/components/Slider.tsx +12 -1
  14. package/lib/components/Tab.react.js.old +79 -0
  15. package/lib/components/Textarea.tsx +79 -0
  16. package/lib/components/Tooltip.tsx +66 -0
  17. package/lib/components/css/checklist.css +4 -0
  18. package/lib/components/css/dcc.css +2 -0
  19. package/lib/components/css/dropdown.css +30 -49
  20. package/lib/components/css/input.css +22 -6
  21. package/lib/components/css/optionslist.css +34 -0
  22. package/lib/components/css/radioitems.css +4 -0
  23. package/lib/components/css/sliders.css +11 -7
  24. package/lib/components/css/textarea.css +21 -0
  25. package/lib/components/css/tooltip.css +127 -0
  26. package/lib/fragments/Dropdown.tsx +205 -269
  27. package/lib/fragments/Loading/spinners/CircleSpinner.tsx +185 -0
  28. package/lib/fragments/Loading/spinners/CubeSpinner.tsx +193 -0
  29. package/lib/fragments/Loading/spinners/DebugTitle.tsx +11 -0
  30. package/lib/fragments/Loading/spinners/DefaultSpinner.tsx +110 -0
  31. package/lib/fragments/Loading/spinners/DotSpinner.tsx +96 -0
  32. package/lib/fragments/Loading/spinners/GraphSpinner.tsx +332 -0
  33. package/lib/fragments/Loading/types.ts +13 -0
  34. package/lib/fragments/RangeSlider.tsx +193 -63
  35. package/lib/index.ts +5 -5
  36. package/lib/types.ts +455 -116
  37. package/lib/utils/dropdownSearch.ts +4 -4
  38. package/lib/utils/optionRendering.tsx +168 -0
  39. package/lib/utils/optionTypes.ts +5 -5
  40. package/lib/utils/sliderRendering.tsx +23 -4
  41. package/package.json +1 -2
  42. package/lib/components/Checklist.react.js +0 -193
  43. package/lib/components/Input.react.js.old +0 -507
  44. package/lib/components/Loading.react.js +0 -254
  45. package/lib/components/RadioItems.react.js +0 -192
  46. package/lib/components/Slider.react.js.old +0 -214
  47. package/lib/components/Textarea.react.js +0 -223
  48. package/lib/components/Tooltip.react.js +0 -120
  49. package/lib/components/css/Dropdown.css +0 -8
  50. package/lib/fragments/Loading/spinners/CircleSpinner.js +0 -68
  51. package/lib/fragments/Loading/spinners/CubeSpinner.js +0 -57
  52. package/lib/fragments/Loading/spinners/DebugTitle.js +0 -15
  53. package/lib/fragments/Loading/spinners/DefaultSpinner.js +0 -54
  54. package/lib/fragments/Loading/spinners/DotSpinner.js +0 -50
  55. package/lib/fragments/Loading/spinners/GraphSpinner.js +0 -68
@@ -17,68 +17,9 @@ import * as Popover from '@radix-ui/react-popover';
17
17
  import '../components/css/dropdown.css';
18
18
 
19
19
  import isEqual from 'react-fast-compare';
20
- import {DetailedDropdownOption, DropdownProps, DropdownValue} from '../types';
21
-
22
- interface DropdownOptionProps {
23
- index: number;
24
- option: DetailedDropdownOption;
25
- isSelected: boolean;
26
- onClick: (option: DetailedDropdownOption) => void;
27
- style?: React.CSSProperties;
28
- }
29
-
30
- function DropdownLabel(
31
- props: DetailedDropdownOption & {index: string | number}
32
- ): JSX.Element {
33
- const ctx = window.dash_component_api.useDashContext();
34
- const ExternalWrapper = window.dash_component_api.ExternalWrapper;
35
-
36
- if (typeof props.label === 'object') {
37
- return (
38
- <ExternalWrapper
39
- component={props.label}
40
- componentPath={[...ctx.componentPath, props.index]}
41
- />
42
- );
43
- }
44
- const displayLabel = `${props.label ?? props.value}`;
45
- return <span title={props.title}>{displayLabel}</span>;
46
- }
47
-
48
- const DropdownOption: React.FC<DropdownOptionProps> = ({
49
- option,
50
- isSelected,
51
- onClick,
52
- style,
53
- index,
54
- }) => {
55
- return (
56
- <label
57
- className={`dash-dropdown-option ${isSelected ? 'selected' : ''}`}
58
- role="option"
59
- aria-selected={isSelected}
60
- style={style}
61
- title={option.title}
62
- >
63
- <input
64
- type="checkbox"
65
- checked={isSelected}
66
- value={
67
- typeof option.value === 'boolean'
68
- ? `${option.value}`
69
- : option.value
70
- }
71
- disabled={!!option.disabled}
72
- onChange={() => onClick(option)}
73
- readOnly
74
- className="dash-dropdown-option-checkbox"
75
- />
76
- <span className="dash-dropdown-option-text">
77
- <DropdownLabel {...option} index={index} />
78
- </span>
79
- </label>
80
- );
81
- };
20
+ import {DetailedOption, DropdownProps, OptionValue} from '../types';
21
+ import {OptionsList, OptionLabel} from '../utils/optionRendering';
22
+ import uuid from 'uniqid';
82
23
 
83
24
  const Dropdown = (props: DropdownProps) => {
84
25
  const {
@@ -87,6 +28,7 @@ const Dropdown = (props: DropdownProps) => {
87
28
  closeOnSelect,
88
29
  clearable,
89
30
  disabled,
31
+ labels,
90
32
  maxHeight,
91
33
  multi,
92
34
  options,
@@ -97,14 +39,14 @@ const Dropdown = (props: DropdownProps) => {
97
39
  style,
98
40
  value,
99
41
  } = props;
100
- const [optionsCheck, setOptionsCheck] =
101
- useState<DetailedDropdownOption[]>();
42
+ const [optionsCheck, setOptionsCheck] = useState<DetailedOption[]>();
102
43
  const [isOpen, setIsOpen] = useState(false);
103
- const [displayOptions, setDisplayOptions] = useState<
104
- DetailedDropdownOption[]
105
- >([]);
44
+ const [displayOptions, setDisplayOptions] = useState<DetailedOption[]>([]);
106
45
  const persistentOptions = useRef<DropdownProps['options']>([]);
107
- const dropdownContainerRef = useRef<HTMLDivElement>(null);
46
+ const dropdownContainerRef = useRef<HTMLButtonElement>(null);
47
+ const dropdownContentRef = useRef<HTMLDivElement>(
48
+ document.createElement('div')
49
+ );
108
50
 
109
51
  const ctx = window.dash_component_api.useDashContext();
110
52
  const loading = ctx.useLoading();
@@ -123,7 +65,7 @@ const Dropdown = (props: DropdownProps) => {
123
65
  [persistentOptions.current, searchable, search_value]
124
66
  );
125
67
 
126
- const sanitizedValues: DropdownValue[] = useMemo(() => {
68
+ const sanitizedValues: OptionValue[] = useMemo(() => {
127
69
  if (value instanceof Array) {
128
70
  return value;
129
71
  }
@@ -133,54 +75,41 @@ const Dropdown = (props: DropdownProps) => {
133
75
  return [value];
134
76
  }, [value]);
135
77
 
136
- const toggleOption = useCallback(
137
- (option: DetailedDropdownOption) => {
138
- const isCurrentlySelected = sanitizedValues.includes(option.value);
139
-
140
- // Close dropdown if closeOnSelect is true (default behavior)
78
+ const updateSelection = useCallback(
79
+ (selection: OptionValue[]) => {
141
80
  if (closeOnSelect !== false) {
142
81
  setIsOpen(false);
143
82
  }
144
83
 
145
84
  if (multi) {
146
- let newValues: DropdownValue[];
147
-
148
- if (isCurrentlySelected) {
149
- // Deselecting: only allow if clearable is true or more than one option selected
150
- if (clearable || sanitizedValues.length > 1) {
151
- newValues = sanitizedValues.filter(
152
- v => v !== option.value
153
- );
154
- } else {
155
- // Cannot deselect the last option when clearable is false
156
- return;
85
+ // For multi-select, validate the selection respects clearable rules
86
+ if (selection.length === 0) {
87
+ // Empty selection: only allow if clearable is true
88
+ if (clearable) {
89
+ setProps({value: []});
157
90
  }
91
+ // If clearable is false and trying to set empty, do nothing
92
+ // return;
158
93
  } else {
159
- // Selecting: add to current selection
160
- newValues = [...sanitizedValues, option.value];
94
+ // Non-empty selection: always allowed in multi-select
95
+ setProps({value: selection});
161
96
  }
162
-
163
- setProps({value: newValues});
164
97
  } else {
165
- let newValue: DropdownValue | null;
166
-
167
- if (isCurrentlySelected) {
168
- // Deselecting: only allow if clearable is true
98
+ // For single-select, take the first value or null
99
+ if (selection.length === 0) {
100
+ // Empty selection: only allow if clearable is true
169
101
  if (clearable) {
170
- newValue = null;
171
- } else {
172
- // Cannot deselect when clearable is false
173
- return;
102
+ setProps({value: null});
174
103
  }
104
+ // If clearable is false and trying to set empty, do nothing
105
+ // return;
175
106
  } else {
176
- // Selecting: set as the single value
177
- newValue = option.value;
107
+ // Take the first value for single-select
108
+ setProps({value: selection[selection.length - 1]});
178
109
  }
179
-
180
- setProps({value: newValue});
181
110
  }
182
111
  },
183
- [multi, clearable, closeOnSelect, sanitizedValues]
112
+ [multi, clearable, closeOnSelect]
184
113
  );
185
114
 
186
115
  const onInputChange = useCallback(
@@ -228,7 +157,7 @@ const Dropdown = (props: DropdownProps) => {
228
157
  );
229
158
  return (
230
159
  <React.Fragment key={`${option?.value}-${i}`}>
231
- {option && <DropdownLabel {...option} index={i} />}
160
+ {option && <OptionLabel {...option} index={i} />}
232
161
  {i === sanitizedValues.length - 1 ? '' : ', '}
233
162
  </React.Fragment>
234
163
  );
@@ -245,13 +174,6 @@ const Dropdown = (props: DropdownProps) => {
245
174
  );
246
175
  }, [clearable, sanitizedValues, displayOptions, search_value]);
247
176
 
248
- const handleOptionClick = useCallback(
249
- (option: DetailedDropdownOption) => {
250
- toggleOption(option);
251
- },
252
- [toggleOption]
253
- );
254
-
255
177
  const handleClear = useCallback(() => {
256
178
  const finalValue: DropdownProps['value'] = multi ? [] : null;
257
179
  setProps({value: finalValue});
@@ -289,23 +211,46 @@ const Dropdown = (props: DropdownProps) => {
289
211
  // Update display options when filtered options or selection changes
290
212
  useEffect(() => {
291
213
  if (isOpen) {
292
- // Sort filtered options: selected first, then unselected
293
- const sortedOptions = [...filteredOptions].sort((a, b) => {
294
- const aSelected = sanitizedValues.includes(a.value);
295
- const bSelected = sanitizedValues.includes(b.value);
214
+ let sortedOptions = filteredOptions;
215
+ if (multi) {
216
+ // Sort filtered options: selected first, then unselected
217
+ sortedOptions = [...filteredOptions].sort((a, b) => {
218
+ const aSelected = sanitizedValues.includes(a.value);
219
+ const bSelected = sanitizedValues.includes(b.value);
296
220
 
297
- if (aSelected && !bSelected) {
298
- return -1;
299
- }
300
- if (!aSelected && bSelected) {
301
- return 1;
302
- }
303
- return 0; // Maintain original order within each group
304
- });
221
+ if (aSelected && !bSelected) {
222
+ return -1;
223
+ }
224
+ if (!aSelected && bSelected) {
225
+ return 1;
226
+ }
227
+ return 0; // Maintain original order within each group
228
+ });
229
+ }
305
230
 
306
231
  setDisplayOptions(sortedOptions);
307
232
  }
308
- }, [filteredOptions, isOpen]); // Removed sanitizedValues to prevent re-sorting on selection changes
233
+ }, [filteredOptions, isOpen]);
234
+
235
+ // Focus (and scroll) the first selected item when dropdown opens
236
+ useEffect(() => {
237
+ if (!isOpen || multi || search_value) {
238
+ return;
239
+ }
240
+
241
+ // waiting for the DOM to be ready after the dropdown renders
242
+ requestAnimationFrame(() => {
243
+ const selectedValue = sanitizedValues[0];
244
+
245
+ const selectedElement = dropdownContentRef.current.querySelector(
246
+ `.dash-options-list-option-checkbox[value="${selectedValue}"]`
247
+ );
248
+
249
+ if (selectedElement instanceof HTMLElement) {
250
+ selectedElement?.focus();
251
+ }
252
+ });
253
+ }, [isOpen, multi, displayOptions, sanitizedValues]);
309
254
 
310
255
  // Handle keyboard navigation in popover
311
256
  const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
@@ -381,10 +326,16 @@ const Dropdown = (props: DropdownProps) => {
381
326
 
382
327
  if (nextIndex > -1) {
383
328
  focusableElements[nextIndex].focus();
384
- focusableElements[nextIndex].scrollIntoView({
385
- behavior: 'auto',
386
- block: 'center',
387
- });
329
+ if (nextIndex === 0) {
330
+ // first element is a sticky search bar, so if we are focusing
331
+ // on that, also move the scroll to the top
332
+ dropdownContentRef.current?.scrollTo({top: 0});
333
+ } else {
334
+ focusableElements[nextIndex].scrollIntoView({
335
+ behavior: 'auto',
336
+ block: 'center',
337
+ });
338
+ }
388
339
  }
389
340
  }, []);
390
341
 
@@ -393,73 +344,61 @@ const Dropdown = (props: DropdownProps) => {
393
344
  (open: boolean) => {
394
345
  setIsOpen(open);
395
346
 
396
- if (open) {
397
- // Sort options: selected first, then unselected
398
- const selectedOptions: DetailedDropdownOption[] = [];
399
- const unselectedOptions: DetailedDropdownOption[] = [];
400
-
401
- // First, collect selected options in the order they appear in the `value` array
402
- sanitizedValues.forEach(value => {
403
- const option = filteredOptions.find(
404
- opt => opt.value === value
405
- );
406
- if (option) {
407
- selectedOptions.push(option);
408
- }
409
- });
410
-
411
- // Then, collect unselected options in the order they appear in `options` array
412
- filteredOptions.forEach(option => {
413
- if (!sanitizedValues.includes(option.value)) {
414
- unselectedOptions.push(option);
415
- }
416
- });
417
- const sortedOptions = [
418
- ...selectedOptions,
419
- ...unselectedOptions,
420
- ];
421
- setDisplayOptions(sortedOptions);
422
- } else {
347
+ if (!open) {
423
348
  setProps({search_value: undefined});
424
349
  }
425
350
  },
426
351
  [filteredOptions, sanitizedValues]
427
352
  );
428
353
 
354
+ const accessibleId = id ?? uuid();
355
+
429
356
  return (
430
- <div
431
- id={id}
432
- ref={dropdownContainerRef}
433
- className={`dash-dropdown ${className ?? ''}`}
434
- style={style}
435
- data-dash-is-loading={loading || undefined}
436
- >
437
- <Popover.Root open={isOpen} onOpenChange={handleOpenChange}>
438
- <Popover.Trigger asChild>
439
- <button
440
- className="dash-dropdown-grid-container dash-dropdown-trigger"
441
- aria-label={props.placeholder}
442
- disabled={disabled}
443
- type="button"
444
- onKeyDown={e => {
445
- if (e.key === 'ArrowDown') {
446
- setIsOpen(true);
447
- }
448
- }}
449
- >
357
+ <Popover.Root open={isOpen} onOpenChange={handleOpenChange}>
358
+ <Popover.Trigger asChild>
359
+ <button
360
+ id={id}
361
+ ref={dropdownContainerRef}
362
+ disabled={disabled}
363
+ type="button"
364
+ onKeyDown={e => {
365
+ if (e.key === 'ArrowDown') {
366
+ setIsOpen(true);
367
+ }
368
+ }}
369
+ className={`dash-dropdown ${className ?? ''}`}
370
+ style={style}
371
+ aria-labelledby={`${accessibleId}-value-count ${accessibleId}-value`}
372
+ aria-haspopup="listbox"
373
+ aria-expanded={isOpen}
374
+ data-dash-is-loading={loading || undefined}
375
+ >
376
+ <span className="dash-dropdown-grid-container dash-dropdown-trigger">
450
377
  {displayValue.length > 0 && (
451
- <span className="dash-dropdown-value">
378
+ <span
379
+ id={accessibleId + '-value'}
380
+ className="dash-dropdown-value"
381
+ >
452
382
  {displayValue}
453
383
  </span>
454
384
  )}
455
385
  {displayValue.length === 0 && (
456
- <span className="dash-dropdown-value dash-dropdown-placeholder">
386
+ <span
387
+ id={accessibleId + '-value'}
388
+ className="dash-dropdown-value dash-dropdown-placeholder"
389
+ >
457
390
  {props.placeholder}
458
391
  </span>
459
392
  )}
460
393
  {sanitizedValues.length > 1 && (
461
- <span className="dash-dropdown-value-count">
462
- {sanitizedValues.length} selected
394
+ <span
395
+ id={accessibleId + '-value-count'}
396
+ className="dash-dropdown-value-count"
397
+ >
398
+ {labels?.selected_count?.replace(
399
+ '{num_selected}',
400
+ `${sanitizedValues.length}`
401
+ )}
463
402
  </span>
464
403
  )}
465
404
  {clearable && !disabled && !!sanitizedValues.length && (
@@ -469,108 +408,105 @@ const Dropdown = (props: DropdownProps) => {
469
408
  e.preventDefault();
470
409
  handleClear();
471
410
  }}
472
- title="Clear selection"
473
- aria-label="Clear selection"
411
+ title={labels?.clear_selection}
412
+ aria-label={labels?.clear_selection}
474
413
  >
475
414
  <Cross1Icon />
476
415
  </a>
477
416
  )}
478
417
 
479
418
  <CaretDownIcon className="dash-dropdown-trigger-icon" />
480
- </button>
481
- </Popover.Trigger>
482
-
483
- <Popover.Portal container={dropdownContainerRef.current}>
484
- <Popover.Content
485
- className="dash-dropdown-content"
486
- align="start"
487
- sideOffset={5}
488
- onOpenAutoFocus={e => e.preventDefault()}
489
- onKeyDown={handleKeyDown}
490
- style={{
491
- maxHeight: maxHeight ? `${maxHeight}px` : 'auto',
492
- }}
493
- >
494
- {searchable && (
495
- <div className="dash-dropdown-grid-container dash-dropdown-search-container">
496
- <MagnifyingGlassIcon className="dash-dropdown-search-icon" />
497
- <input
498
- type="search"
499
- className="dash-dropdown-search"
500
- placeholder="Search"
501
- value={search_value || ''}
502
- autoComplete="off"
503
- onChange={e =>
504
- onInputChange(e.target.value)
505
- }
506
- autoFocus
507
- />
508
- {search_value && (
509
- <button
510
- type="button"
511
- className="dash-dropdown-clear"
512
- onClick={handleClearSearch}
513
- aria-label="Clear search"
514
- >
515
- <Cross1Icon />
516
- </button>
517
- )}
518
- </div>
519
- )}
520
- {multi && (
521
- <div className="dash-dropdown-actions">
419
+ </span>
420
+ </button>
421
+ </Popover.Trigger>
422
+
423
+ <Popover.Portal
424
+ // container is required otherwise popover will be rendered
425
+ // at document root, which may be outside of the Dash app (i.e.
426
+ // an embedded app)
427
+ container={dropdownContainerRef.current?.parentElement}
428
+ >
429
+ <Popover.Content
430
+ ref={dropdownContentRef}
431
+ className="dash-dropdown-content"
432
+ align="start"
433
+ sideOffset={5}
434
+ onOpenAutoFocus={e => e.preventDefault()}
435
+ onKeyDown={handleKeyDown}
436
+ style={{
437
+ maxHeight: maxHeight ? `${maxHeight}px` : 'auto',
438
+ }}
439
+ >
440
+ {searchable && (
441
+ <div className="dash-dropdown-grid-container dash-dropdown-search-container">
442
+ <MagnifyingGlassIcon className="dash-dropdown-search-icon" />
443
+ <input
444
+ type="search"
445
+ className="dash-dropdown-search"
446
+ placeholder={labels?.search}
447
+ value={search_value || ''}
448
+ autoComplete="off"
449
+ onChange={e => onInputChange(e.target.value)}
450
+ autoFocus
451
+ />
452
+ {search_value && (
453
+ <button
454
+ type="button"
455
+ className="dash-dropdown-clear"
456
+ onClick={handleClearSearch}
457
+ aria-label={labels?.clear_search}
458
+ >
459
+ <Cross1Icon />
460
+ </button>
461
+ )}
462
+ </div>
463
+ )}
464
+ {multi && (
465
+ <div className="dash-dropdown-actions">
466
+ <button
467
+ type="button"
468
+ className="dash-dropdown-action-button"
469
+ onClick={handleSelectAll}
470
+ >
471
+ {labels?.select_all}
472
+ </button>
473
+ {canDeselectAll && (
522
474
  <button
523
475
  type="button"
524
476
  className="dash-dropdown-action-button"
525
- onClick={handleSelectAll}
477
+ onClick={handleDeselectAll}
526
478
  >
527
- Select All
479
+ {labels?.deselect_all}
528
480
  </button>
529
- {canDeselectAll && (
530
- <button
531
- type="button"
532
- className="dash-dropdown-action-button"
533
- onClick={handleDeselectAll}
534
- >
535
- Deselect All
536
- </button>
537
- )}
538
- </div>
539
- )}
540
- {isOpen && (
541
- <div className="dash-dropdown-options">
542
- {displayOptions.map((option, i) => {
543
- const isSelected = multi
544
- ? sanitizedValues.includes(option.value)
545
- : value === option.value;
546
-
547
- return (
548
- <DropdownOption
549
- key={`${option.value}-${i}`}
550
- index={i}
551
- option={option}
552
- isSelected={isSelected}
553
- onClick={handleOptionClick}
554
- style={{
555
- height: optionHeight
556
- ? `${optionHeight}px`
557
- : undefined,
558
- }}
559
- />
560
- );
561
- })}
562
- {search_value &&
563
- displayOptions.length === 0 && (
564
- <span className="dash-dropdown-option">
565
- No options found
566
- </span>
567
- )}
568
- </div>
569
- )}
570
- </Popover.Content>
571
- </Popover.Portal>
572
- </Popover.Root>
573
- </div>
481
+ )}
482
+ </div>
483
+ )}
484
+ {isOpen && !!displayOptions.length && (
485
+ <>
486
+ <OptionsList
487
+ options={displayOptions}
488
+ selected={sanitizedValues}
489
+ onSelectionChange={updateSelection}
490
+ className="dash-dropdown-options"
491
+ optionClassName="dash-dropdown-option"
492
+ optionStyle={{
493
+ height: optionHeight
494
+ ? `${optionHeight}px`
495
+ : undefined,
496
+ }}
497
+ />
498
+ </>
499
+ )}
500
+ {isOpen && search_value && !displayOptions.length && (
501
+ <div className="dash-dropdown-options">
502
+ <span className="dash-dropdown-option">
503
+ {labels?.no_options_found}
504
+ </span>
505
+ </div>
506
+ )}
507
+ </Popover.Content>
508
+ </Popover.Portal>
509
+ </Popover.Root>
574
510
  );
575
511
  };
576
512