datatable-tree 0.0.0-stage → 0.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +240 -2
  3. package/dist/DataTable.d.ts +583 -0
  4. package/dist/DataTable.js +620 -0
  5. package/dist/Draggable.d.ts +35 -0
  6. package/dist/Draggable.js +91 -0
  7. package/dist/ListBox.d.ts +132 -0
  8. package/dist/ListBox.js +243 -0
  9. package/dist/ListVirtualizer.d.ts +59 -0
  10. package/dist/ListVirtualizer.js +180 -0
  11. package/dist/TreeList.d.ts +196 -0
  12. package/dist/TreeList.js +300 -0
  13. package/dist/config.d.ts +55 -0
  14. package/dist/config.js +33 -0
  15. package/dist/datatable.css +161 -0
  16. package/dist/hooks.d.ts +27 -0
  17. package/dist/hooks.js +115 -0
  18. package/dist/index.d.ts +12 -0
  19. package/dist/index.js +15 -0
  20. package/dist/interfaces.d.ts +269 -0
  21. package/dist/interfaces.js +3 -0
  22. package/dist/mobx-adapter.d.ts +12 -0
  23. package/dist/mobx-adapter.js +21 -0
  24. package/dist/reactivity.d.ts +112 -0
  25. package/dist/reactivity.js +155 -0
  26. package/dist/styles.d.ts +64 -0
  27. package/dist/styles.js +86 -0
  28. package/dist/theme.d.ts +160 -0
  29. package/dist/theme.js +239 -0
  30. package/dist/util.d.ts +28 -0
  31. package/dist/util.js +45 -0
  32. package/package.json +137 -4
  33. package/src/DataTable.tsx +1332 -0
  34. package/src/Draggable.tsx +133 -0
  35. package/src/ListBox.tsx +423 -0
  36. package/src/ListVirtualizer.ts +234 -0
  37. package/src/TreeList.tsx +590 -0
  38. package/src/config.tsx +96 -0
  39. package/src/css.d.ts +1 -0
  40. package/src/datatable.css +161 -0
  41. package/src/hooks.ts +138 -0
  42. package/src/index.ts +15 -0
  43. package/src/interfaces.ts +309 -0
  44. package/src/mobx-adapter.ts +24 -0
  45. package/src/reactivity.ts +222 -0
  46. package/src/styles.tsx +102 -0
  47. package/src/theme.ts +346 -0
  48. package/src/util.ts +60 -0
