dash-core-components 4.0.0 → 4.1.0
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-graph.js.map +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 -1
- package/dash_core_components/async-slider.js.map +1 -1
- package/dash_core_components/async-upload.js +1 -2
- package/dash_core_components/async-upload.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/proptypes.js +1 -0
- package/lib/components/Checklist.tsx +1 -1
- package/lib/components/Clipboard.react.js +53 -100
- package/lib/components/ConfirmDialog.react.js +6 -5
- package/lib/components/ConfirmDialogProvider.react.js +6 -5
- package/lib/components/Download.react.js +3 -1
- package/lib/components/Graph.react.js +5 -10
- package/lib/components/Interval.react.js +4 -3
- package/lib/components/Link.react.js +13 -10
- package/lib/components/Location.react.js +11 -7
- package/lib/components/Markdown.react.js +1 -8
- package/lib/components/RadioItems.tsx +1 -1
- package/lib/components/Store.react.js +16 -13
- package/lib/components/css/datepickers.css +5 -0
- package/lib/components/css/dropdown.css +12 -0
- package/lib/fragments/DatePickerRange.tsx +195 -143
- package/lib/fragments/DatePickerSingle.tsx +126 -111
- package/lib/fragments/Dropdown.tsx +202 -131
- package/lib/fragments/Graph.react.js +43 -40
- package/lib/fragments/Markdown.react.js +27 -38
- package/lib/fragments/Upload.react.js +117 -202
- package/lib/types.ts +5 -0
- package/lib/utils/LoadingElement.js +3 -2
- package/lib/utils/ResizeDetector.js +5 -3
- package/lib/utils/calendar/Calendar.tsx +1 -3
- package/lib/utils/calendar/helpers.ts +1 -0
- package/lib/utils/dropdownSearch.ts +36 -45
- package/lib/utils/optionRendering.tsx +284 -43
- package/lib/utils/optionTypes.ts +25 -11
- package/package.json +4 -3
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import React
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
memo,
|
|
4
|
+
useCallback,
|
|
5
|
+
useImperativeHandle,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from 'react';
|
|
2
11
|
import {append, includes, without} from 'ramda';
|
|
12
|
+
import {VariableSizeList, ListChildComponentProps} from 'react-window';
|
|
3
13
|
import {DetailedOption, OptionValue} from 'src/types';
|
|
4
14
|
import '../components/css/optionslist.css';
|
|
5
15
|
|
|
16
|
+
const DEFAULT_ITEM_HEIGHT = 35;
|
|
17
|
+
|
|
6
18
|
interface StylingProps {
|
|
7
19
|
id?: string;
|
|
8
20
|
className?: string;
|
|
@@ -37,7 +49,7 @@ export const OptionLabel: React.FC<OptionLabelProps> = ({
|
|
|
37
49
|
<ExternalWrapper
|
|
38
50
|
key={i}
|
|
39
51
|
component={label}
|
|
40
|
-
componentPath={[...ctx.componentPath,
|
|
52
|
+
componentPath={[...ctx.componentPath, String(value), i]}
|
|
41
53
|
/>
|
|
42
54
|
))}
|
|
43
55
|
</>
|
|
@@ -91,6 +103,7 @@ export const Option: React.FC<OptionProps> = ({
|
|
|
91
103
|
role="option"
|
|
92
104
|
aria-selected={isSelected}
|
|
93
105
|
style={optionStyle}
|
|
106
|
+
data-option-index={index}
|
|
94
107
|
>
|
|
95
108
|
<span className="dash-options-list-option-wrapper">
|
|
96
109
|
<input
|
|
@@ -125,51 +138,279 @@ export const Option: React.FC<OptionProps> = ({
|
|
|
125
138
|
);
|
|
126
139
|
};
|
|
127
140
|
|
|
128
|
-
interface
|
|
141
|
+
interface RowData {
|
|
129
142
|
options: DetailedOption[];
|
|
130
143
|
selected: OptionValue[];
|
|
131
|
-
|
|
144
|
+
onChange: (option: DetailedOption) => void;
|
|
145
|
+
passThruProps: StylingProps;
|
|
146
|
+
setOptionHeight: (index: number, height: number) => void;
|
|
132
147
|
}
|
|
133
148
|
|
|
134
|
-
|
|
135
|
-
options,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
149
|
+
const Row = memo(({index, style, data}: ListChildComponentProps<RowData>) => {
|
|
150
|
+
const {options, selected, onChange, passThruProps, setOptionHeight} = data;
|
|
151
|
+
const option = options[index];
|
|
152
|
+
const isSelected = includes(option.value, selected);
|
|
153
|
+
|
|
154
|
+
const measureRef = useCallback(
|
|
155
|
+
(el: HTMLDivElement | null) => {
|
|
156
|
+
if (!el) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
// Synchronous measurement for string labels.
|
|
160
|
+
const immediateHeight = el.getBoundingClientRect().height;
|
|
161
|
+
if (immediateHeight > 0) {
|
|
162
|
+
setOptionHeight(index, immediateHeight);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ResizeObserver catches async Dash component labels
|
|
166
|
+
// that render after the initial commit.
|
|
167
|
+
const observer = new ResizeObserver(([entry]) => {
|
|
168
|
+
const height =
|
|
169
|
+
entry.borderBoxSize?.[0]?.blockSize ??
|
|
170
|
+
entry.contentRect.height;
|
|
171
|
+
if (height > 0) {
|
|
172
|
+
setOptionHeight(index, height);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
observer.observe(el);
|
|
176
|
+
},
|
|
177
|
+
[index, setOptionHeight]
|
|
178
|
+
);
|
|
179
|
+
|
|
144
180
|
return (
|
|
145
|
-
<div
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
key={i}
|
|
157
|
-
index={i}
|
|
158
|
-
option={option}
|
|
159
|
-
isSelected={isSelected}
|
|
160
|
-
onChange={option => {
|
|
161
|
-
let newValue: OptionValue[];
|
|
162
|
-
if (includes(option.value, selected)) {
|
|
163
|
-
newValue = without([option.value], selected);
|
|
164
|
-
} else {
|
|
165
|
-
newValue = append(option.value, selected);
|
|
166
|
-
}
|
|
167
|
-
onSelectionChange(newValue);
|
|
168
|
-
}}
|
|
169
|
-
{...passThruProps}
|
|
170
|
-
/>
|
|
171
|
-
);
|
|
172
|
-
})}
|
|
181
|
+
<div style={style}>
|
|
182
|
+
<div ref={measureRef}>
|
|
183
|
+
<Option
|
|
184
|
+
id={passThruProps.id}
|
|
185
|
+
index={index}
|
|
186
|
+
option={option}
|
|
187
|
+
isSelected={isSelected}
|
|
188
|
+
onChange={onChange}
|
|
189
|
+
{...passThruProps}
|
|
190
|
+
/>
|
|
191
|
+
</div>
|
|
173
192
|
</div>
|
|
174
193
|
);
|
|
175
|
-
};
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
Row.displayName = 'Row';
|
|
197
|
+
|
|
198
|
+
export interface OptionsListHandle {
|
|
199
|
+
scrollToItem: (index: number) => void;
|
|
200
|
+
focusItem: (index: number) => void;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
interface OptionsListProps extends StylingProps {
|
|
204
|
+
options: DetailedOption[];
|
|
205
|
+
selected: OptionValue[];
|
|
206
|
+
onSelectionChange: (selected: OptionValue[]) => void;
|
|
207
|
+
optionHeight?: number;
|
|
208
|
+
maxHeight?: number;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export const OptionsList = forwardRef<OptionsListHandle, OptionsListProps>(
|
|
212
|
+
(
|
|
213
|
+
{
|
|
214
|
+
options,
|
|
215
|
+
selected,
|
|
216
|
+
onSelectionChange,
|
|
217
|
+
optionHeight,
|
|
218
|
+
maxHeight = window.innerHeight,
|
|
219
|
+
id,
|
|
220
|
+
className,
|
|
221
|
+
style,
|
|
222
|
+
...passThruProps
|
|
223
|
+
},
|
|
224
|
+
ref
|
|
225
|
+
) => {
|
|
226
|
+
const listRef = useRef<VariableSizeList>(null);
|
|
227
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
228
|
+
const pendingFocusRef = useRef<number | null>(null);
|
|
229
|
+
const heightsRef = useRef<Map<number, number>>(new Map());
|
|
230
|
+
const [measuredHeight, setMeasuredHeight] = useState<number | null>(
|
|
231
|
+
null
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const defaultOptionHeight = optionHeight ?? DEFAULT_ITEM_HEIGHT;
|
|
235
|
+
|
|
236
|
+
const getOptionHeight = useCallback(
|
|
237
|
+
(index: number): number =>
|
|
238
|
+
heightsRef.current.get(index) ?? defaultOptionHeight,
|
|
239
|
+
[defaultOptionHeight]
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const setOptionHeight = useCallback((index: number, height: number) => {
|
|
243
|
+
if (heightsRef.current.get(index) !== height) {
|
|
244
|
+
heightsRef.current.set(index, height);
|
|
245
|
+
listRef.current?.resetAfterIndex(index, true);
|
|
246
|
+
}
|
|
247
|
+
}, []);
|
|
248
|
+
|
|
249
|
+
// Measure container height before first paint. Row measureRefs
|
|
250
|
+
// have already fired, so invalidate the list's internal cache
|
|
251
|
+
// so the re-render picks up measured heights.
|
|
252
|
+
useLayoutEffect(() => {
|
|
253
|
+
const el = containerRef.current;
|
|
254
|
+
if (!el || measuredHeight !== null) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
listRef.current?.resetAfterIndex(0, false);
|
|
258
|
+
const h = el.getBoundingClientRect().height;
|
|
259
|
+
if (h > 0) {
|
|
260
|
+
setMeasuredHeight(h);
|
|
261
|
+
}
|
|
262
|
+
}, []);
|
|
263
|
+
|
|
264
|
+
const defaultListHeight = options.length * defaultOptionHeight;
|
|
265
|
+
const listHeight =
|
|
266
|
+
measuredHeight === null
|
|
267
|
+
? maxHeight
|
|
268
|
+
: Math.min(measuredHeight, defaultListHeight);
|
|
269
|
+
|
|
270
|
+
const focusOption = useCallback((optionIndex: number) => {
|
|
271
|
+
requestAnimationFrame(() => {
|
|
272
|
+
if (pendingFocusRef.current !== optionIndex) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const deferred = containerRef.current?.querySelector(
|
|
276
|
+
`[data-option-index="${optionIndex}"] input`
|
|
277
|
+
);
|
|
278
|
+
if (deferred instanceof HTMLElement) {
|
|
279
|
+
pendingFocusRef.current = null;
|
|
280
|
+
deferred.focus();
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}, []);
|
|
284
|
+
|
|
285
|
+
useImperativeHandle(ref, () => ({
|
|
286
|
+
scrollToItem: (index: number) => {
|
|
287
|
+
if (listRef.current) {
|
|
288
|
+
listRef.current.scrollToItem(index, 'smart');
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
focusItem: (index: number) => {
|
|
292
|
+
pendingFocusRef.current = index;
|
|
293
|
+
if (listRef.current) {
|
|
294
|
+
listRef.current.scrollToItem(index, 'center');
|
|
295
|
+
}
|
|
296
|
+
focusOption(index);
|
|
297
|
+
},
|
|
298
|
+
}));
|
|
299
|
+
|
|
300
|
+
const handleChange = useCallback(
|
|
301
|
+
(option: DetailedOption) => {
|
|
302
|
+
let newValue: OptionValue[];
|
|
303
|
+
if (includes(option.value, selected)) {
|
|
304
|
+
newValue = without([option.value], selected);
|
|
305
|
+
} else {
|
|
306
|
+
newValue = append(option.value, selected);
|
|
307
|
+
}
|
|
308
|
+
onSelectionChange(newValue);
|
|
309
|
+
},
|
|
310
|
+
[selected, onSelectionChange]
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const classNames = ['dash-options-list', className].filter(Boolean);
|
|
314
|
+
|
|
315
|
+
const handleItemsRendered = useCallback(() => {
|
|
316
|
+
if (typeof pendingFocusRef.current === 'number') {
|
|
317
|
+
focusOption(pendingFocusRef.current);
|
|
318
|
+
}
|
|
319
|
+
}, []);
|
|
320
|
+
|
|
321
|
+
// Render the longest-label option (sampled) for intrinsic width.
|
|
322
|
+
const sizerDiv = useMemo(() => {
|
|
323
|
+
if (!options.length) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
const step = Math.max(1, Math.floor(options.length / 100));
|
|
327
|
+
let longestOption = options[0];
|
|
328
|
+
let maxLen = 0;
|
|
329
|
+
for (let i = 0; i < options.length; i += step) {
|
|
330
|
+
const {label} = options[i];
|
|
331
|
+
if (typeof label !== 'string') {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
if (label.length > maxLen) {
|
|
335
|
+
longestOption = options[i];
|
|
336
|
+
maxLen = label.length;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return (
|
|
340
|
+
<Option
|
|
341
|
+
index={-1}
|
|
342
|
+
option={longestOption}
|
|
343
|
+
isSelected={false}
|
|
344
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
345
|
+
onChange={() => {}}
|
|
346
|
+
/>
|
|
347
|
+
);
|
|
348
|
+
}, [options]);
|
|
349
|
+
|
|
350
|
+
const itemData = useMemo<RowData>(
|
|
351
|
+
() => ({
|
|
352
|
+
options,
|
|
353
|
+
selected,
|
|
354
|
+
onChange: handleChange,
|
|
355
|
+
passThruProps: {id, ...passThruProps},
|
|
356
|
+
setOptionHeight,
|
|
357
|
+
}),
|
|
358
|
+
[options, selected, handleChange, id, passThruProps]
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
if (options.length < 100) {
|
|
362
|
+
return (
|
|
363
|
+
<div
|
|
364
|
+
ref={containerRef}
|
|
365
|
+
id={id}
|
|
366
|
+
className={classNames.join(' ')}
|
|
367
|
+
style={style}
|
|
368
|
+
role="listbox"
|
|
369
|
+
>
|
|
370
|
+
{options.map((option, i) => (
|
|
371
|
+
<Option
|
|
372
|
+
key={i}
|
|
373
|
+
id={id}
|
|
374
|
+
index={i}
|
|
375
|
+
option={option}
|
|
376
|
+
isSelected={includes(option.value, selected)}
|
|
377
|
+
onChange={handleChange}
|
|
378
|
+
{...passThruProps}
|
|
379
|
+
/>
|
|
380
|
+
))}
|
|
381
|
+
</div>
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return (
|
|
386
|
+
<div
|
|
387
|
+
ref={containerRef}
|
|
388
|
+
id={id}
|
|
389
|
+
className={classNames.join(' ')}
|
|
390
|
+
style={style}
|
|
391
|
+
role="listbox"
|
|
392
|
+
>
|
|
393
|
+
<VariableSizeList
|
|
394
|
+
ref={listRef}
|
|
395
|
+
height={listHeight}
|
|
396
|
+
itemCount={options.length}
|
|
397
|
+
itemSize={getOptionHeight}
|
|
398
|
+
estimatedItemSize={defaultOptionHeight}
|
|
399
|
+
width="100%"
|
|
400
|
+
className="dash-options-list-virtualized"
|
|
401
|
+
onItemsRendered={handleItemsRendered}
|
|
402
|
+
itemData={itemData}
|
|
403
|
+
>
|
|
404
|
+
{Row}
|
|
405
|
+
</VariableSizeList>
|
|
406
|
+
{sizerDiv && (
|
|
407
|
+
<div aria-hidden style={{height: 0, overflow: 'hidden'}}>
|
|
408
|
+
{sizerDiv}
|
|
409
|
+
</div>
|
|
410
|
+
)}
|
|
411
|
+
</div>
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
OptionsList.displayName = 'OptionsList';
|
package/lib/utils/optionTypes.ts
CHANGED
|
@@ -5,23 +5,37 @@ const isOptionValue = (option: unknown): option is OptionValue => {
|
|
|
5
5
|
return ['string', 'number', 'boolean'].includes(typeof option);
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
+
export interface SanitizedResult {
|
|
9
|
+
options: DetailedOption[];
|
|
10
|
+
valueSet: Set<OptionValue>;
|
|
11
|
+
}
|
|
12
|
+
|
|
8
13
|
export const sanitizeOptions = (
|
|
9
14
|
options: DropdownProps['options']
|
|
10
|
-
):
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
label: React.isValidElement(label) ? label : String(label),
|
|
14
|
-
value,
|
|
15
|
-
}));
|
|
16
|
-
}
|
|
15
|
+
): SanitizedResult => {
|
|
16
|
+
const valueSet = new Set<OptionValue>();
|
|
17
|
+
let result: DetailedOption[];
|
|
17
18
|
|
|
18
|
-
if (options instanceof Array) {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
if (typeof options === 'object' && !(options instanceof Array)) {
|
|
20
|
+
result = Object.entries(options).map(([value, label]) => {
|
|
21
|
+
const opt = {
|
|
22
|
+
label: React.isValidElement(label) ? label : String(label),
|
|
23
|
+
value,
|
|
24
|
+
};
|
|
25
|
+
valueSet.add(value);
|
|
26
|
+
return opt;
|
|
27
|
+
});
|
|
28
|
+
} else if (options instanceof Array) {
|
|
29
|
+
result = options.map(option => {
|
|
30
|
+
const opt = isOptionValue(option)
|
|
21
31
|
? {label: String(option), value: option}
|
|
22
32
|
: option;
|
|
33
|
+
valueSet.add(opt.value);
|
|
34
|
+
return opt;
|
|
23
35
|
});
|
|
36
|
+
} else {
|
|
37
|
+
result = [];
|
|
24
38
|
}
|
|
25
39
|
|
|
26
|
-
return
|
|
40
|
+
return {options: result, valueSet};
|
|
27
41
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dash-core-components",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Core component suite for Dash",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"@radix-ui/react-tooltip": "^1.2.8",
|
|
48
48
|
"base64-js": "^1.5.1",
|
|
49
49
|
"d3-format": "^1.4.5",
|
|
50
|
+
"date-fns": "^4.1.0",
|
|
50
51
|
"fast-isnumeric": "^1.1.4",
|
|
51
52
|
"file-saver": "^2.0.5",
|
|
52
53
|
"highlight.js": "^11.8.0",
|
|
53
54
|
"js-search": "^2.0.1",
|
|
54
55
|
"mathjax": "^3.2.2",
|
|
55
|
-
"date-fns": "^4.1.0",
|
|
56
56
|
"node-polyfill-webpack-plugin": "^2.0.1",
|
|
57
57
|
"prop-types": "^15.8.1",
|
|
58
58
|
"ramda": "^0.30.1",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"react-fast-compare": "^3.2.2",
|
|
63
63
|
"react-input-autosize": "^3.0.0",
|
|
64
64
|
"react-markdown": "^4.3.1",
|
|
65
|
-
"react-
|
|
65
|
+
"react-window": "^1.8.11",
|
|
66
66
|
"remark-math": "^3.0.1",
|
|
67
67
|
"uniqid": "^5.4.0"
|
|
68
68
|
},
|
|
@@ -86,6 +86,7 @@
|
|
|
86
86
|
"@types/react": "^16.14.8",
|
|
87
87
|
"@types/react-dom": "^16.9.13",
|
|
88
88
|
"@types/react-input-autosize": "^2.2.4",
|
|
89
|
+
"@types/react-window": "^1.8.8",
|
|
89
90
|
"@types/uniqid": "^5.3.4",
|
|
90
91
|
"@typescript-eslint/eslint-plugin": "^5.59.7",
|
|
91
92
|
"@typescript-eslint/parser": "^5.59.7",
|