dash-core-components 3.2.0 → 4.0.0-rc0

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 (50) 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-highlight.js +1 -1
  6. package/dash_core_components/async-highlight.js.map +1 -1
  7. package/dash_core_components/async-markdown.js +1 -1
  8. package/dash_core_components/async-markdown.js.map +1 -1
  9. package/dash_core_components/async-slider.js +1 -1
  10. package/dash_core_components/async-slider.js.map +1 -1
  11. package/dash_core_components/dash_core_components-shared.js +1 -1
  12. package/dash_core_components/dash_core_components-shared.js.map +1 -1
  13. package/dash_core_components/dash_core_components.js +1 -1
  14. package/dash_core_components/dash_core_components.js.map +1 -1
  15. package/dash_core_components/plotly.min.js +8 -0
  16. package/dash_core_components/proptypes.js +81 -0
  17. package/lib/components/Dropdown.react.js.old +228 -0
  18. package/lib/components/Input.react.js.old +507 -0
  19. package/lib/components/Input.tsx +543 -0
  20. package/lib/components/RangeSlider.tsx +39 -0
  21. package/lib/components/Slider.react.js.old +214 -0
  22. package/lib/components/Slider.tsx +81 -0
  23. package/lib/components/css/dcc.css +13 -0
  24. package/lib/components/css/input.css +116 -2
  25. package/lib/components/css/react-select@1.0.0-rc.3.min.css +1 -1
  26. package/lib/components/css/sliders.css +198 -4
  27. package/lib/fragments/RangeSlider.tsx +412 -0
  28. package/lib/fragments/Slider.tsx.old +364 -0
  29. package/lib/index.ts +58 -0
  30. package/lib/types.ts +331 -0
  31. package/lib/utils/LazyLoader/rangeSlider.ts +2 -0
  32. package/lib/utils/LazyLoader/slider.ts +2 -0
  33. package/lib/utils/_LoadingElement.tsx +36 -0
  34. package/lib/utils/computeSliderMarkers.ts +302 -0
  35. package/lib/utils/formatSliderTooltip.ts +20 -0
  36. package/lib/utils/sliderRendering.tsx +144 -0
  37. package/lib/utils/sliderSnapToMark.ts +17 -0
  38. package/lib/utils/sliderTooltip.tsx +65 -0
  39. package/package.json +22 -4
  40. package/lib/components/Input.react.js +0 -403
  41. package/lib/components/RangeSlider.react.js +0 -194
  42. package/lib/components/Slider.react.js +0 -179
  43. package/lib/fragments/RangeSlider.react.js +0 -143
  44. package/lib/fragments/Slider.react.js +0 -159
  45. package/lib/index.js +0 -183
  46. package/lib/utils/LazyLoader/rangeSlider.js +0 -8
  47. package/lib/utils/LazyLoader/slider.js +0 -8
  48. package/lib/utils/computeSliderMarkers.js +0 -153
  49. package/lib/utils/computeSliderStyle.js +0 -26
  50. 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
