dash-core-components 3.2.0 → 4.0.0-rc1

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 (60) 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-highlight.js +1 -1
  7. package/dash_core_components/async-highlight.js.map +1 -1
  8. package/dash_core_components/async-markdown.js +1 -1
  9. package/dash_core_components/async-markdown.js.map +1 -1
  10. package/dash_core_components/async-slider.js +1 -2
  11. package/dash_core_components/async-slider.js.map +1 -1
  12. package/dash_core_components/dash_core_components-shared.js +1 -1
  13. package/dash_core_components/dash_core_components-shared.js.map +1 -1
  14. package/dash_core_components/dash_core_components.js +1 -1
  15. package/dash_core_components/dash_core_components.js.map +1 -1
  16. package/dash_core_components/plotly.min.js +8 -0
  17. package/dash_core_components/proptypes.js +99 -0
  18. package/lib/components/Dropdown.react.js.old +228 -0
  19. package/lib/components/Dropdown.tsx +52 -0
  20. package/lib/components/Input.react.js.old +507 -0
  21. package/lib/components/Input.tsx +543 -0
  22. package/lib/components/RangeSlider.tsx +46 -0
  23. package/lib/components/Slider.react.js.old +214 -0
  24. package/lib/components/Slider.tsx +81 -0
  25. package/lib/components/Textarea.react.js.old +287 -0
  26. package/lib/components/css/dcc.css +14 -0
  27. package/lib/components/css/dropdown.css +230 -0
  28. package/lib/components/css/input.css +116 -2
  29. package/lib/components/css/react-select@1.0.0-rc.3.min.css +1 -1
  30. package/lib/components/css/sliders.css +206 -4
  31. package/lib/fragments/Dropdown.tsx +577 -0
  32. package/lib/fragments/RangeSlider.tsx +412 -0
  33. package/lib/index.ts +58 -0
  34. package/lib/types.ts +499 -0
  35. package/lib/utils/LazyLoader/dropdown.ts +2 -0
  36. package/lib/utils/LazyLoader/rangeSlider.ts +2 -0
  37. package/lib/utils/LazyLoader/slider.ts +2 -0
  38. package/lib/utils/_LoadingElement.tsx +26 -0
  39. package/lib/utils/computeSliderMarkers.ts +302 -0
  40. package/lib/utils/dropdownSearch.ts +92 -0
  41. package/lib/utils/formatSliderTooltip.ts +20 -0
  42. package/lib/utils/optionTypes.ts +27 -0
  43. package/lib/utils/sliderRendering.tsx +144 -0
  44. package/lib/utils/sliderSnapToMark.ts +17 -0
  45. package/lib/utils/sliderTooltip.tsx +65 -0
  46. package/package.json +27 -5
  47. package/lib/components/Dropdown.react.js +0 -182
  48. package/lib/components/Input.react.js +0 -403
  49. package/lib/components/RangeSlider.react.js +0 -194
  50. package/lib/components/Slider.react.js +0 -179
  51. package/lib/fragments/Dropdown.react.js +0 -158
  52. package/lib/fragments/RangeSlider.react.js +0 -143
  53. package/lib/fragments/Slider.react.js +0 -159
  54. package/lib/index.js +0 -183
  55. package/lib/utils/LazyLoader/dropdown.js +0 -8
  56. package/lib/utils/LazyLoader/rangeSlider.js +0 -8
  57. package/lib/utils/LazyLoader/slider.js +0 -8
  58. package/lib/utils/computeSliderMarkers.js +0 -153
  59. package/lib/utils/computeSliderStyle.js +0 -26
  60. package/lib/utils/formatSliderTooltip.js +0 -19