@@ -0,0 +1,91 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useRef, useState } from 'react';
3
+ import { useUnmountHandler } from './hooks.js';
4
+ import { reactive } from './reactivity.js';
5
+ import { omit } from './util.js';
6
+ /** An unstyled div that can be clicked and dragged to change an "X" or "Y" value,
7
+ * e.g. representing a priority on PrioritiesDiagram, or the size of a modal.
8
+ *
9
+ * Draggable does not know or care about the meaning of the x and y values it changes.
10
+ * One example usage can be found in `SplitPane`, where it is used to alter the size
11
+ * of a different element.
12
+ *
13
+ * Typically one should set a cursor, e.g. with `style={{ cursor: 'col-resize' }}`,
14
+ * and also `tabIndex={0}` so that the drag handle can get the keyboard focus,
15
+ * which allows the escape key to cancel the drag by setting the x/y value back to
16
+ * what it was when the drag started.
17
+ */
18
+ export const Draggable = reactive(function Draggable(p) {
19
+ const ref = useRef(null);
20
+ const [dragState] = useState({
21
+ movedEnough: false,
22
+ initialX: Number.NaN, // clientX
23
+ initialY: Number.NaN, // clientY
24
+ initialXValue: 0,
25
+ initialYValue: 0,
26
+ stopDragging: (event) => { },
27
+ });
28
+ useUnmountHandler(() => dragState.stopDragging());
29
+ return (_jsx("div", Object.assign({ ref: ref }, omit(p, 'xValue', 'yValue', 'xPerPixel', 'yPerPixel', 'onStopDrag', 'onStartDrag', 'onDragButtonDown', 'hasMovedEnough', 'onClick'), { onPointerDown: (p.xValue || p.yValue) && onPointerDown })));
30
+ function onPointerDown(event) {
31
+ var _a, _b, _c, _d, _e, _f;
32
+ (_a = p.onPointerDown) === null || _a === void 0 ? void 0 : _a.call(p, event);
33
+ if (event.button === 0) { // Left button
34
+ // prevent regular HTML drag & drop (draggable={false} doesn't work 🤷‍♂️)
35
+ event.preventDefault();
36
+ // Manually focus so we capture the escape key (preventDefault blocks focus)
37
+ ref.current.focus();
38
+ dragState.initialX = event.clientX;
39
+ dragState.initialY = event.clientY;
40
+ dragState.initialXValue = (_c = (_b = p.xValue) === null || _b === void 0 ? void 0 : _b.get()) !== null && _c !== void 0 ? _c : 0;
41
+ dragState.initialYValue = (_e = (_d = p.yValue) === null || _d === void 0 ? void 0 : _d.get()) !== null && _e !== void 0 ? _e : 0;
42
+ dragState.movedEnough = false;
43
+ dragState.stopDragging = stopDragging;
44
+ ref.current.setPointerCapture(event.pointerId);
45
+ document.addEventListener('lostpointercapture', stopDragging);
46
+ document.addEventListener('pointermove', onMouseMoveDuringDrag);
47
+ ref.current.addEventListener('keydown', onKeyDownDuringDrag);
48
+ (_f = p.onDragButtonDown) === null || _f === void 0 ? void 0 : _f.call(p, event);
49
+ }
50
+ function stopDragging(event) {
51
+ var _a, _b, _c;
52
+ document.removeEventListener('lostpointercapture', stopDragging);
53
+ document.removeEventListener('pointermove', onMouseMoveDuringDrag);
54
+ (_a = ref.current) === null || _a === void 0 ? void 0 : _a.removeEventListener('keydown', onKeyDownDuringDrag);
55
+ if (dragState.movedEnough) {
56
+ (_b = p.onStopDrag) === null || _b === void 0 ? void 0 : _b.call(p, event);
57
+ }
58
+ else if ((event === null || event === void 0 ? void 0 : event.type) === 'lostpointercapture') {
59
+ (_c = p.onClick) === null || _c === void 0 ? void 0 : _c.call(p, event);
60
+ }
61
+ }
62
+ function onKeyDownDuringDrag(event) {
63
+ var _a, _b;
64
+ if (event.key === 'Escape') {
65
+ event.stopPropagation(); // prevent Modal from closing (if any)
66
+ dragState.stopDragging(event);
67
+ // Restore value(s) to what they were when the drag started
68
+ (_a = p.xValue) === null || _a === void 0 ? void 0 : _a.set(dragState.initialXValue);
69
+ (_b = p.yValue) === null || _b === void 0 ? void 0 : _b.set(dragState.initialYValue);
70
+ }
71
+ }
72
+ }
73
+ async function onMouseMoveDuringDrag(event) {
74
+ var _a, _b, _c, _d, _e;
75
+ let deltaX = event.clientX - dragState.initialX;
76
+ let deltaY = event.clientY - dragState.initialY;
77
+ if (dragState.movedEnough || !p.hasMovedEnough || p.hasMovedEnough(deltaX, deltaY)) {
78
+ if (!dragState.movedEnough) {
79
+ dragState.movedEnough = true;
80
+ (_a = p.onStartDrag) === null || _a === void 0 ? void 0 : _a.call(p);
81
+ }
82
+ let newX = dragState.initialXValue + deltaX * ((_b = get(p.xPerPixel)) !== null && _b !== void 0 ? _b : 1);
83
+ let newY = dragState.initialYValue + deltaY * ((_c = get(p.yPerPixel)) !== null && _c !== void 0 ? _c : 1);
84
+ (_d = p.xValue) === null || _d === void 0 ? void 0 : _d.set(newX);
85
+ (_e = p.yValue) === null || _e === void 0 ? void 0 : _e.set(newY);
86
+ }
87
+ }
88
+ });
89
+ function get(x) {
90
+ return typeof x === 'function' ? x() : x;
91
+ }
@@ -0,0 +1,132 @@
1
+ import React from 'react';
2
+ import { Holder } from 'holders';
3
+ import { ISet } from './interfaces.js';
4
+ import { VirtualizationSettings } from './ListVirtualizer.js';
5
+ import { Styles } from './styles.js';
6
+ import { TableTheme } from './theme.js';
7
+ import { nully } from './util.js';
8
+ /** Main properties of a `<ListBox>` or `<ListCore>`. The only required prop is `list`.
9
+ *
10
+ * T: Type of each item in the list
11
+ * K: Type of the "sel key" for each item (see `selection` and `getSelKey`)
12
+ * ItemProps: type of additional props passed to each item, e.g. for styling
13
+ */
14
+ export interface ListCoreProps<T, K = T, ItemProps = DefaultListItemProps> {
15
+ /** List of items to display. */
16
+ list: T[];
17
+ /** This is the set of selected items. If not provided, items can still be
18
+ * selected but the selection state is internal to the list.
19
+ * If the caller wishes to re-render on changes to the set of selected
20
+ * items, it can use a MobX `observable(set)` rather than a plain `Set<K>`.
21
+ * `ListCore` uses `runInAction` just in case it's an `ObservableSet`.
22
+ * If type `K` isn't the same as `T`, you must provide a `getSelKey` prop. */
23
+ selection?: ISet<K>;
24
+ /** Whether to support selecting multiple things (default: no). 'noCtrl'
25
+ * mode is a multiselect mode in which each click is treated as if the
26
+ * Ctrl key (⌘ on Mac) is pressed; if it is actually pressed then a single
27
+ * item is selected and the rest of the selection is cleared.
28
+ *
29
+ * 'noSelect' mode prevents the selection from being changed by the user,
30
+ * so if the app itself does not select anything, nothing can be selected. */
31
+ selMode?: 'single' | 'multi' | 'invertCtrl' | 'noSelect';
32
+ /** Called to render a T value. If this is not provided, `defaultRenderItem` is
33
+ * called to render each item. */
34
+ renderItem?: (item: T, props: ListItemProps<T, K, ItemProps>) => React.ReactNode;
35
+ /** Component type used to render the entire row (including an outer `div` or `tr`).
36
+ * This is expected to forward a ref so that arrow keys can scroll the item into
37
+ * view. */
38
+ RenderItem?: React.ComponentType<ListItemProps<T, K, ItemProps>>;
39
+ /** This is called immediately when an item is clicked, and if it returns
40
+ 'abort' the click on the item will be ignored rather than changing the
41
+ selection. */
42
+ onClickItem?: (this: ModifiedListItemProps<T, K, ItemProps>, item: T) => void | 'abort' | Promise<void | 'abort'>;
43
+ /** Lists in React require a `key` prop. This function is called to get that key.
44
+ * If this is omitted, the index is used as the key. NOTE: in React 18 it is
45
+ * documented that "Non-unique keys may cause children to be duplicated and/or
46
+ * omitted" and duplicate list items have been observed in this context when there
47
+ * are duplicate keys. If you cannot guarantee unique keys, it is better not to
48
+ * provide `getKey` at all (but this can cause its own problems, e.g. items losing
49
+ * focus when the list order changes). */
50
+ getKey?: (item: T) => number | string;
51
+ /** If the items are stored in wrappers that can change between renders,
52
+ * provide this function to get a subobject or id of the item that does
53
+ * not change between renders. These "sel keys" are used in `selection`
54
+ * (and in `expandedParents` in the related `TreeList` component). */
55
+ getSelKey?: (item: T) => K;
56
+ /** This is called for each item, if you provide it. Items for which this function
57
+ * returns false are hidden. */
58
+ filter?: (item: T, index: number) => boolean;
59
+ /** The most recently clicked item */
60
+ lastClicked?: Holder<K | undefined>;
61
+ /** props that should apply to each item (usually Styles: `className` and `style`).
62
+ * If `RenderItem` is provided, these props are forwarded to it. In a `<DataTable>`,
63
+ * any styles in this object are applied to the row as a whole, not to each cell. */
64
+ itemProps?: ItemProps | ((item: T, i: number) => ItemProps | undefined);
65
+ /** If true, the whole list is read-only. If `undefined`, `useIsReadOnlyContext`
66
+ * is used to decide whether the whole list is read-only. */
67
+ readOnly?: boolean;
68
+ /** These settings enable virtualization, which speeds up list rendering by not rendering
69
+ * off-screen items. */
70
+ virtualize?: VirtualizationSettings<T>;
71
+ /** Called when virtualization is enabled to render a placeholder element for a
72
+ * virtualized gap. Set the gap element's `ref` prop to the `ref` parameter; the list uses
73
+ * the `ref` to observe the gap so it can become real when it nears the viewport. */
74
+ virtualGapRenderer?: (height: number, ref?: React.Ref<Element>) => React.ReactNode;
75
+ /** Content to display when the list is empty. If this is a string, it's "grayed out"
76
+ * using `opacity: 0.5`. */
77
+ placeholder?: React.ReactNode;
78
+ }
79
+ export type ModifiedListItemProps<T, K, ItemProps> = Omit<ListItemProps<T, K, ItemProps>, 'renderItem'>;
80
+ /** Props passed to the `ListCoreProps.RenderItem` component, if you provide one. */
81
+ export interface ListItemProps<T, K = T, ItemProps = DefaultListItemProps> {
82
+ /** An item to be rendered */
83
+ item: T;
84
+ /** Whether this item is selected. The special value `null` indicates that the selection
85
+ * box above a list or dropdown list is being rendered. */
86
+ isSelected: boolean | null;
87
+ /** A function to call when the list item is clicked. It selects the item. */
88
+ onClick(this: ModifiedListItemProps<T, K, ItemProps>, item: T, event: React.MouseEvent<HTMLDivElement>): void;
89
+ /** The React key for this item (from `ListCoreProps.getKey()`).
90
+ * (React doesn't allow it to be called `key`.) */
91
+ reactKey: number | string;
92
+ /** The "sel key" for this item (from `ListCoreProps.getSelKey()`) */
93
+ selKey: K;
94
+ /** This is true if the entire list should be rendered as read-only */
95
+ readOnly: boolean;
96
+ /** Item renderer provided to `ListCoreProps`, if any */
97
+ renderItem: (item: T, props: ListItemProps<T, K, ItemProps>) => React.ReactNode;
98
+ /** Item properties from `ListCoreProps.itemProps` */
99
+ itemProps?: ItemProps;
100
+ /** Used by TreeList and DataTable */
101
+ tempState: {
102
+ selectedParents?: Pick<ISet<T>, 'add' | 'delete' | 'has'>;
103
+ };
104
+ }
105
+ export type DefaultListItemProps = Styles & {
106
+ ref?: React.Ref<HTMLDivElement>;
107
+ /** Requests that a default level of padding not be added by the default item renderer */
108
+ noPadding?: boolean | nully;
109
+ };
110
+ /** A basic list widget that supports selecting items, with optional multiselect support.
111
+ It creates a sequence of rendered list items in <>{...}</>, i.e. with no parent
112
+ element. However, each item is wrapped in a <div> in order to (1) attach a click
113
+ handler and (2) highlight the item if it is selected. */
114
+ export declare const ListCore: <T, K = T, ItemProps extends Styles = DefaultListItemProps>(p: ListCoreProps<T, K, ItemProps>) => React.JSX.Element;
115
+ export declare const ListCoreContext: React.Context<ListCoreProps<any, any, any>>;
116
+ export declare const DefaultListItem: <T, ItemProps extends DefaultListItemProps>(p: ListItemProps<T, unknown, ItemProps>) => React.JSX.Element;
117
+ export declare function defaultRenderItem(item: any, props?: {
118
+ isSelected: boolean | null;
119
+ }): React.ReactNode;
120
+ export interface ListBoxProps<T, K = T, ItemProps = Styles> extends ListCoreProps<T, K, ItemProps>, Styles {
121
+ /** Theme for this list; overrides the ambient `TableConfig` theme. */
122
+ theme?: TableTheme | string | nully;
123
+ }
124
+ /** A list of items with keyboard and mouse interface for selecting items.
125
+ TODO: add the keyboard interface! (note: we already have a keyboard
126
+ interface in `DropDownSearchBox` after dropdown occurs) */
127
+ export declare function ListBox<T, K = T, ItemProps extends Styles = DefaultListItemProps>(p: ListBoxProps<T, K, ItemProps>): React.JSX.Element;
128
+ /** The class names used to render list items (defined in datatable.css). These are stable,
129
+ * but exposed as an object so that code written against the earlier makeStyles-based API
130
+ * keeps working. Colors come from CSS variables; see `TableTheme`. */
131
+ export declare const listCoreClasses: Record<'outerDiv' | 'selected' | 'unselected' | 'parentSelected' | 'padding' | 'readOnlyWrapper', string>;
132
+ export declare function useListCoreStyles(): Record<"padding" | "outerDiv" | "selected" | "unselected" | "parentSelected" | "readOnlyWrapper", string>;
@@ -0,0 +1,243 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
+ import React, { createContext, useEffect, useMemo, useRef } from 'react';
14
+ import { coalesce, useForceUpdate, useHeldStateIfNotGivenBy, useIsReadOnlyContext, useStatefulObjectIfNotGivenBy } from './hooks.js';
15
+ import { ListVirtualizer } from './ListVirtualizer.js';
16
+ import { getReactivityAdapter, reactive, useWatched } from './reactivity.js';
17
+ import { classAndStyle } from './styles.js';
18
+ import { useThemeProps } from './theme.js';
19
+ import { cx, isCtrlOrCmd } from './util.js';
20
+ /** A basic list widget that supports selecting items, with optional multiselect support.
21
+ It creates a sequence of rendered list items in <>{...}</>, i.e. with no parent
22
+ element. However, each item is wrapped in a <div> in order to (1) attach a click
23
+ handler and (2) highlight the item if it is selected. */
24
+ export const ListCore = reactive(function ListCore(p) {
25
+ var _a, _b, _c;
26
+ const forceUpdate = useForceUpdate();
27
+ const selection = useWatched(useStatefulObjectIfNotGivenBy(p.selection, () => new Set()));
28
+ const lastClicked = useHeldStateIfNotGivenBy(p.lastClicked);
29
+ const readOnly = coalesce(p.readOnly, useIsReadOnlyContext());
30
+ const RenderItem = (_a = p.RenderItem) !== null && _a !== void 0 ? _a : DefaultListItem;
31
+ const renderItem = (_b = p.renderItem) !== null && _b !== void 0 ? _b : defaultRenderItem;
32
+ const getSelKey = (_c = p.getSelKey) !== null && _c !== void 0 ? _c : (x => x);
33
+ const { virtualizer, virtualGapRenderer, isFixedHeight } = useVirtualization();
34
+ // To avoid re-rendering every item on every render, the `onClick` and `tempState` props must
35
+ // not change between renders. To accomplish that, we need a ref that holds every local
36
+ // variable used by `onClick` so that `onClick` can use that instead of local variables, so
37
+ // that we can always pass the "original" version of `onClick` from the first render. Although
38
+ // this `tempState` refis used as the `tempState` prop on `RenderItem`, `RenderItem` is
39
+ // expected not to access these local variables (it only uses `selectedParents`).
40
+ const tempState = useRef({
41
+ selectedParents: undefined, onClick, listCoreProps: p, readOnly, getSelKey, selection, lastClicked, forceUpdate
42
+ });
43
+ Object.assign(tempState.current, {
44
+ listCoreProps: p, readOnly, getSelKey, selection, lastClicked, forceUpdate
45
+ });
46
+ let placeholder;
47
+ if (p.list.length === 0) {
48
+ placeholder = typeof p.placeholder === 'string'
49
+ ? _jsx("span", { style: { opacity: 0.5 }, children: p.placeholder })
50
+ : p.placeholder;
51
+ }
52
+ return (_jsxs(ListCoreContext.Provider, { value: p, children: [virtualizer
53
+ ? renderVirtualizedList()
54
+ : p.list.map((item, i) => {
55
+ if (p.filter && !p.filter(item, i)) {
56
+ return undefined;
57
+ }
58
+ else {
59
+ return renderRow(item, i, getReactKey(item, i));
60
+ }
61
+ }), placeholder] }));
62
+ function renderRow(item, index, reactKey, virtualRef) {
63
+ const selKey = getSelKey(item);
64
+ let itemProps = typeof p.itemProps === 'function' ? p.itemProps(item, index) : p.itemProps;
65
+ if (virtualRef) {
66
+ const mergedRef = mergeRefs(itemProps === null || itemProps === void 0 ? void 0 : itemProps.ref, virtualRef);
67
+ itemProps = Object.assign(Object.assign({}, (itemProps !== null && itemProps !== void 0 ? itemProps : {})), { ref: mergedRef });
68
+ }
69
+ return (_jsx(RenderItem, { item: item, isSelected: selection.has(selKey), onClick: tempState.current.onClick, reactKey: reactKey, selKey: selKey, readOnly: readOnly, renderItem: renderItem, itemProps: itemProps, tempState: tempState.current }, reactKey));
70
+ }
71
+ function getReactKey(item, index) {
72
+ if (p.getKey) {
73
+ return p.getKey(item);
74
+ }
75
+ const selKey = getSelKey(item);
76
+ if (typeof selKey === 'string' || typeof selKey === 'number') {
77
+ return selKey;
78
+ }
79
+ return index;
80
+ }
81
+ async function onClick(item, e) {
82
+ // THIS FUNCTION MUST NOT USE ANY LOCAL VARIABLES (except tempState.current) because
83
+ // the same instance of it is used during every render (so all locals may be outdated).
84
+ const s = tempState.current, p = s.listCoreProps;
85
+ if (s.readOnly)
86
+ return;
87
+ if (p.onClickItem && (await p.onClickItem.call(this, item)) === 'abort')
88
+ return;
89
+ let i = p.list.indexOf(item);
90
+ let selKey = s.getSelKey(item);
91
+ getReactivityAdapter().runInAction(() => {
92
+ let lastClickedIndex;
93
+ let allowMultiSelect = p.selMode === 'multi' || p.selMode === 'invertCtrl';
94
+ if (allowMultiSelect && e.shiftKey && (lastClickedIndex = getLastClickedIndex()) >= 0 && i >= 0) {
95
+ // Select all (and only) from lastIndex to i inclusive
96
+ let i0 = i, iLast = lastClickedIndex;
97
+ if (i0 > iLast)
98
+ (i0 = iLast, iLast = i); // swap them
99
+ s.selection.clear();
100
+ for (; i0 <= iLast; i0++) {
101
+ if (!p.filter || p.filter(p.list[i0], i0)) {
102
+ s.selection.add(getSelKey(p.list[i0]));
103
+ }
104
+ }
105
+ }
106
+ else if (allowMultiSelect && isCtrlOrCmd(e) === (p.selMode !== 'invertCtrl')) {
107
+ if (s.selection.has(selKey))
108
+ s.selection.delete(selKey);
109
+ else
110
+ s.selection.add(selKey);
111
+ }
112
+ else if (p.selMode !== 'noSelect') {
113
+ s.selection.clear();
114
+ s.selection.add(selKey);
115
+ }
116
+ s.lastClicked.set(selKey);
117
+ s.forceUpdate();
118
+ });
119
+ function getLastClickedIndex() {
120
+ let lastItem = s.lastClicked.get();
121
+ if (lastItem === undefined) {
122
+ return -1;
123
+ }
124
+ return p.list.findIndex(x => s.getSelKey(x) === lastItem);
125
+ }
126
+ }
127
+ function useVirtualization() {
128
+ var _a, _b;
129
+ const virtualize = p.virtualize;
130
+ const canVirtualize = !!(virtualize === null || virtualize === void 0 ? void 0 : virtualize.height) && ListVirtualizer.isSupportedByBrowser();
131
+ const virtualizer = useMemo(() => {
132
+ if (!canVirtualize)
133
+ return undefined;
134
+ return new ListVirtualizer({
135
+ containerRef: virtualize === null || virtualize === void 0 ? void 0 : virtualize.containerRef,
136
+ margin: virtualize === null || virtualize === void 0 ? void 0 : virtualize.margin,
137
+ height: virtualize === null || virtualize === void 0 ? void 0 : virtualize.height,
138
+ onVisibilityChange: forceUpdate,
139
+ });
140
+ }, [canVirtualize, virtualize === null || virtualize === void 0 ? void 0 : virtualize.containerRef, (_a = virtualize === null || virtualize === void 0 ? void 0 : virtualize.containerRef) === null || _a === void 0 ? void 0 : _a.current, virtualize === null || virtualize === void 0 ? void 0 : virtualize.margin, forceUpdate]);
141
+ useEffect(() => {
142
+ return () => virtualizer === null || virtualizer === void 0 ? void 0 : virtualizer.disconnect();
143
+ }, [virtualizer]);
144
+ if (virtualizer && virtualize) {
145
+ const height = virtualize.height;
146
+ virtualizer.itemHeight = typeof height === 'number' ? Math.max(height, 1) : height;
147
+ }
148
+ return {
149
+ virtualizer,
150
+ isFixedHeight: typeof (virtualize === null || virtualize === void 0 ? void 0 : virtualize.height) === 'number',
151
+ virtualGapRenderer: (_b = p.virtualGapRenderer) !== null && _b !== void 0 ? _b : ((height, ref) => _jsx("div", { ref: ref, style: { height } }))
152
+ };
153
+ }
154
+ function renderVirtualizedList() {
155
+ const v = virtualizer;
156
+ const itemsToRender = [];
157
+ for (let i = 0; i < p.list.length; i++) {
158
+ const item = p.list[i];
159
+ if (p.filter && !p.filter(item, i)) {
160
+ continue;
161
+ }
162
+ const reactKey = getReactKey(item, i);
163
+ itemsToRender.push({ item, key: reactKey, index: i, reactKey });
164
+ }
165
+ const slots = v.buildPlan(itemsToRender);
166
+ return slots.map(slot => {
167
+ if (!slot.real) {
168
+ const gapRef = slot.keys.length > 0 ? v.createObserverRef(slot.keys) : undefined;
169
+ return (_jsx(React.Fragment, { children: virtualGapRenderer(slot.height, gapRef) }, slot.key));
170
+ }
171
+ const rowRef = isFixedHeight ? undefined : v.createObserverRef(slot.key);
172
+ return renderRow(slot.item, slot.index, slot.reactKey, rowRef);
173
+ });
174
+ }
175
+ });
176
+ function mergeRefs(...refs) {
177
+ const activeRefs = refs.filter(Boolean);
178
+ if (activeRefs.length === 0) {
179
+ return undefined;
180
+ }
181
+ if (activeRefs.length === 1) {
182
+ return activeRefs[0];
183
+ }
184
+ return (value) => {
185
+ for (const ref of activeRefs) {
186
+ if (typeof ref === 'function') {
187
+ ref(value);
188
+ }
189
+ else {
190
+ ref.current = value;
191
+ }
192
+ }
193
+ };
194
+ }
195
+ export const ListCoreContext = createContext({ list: [] });
196
+ export const DefaultListItem = reactive(function DefaultListItem(p) {
197
+ var _a, _b, _c;
198
+ const styles = useListCoreStyles();
199
+ let className = p.isSelected ? styles.selected : styles.unselected;
200
+ if (!((_a = p.itemProps) === null || _a === void 0 ? void 0 : _a.noPadding))
201
+ className = cx(className, styles.padding);
202
+ const styling = classAndStyle(p.itemProps, className);
203
+ const _d = ((_b = p.itemProps) !== null && _b !== void 0 ? _b : {}), { ref: itemRef } = _d, itemProps = __rest(_d, ["ref"]);
204
+ const rendered = ((_c = p.renderItem) !== null && _c !== void 0 ? _c : defaultRenderItem)(p.item, p);
205
+ let wrapped = (_jsx("div", Object.assign({ ref: itemRef }, itemProps, { onClick: e => p.onClick(p.item, e) }, styling, { children: rendered })));
206
+ if (p.readOnly) {
207
+ // Put a filter on top that grays out the contents of the row
208
+ wrapped = _jsxs("div", { style: { position: 'relative' }, children: [wrapped, _jsx("div", { className: styles.readOnlyWrapper })] });
209
+ }
210
+ return wrapped;
211
+ });
212
+ export function defaultRenderItem(item, props) {
213
+ if (typeof (item === null || item === void 0 ? void 0 : item.name) === 'string')
214
+ return _jsx("span", { children: item.name });
215
+ else if (typeof (item === null || item === void 0 ? void 0 : item.toString) === 'function')
216
+ return _jsx("span", { children: item.toString() });
217
+ else
218
+ return _jsx("span", { children: item });
219
+ }
220
+ ;
221
+ /** A list of items with keyboard and mouse interface for selecting items.
222
+ TODO: add the keyboard interface! (note: we already have a keyboard
223
+ interface in `DropDownSearchBox` after dropdown occurs) */
224
+ export function ListBox(p) {
225
+ const styles = useListCoreStyles();
226
+ const theme = useThemeProps(p.theme);
227
+ return (_jsx("div", Object.assign({ tabIndex: 0 }, classAndStyle(p, cx(styles.outerDiv, theme.className), theme.style), { children: _jsx(ListCore, Object.assign({}, p)) })));
228
+ }
229
+ /** The class names used to render list items (defined in datatable.css). These are stable,
230
+ * but exposed as an object so that code written against the earlier makeStyles-based API
231
+ * keeps working. Colors come from CSS variables; see `TableTheme`. */
232
+ export const listCoreClasses = {
233
+ outerDiv: 'dtbl-list-outer',
234
+ selected: 'dtbl-selected',
235
+ unselected: 'dtbl-unselected',
236
+ // Used by TreeList.tsx and DataTable.tsx
237
+ parentSelected: 'dtbl-parent-selected',
238
+ padding: 'dtbl-item-padding',
239
+ readOnlyWrapper: 'dtbl-readonly-overlay',
240
+ };
241
+ export function useListCoreStyles() {
242
+ return listCoreClasses;
243
+ }
@@ -0,0 +1,59 @@
1
+ import { RefObject } from 'react';
2
+ export interface VirtualizationSettings<T> {
3
+ /** Per-row virtualization height in pixels, or a function that returns a height for a
4
+ * given row item. The virtualizer renders groups of off-screen rows as empty placeholders
5
+ * based on this height until they come into view. If this is a function that returns
6
+ * `false` for an item, the item is left unvirtualized (always rendered). A height of 0
7
+ * is not supported and acts like false. **/
8
+ height: number | ((item: T) => number | false);
9
+ /** Vertical buffer margin that makes rows "real" before they enter the viewport,
10
+ * to reduce pop-in and allow a wider range of Ctrl+F searching. */
11
+ margin?: number;
12
+ /** This is only necessary when using virtualization with `DataTableCore` (not
13
+ * `DataTable`). It's the ref of the scrolling container in which the `DataTableCore`
14
+ * is placed, which is used as the root element for row virtualization. */
15
+ containerRef?: RefObject<HTMLElement> | RefObject<HTMLDivElement>;
16
+ }
17
+ export type ListVirtualItem<T, K> = {
18
+ item: T;
19
+ key: K;
20
+ index: number;
21
+ reactKey: string | number;
22
+ };
23
+ export type ListVirtualSlot<T, K> = {
24
+ real: true;
25
+ item: T;
26
+ key: K;
27
+ index: number;
28
+ reactKey: string | number;
29
+ } | {
30
+ real: false;
31
+ height: number;
32
+ key: string;
33
+ keys: K[];
34
+ };
35
+ export type ListVirtualizerOptions<T> = VirtualizationSettings<T> & {
36
+ onVisibilityChange: () => void;
37
+ };
38
+ export declare class ListVirtualizer<T, K> {
39
+ itemHeight: VirtualizationSettings<T>['height'];
40
+ visible: Set<K>;
41
+ private readonly containerRef?;
42
+ private readonly margin;
43
+ private readonly onVisibilityChange;
44
+ private readonly nodeToKeys;
45
+ private readonly observer?;
46
+ static isSupportedByBrowser(): boolean;
47
+ constructor(options: ListVirtualizerOptions<T>);
48
+ disconnect(): void;
49
+ createObserverRef(keys: K | K[]): ((element: Element | null) => void) | undefined;
50
+ buildPlan(items: ListVirtualItem<T, K>[]): ListVirtualSlot<T, K>[];
51
+ private buildGap;
52
+ private getItemHeight;
53
+ private observe;
54
+ private unobserve;
55
+ private onIntersect;
56
+ private getChunkSize;
57
+ private getDefaultRowHeight;
58
+ private markVisibleRange;
59
+ }