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.
- package/dash_core_components/async-datepicker.js +1 -1
- package/dash_core_components/async-datepicker.js.map +1 -1
- package/dash_core_components/async-dropdown.js +1 -1
- package/dash_core_components/async-dropdown.js.map +1 -1
- package/dash_core_components/async-graph.js +1 -1
- package/dash_core_components/async-highlight.js +1 -1
- package/dash_core_components/async-highlight.js.map +1 -1
- package/dash_core_components/async-markdown.js +1 -1
- package/dash_core_components/async-markdown.js.map +1 -1
- package/dash_core_components/async-slider.js +1 -2
- package/dash_core_components/async-slider.js.map +1 -1
- package/dash_core_components/dash_core_components-shared.js +1 -1
- package/dash_core_components/dash_core_components-shared.js.map +1 -1
- package/dash_core_components/dash_core_components.js +1 -1
- package/dash_core_components/dash_core_components.js.map +1 -1
- package/dash_core_components/plotly.min.js +8 -0
- package/dash_core_components/proptypes.js +99 -0
- package/lib/components/Dropdown.react.js.old +228 -0
- package/lib/components/Dropdown.tsx +52 -0
- package/lib/components/Input.react.js.old +507 -0
- package/lib/components/Input.tsx +543 -0
- package/lib/components/RangeSlider.tsx +46 -0
- package/lib/components/Slider.react.js.old +214 -0
- package/lib/components/Slider.tsx +81 -0
- package/lib/components/Textarea.react.js.old +287 -0
- package/lib/components/css/dcc.css +14 -0
- package/lib/components/css/dropdown.css +230 -0
- package/lib/components/css/input.css +116 -2
- package/lib/components/css/react-select@1.0.0-rc.3.min.css +1 -1
- package/lib/components/css/sliders.css +206 -4
- package/lib/fragments/Dropdown.tsx +577 -0
- package/lib/fragments/RangeSlider.tsx +412 -0
- package/lib/index.ts +58 -0
- package/lib/types.ts +499 -0
- package/lib/utils/LazyLoader/dropdown.ts +2 -0
- package/lib/utils/LazyLoader/rangeSlider.ts +2 -0
- package/lib/utils/LazyLoader/slider.ts +2 -0
- package/lib/utils/_LoadingElement.tsx +26 -0
- package/lib/utils/computeSliderMarkers.ts +302 -0
- package/lib/utils/dropdownSearch.ts +92 -0
- package/lib/utils/formatSliderTooltip.ts +20 -0
- package/lib/utils/optionTypes.ts +27 -0
- package/lib/utils/sliderRendering.tsx +144 -0
- package/lib/utils/sliderSnapToMark.ts +17 -0
- package/lib/utils/sliderTooltip.tsx +65 -0
- package/package.json +27 -5
- package/lib/components/Dropdown.react.js +0 -182
- package/lib/components/Input.react.js +0 -403
- package/lib/components/RangeSlider.react.js +0 -194
- package/lib/components/Slider.react.js +0 -179
- package/lib/fragments/Dropdown.react.js +0 -158
- package/lib/fragments/RangeSlider.react.js +0 -143
- package/lib/fragments/Slider.react.js +0 -159
- package/lib/index.js +0 -183
- package/lib/utils/LazyLoader/dropdown.js +0 -8
- package/lib/utils/LazyLoader/rangeSlider.js +0 -8
- package/lib/utils/LazyLoader/slider.js +0 -8
- package/lib/utils/computeSliderMarkers.js +0 -153
- package/lib/utils/computeSliderStyle.js +0 -26
- package/lib/utils/formatSliderTooltip.js +0 -19
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import {pickBy, isEmpty, isNil} from 'ramda';
|
|
2
|
+
import {formatPrefix} from 'd3-format';
|
|
3
|
+
import {SliderMarks} from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Truncate marks if they are out of Slider interval
|
|
7
|
+
*/
|
|
8
|
+
const truncateMarks = (
|
|
9
|
+
min: number,
|
|
10
|
+
max: number,
|
|
11
|
+
marks: SliderMarks
|
|
12
|
+
): SliderMarks => pickBy((k, mark) => mark >= min && mark <= max, marks);
|
|
13
|
+
|
|
14
|
+
const truncateNumber = (num: number) => {
|
|
15
|
+
const match = num.toString().match(/^-?\d+(?:\.\d{0,0})?/);
|
|
16
|
+
return match ? parseInt(match[0], 10) : 0;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const decimalCount = (d: number) =>
|
|
20
|
+
String(d).split('.').length > 1 ? String(d).split('.')[1].length : 0;
|
|
21
|
+
const alignIntValue = (v: number, d: number) =>
|
|
22
|
+
d < 10
|
|
23
|
+
? v
|
|
24
|
+
: parseInt((truncateNumber(v / d) * d).toFixed(decimalCount(d)), 10);
|
|
25
|
+
const alignDecimalValue = (v: number, d: number) =>
|
|
26
|
+
d < 10
|
|
27
|
+
? parseFloat(v.toFixed(decimalCount(d)))
|
|
28
|
+
: parseFloat(
|
|
29
|
+
(parseFloat((v / d).toFixed(0)) * d).toFixed(decimalCount(d))
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const alignValue = (v: number, d: number) =>
|
|
33
|
+
decimalCount(d) < 1 ? alignIntValue(v, d) : alignDecimalValue(v, d);
|
|
34
|
+
|
|
35
|
+
const estimateBestSteps = (
|
|
36
|
+
minValue: number,
|
|
37
|
+
maxValue: number,
|
|
38
|
+
stepValue: number,
|
|
39
|
+
sliderWidth?: number | null
|
|
40
|
+
) => {
|
|
41
|
+
// Base desired count for 330px slider with 0-100 scale (10 marks = 11 total including endpoints)
|
|
42
|
+
let targetMarkCount = 11; // Default baseline
|
|
43
|
+
|
|
44
|
+
// Scale mark density based on slider width
|
|
45
|
+
if (sliderWidth) {
|
|
46
|
+
const baselineWidth = 330;
|
|
47
|
+
const baselineMarkCount = 11; // 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
|
|
48
|
+
|
|
49
|
+
// Calculate density multiplier based on width
|
|
50
|
+
const widthMultiplier = sliderWidth / baselineWidth;
|
|
51
|
+
|
|
52
|
+
// Target mark count scales with width but maintains consistent density
|
|
53
|
+
// The range adjustment should be removed - we want consistent mark density based on width
|
|
54
|
+
targetMarkCount = Math.round(baselineMarkCount * widthMultiplier);
|
|
55
|
+
|
|
56
|
+
// Ensure reasonable bounds
|
|
57
|
+
const UPPER_BOUND = 50;
|
|
58
|
+
targetMarkCount = Math.max(3, Math.min(targetMarkCount, UPPER_BOUND));
|
|
59
|
+
|
|
60
|
+
// Adjust density based on maximum character width of mark labels
|
|
61
|
+
// Estimate the maximum characters in any mark label
|
|
62
|
+
const maxValueChars = Math.max(
|
|
63
|
+
String(minValue).length,
|
|
64
|
+
String(maxValue).length,
|
|
65
|
+
String(Math.abs(minValue)).length,
|
|
66
|
+
String(Math.abs(maxValue)).length
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Baseline: 3-4 characters (like "100", "250") work well with baseline density
|
|
70
|
+
// For longer labels, reduce density to prevent overlap
|
|
71
|
+
const baselineChars = 3.5;
|
|
72
|
+
if (maxValueChars > baselineChars) {
|
|
73
|
+
const charReductionFactor = baselineChars / maxValueChars;
|
|
74
|
+
targetMarkCount = Math.round(targetMarkCount * charReductionFactor);
|
|
75
|
+
targetMarkCount = Math.max(2, targetMarkCount); // Ensure minimum of 2 marks
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Calculate the ideal interval between marks based on target count
|
|
80
|
+
const range = maxValue - minValue;
|
|
81
|
+
let idealInterval = range / (targetMarkCount - 1);
|
|
82
|
+
|
|
83
|
+
// Check if the step value is fractional and adjust density
|
|
84
|
+
if (stepValue % 1 !== 0) {
|
|
85
|
+
// For fractional steps, reduce mark density by half to avoid clutter
|
|
86
|
+
targetMarkCount = Math.max(3, Math.round(targetMarkCount / 2));
|
|
87
|
+
idealInterval = range / (targetMarkCount - 1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Find the best interval that's a multiple of stepValue
|
|
91
|
+
// Start with multiples of stepValue and find the one closest to idealInterval
|
|
92
|
+
const stepMultipliers = [
|
|
93
|
+
// eslint-disable-next-line no-magic-numbers
|
|
94
|
+
1, 2, 2.5, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000,
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
let bestInterval = stepValue;
|
|
98
|
+
let bestDifference = Math.abs(idealInterval - stepValue);
|
|
99
|
+
|
|
100
|
+
for (const multiplier of stepMultipliers) {
|
|
101
|
+
const candidateInterval = stepValue * multiplier;
|
|
102
|
+
const difference = Math.abs(idealInterval - candidateInterval);
|
|
103
|
+
|
|
104
|
+
if (difference < bestDifference) {
|
|
105
|
+
bestInterval = candidateInterval;
|
|
106
|
+
bestDifference = difference;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Stop if we've gone too far beyond the ideal
|
|
110
|
+
if (candidateInterval > idealInterval * 2) {
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// All marks must be at valid step positions: minValue + (n * stepValue)
|
|
116
|
+
// Find the first mark after minValue that fits our desired interval
|
|
117
|
+
const stepsInInterval = Math.round(bestInterval / stepValue);
|
|
118
|
+
const actualInterval = stepsInInterval * stepValue;
|
|
119
|
+
|
|
120
|
+
// Find the first mark position that's aligned with our interval
|
|
121
|
+
// This ensures we don't have overlapping marks at narrow widths
|
|
122
|
+
const firstMarkSteps = Math.max(1, stepsInInterval);
|
|
123
|
+
const firstStepAfterMin = minValue + firstMarkSteps * stepValue;
|
|
124
|
+
|
|
125
|
+
return [firstStepAfterMin, actualInterval, stepValue];
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Calculate default step if not defined
|
|
130
|
+
*/
|
|
131
|
+
export const calcStep = (min?: number, max?: number, step?: number | null) => {
|
|
132
|
+
if (step) {
|
|
133
|
+
return step;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (isNil(min) || isNil(max)) {
|
|
137
|
+
return 1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const diff = max > min ? max - min : min - max;
|
|
141
|
+
|
|
142
|
+
const v = (Math.abs(diff) + Number.EPSILON) / 100;
|
|
143
|
+
const N = Math.floor(Math.log10(v));
|
|
144
|
+
return [
|
|
145
|
+
Number(Math.pow(10, N)),
|
|
146
|
+
2 * Math.pow(10, N),
|
|
147
|
+
5 * Math.pow(10, N),
|
|
148
|
+
].sort((a, b) => Math.abs(a - v) - Math.abs(b - v))[0];
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Set min and max if they are undefined and marks are defined
|
|
153
|
+
*/
|
|
154
|
+
export const setUndefined = (
|
|
155
|
+
min: number | undefined,
|
|
156
|
+
max: number | undefined,
|
|
157
|
+
marks: SliderMarks | undefined | null
|
|
158
|
+
) => {
|
|
159
|
+
const definedMarks = {min_mark: min ?? 0, max_mark: max ?? 1};
|
|
160
|
+
|
|
161
|
+
if (isNil(marks)) {
|
|
162
|
+
return definedMarks;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const marksObject = Object.keys(marks).map(Number);
|
|
166
|
+
|
|
167
|
+
if (isNil(min)) {
|
|
168
|
+
definedMarks.min_mark = Math.min(...marksObject);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isNil(max)) {
|
|
172
|
+
definedMarks.max_mark = Math.max(...marksObject);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return definedMarks;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export const applyD3Format = (mark: number, min: number, max: number) => {
|
|
179
|
+
const mu_ten_factor = -3;
|
|
180
|
+
const k_ten_factor = 3;
|
|
181
|
+
|
|
182
|
+
const ten_factor = Math.log10(Math.abs(mark));
|
|
183
|
+
if (
|
|
184
|
+
mark === 0 ||
|
|
185
|
+
(ten_factor > mu_ten_factor && ten_factor < k_ten_factor)
|
|
186
|
+
) {
|
|
187
|
+
return String(mark);
|
|
188
|
+
}
|
|
189
|
+
const max_min_mean = (Math.abs(max) + Math.abs(min)) / 2;
|
|
190
|
+
const si_formatter = formatPrefix(',.0', max_min_mean);
|
|
191
|
+
return String(si_formatter(mark));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const autoGenerateMarks = (
|
|
195
|
+
min: number,
|
|
196
|
+
max: number,
|
|
197
|
+
step?: number | null,
|
|
198
|
+
sliderWidth?: number | null
|
|
199
|
+
) => {
|
|
200
|
+
const marks = [];
|
|
201
|
+
// Always use dynamic logic, but pass the provided step as a constraint
|
|
202
|
+
const effectiveStep = step || calcStep(min, max, 0);
|
|
203
|
+
const [start, interval, chosenStep] = estimateBestSteps(
|
|
204
|
+
min,
|
|
205
|
+
max,
|
|
206
|
+
effectiveStep,
|
|
207
|
+
sliderWidth
|
|
208
|
+
);
|
|
209
|
+
let cursor = start;
|
|
210
|
+
|
|
211
|
+
// Apply a safety cap to prevent excessive mark generation while preserving existing behavior
|
|
212
|
+
// Only restrict when marks would be truly excessive (much higher than the existing UPPER_BOUND)
|
|
213
|
+
const MARK_WIDTH_PX = 20; // More generous spacing for width-based calculation
|
|
214
|
+
const FALLBACK_MAX_MARKS = 200; // High fallback to preserve existing behavior when no width
|
|
215
|
+
const ABSOLUTE_MAX_MARKS = 200; // Safety cap against extreme cases
|
|
216
|
+
|
|
217
|
+
const widthBasedMax = sliderWidth
|
|
218
|
+
? Math.max(10, Math.floor(sliderWidth / MARK_WIDTH_PX))
|
|
219
|
+
: FALLBACK_MAX_MARKS;
|
|
220
|
+
|
|
221
|
+
const maxAutoGeneratedMarks = Math.min(widthBasedMax, ABSOLUTE_MAX_MARKS);
|
|
222
|
+
|
|
223
|
+
// Calculate how many marks would be generated with current interval
|
|
224
|
+
const estimatedMarkCount = Math.floor((max - start) / interval) + 1;
|
|
225
|
+
|
|
226
|
+
// If we would exceed the limit, increase the interval to fit within the limit
|
|
227
|
+
let actualInterval = interval;
|
|
228
|
+
if (estimatedMarkCount > maxAutoGeneratedMarks) {
|
|
229
|
+
// Recalculate interval to fit exactly within the limit
|
|
230
|
+
actualInterval = (max - start) / (maxAutoGeneratedMarks - 1);
|
|
231
|
+
// Round to a reasonable step multiple to keep marks clean
|
|
232
|
+
const stepMultiple = Math.ceil(actualInterval / chosenStep);
|
|
233
|
+
actualInterval = stepMultiple * chosenStep;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if ((max - cursor) / actualInterval > 0) {
|
|
237
|
+
do {
|
|
238
|
+
marks.push(alignValue(cursor, chosenStep));
|
|
239
|
+
cursor += actualInterval;
|
|
240
|
+
} while (cursor < max && marks.length < maxAutoGeneratedMarks);
|
|
241
|
+
|
|
242
|
+
// do some cosmetic
|
|
243
|
+
const discardThreshold = 1.5;
|
|
244
|
+
if (
|
|
245
|
+
marks.length >= 2 &&
|
|
246
|
+
max - marks[marks.length - 2] <= interval * discardThreshold
|
|
247
|
+
) {
|
|
248
|
+
marks.pop();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const marksObject: SliderMarks = {};
|
|
252
|
+
marks.forEach(mark => {
|
|
253
|
+
marksObject[mark] = applyD3Format(mark, min, max);
|
|
254
|
+
});
|
|
255
|
+
marksObject[min] = applyD3Format(min, min, max);
|
|
256
|
+
marksObject[max] = applyD3Format(max, min, max);
|
|
257
|
+
return marksObject;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* - Auto generate marks if not given,
|
|
262
|
+
* - Not generate anything at all when explicit null is given to marks
|
|
263
|
+
* - Then truncate marks so no out of range marks
|
|
264
|
+
*/
|
|
265
|
+
type SanitizeMarksParams = {
|
|
266
|
+
min?: number;
|
|
267
|
+
max?: number;
|
|
268
|
+
marks?: SliderMarks;
|
|
269
|
+
step?: number | null;
|
|
270
|
+
sliderWidth?: number | null;
|
|
271
|
+
};
|
|
272
|
+
export const sanitizeMarks = ({
|
|
273
|
+
min,
|
|
274
|
+
max,
|
|
275
|
+
marks,
|
|
276
|
+
step,
|
|
277
|
+
sliderWidth,
|
|
278
|
+
}: SanitizeMarksParams): SliderMarks => {
|
|
279
|
+
const {min_mark, max_mark} = setUndefined(min, max, marks);
|
|
280
|
+
|
|
281
|
+
const truncated_marks =
|
|
282
|
+
marks && isEmpty(marks) === false
|
|
283
|
+
? truncateMarks(min_mark, max_mark, marks)
|
|
284
|
+
: marks;
|
|
285
|
+
|
|
286
|
+
if (truncated_marks && isEmpty(truncated_marks) === false) {
|
|
287
|
+
return truncated_marks;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return autoGenerateMarks(min_mark, max_mark, step, sliderWidth);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Calculate default value if not defined
|
|
295
|
+
*/
|
|
296
|
+
export const calcValue = (min: number, max: number, value: unknown) => {
|
|
297
|
+
if (value !== undefined) {
|
|
298
|
+
return value;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return [min, max];
|
|
302
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Search,
|
|
4
|
+
AllSubstringsIndexStrategy,
|
|
5
|
+
UnorderedSearchIndex,
|
|
6
|
+
} from 'js-search';
|
|
7
|
+
import {sanitizeOptions} from './optionTypes';
|
|
8
|
+
import {DetailedDropdownOption, DropdownProps} from '../types';
|
|
9
|
+
|
|
10
|
+
// Custom tokenizer, see https://github.com/bvaughn/js-search/issues/43
|
|
11
|
+
// Split on spaces
|
|
12
|
+
const REGEX = /\s+/;
|
|
13
|
+
const TOKENIZER = {
|
|
14
|
+
tokenize(text: string) {
|
|
15
|
+
return text.split(REGEX).filter(
|
|
16
|
+
// Filter empty tokens
|
|
17
|
+
text => text
|
|
18
|
+
);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface FilteredOptionsResult {
|
|
23
|
+
sanitizedOptions: DetailedDropdownOption[];
|
|
24
|
+
filteredOptions: DetailedDropdownOption[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates filtered dropdown options using js-search with the exact same behavior
|
|
29
|
+
* as react-select-fast-filter-options
|
|
30
|
+
*/
|
|
31
|
+
export function createFilteredOptions(
|
|
32
|
+
options: DropdownProps['options'],
|
|
33
|
+
searchable: boolean,
|
|
34
|
+
searchValue?: string
|
|
35
|
+
): FilteredOptionsResult {
|
|
36
|
+
// Sanitize and prepare options
|
|
37
|
+
let sanitized = sanitizeOptions(options);
|
|
38
|
+
|
|
39
|
+
const indexes = ['value'];
|
|
40
|
+
let hasElement = false,
|
|
41
|
+
hasSearch = false;
|
|
42
|
+
|
|
43
|
+
sanitized = Array.isArray(sanitized)
|
|
44
|
+
? sanitized.map(option => {
|
|
45
|
+
if (option.search) {
|
|
46
|
+
hasSearch = true;
|
|
47
|
+
}
|
|
48
|
+
if (React.isValidElement(option.label)) {
|
|
49
|
+
hasElement = true;
|
|
50
|
+
}
|
|
51
|
+
return option;
|
|
52
|
+
})
|
|
53
|
+
: sanitized;
|
|
54
|
+
|
|
55
|
+
if (!hasElement) {
|
|
56
|
+
indexes.push('label');
|
|
57
|
+
}
|
|
58
|
+
if (hasSearch) {
|
|
59
|
+
indexes.push('search');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// If not searchable or no search value, return all sanitized options
|
|
63
|
+
if (!searchable || !searchValue) {
|
|
64
|
+
return {
|
|
65
|
+
sanitizedOptions: sanitized || [],
|
|
66
|
+
filteredOptions: sanitized || [],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Create js-search instance exactly like react-select-fast-filter-options
|
|
71
|
+
const search = new Search('value'); // valueKey defaults to 'value'
|
|
72
|
+
search.searchIndex = new UnorderedSearchIndex();
|
|
73
|
+
search.indexStrategy = new AllSubstringsIndexStrategy();
|
|
74
|
+
search.tokenizer = TOKENIZER;
|
|
75
|
+
|
|
76
|
+
// Add indexes
|
|
77
|
+
indexes.forEach(index => {
|
|
78
|
+
search.addIndex(index);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Add documents
|
|
82
|
+
if (sanitized && sanitized.length > 0) {
|
|
83
|
+
search.addDocuments(sanitized);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const filtered = search.search(searchValue) as DetailedDropdownOption[];
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
sanitizedOptions: sanitized || [],
|
|
90
|
+
filteredOptions: filtered || [],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {replace, path, split, concat, pipe} from 'ramda';
|
|
2
|
+
|
|
3
|
+
export const formatSliderTooltip = (template: string, value: string | number) => {
|
|
4
|
+
return replace('{value}', `${value}`, template);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const transformSliderTooltip = (funcName: string, value: string | number) => {
|
|
8
|
+
type TransformFunc = (value: string | number) => string | number;
|
|
9
|
+
const func = pipe(
|
|
10
|
+
split('.'),
|
|
11
|
+
s => concat(['dccFunctions'], s),
|
|
12
|
+
s => path(s, window)
|
|
13
|
+
)(funcName) as TransformFunc;
|
|
14
|
+
if (!func) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Invalid func for slider tooltip transform: ${funcName}`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return func(value);
|
|
20
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {DetailedDropdownOption, DropdownProps, DropdownValue} from '../types';
|
|
3
|
+
|
|
4
|
+
const isDropdownValue = (option: unknown): option is DropdownValue => {
|
|
5
|
+
return ['string', 'number', 'boolean'].includes(typeof option);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const sanitizeOptions = (
|
|
9
|
+
options: DropdownProps['options']
|
|
10
|
+
): DetailedDropdownOption[] => {
|
|
11
|
+
if (typeof options === 'object' && !(options instanceof Array)) {
|
|
12
|
+
return Object.entries(options).map(([value, label]) => ({
|
|
13
|
+
label: React.isValidElement(label) ? label : String(label),
|
|
14
|
+
value,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (options instanceof Array) {
|
|
19
|
+
return options.map(option => {
|
|
20
|
+
return isDropdownValue(option)
|
|
21
|
+
? {label: String(option), value: option}
|
|
22
|
+
: option;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return options;
|
|
27
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {SliderMarks} from '../types';
|
|
3
|
+
|
|
4
|
+
// Replicate Radix UI's exact positioning logic for displaying marks
|
|
5
|
+
const convertValueToPercentage = (value: number, min: number, max: number) => {
|
|
6
|
+
const maxSteps = max - min;
|
|
7
|
+
const percentPerStep = 100 / maxSteps;
|
|
8
|
+
const percentage = percentPerStep * (value - min);
|
|
9
|
+
// Clamp to 0-100 range like Radix does
|
|
10
|
+
return Math.max(0, Math.min(100, percentage));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const linearScale = (
|
|
14
|
+
input: readonly [number, number],
|
|
15
|
+
output: readonly [number, number]
|
|
16
|
+
) => {
|
|
17
|
+
return (value: number) => {
|
|
18
|
+
if (input[0] === input[1] || output[0] === output[1]) {
|
|
19
|
+
return output[0];
|
|
20
|
+
}
|
|
21
|
+
const ratio = (output[1] - output[0]) / (input[1] - input[0]);
|
|
22
|
+
return output[0] + ratio * (value - input[0]);
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getThumbInBoundsOffset = (
|
|
27
|
+
width: number,
|
|
28
|
+
left: number,
|
|
29
|
+
direction: number
|
|
30
|
+
) => {
|
|
31
|
+
const halfWidth = width / 2;
|
|
32
|
+
const halfPercent = 50;
|
|
33
|
+
const offset = linearScale([0, halfPercent], [0, halfWidth]);
|
|
34
|
+
return (halfWidth - offset(left) * direction) * direction;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Calculate the exact position including pixel offset as Radix does
|
|
38
|
+
const getRadixThumbPosition = (
|
|
39
|
+
value: number,
|
|
40
|
+
minMaxValues: {min_mark: number; max_mark: number},
|
|
41
|
+
thumbWidth = 16
|
|
42
|
+
) => {
|
|
43
|
+
const percentage = convertValueToPercentage(
|
|
44
|
+
value,
|
|
45
|
+
minMaxValues.min_mark,
|
|
46
|
+
minMaxValues.max_mark
|
|
47
|
+
);
|
|
48
|
+
const direction = 1; // LTR direction
|
|
49
|
+
const thumbInBoundsOffset = getThumbInBoundsOffset(
|
|
50
|
+
thumbWidth,
|
|
51
|
+
percentage,
|
|
52
|
+
direction
|
|
53
|
+
);
|
|
54
|
+
return {percentage, offset: thumbInBoundsOffset};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const renderSliderMarks = (
|
|
58
|
+
renderedMarks: SliderMarks,
|
|
59
|
+
vertical: boolean,
|
|
60
|
+
minMaxValues: {min_mark: number; max_mark: number},
|
|
61
|
+
dots: boolean
|
|
62
|
+
) => {
|
|
63
|
+
return Object.entries(renderedMarks).map(([position, mark]) => {
|
|
64
|
+
const pos = parseFloat(position);
|
|
65
|
+
const thumbPosition = getRadixThumbPosition(pos, minMaxValues);
|
|
66
|
+
const style = vertical
|
|
67
|
+
? {
|
|
68
|
+
bottom: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px - 13px)`,
|
|
69
|
+
left: 'calc(100% + 8px)',
|
|
70
|
+
transform: 'translateY(-50%)',
|
|
71
|
+
}
|
|
72
|
+
: {
|
|
73
|
+
left: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
|
|
74
|
+
bottom: 0,
|
|
75
|
+
transform: 'translateX(-50%)',
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div
|
|
80
|
+
key={position}
|
|
81
|
+
className={`dash-slider-mark ${dots ? 'with-dots' : ''}`}
|
|
82
|
+
style={{
|
|
83
|
+
...style,
|
|
84
|
+
...(typeof mark === 'object' && mark.style
|
|
85
|
+
? mark.style
|
|
86
|
+
: {}),
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
{typeof mark === 'string' ? mark : mark?.label || pos}
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const renderSliderDots = (
|
|
96
|
+
stepValue: number,
|
|
97
|
+
minMaxValues: {min_mark: number; max_mark: number},
|
|
98
|
+
vertical: boolean
|
|
99
|
+
) => {
|
|
100
|
+
if (stepValue <= 1) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const dotCount =
|
|
105
|
+
Math.floor(
|
|
106
|
+
(minMaxValues.max_mark - minMaxValues.min_mark) / stepValue
|
|
107
|
+
) + 1;
|
|
108
|
+
|
|
109
|
+
// Cap at 100 dots - if more, don't render any dots
|
|
110
|
+
if (dotCount > 100) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return Array.from(
|
|
115
|
+
{
|
|
116
|
+
length: dotCount,
|
|
117
|
+
},
|
|
118
|
+
(_, i) => {
|
|
119
|
+
const dotValue = minMaxValues.min_mark + i * stepValue;
|
|
120
|
+
const thumbPosition = getRadixThumbPosition(dotValue, minMaxValues);
|
|
121
|
+
const dotStyle = vertical
|
|
122
|
+
? {
|
|
123
|
+
bottom: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
|
|
124
|
+
left: '2px',
|
|
125
|
+
transform: 'translate(50%, 50%)',
|
|
126
|
+
}
|
|
127
|
+
: {
|
|
128
|
+
left: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
|
|
129
|
+
top: '0',
|
|
130
|
+
transform: 'translate(-50%, 50%)',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div
|
|
135
|
+
key={i}
|
|
136
|
+
className="dash-slider-dot"
|
|
137
|
+
style={{
|
|
138
|
+
...dotStyle,
|
|
139
|
+
}}
|
|
140
|
+
/>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {SliderMarks} from '../types';
|
|
2
|
+
|
|
3
|
+
export const snapToNearestMark = (
|
|
4
|
+
value: number,
|
|
5
|
+
marks: SliderMarks
|
|
6
|
+
): number => {
|
|
7
|
+
const markValues = Object.keys(marks).map(Number);
|
|
8
|
+
if (markValues.length === 0) {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return markValues.reduce((closest, current) => {
|
|
13
|
+
return Math.abs(current - value) < Math.abs(closest - value)
|
|
14
|
+
? current
|
|
15
|
+
: closest;
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
formatSliderTooltip,
|
|
4
|
+
transformSliderTooltip,
|
|
5
|
+
} from '../utils/formatSliderTooltip';
|
|
6
|
+
import {SliderTooltip} from '../types';
|
|
7
|
+
|
|
8
|
+
type TooltipProps = {
|
|
9
|
+
id?: string;
|
|
10
|
+
index: number;
|
|
11
|
+
value: number;
|
|
12
|
+
tooltip: SliderTooltip;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const Tooltip = ({tooltip, value, index: key, id}: TooltipProps) => {
|
|
16
|
+
const formatTooltipContent = (value: number, index: number) => {
|
|
17
|
+
let displayValue: string | number = value;
|
|
18
|
+
if (tooltip?.transform) {
|
|
19
|
+
displayValue = transformSliderTooltip(tooltip.transform, value);
|
|
20
|
+
}
|
|
21
|
+
return (
|
|
22
|
+
<div
|
|
23
|
+
id={`${id}-tooltip-${index + 1}-content`}
|
|
24
|
+
style={tooltip?.style}
|
|
25
|
+
>
|
|
26
|
+
{formatSliderTooltip(
|
|
27
|
+
tooltip?.template || '{value}',
|
|
28
|
+
displayValue
|
|
29
|
+
)}
|
|
30
|
+
</div>
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const classNames = [
|
|
35
|
+
'dash-slider-tooltip',
|
|
36
|
+
tooltip.always_visible ? 'always-visible' : '',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const placement = (tooltip.placement ?? 'top').toLowerCase();
|
|
40
|
+
const position: React.CSSProperties = {transform: ''};
|
|
41
|
+
|
|
42
|
+
if (placement.includes('top')) {
|
|
43
|
+
position.bottom = 'calc(100% + var(--Dash-Spacing))';
|
|
44
|
+
} else if (placement.includes('bottom')) {
|
|
45
|
+
position.top = 'calc(100% + var(--Dash-Spacing))';
|
|
46
|
+
} else {
|
|
47
|
+
position.top = '50%';
|
|
48
|
+
position.transform += 'translateY(-50%)';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (placement.includes('left')) {
|
|
52
|
+
position.right = 'calc(100% + var(--Dash-Spacing))';
|
|
53
|
+
} else if (placement.includes('right')) {
|
|
54
|
+
position.left = 'calc(100% + var(--Dash-Spacing))';
|
|
55
|
+
} else {
|
|
56
|
+
position.left = '50%';
|
|
57
|
+
position.transform += 'translateX(-50%)';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div className={classNames.join(' ')} style={position}>
|
|
62
|
+
{formatTooltipContent(value, key)}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
};
|