+ <div
266
+ className="dash-slider-wrapper"
267
+ onClickCapture={e => e.preventDefault()} // prevent interactions from "clicking" the parent, particularly when slider is inside a label tag
268
+ >
269
+ <RadixSlider.Root
270
+ ref={sliderRef}
271
+ className={`dash-slider-root ${
272
+ renderedMarks ? 'has-marks' : ''
273
+ } ${className || ''}`.trim()}
274
+ style={{
275
+ ...(vertical && {
276
+ height: `${verticalHeight}px`,
277
+ }),
278
+ }}
279
+ value={value}
280
+ onValueChange={handleValueChange}
281
+ onValueCommit={handleValueCommit}
282
+ min={minMaxValues.min_mark}
283
+ max={minMaxValues.max_mark}
284
+ step={stepValue}
285
+ disabled={disabled}
286
+ orientation={vertical ? 'vertical' : 'horizontal'}
287
+ data-included={included !== false}
288
+ minStepsBetweenThumbs={
289
+ typeof pushable === 'number'
290
+ ? pushable
291
+ : undefined
292
+ }
293
+ >
294
+ <RadixSlider.Track className="dash-slider-track">
295
+ {included !== false && (
296
+ <RadixSlider.Range className="dash-slider-range" />
297
+ )}
298
+ </RadixSlider.Track>
299
+ {renderedMarks &&
300
+ renderSliderMarks(
301
+ renderedMarks,
302
+ !!vertical,
303
+ minMaxValues,
304
+ !!dots
305
+ )}
306
+ {dots &&
307
+ stepValue &&
308
+ renderSliderDots(
309
+ stepValue,
310
+ minMaxValues,
311
+ !!vertical
312
+ )}
313
+ {/* Render thumbs with tooltips for each value */}
314
+ {value.map((val, index) => {
315
+ const thumbClassName = `dash-slider-thumb dash-slider-thumb-${
316
+ index + 1
317
+ }`;
318
+
319
+ return (
320
+ <RadixSlider.Thumb
321
+ key={'thumb' + index}
322
+ className={thumbClassName}
323
+ >
324
+ {tooltip && (
325
+ <Tooltip
326
+ id={id}
327
+ index={index}
328
+ value={val}
329
+ tooltip={tooltip}
330
+ />
331
+ )}
332
+ </RadixSlider.Thumb>
333
+ );
334
+ })}
335
+ </RadixSlider.Root>
336
+ </div>
337
+ {showInputs && !vertical && (
338
+ <input
339
+ type="number"
340
+ className="dash-input-container dash-range-slider-input"
341
+ value={value[value.length - 1] ?? ''}
342
+ onChange={e => {
343
+ const inputValue = e.target.value;
344
+ // Allow empty string (user is clearing the field)
345
+ if (inputValue === '') {
346
+ // Don't update props while user is typing, just update local state
347
+ const newValue = [...value];
348
+ newValue[newValue.length - 1] = '' as any;
349
+ setValue(newValue);
350
+ } else {
351
+ const newMax = parseFloat(inputValue);
352
+ const constrainedMax = Math.max(
353
+ minMaxValues.min_mark,
354
+ Math.min(minMaxValues.max_mark, newMax)
355
+ );
356
+
357
+ if (newMax === constrainedMax) {
358
+ const newValue = [...value];
359
+ newValue[newValue.length - 1] = newMax;
360
+ setProps({
361
+ value: newValue,
362
+ drag_value: newValue,
363
+ });
364
+ }
365
+ }
366
+ }}
367
+ onBlur={e => {
368
+ const inputValue = e.target.value;
369
+ let newMax: number;
370
+
371
+ // If empty, default to current value or max_mark
372
+ if (inputValue === '') {
373
+ newMax =
374
+ value[value.length - 1] ??
375
+ minMaxValues.max_mark;
376
+ } else {
377
+ newMax = parseFloat(inputValue);
378
+ newMax = isNaN(newMax)
379
+ ? minMaxValues.max_mark
380
+ : newMax;
381
+ }
382
+
383
+ const constrainedMax = Math.min(
384
+ minMaxValues.max_mark,
385
+ Math.max(
386
+ value[0] ?? minMaxValues.min_mark,
387
+ newMax
388
+ )
389
+ );
390
+ const newValue = [...value];
391
+ newValue[newValue.length - 1] = constrainedMax;
392
+ setValue(newValue);
393
+ if (updatemode === 'mouseup') {
394
+ setProps({value: newValue});
395
+ }
396
+ }}
397
+ pattern="^\\d*\\.?\\d*$"
398
+ min={
399
+ value.length === 1
400
+ ? minMaxValues.min_mark
401
+ : value[0]
402
+ }
403
+ max={minMaxValues.max_mark}
404
+ step={step || undefined}
405
+ disabled={disabled}
406
+ />
407
+ )}
408
+ </div>
409
+ )}
410
+ </LoadingElement>
411
+ );
412
+ }