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,412 @@
1
+ import React, {useEffect, useState, useMemo, useRef} from 'react';
2
+ import * as RadixSlider from '@radix-ui/react-slider';
3
+ import {isNil} from 'ramda';
4
+
5
+ import {
6
+ sanitizeMarks,
7
+ calcStep,
8
+ setUndefined,
9
+ } from '../utils/computeSliderMarkers';
10
+ import {snapToNearestMark} from '../utils/sliderSnapToMark';
11
+ import {renderSliderMarks, renderSliderDots} from '../utils/sliderRendering';
12
+ import LoadingElement from '../utils/_LoadingElement';
13
+ import {Tooltip} from '../utils/sliderTooltip';
14
+ import {RangeSliderProps} from '../types';
15
+
16
+ const MAX_MARKS = 500;
17
+
18
+ /**
19
+ * A double slider with two handles.
20
+ * Used for specifying a range of numerical values.
21
+ */
22
+ export default function RangeSlider(props: RangeSliderProps) {
23
+ const {
24
+ className,
25
+ id,
26
+ setProps,
27
+ tooltip,
28
+ updatemode,
29
+ min,
30
+ max,
31
+ marks,
32
+ step,
33
+ vertical,
34
+ verticalHeight,
35
+ value: propValue,
36
+ disabled,
37
+ dots,
38
+ included,
39
+ allowCross,
40
+ pushable,
41
+ count,
42
+ } = props;
43
+
44
+ // For range slider, we expect an array of values
45
+ const [value, setValue] = useState<number[]>(propValue || []);
46
+
47
+ // Track slider dimension (width for horizontal, height for vertical) for conditional input rendering
48
+ const [sliderWidth, setSliderWidth] = useState<number | null>(null);
49
+ const [showInputs, setShowInputs] = useState<boolean>(value.length === 2);
50
+
51
+ const sliderRef = useRef<HTMLDivElement>(null);
52
+
53
+ // Handle initial mount - equivalent to componentWillMount
54
+ useEffect(() => {
55
+ if (propValue && propValue.length > 0) {
56
+ setProps({drag_value: propValue});
57
+ setValue(propValue);
58
+ } else {
59
+ // Default to range from min to max if no value provided
60
+ const defaultValue = [min ?? (propValue ? propValue[0] : 0)];
61
+ setValue(defaultValue);
62
+ }
63
+ }, []);
64
+
65
+ // Dynamic dimension detection using ResizeObserver (width for horizontal, height for vertical)
66
+ useEffect(() => {
67
+ if (!sliderRef.current) {
68
+ return;
69
+ }
70
+
71
+ if (step === null) {
72
+ // If the user has explicitly disabled stepping (step=None), then
73
+ // the slider values are constrained to the given marks and user
74
+ // cannot enter arbitrary values via the input element
75
+ setShowInputs(false);
76
+ return;
77
+ }
78
+
79
+ if (!value || value.length > 2) {
80
+ setShowInputs(false);
81
+ return;
82
+ }
83
+
84
+ const measureWidth = () => {
85
+ if (sliderRef.current) {
86
+ const rect = sliderRef.current.getBoundingClientRect();
87
+ // Use height for vertical sliders, width for horizontal sliders
88
+ const dimension = vertical ? rect.height : rect.width;
89
+ if (dimension > 0) {
90
+ setSliderWidth(dimension);
91
+
92
+ // eslint-disable-next-line no-magic-numbers
93
+ const HIDE_AT_WIDTH = value.length === 1 ? 200 : 250;
94
+ // eslint-disable-next-line no-magic-numbers
95
+ const SHOW_AT_WIDTH = value.length === 1 ? 300 : 450;
96
+ if (showInputs && dimension < HIDE_AT_WIDTH) {
97
+ setShowInputs(false);
98
+ } else if (!showInputs && dimension >= SHOW_AT_WIDTH) {
99
+ setShowInputs(true);
100
+ }
101
+ }
102
+ }
103
+ };
104
+
105
+ // Initial measurement
106
+ measureWidth();
107
+
108
+ // Set up ResizeObserver for dynamic resizing
109
+ const resizeObserver = new ResizeObserver(() => {
110
+ measureWidth();
111
+ });
112
+
113
+ resizeObserver.observe(sliderRef.current);
114
+
115
+ // Cleanup function when component unmounts
116
+ // eslint-disable-next-line consistent-return
117
+ return () => {
118
+ resizeObserver.disconnect();
119
+ };
120
+ }, [showInputs, vertical, step, value]);
121
+
122
+ // Handle prop value changes - equivalent to componentWillReceiveProps
123
+ useEffect(() => {
124
+ if (propValue && JSON.stringify(propValue) !== JSON.stringify(value)) {
125
+ setProps({drag_value: propValue});
126
+ setValue(propValue);
127
+ }
128
+ }, [propValue]);
129
+
130
+ // Check if marks exceed 500 limit for performance
131
+ let processedMarks = marks;
132
+ if (marks && typeof marks === 'object' && marks !== null) {
133
+ const marksCount = Object.keys(marks).length;
134
+ if (marksCount > MAX_MARKS) {
135
+ /* eslint-disable no-console */
136
+ console.error(
137
+ `Slider: Too many marks (${marksCount}) provided. ` +
138
+ `For performance reasons, marks are limited to 500. ` +
139
+ `Using auto-generated marks instead.`
140
+ );
141
+ processedMarks = undefined;
142
+ }
143
+ }
144
+
145
+ const minMaxValues = useMemo(() => {
146
+ return setUndefined(min, max, processedMarks);
147
+ }, [min, max, processedMarks]);
148
+
149
+ const stepValue = useMemo(() => {
150
+ return step === null && isNil(processedMarks)
151
+ ? undefined
152
+ : calcStep(min, max, step);
153
+ }, [min, max, processedMarks, step]);
154
+
155
+ // Sanitize marks for rendering
156
+ const renderedMarks = useMemo(() => {
157
+ if (processedMarks === null) {
158
+ return null;
159
+ }
160
+ return sanitizeMarks({
161
+ min,
162
+ max,
163
+ marks: processedMarks,
164
+ step,
165
+ sliderWidth,
166
+ });
167
+ }, [min, max, processedMarks, step, sliderWidth]);
168
+
169
+ const handleValueChange = (newValue: number[]) => {
170
+ let adjustedValue = newValue;
171
+
172
+ // Snap to nearest marks if step is null and marks exist
173
+ if (
174
+ step === null &&
175
+ processedMarks &&
176
+ typeof processedMarks === 'object'
177
+ ) {
178
+ const marks = processedMarks;
179
+ adjustedValue = newValue.map(val => snapToNearestMark(val, marks));
180
+ }
181
+
182
+ setValue(adjustedValue);
183
+ if (updatemode === 'drag') {
184
+ setProps({value: adjustedValue, drag_value: adjustedValue});
185
+ } else {
186
+ setProps({drag_value: adjustedValue});
187
+ }
188
+ };
189
+
190
+ const handleValueCommit = (newValue: number[]) => {
191
+ if (updatemode === 'mouseup') {
192
+ setProps({value: newValue});
193
+ }
194
+ };
195
+
196
+ return (
197
+ <LoadingElement>
198
+ {loadingProps => (
199
+ <div
200
+ id={id}
201
+ className="dash-slider-container"
202
+ {...loadingProps}
203
+ >
204
+ {showInputs && value.length === 2 && !vertical && (
205
+ <input
206
+ type="number"
207
+ className="dash-input-container dash-range-slider-input dash-range-slider-min-input"
208
+ value={value[0] ?? ''}
209
+ onChange={e => {
210
+ const inputValue = e.target.value;
211
+ // Allow empty string (user is clearing the field)
212
+ if (inputValue === '') {
213
+ // Don't update props while user is typing, just update local state
214
+ setValue([null as any, value[1]]);
215
+ } else {
216
+ const newMin = parseFloat(inputValue);
217
+ if (!isNaN(newMin)) {
218
+ const newValue = [newMin, value[1]];
219
+ setValue(newValue);
220
+ if (updatemode === 'drag') {
221
+ setProps({
222
+ value: newValue,
223
+ drag_value: newValue,
224
+ });
225
+ } else {
226
+ setProps({drag_value: newValue});
227
+ }
228
+ }
229
+ }
230
+ }}
231
+ onBlur={e => {
232
+ const inputValue = e.target.value;
233
+ let newMin: number;
234
+
235
+ // If empty, default to current value or min_mark
236
+ if (inputValue === '') {
237
+ newMin = value[0] ?? minMaxValues.min_mark;
238
+ } else {
239
+ newMin = parseFloat(inputValue);
240
+ newMin = isNaN(newMin)
241
+ ? minMaxValues.min_mark
242
+ : newMin;
243
+ }
244
+
245
+ const constrainedMin = Math.max(
246
+ minMaxValues.min_mark,
247
+ Math.min(
248
+ value[1] ?? minMaxValues.max_mark,
249
+ newMin
250
+ )
251
+ );
252
+ const newValue = [constrainedMin, value[1]];
253
+ setValue(newValue);
254
+ if (updatemode === 'mouseup') {
255
+ setProps({value: newValue});
256
+ }
257
+ }}
258
+ pattern="^\\d*\\.?\\d*$"
259
+ min={minMaxValues.min_mark}
260
+ max={value[1]}
261
+ step={step || undefined}
262
+ disabled={disabled}
263
+ />
264
+ )}
265
+ {showInputs && !vertical && (
266
+ <input
267
+ type="number"
268
+ className="dash-input-container dash-range-slider-input dash-range-slider-max-input"
269
+ value={value[value.length - 1] ?? ''}
270
+ onChange={e => {
271
+ const inputValue = e.target.value;
272
+ // Allow empty string (user is clearing the field)
273
+ if (inputValue === '') {
274
+ // Don't update props while user is typing, just update local state
275
+ const newValue = [...value];
276
+ newValue[newValue.length - 1] = '' as any;
277
+ setValue(newValue);
278
+ } else {
279
+ const newMax = parseFloat(inputValue);
280
+ const constrainedMax = Math.max(
281
+ minMaxValues.min_mark,
282
+ Math.min(minMaxValues.max_mark, newMax)
283
+ );
284
+
285
+ if (newMax === constrainedMax) {
286
+ const newValue = [...value];
287
+ newValue[newValue.length - 1] = newMax;
288
+ setProps({
289
+ value: newValue,
290
+ drag_value: newValue,
291
+ });
292
+ }
293
+ }
294
+ }}
295
+ onBlur={e => {
296
+ const inputValue = e.target.value;
297
+ let newMax: number;
298
+
299
+ // If empty, default to current value or max_mark
300
+ if (inputValue === '') {
301
+ newMax =
302
+ value[value.length - 1] ??
303
+ minMaxValues.max_mark;
304
+ } else {
305
+ newMax = parseFloat(inputValue);
306
+ newMax = isNaN(newMax)
307
+ ? minMaxValues.max_mark
308
+ : newMax;
309
+ }
310
+
311
+ const constrainedMax = Math.min(
312
+ minMaxValues.max_mark,
313
+ Math.max(
314
+ value[0] ?? minMaxValues.min_mark,
315
+ newMax
316
+ )
317
+ );
318
+ const newValue = [...value];
319
+ newValue[newValue.length - 1] = constrainedMax;
320
+ setValue(newValue);
321
+ if (updatemode === 'mouseup') {
322
+ setProps({value: newValue});
323
+ }
324
+ }}
325
+ pattern="^\\d*\\.?\\d*$"
326
+ min={
327
+ value.length === 1
328
+ ? minMaxValues.min_mark
329
+ : value[0]
330
+ }
331
+ max={minMaxValues.max_mark}
332
+ step={step || undefined}
333
+ disabled={disabled}
334
+ />
335
+ )}
336
+ <div
337
+ className="dash-slider-wrapper"
338
+ onClickCapture={e => e.preventDefault()} // prevent interactions from "clicking" the parent, particularly when slider is inside a label tag
339
+ >
340
+ <RadixSlider.Root
341
+ ref={sliderRef}
342
+ className={`dash-slider-root ${
343
+ renderedMarks ? 'has-marks' : ''
344
+ } ${className || ''}`.trim()}
345
+ style={{
346
+ ...(vertical && {
347
+ height: `${verticalHeight}px`,
348
+ }),
349
+ }}
350
+ value={value}
351
+ onValueChange={handleValueChange}
352
+ onValueCommit={handleValueCommit}
353
+ min={minMaxValues.min_mark}
354
+ max={minMaxValues.max_mark}
355
+ step={stepValue}
356
+ disabled={disabled}
357
+ orientation={vertical ? 'vertical' : 'horizontal'}
358
+ data-included={included !== false}
359
+ minStepsBetweenThumbs={
360
+ typeof pushable === 'number'
361
+ ? pushable
362
+ : undefined
363
+ }
364
+ >
365
+ <RadixSlider.Track className="dash-slider-track">
366
+ {included !== false && (
367
+ <RadixSlider.Range className="dash-slider-range" />
368
+ )}
369
+ </RadixSlider.Track>
370
+ {renderedMarks &&
371
+ renderSliderMarks(
372
+ renderedMarks,
373
+ !!vertical,
374
+ minMaxValues,
375
+ !!dots
376
+ )}
377
+ {dots &&
378
+ stepValue &&
379
+ renderSliderDots(
380
+ stepValue,
381
+ minMaxValues,
382
+ !!vertical
383
+ )}
384
+ {/* Render thumbs with tooltips for each value */}
385
+ {value.map((val, index) => {
386
+ const thumbClassName = `dash-slider-thumb dash-slider-thumb-${
387
+ index + 1
388
+ }`;
389
+
390
+ return (
391
+ <RadixSlider.Thumb
392
+ key={'thumb' + index}
393
+ className={thumbClassName}
394
+ >
395
+ {tooltip && (
396
+ <Tooltip
397
+ id={id}
398
+ index={index}
399
+ value={val}
400
+ tooltip={tooltip}
401
+ />
402
+ )}
403
+ </RadixSlider.Thumb>
404
+ );
405
+ })}
406
+ </RadixSlider.Root>
407
+ </div>
408
+ </div>
409
+ )}
410
+ </LoadingElement>
411
+ );
412
+ }
package/lib/index.ts ADDED
@@ -0,0 +1,58 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import Checklist from './components/Checklist.react';
3
+ import Clipboard from './components/Clipboard.react';
4
+ import ConfirmDialog from './components/ConfirmDialog.react';
5
+ import ConfirmDialogProvider from './components/ConfirmDialogProvider.react';
6
+ import DatePickerRange from './components/DatePickerRange.react';
7
+ import DatePickerSingle from './components/DatePickerSingle.react';
8
+ import Download from './components/Download.react';
9
+ import Dropdown from './components/Dropdown';
10
+ import Geolocation from './components/Geolocation.react';
11
+ import Graph from './components/Graph.react';
12
+ import Input from './components/Input';
13
+ import Interval from './components/Interval.react';
14
+ import Link from './components/Link.react';
15
+ import Loading from './components/Loading.react';
16
+ import Location from './components/Location.react';
17
+ import Markdown from './components/Markdown.react';
18
+ import RadioItems from './components/RadioItems.react';
19
+ import RangeSlider from './components/RangeSlider';
20
+ import Slider from './components/Slider';
21
+ import Store from './components/Store.react';
22
+ import Tab from './components/Tab.react';
23
+ import Tabs from './components/Tabs.react';
24
+ import Textarea from './components/Textarea.react';
25
+ import Tooltip from './components/Tooltip.react';
26
+ import Upload from './components/Upload.react';
27
+
28
+ import './components/css/dcc.css';
29
+ import 'react-dates/lib/css/_datepicker.css';
30
+ import './components/css/react-dates@20.1.0-fix.css';
31
+
32
+ export {
33
+ Checklist,
34
+ Clipboard,
35
+ ConfirmDialog,
36
+ ConfirmDialogProvider,
37
+ DatePickerRange,
38
+ DatePickerSingle,
39
+ Download,
40
+ Dropdown,
41
+ Geolocation,
42
+ Graph,
43
+ Interval,
44
+ Input,
45
+ Link,
46
+ Loading,
47
+ Location,
48
+ Markdown,
49
+ RadioItems,
50
+ RangeSlider,
51
+ Slider,
52
+ Store,
53
+ Tab,
54
+ Tabs,
55
+ Textarea,
56
+ Tooltip,
57
+ Upload,
58
+ };