@@ -0,0 +1,577 @@
1
+ import {isNil, without, isEmpty} from 'ramda';
2
+ import React, {
3
+ useState,
4
+ useCallback,
5
+ useEffect,
6
+ useMemo,
7
+ useRef,
8
+ MouseEvent,
9
+ } from 'react';
10
+ import {createFilteredOptions} from '../utils/dropdownSearch';
11
+ import {
12
+ CaretDownIcon,
13
+ MagnifyingGlassIcon,
14
+ Cross1Icon,
15
+ } from '@radix-ui/react-icons';
16
+ import * as Popover from '@radix-ui/react-popover';
17
+ import '../components/css/dropdown.css';
18
+
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
+ };
82
+
83
+ const Dropdown = (props: DropdownProps) => {
84
+ const {
85
+ id,
86
+ className,
87
+ closeOnSelect,
88
+ clearable,
89
+ disabled,
90
+ maxHeight,
91
+ multi,
92
+ options,
93
+ optionHeight,
94
+ setProps,
95
+ searchable,
96
+ search_value,
97
+ style,
98
+ value,
99
+ } = props;
100
+ const [optionsCheck, setOptionsCheck] =
101
+ useState<DetailedDropdownOption[]>();
102
+ const [isOpen, setIsOpen] = useState(false);
103
+ const [displayOptions, setDisplayOptions] = useState<
104
+ DetailedDropdownOption[]
105
+ >([]);
106
+ const persistentOptions = useRef<DropdownProps['options']>([]);
107
+ const dropdownContainerRef = useRef<HTMLDivElement>(null);
108
+
109
+ const ctx = window.dash_component_api.useDashContext();
110
+ const loading = ctx.useLoading();
111
+
112
+ if (!persistentOptions || !isEqual(options, persistentOptions.current)) {
113
+ persistentOptions.current = options;
114
+ }
115
+
116
+ const {sanitizedOptions, filteredOptions} = useMemo(
117
+ () =>
118
+ createFilteredOptions(
119
+ persistentOptions.current,
120
+ !!searchable,
121
+ search_value
122
+ ),
123
+ [persistentOptions.current, searchable, search_value]
124
+ );
125
+
126
+ const sanitizedValues: DropdownValue[] = useMemo(() => {
127
+ if (value instanceof Array) {
128
+ return value;
129
+ }
130
+ if (isNil(value)) {
131
+ return [];
132
+ }
133
+ return [value];
134
+ }, [value]);
135
+
136
+ const toggleOption = useCallback(
137
+ (option: DetailedDropdownOption) => {
138
+ const isCurrentlySelected = sanitizedValues.includes(option.value);
139
+
140
+ // Close dropdown if closeOnSelect is true (default behavior)
141
+ if (closeOnSelect !== false) {
142
+ setIsOpen(false);
143
+ }
144
+
145
+ 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;
157
+ }
158
+ } else {
159
+ // Selecting: add to current selection
160
+ newValues = [...sanitizedValues, option.value];
161
+ }
162
+
163
+ setProps({value: newValues});
164
+ } else {
165
+ let newValue: DropdownValue | null;
166
+
167
+ if (isCurrentlySelected) {
168
+ // Deselecting: only allow if clearable is true
169
+ if (clearable) {
170
+ newValue = null;
171
+ } else {
172
+ // Cannot deselect when clearable is false
173
+ return;
174
+ }
175
+ } else {
176
+ // Selecting: set as the single value
177
+ newValue = option.value;
178
+ }
179
+
180
+ setProps({value: newValue});
181
+ }
182
+ },
183
+ [multi, clearable, closeOnSelect, sanitizedValues]
184
+ );
185
+
186
+ const onInputChange = useCallback(
187
+ search_value => setProps({search_value}),
188
+ []
189
+ );
190
+
191
+ const handleClearSearch = useCallback((e: MouseEvent) => {
192
+ if (e.currentTarget instanceof HTMLElement) {
193
+ const parentElement = e.currentTarget.parentElement;
194
+ parentElement?.querySelector('input')?.focus();
195
+ }
196
+ setProps({search_value: undefined});
197
+ }, []);
198
+
199
+ useEffect(() => {
200
+ if (
201
+ !search_value &&
202
+ !isNil(sanitizedOptions) &&
203
+ optionsCheck !== sanitizedOptions &&
204
+ !isNil(value) &&
205
+ !isEmpty(value)
206
+ ) {
207
+ const values = sanitizedOptions.map(option => option.value);
208
+ if (Array.isArray(value)) {
209
+ if (multi) {
210
+ const invalids = value.filter(v => !values.includes(v));
211
+ if (invalids.length) {
212
+ setProps({value: without(invalids, value)});
213
+ }
214
+ }
215
+ } else {
216
+ if (!values.includes(value)) {
217
+ setProps({value: null});
218
+ }
219
+ }
220
+ setOptionsCheck(sanitizedOptions);
221
+ }
222
+ }, [sanitizedOptions, optionsCheck, multi, value]);
223
+
224
+ const displayValue = useMemo(() => {
225
+ const labels = sanitizedValues.map((val, i) => {
226
+ const option = sanitizedOptions.find(
227
+ option => option.value === val
228
+ );
229
+ return (
230
+ <React.Fragment key={`${option?.value}-${i}`}>
231
+ {option && <DropdownLabel {...option} index={i} />}
232
+ {i === sanitizedValues.length - 1 ? '' : ', '}
233
+ </React.Fragment>
234
+ );
235
+ });
236
+ return labels;
237
+ }, [sanitizedOptions, sanitizedValues]);
238
+
239
+ const canDeselectAll = useMemo(() => {
240
+ if (clearable) {
241
+ return true;
242
+ }
243
+ return !sanitizedValues.every(value =>
244
+ displayOptions.some(option => option.value === value)
245
+ );
246
+ }, [clearable, sanitizedValues, displayOptions, search_value]);
247
+
248
+ const handleOptionClick = useCallback(
249
+ (option: DetailedDropdownOption) => {
250
+ toggleOption(option);
251
+ },
252
+ [toggleOption]
253
+ );
254
+
255
+ const handleClear = useCallback(() => {
256
+ const finalValue: DropdownProps['value'] = multi ? [] : null;
257
+ setProps({value: finalValue});
258
+ }, [multi]);
259
+
260
+ const handleSelectAll = useCallback(() => {
261
+ if (multi) {
262
+ const allValues = sanitizedValues.concat(
263
+ displayOptions
264
+ .filter(option => !sanitizedValues.includes(option.value))
265
+ .map(option => option.value)
266
+ );
267
+ setProps({value: allValues});
268
+ }
269
+ if (closeOnSelect) {
270
+ setIsOpen(false);
271
+ }
272
+ }, [multi, displayOptions, sanitizedValues, closeOnSelect]);
273
+
274
+ const handleDeselectAll = useCallback(() => {
275
+ if (multi) {
276
+ const withDeselected = sanitizedValues.filter(option => {
277
+ return !displayOptions.some(
278
+ displayOption => displayOption.value === option
279
+ );
280
+ });
281
+ setProps({value: withDeselected});
282
+ }
283
+ if (closeOnSelect) {
284
+ setIsOpen(false);
285
+ }
286
+ }, [multi, displayOptions, sanitizedValues, closeOnSelect]);
287
+
288
+ // Sort options when popover opens - selected options first
289
+ // Update display options when filtered options or selection changes
290
+ useEffect(() => {
291
+ 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);
296
+
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
+ });
305
+
306
+ setDisplayOptions(sortedOptions);
307
+ }
308
+ }, [filteredOptions, isOpen]); // Removed sanitizedValues to prevent re-sorting on selection changes
309
+
310
+ // Handle keyboard navigation in popover
311
+ const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
312
+ const relevantKeys = [
313
+ 'ArrowDown',
314
+ 'ArrowUp',
315
+ 'PageDown',
316
+ 'PageUp',
317
+ 'Home',
318
+ 'End',
319
+ ];
320
+ if (!relevantKeys.includes(e.key)) {
321
+ return;
322
+ }
323
+
324
+ // Don't interfere with the event if the user is using Home/End keys on the search input
325
+ if (
326
+ ['Home', 'End'].includes(e.key) &&
327
+ document.activeElement instanceof HTMLInputElement
328
+ ) {
329
+ return;
330
+ }
331
+
332
+ const focusableElements = e.currentTarget.querySelectorAll(
333
+ 'input[type="search"], input[type="checkbox"]:not([disabled])'
334
+ ) as NodeListOf<HTMLElement>;
335
+
336
+ // Don't interfere with the event if there aren't any options that the user can interact with
337
+ if (focusableElements.length === 0) {
338
+ return;
339
+ }
340
+
341
+ e.preventDefault();
342
+
343
+ const currentIndex = Array.from(focusableElements).indexOf(
344
+ document.activeElement as HTMLElement
345
+ );
346
+ let nextIndex = -1;
347
+
348
+ switch (e.key) {
349
+ case 'ArrowDown':
350
+ nextIndex =
351
+ currentIndex < focusableElements.length - 1
352
+ ? currentIndex + 1
353
+ : 0;
354
+ break;
355
+
356
+ case 'ArrowUp':
357
+ nextIndex =
358
+ currentIndex > 0
359
+ ? currentIndex - 1
360
+ : focusableElements.length - 1;
361
+
362
+ break;
363
+ case 'PageDown':
364
+ nextIndex = Math.min(
365
+ currentIndex + 10,
366
+ focusableElements.length - 1
367
+ );
368
+ break;
369
+ case 'PageUp':
370
+ nextIndex = Math.max(currentIndex - 10, 0);
371
+ break;
372
+ case 'Home':
373
+ nextIndex = 0;
374
+ break;
375
+ case 'End':
376
+ nextIndex = focusableElements.length - 1;
377
+ break;
378
+ default:
379
+ break;
380
+ }
381
+
382
+ if (nextIndex > -1) {
383
+ focusableElements[nextIndex].focus();
384
+ focusableElements[nextIndex].scrollIntoView({
385
+ behavior: 'auto',
386
+ block: 'center',
387
+ });
388
+ }
389
+ }, []);
390
+
391
+ // Handle popover open/close
392
+ const handleOpenChange = useCallback(
393
+ (open: boolean) => {
394
+ setIsOpen(open);
395
+
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 {
423
+ setProps({search_value: undefined});
424
+ }
425
+ },
426
+ [filteredOptions, sanitizedValues]
427
+ );
428
+
429
+ 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
+ >
450
+ {displayValue.length > 0 && (
451
+ <span className="dash-dropdown-value">
452
+ {displayValue}
453
+ </span>
454
+ )}
455
+ {displayValue.length === 0 && (
456
+ <span className="dash-dropdown-value dash-dropdown-placeholder">
457
+ {props.placeholder}
458
+ </span>
459
+ )}
460
+ {sanitizedValues.length > 1 && (
461
+ <span className="dash-dropdown-value-count">
462
+ {sanitizedValues.length} selected
463
+ </span>
464
+ )}
465
+ {clearable && !disabled && !!sanitizedValues.length && (
466
+ <a
467
+ className="dash-dropdown-clear"
468
+ onClick={e => {
469
+ e.preventDefault();
470
+ handleClear();
471
+ }}
472
+ title="Clear selection"
473
+ aria-label="Clear selection"
474
+ >
475
+ <Cross1Icon />
476
+ </a>
477
+ )}
478
+
479
+ <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">
522
+ <button
523
+ type="button"
524
+ className="dash-dropdown-action-button"
525
+ onClick={handleSelectAll}
526
+ >
527
+ Select All
528
+ </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>
574
+ );
575
+ };
576
+
577
+ export default Dropdown;