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.
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/dist/DataTable.d.ts +583 -0
- package/dist/DataTable.js +620 -0
- package/dist/Draggable.d.ts +35 -0
- package/dist/Draggable.js +91 -0
- package/dist/ListBox.d.ts +132 -0
- package/dist/ListBox.js +243 -0
- package/dist/ListVirtualizer.d.ts +59 -0
- package/dist/ListVirtualizer.js +180 -0
- package/dist/TreeList.d.ts +196 -0
- package/dist/TreeList.js +300 -0
- package/dist/config.d.ts +55 -0
- package/dist/config.js +33 -0
- package/dist/datatable.css +161 -0
- package/dist/hooks.d.ts +27 -0
- package/dist/hooks.js +115 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +15 -0
- package/dist/interfaces.d.ts +269 -0
- package/dist/interfaces.js +3 -0
- package/dist/mobx-adapter.d.ts +12 -0
- package/dist/mobx-adapter.js +21 -0
- package/dist/reactivity.d.ts +112 -0
- package/dist/reactivity.js +155 -0
- package/dist/styles.d.ts +64 -0
- package/dist/styles.js +86 -0
- package/dist/theme.d.ts +160 -0
- package/dist/theme.js +239 -0
- package/dist/util.d.ts +28 -0
- package/dist/util.js +45 -0
- package/package.json +137 -4
- package/src/DataTable.tsx +1332 -0
- package/src/Draggable.tsx +133 -0
- package/src/ListBox.tsx +423 -0
- package/src/ListVirtualizer.ts +234 -0
- package/src/TreeList.tsx +590 -0
- package/src/config.tsx +96 -0
- package/src/css.d.ts +1 -0
- package/src/datatable.css +161 -0
- package/src/hooks.ts +138 -0
- package/src/index.ts +15 -0
- package/src/interfaces.ts +309 -0
- package/src/mobx-adapter.ts +24 -0
- package/src/reactivity.ts +222 -0
- package/src/styles.tsx +102 -0
- package/src/theme.ts +346 -0
- package/src/util.ts +60 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Holder } from 'holders';
|
|
4
|
+
|
|
5
|
+
import { useUnmountHandler } from './hooks.js';
|
|
6
|
+
import { reactive } from './reactivity.js';
|
|
7
|
+
import { omit } from './util.js';
|
|
8
|
+
|
|
9
|
+
export type DraggableProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
10
|
+
xValue?: Holder<number>,
|
|
11
|
+
yValue?: Holder<number>,
|
|
12
|
+
/** Amount by which to change xValue for each pixel the mouse moves horizontally (default 1) */
|
|
13
|
+
xPerPixel?: number | (() => number),
|
|
14
|
+
/** Amount by which to change yValue for each pixel the mouse moves vertically (default 1) */
|
|
15
|
+
yPerPixel?: number | (() => number),
|
|
16
|
+
|
|
17
|
+
/** If provided, xValue / yValue are not modified until this function returns true. */
|
|
18
|
+
hasMovedEnough?(deltaX: number, deltaY: number): boolean,
|
|
19
|
+
/** Called after the left mouse button is clicked */
|
|
20
|
+
onDragButtonDown?(event: React.PointerEvent<HTMLDivElement>): void,
|
|
21
|
+
/** Called when dragging stops (and the mouse pointer moved enough to detect that dragging
|
|
22
|
+
* started), either because the left mouse button is released or the user pressed Escape
|
|
23
|
+
* or mouse capture was lost. */
|
|
24
|
+
onStopDrag?(event?: PointerEvent|KeyboardEvent): void,
|
|
25
|
+
/** Called when dragging starts (and the mouse pointer moved enough to detect that dragging
|
|
26
|
+
* started). */
|
|
27
|
+
onStartDrag?(): void;
|
|
28
|
+
/** Called when the left mouse button is released without sufficient movement to initiate a drag. */
|
|
29
|
+
onClick?(event: PointerEvent): void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** An unstyled div that can be clicked and dragged to change an "X" or "Y" value,
|
|
33
|
+
* e.g. representing a priority on PrioritiesDiagram, or the size of a modal.
|
|
34
|
+
*
|
|
35
|
+
* Draggable does not know or care about the meaning of the x and y values it changes.
|
|
36
|
+
* One example usage can be found in `SplitPane`, where it is used to alter the size
|
|
37
|
+
* of a different element.
|
|
38
|
+
*
|
|
39
|
+
* Typically one should set a cursor, e.g. with `style={{ cursor: 'col-resize' }}`,
|
|
40
|
+
* and also `tabIndex={0}` so that the drag handle can get the keyboard focus,
|
|
41
|
+
* which allows the escape key to cancel the drag by setting the x/y value back to
|
|
42
|
+
* what it was when the drag started.
|
|
43
|
+
*/
|
|
44
|
+
export const Draggable = reactive(function Draggable(p: DraggableProps) {
|
|
45
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
46
|
+
const [dragState] = useState({
|
|
47
|
+
movedEnough: false,
|
|
48
|
+
initialX: Number.NaN, // clientX
|
|
49
|
+
initialY: Number.NaN, // clientY
|
|
50
|
+
initialXValue: 0,
|
|
51
|
+
initialYValue: 0,
|
|
52
|
+
stopDragging: (event?: PointerEvent|KeyboardEvent) => { },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
useUnmountHandler(() => dragState.stopDragging());
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div ref={ref}
|
|
59
|
+
{...omit(p,
|
|
60
|
+
'xValue', 'yValue', 'xPerPixel', 'yPerPixel',
|
|
61
|
+
'onStopDrag', 'onStartDrag', 'onDragButtonDown', 'hasMovedEnough', 'onClick')}
|
|
62
|
+
onPointerDown={(p.xValue || p.yValue) && onPointerDown}
|
|
63
|
+
/>);
|
|
64
|
+
|
|
65
|
+
function onPointerDown(event: React.PointerEvent<HTMLDivElement>) {
|
|
66
|
+
p.onPointerDown?.(event);
|
|
67
|
+
|
|
68
|
+
if (event.button === 0) { // Left button
|
|
69
|
+
// prevent regular HTML drag & drop (draggable={false} doesn't work 🤷♂️)
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
// Manually focus so we capture the escape key (preventDefault blocks focus)
|
|
72
|
+
ref.current!.focus();
|
|
73
|
+
|
|
74
|
+
dragState.initialX = event.clientX;
|
|
75
|
+
dragState.initialY = event.clientY;
|
|
76
|
+
dragState.initialXValue = p.xValue?.get() ?? 0;
|
|
77
|
+
dragState.initialYValue = p.yValue?.get() ?? 0;
|
|
78
|
+
dragState.movedEnough = false;
|
|
79
|
+
dragState.stopDragging = stopDragging;
|
|
80
|
+
|
|
81
|
+
ref.current!.setPointerCapture(event.pointerId);
|
|
82
|
+
|
|
83
|
+
document.addEventListener('lostpointercapture', stopDragging);
|
|
84
|
+
document.addEventListener('pointermove', onMouseMoveDuringDrag);
|
|
85
|
+
ref.current!.addEventListener('keydown', onKeyDownDuringDrag);
|
|
86
|
+
|
|
87
|
+
p.onDragButtonDown?.(event);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function stopDragging(event?: PointerEvent|KeyboardEvent) {
|
|
91
|
+
document.removeEventListener('lostpointercapture', stopDragging);
|
|
92
|
+
document.removeEventListener('pointermove', onMouseMoveDuringDrag);
|
|
93
|
+
ref.current?.removeEventListener('keydown', onKeyDownDuringDrag);
|
|
94
|
+
|
|
95
|
+
if (dragState.movedEnough) {
|
|
96
|
+
p.onStopDrag?.(event);
|
|
97
|
+
} else if (event?.type === 'lostpointercapture') {
|
|
98
|
+
p.onClick?.(event as PointerEvent);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function onKeyDownDuringDrag(event: KeyboardEvent) {
|
|
103
|
+
if (event.key === 'Escape') {
|
|
104
|
+
event.stopPropagation(); // prevent Modal from closing (if any)
|
|
105
|
+
dragState.stopDragging(event);
|
|
106
|
+
|
|
107
|
+
// Restore value(s) to what they were when the drag started
|
|
108
|
+
p.xValue?.set(dragState.initialXValue);
|
|
109
|
+
p.yValue?.set(dragState.initialYValue);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function onMouseMoveDuringDrag(event: PointerEvent) {
|
|
115
|
+
let deltaX = event.clientX - dragState.initialX;
|
|
116
|
+
let deltaY = event.clientY - dragState.initialY;
|
|
117
|
+
|
|
118
|
+
if (dragState.movedEnough || !p.hasMovedEnough || p.hasMovedEnough(deltaX, deltaY)) {
|
|
119
|
+
if (!dragState.movedEnough) {
|
|
120
|
+
dragState.movedEnough = true;
|
|
121
|
+
p.onStartDrag?.();
|
|
122
|
+
}
|
|
123
|
+
let newX = dragState.initialXValue + deltaX * (get(p.xPerPixel) ?? 1);
|
|
124
|
+
let newY = dragState.initialYValue + deltaY * (get(p.yPerPixel) ?? 1);
|
|
125
|
+
p.xValue?.set(newX);
|
|
126
|
+
p.yValue?.set(newY);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
function get(x: number | undefined | (() => number)): number | undefined {
|
|
132
|
+
return typeof x === 'function' ? x() : x;
|
|
133
|
+
}
|
package/src/ListBox.tsx
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import React, { createContext, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Holder } from 'holders';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
coalesce, useForceUpdate, useHeldStateIfNotGivenBy, useIsReadOnlyContext,
|
|
7
|
+
useStatefulObjectIfNotGivenBy
|
|
8
|
+
} from './hooks.js';
|
|
9
|
+
import { ISet } from './interfaces.js';
|
|
10
|
+
import {
|
|
11
|
+
ListVirtualItem, ListVirtualizer, ListVirtualSlot, VirtualizationSettings
|
|
12
|
+
} from './ListVirtualizer.js';
|
|
13
|
+
import { getReactivityAdapter, reactive, useWatched } from './reactivity.js';
|
|
14
|
+
import { classAndStyle, Styles } from './styles.js';
|
|
15
|
+
import { TableTheme, useThemeProps } from './theme.js';
|
|
16
|
+
import { cx, isCtrlOrCmd, nully } from './util.js';
|
|
17
|
+
|
|
18
|
+
import type { TreeItemProps, TreeItem } from './TreeList.js';
|
|
19
|
+
|
|
20
|
+
// List => TreeList => DataTable
|
|
21
|
+
|
|
22
|
+
/** Main properties of a `<ListBox>` or `<ListCore>`. The only required prop is `list`.
|
|
23
|
+
*
|
|
24
|
+
* T: Type of each item in the list
|
|
25
|
+
* K: Type of the "sel key" for each item (see `selection` and `getSelKey`)
|
|
26
|
+
* ItemProps: type of additional props passed to each item, e.g. for styling
|
|
27
|
+
*/
|
|
28
|
+
export interface ListCoreProps<T, K=T, ItemProps=DefaultListItemProps> {
|
|
29
|
+
/** List of items to display. */
|
|
30
|
+
list: T[];
|
|
31
|
+
/** This is the set of selected items. If not provided, items can still be
|
|
32
|
+
* selected but the selection state is internal to the list.
|
|
33
|
+
* If the caller wishes to re-render on changes to the set of selected
|
|
34
|
+
* items, it can use a MobX `observable(set)` rather than a plain `Set<K>`.
|
|
35
|
+
* `ListCore` uses `runInAction` just in case it's an `ObservableSet`.
|
|
36
|
+
* If type `K` isn't the same as `T`, you must provide a `getSelKey` prop. */
|
|
37
|
+
selection?: ISet<K>;
|
|
38
|
+
/** Whether to support selecting multiple things (default: no). 'noCtrl'
|
|
39
|
+
* mode is a multiselect mode in which each click is treated as if the
|
|
40
|
+
* Ctrl key (⌘ on Mac) is pressed; if it is actually pressed then a single
|
|
41
|
+
* item is selected and the rest of the selection is cleared.
|
|
42
|
+
*
|
|
43
|
+
* 'noSelect' mode prevents the selection from being changed by the user,
|
|
44
|
+
* so if the app itself does not select anything, nothing can be selected. */
|
|
45
|
+
selMode?: 'single'|'multi'|'invertCtrl'|'noSelect';
|
|
46
|
+
/** Called to render a T value. If this is not provided, `defaultRenderItem` is
|
|
47
|
+
* called to render each item. */
|
|
48
|
+
renderItem?: (item: T, props: ListItemProps<T, K, ItemProps>) => React.ReactNode;
|
|
49
|
+
/** Component type used to render the entire row (including an outer `div` or `tr`).
|
|
50
|
+
* This is expected to forward a ref so that arrow keys can scroll the item into
|
|
51
|
+
* view. */
|
|
52
|
+
RenderItem?: React.ComponentType<ListItemProps<T, K, ItemProps>>;
|
|
53
|
+
/** This is called immediately when an item is clicked, and if it returns
|
|
54
|
+
'abort' the click on the item will be ignored rather than changing the
|
|
55
|
+
selection. */
|
|
56
|
+
onClickItem?: (this: ModifiedListItemProps<T, K, ItemProps>, item: T) => void|'abort' | Promise<void|'abort'>;
|
|
57
|
+
/** Lists in React require a `key` prop. This function is called to get that key.
|
|
58
|
+
* If this is omitted, the index is used as the key. NOTE: in React 18 it is
|
|
59
|
+
* documented that "Non-unique keys may cause children to be duplicated and/or
|
|
60
|
+
* omitted" and duplicate list items have been observed in this context when there
|
|
61
|
+
* are duplicate keys. If you cannot guarantee unique keys, it is better not to
|
|
62
|
+
* provide `getKey` at all (but this can cause its own problems, e.g. items losing
|
|
63
|
+
* focus when the list order changes). */
|
|
64
|
+
getKey?: (item: T) => number|string;
|
|
65
|
+
/** If the items are stored in wrappers that can change between renders,
|
|
66
|
+
* provide this function to get a subobject or id of the item that does
|
|
67
|
+
* not change between renders. These "sel keys" are used in `selection`
|
|
68
|
+
* (and in `expandedParents` in the related `TreeList` component). */
|
|
69
|
+
getSelKey?: (item: T) => K;
|
|
70
|
+
/** This is called for each item, if you provide it. Items for which this function
|
|
71
|
+
* returns false are hidden. */
|
|
72
|
+
filter?: (item: T, index: number) => boolean;
|
|
73
|
+
/** The most recently clicked item */
|
|
74
|
+
lastClicked?: Holder<K|undefined>;
|
|
75
|
+
/** props that should apply to each item (usually Styles: `className` and `style`).
|
|
76
|
+
* If `RenderItem` is provided, these props are forwarded to it. In a `<DataTable>`,
|
|
77
|
+
* any styles in this object are applied to the row as a whole, not to each cell. */
|
|
78
|
+
itemProps?: ItemProps | ((item: T, i: number) => ItemProps | undefined);
|
|
79
|
+
/** If true, the whole list is read-only. If `undefined`, `useIsReadOnlyContext`
|
|
80
|
+
* is used to decide whether the whole list is read-only. */
|
|
81
|
+
readOnly?: boolean;
|
|
82
|
+
/** These settings enable virtualization, which speeds up list rendering by not rendering
|
|
83
|
+
* off-screen items. */
|
|
84
|
+
virtualize?: VirtualizationSettings<T>;
|
|
85
|
+
/** Called when virtualization is enabled to render a placeholder element for a
|
|
86
|
+
* virtualized gap. Set the gap element's `ref` prop to the `ref` parameter; the list uses
|
|
87
|
+
* the `ref` to observe the gap so it can become real when it nears the viewport. */
|
|
88
|
+
virtualGapRenderer?: (height: number, ref?: React.Ref<Element>) => React.ReactNode;
|
|
89
|
+
/** Content to display when the list is empty. If this is a string, it's "grayed out"
|
|
90
|
+
* using `opacity: 0.5`. */
|
|
91
|
+
placeholder?: React.ReactNode;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// This is the type of `this` in `onClick`. Omit certain prop(s) so that `DataTable`/`TreeList`
|
|
95
|
+
// type-check when they calls `onClick`.
|
|
96
|
+
export type ModifiedListItemProps<T, K, ItemProps> = Omit<ListItemProps<T, K, ItemProps>, 'renderItem'>;
|
|
97
|
+
|
|
98
|
+
/** Props passed to the `ListCoreProps.RenderItem` component, if you provide one. */
|
|
99
|
+
export interface ListItemProps<T, K=T, ItemProps=DefaultListItemProps> {
|
|
100
|
+
/** An item to be rendered */
|
|
101
|
+
item: T;
|
|
102
|
+
/** Whether this item is selected. The special value `null` indicates that the selection
|
|
103
|
+
* box above a list or dropdown list is being rendered. */
|
|
104
|
+
isSelected: boolean|null;
|
|
105
|
+
/** A function to call when the list item is clicked. It selects the item. */
|
|
106
|
+
onClick(this: ModifiedListItemProps<T, K, ItemProps>, item: T, event: React.MouseEvent<HTMLDivElement>): void;
|
|
107
|
+
/** The React key for this item (from `ListCoreProps.getKey()`).
|
|
108
|
+
* (React doesn't allow it to be called `key`.) */
|
|
109
|
+
reactKey: number|string;
|
|
110
|
+
/** The "sel key" for this item (from `ListCoreProps.getSelKey()`) */
|
|
111
|
+
selKey: K;
|
|
112
|
+
/** This is true if the entire list should be rendered as read-only */
|
|
113
|
+
readOnly: boolean;
|
|
114
|
+
/** Item renderer provided to `ListCoreProps`, if any */
|
|
115
|
+
renderItem: (item: T, props: ListItemProps<T, K, ItemProps>) => React.ReactNode;
|
|
116
|
+
/** Item properties from `ListCoreProps.itemProps` */
|
|
117
|
+
itemProps?: ItemProps;
|
|
118
|
+
/** Used by TreeList and DataTable */
|
|
119
|
+
tempState: { selectedParents?: Pick<ISet<T>, 'add' | 'delete' | 'has'>; };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type DefaultListItemProps = Styles & {
|
|
123
|
+
ref?: React.Ref<HTMLDivElement>;
|
|
124
|
+
/** Requests that a default level of padding not be added by the default item renderer */
|
|
125
|
+
noPadding?: boolean|nully;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** A basic list widget that supports selecting items, with optional multiselect support.
|
|
129
|
+
It creates a sequence of rendered list items in <>{...}</>, i.e. with no parent
|
|
130
|
+
element. However, each item is wrapped in a <div> in order to (1) attach a click
|
|
131
|
+
handler and (2) highlight the item if it is selected. */
|
|
132
|
+
export const ListCore = reactive(function ListCore<T, K=T, ItemProps extends Styles = DefaultListItemProps>(
|
|
133
|
+
p: ListCoreProps<T, K, ItemProps>
|
|
134
|
+
) {
|
|
135
|
+
const forceUpdate = useForceUpdate();
|
|
136
|
+
const selection = useWatched(useStatefulObjectIfNotGivenBy(p.selection, () => new Set<K>()));
|
|
137
|
+
const lastClicked = useHeldStateIfNotGivenBy<K>(p.lastClicked);
|
|
138
|
+
const readOnly = coalesce(p.readOnly, useIsReadOnlyContext());
|
|
139
|
+
const RenderItem = p.RenderItem ?? DefaultListItem as React.ComponentType<ListItemProps<T, K, any>>;
|
|
140
|
+
const renderItem = p.renderItem ?? defaultRenderItem;
|
|
141
|
+
const getSelKey = p.getSelKey ?? (x => x as unknown as K);
|
|
142
|
+
|
|
143
|
+
type VirtualKey = K | string | number;
|
|
144
|
+
const { virtualizer, virtualGapRenderer, isFixedHeight } = useVirtualization();
|
|
145
|
+
|
|
146
|
+
// To avoid re-rendering every item on every render, the `onClick` and `tempState` props must
|
|
147
|
+
// not change between renders. To accomplish that, we need a ref that holds every local
|
|
148
|
+
// variable used by `onClick` so that `onClick` can use that instead of local variables, so
|
|
149
|
+
// that we can always pass the "original" version of `onClick` from the first render. Although
|
|
150
|
+
// this `tempState` refis used as the `tempState` prop on `RenderItem`, `RenderItem` is
|
|
151
|
+
// expected not to access these local variables (it only uses `selectedParents`).
|
|
152
|
+
const tempState = useRef({
|
|
153
|
+
selectedParents: undefined, onClick, listCoreProps: p, readOnly, getSelKey, selection, lastClicked, forceUpdate
|
|
154
|
+
});
|
|
155
|
+
Object.assign(tempState.current, {
|
|
156
|
+
listCoreProps: p, readOnly, getSelKey, selection, lastClicked, forceUpdate
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
let placeholder;
|
|
161
|
+
if (p.list.length === 0) {
|
|
162
|
+
placeholder = typeof p.placeholder === 'string'
|
|
163
|
+
? <span style={{ opacity: 0.5 }}>{p.placeholder}</span>
|
|
164
|
+
: p.placeholder;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<ListCoreContext.Provider value={p}>
|
|
169
|
+
{virtualizer
|
|
170
|
+
? renderVirtualizedList()
|
|
171
|
+
: p.list.map((item, i) => {
|
|
172
|
+
if (p.filter && !p.filter(item, i)) {
|
|
173
|
+
return undefined;
|
|
174
|
+
} else {
|
|
175
|
+
return renderRow(item, i, getReactKey(item, i));
|
|
176
|
+
}
|
|
177
|
+
})}
|
|
178
|
+
{placeholder}
|
|
179
|
+
</ListCoreContext.Provider>);
|
|
180
|
+
|
|
181
|
+
function renderRow(
|
|
182
|
+
item: T,
|
|
183
|
+
index: number,
|
|
184
|
+
reactKey: number|string,
|
|
185
|
+
virtualRef?: React.Ref<Element>
|
|
186
|
+
) {
|
|
187
|
+
const selKey = getSelKey(item);
|
|
188
|
+
let itemProps = typeof p.itemProps === 'function' ? p.itemProps(item, index) : p.itemProps;
|
|
189
|
+
if (virtualRef) {
|
|
190
|
+
const mergedRef = mergeRefs(
|
|
191
|
+
(itemProps as DefaultListItemProps | undefined)?.ref,
|
|
192
|
+
virtualRef
|
|
193
|
+
);
|
|
194
|
+
itemProps = {
|
|
195
|
+
...(itemProps ?? {}),
|
|
196
|
+
ref: mergedRef,
|
|
197
|
+
} as unknown as ItemProps;
|
|
198
|
+
}
|
|
199
|
+
return (<RenderItem
|
|
200
|
+
item={item}
|
|
201
|
+
isSelected={selection.has(selKey)}
|
|
202
|
+
onClick={tempState.current.onClick}
|
|
203
|
+
key={reactKey} reactKey={reactKey}
|
|
204
|
+
selKey={selKey}
|
|
205
|
+
readOnly={readOnly}
|
|
206
|
+
renderItem={renderItem}
|
|
207
|
+
itemProps={itemProps}
|
|
208
|
+
tempState={tempState.current}/>);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function getReactKey(item: T, index: number): number|string {
|
|
212
|
+
if (p.getKey) {
|
|
213
|
+
return p.getKey(item);
|
|
214
|
+
}
|
|
215
|
+
const selKey = getSelKey(item);
|
|
216
|
+
if (typeof selKey === 'string' || typeof selKey === 'number') {
|
|
217
|
+
return selKey;
|
|
218
|
+
}
|
|
219
|
+
return index;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function onClick(this: ModifiedListItemProps<T, K, ItemProps>, item: T, e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
|
|
223
|
+
// THIS FUNCTION MUST NOT USE ANY LOCAL VARIABLES (except tempState.current) because
|
|
224
|
+
// the same instance of it is used during every render (so all locals may be outdated).
|
|
225
|
+
const s = tempState.current, p = s.listCoreProps;
|
|
226
|
+
|
|
227
|
+
if (s.readOnly)
|
|
228
|
+
return;
|
|
229
|
+
|
|
230
|
+
if (p.onClickItem && (await p.onClickItem.call(this, item)) === 'abort')
|
|
231
|
+
return;
|
|
232
|
+
|
|
233
|
+
let i = p.list.indexOf(item);
|
|
234
|
+
let selKey = s.getSelKey(item);
|
|
235
|
+
|
|
236
|
+
getReactivityAdapter().runInAction(() => {
|
|
237
|
+
let lastClickedIndex: number;
|
|
238
|
+
let allowMultiSelect = p.selMode === 'multi' || p.selMode === 'invertCtrl';
|
|
239
|
+
if (allowMultiSelect && e.shiftKey && (lastClickedIndex = getLastClickedIndex()) >= 0 && i >= 0) {
|
|
240
|
+
// Select all (and only) from lastIndex to i inclusive
|
|
241
|
+
let i0 = i, iLast = lastClickedIndex;
|
|
242
|
+
if (i0 > iLast)
|
|
243
|
+
(i0 = iLast, iLast = i); // swap them
|
|
244
|
+
s.selection.clear();
|
|
245
|
+
for (; i0 <= iLast; i0++) {
|
|
246
|
+
if (!p.filter || p.filter(p.list[i0], i0)) {
|
|
247
|
+
s.selection.add(getSelKey(p.list[i0]));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
} else if (allowMultiSelect && isCtrlOrCmd(e) === (p.selMode !== 'invertCtrl')) {
|
|
251
|
+
if (s.selection.has(selKey))
|
|
252
|
+
s.selection.delete(selKey);
|
|
253
|
+
else
|
|
254
|
+
s.selection.add(selKey);
|
|
255
|
+
} else if (p.selMode !== 'noSelect') {
|
|
256
|
+
s.selection.clear();
|
|
257
|
+
s.selection.add(selKey);
|
|
258
|
+
}
|
|
259
|
+
s.lastClicked.set(selKey);
|
|
260
|
+
s.forceUpdate();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
function getLastClickedIndex() {
|
|
264
|
+
let lastItem = s.lastClicked.get();
|
|
265
|
+
if (lastItem === undefined) {
|
|
266
|
+
return -1;
|
|
267
|
+
}
|
|
268
|
+
return p.list.findIndex(x => s.getSelKey(x) === lastItem);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function useVirtualization() {
|
|
273
|
+
const virtualize = p.virtualize;
|
|
274
|
+
const canVirtualize = !!virtualize?.height && ListVirtualizer.isSupportedByBrowser();
|
|
275
|
+
const virtualizer = useMemo(() => {
|
|
276
|
+
if (!canVirtualize)
|
|
277
|
+
return undefined;
|
|
278
|
+
|
|
279
|
+
return new ListVirtualizer<T, VirtualKey>({
|
|
280
|
+
containerRef: virtualize?.containerRef,
|
|
281
|
+
margin: virtualize?.margin,
|
|
282
|
+
height: virtualize?.height,
|
|
283
|
+
onVisibilityChange: forceUpdate,
|
|
284
|
+
});
|
|
285
|
+
}, [canVirtualize, virtualize?.containerRef, virtualize?.containerRef?.current, virtualize?.margin, forceUpdate]);
|
|
286
|
+
|
|
287
|
+
useEffect(() => {
|
|
288
|
+
return () => virtualizer?.disconnect();
|
|
289
|
+
}, [virtualizer]);
|
|
290
|
+
|
|
291
|
+
if (virtualizer && virtualize) {
|
|
292
|
+
const height = virtualize.height;
|
|
293
|
+
virtualizer.itemHeight = typeof height === 'number' ? Math.max(height, 1) : height;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
virtualizer,
|
|
298
|
+
isFixedHeight: typeof virtualize?.height === 'number',
|
|
299
|
+
virtualGapRenderer: p.virtualGapRenderer ??
|
|
300
|
+
((height: number, ref?: React.Ref<Element>) =>
|
|
301
|
+
<div ref={ref as React.Ref<HTMLDivElement>} style={{ height }}/>)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function renderVirtualizedList() {
|
|
306
|
+
const v = virtualizer!;
|
|
307
|
+
const itemsToRender: ListVirtualItem<T, VirtualKey>[] = [];
|
|
308
|
+
for (let i = 0; i < p.list.length; i++) {
|
|
309
|
+
const item = p.list[i];
|
|
310
|
+
if (p.filter && !p.filter(item, i)) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const reactKey = getReactKey(item, i);
|
|
314
|
+
itemsToRender.push({ item, key: reactKey, index: i, reactKey });
|
|
315
|
+
}
|
|
316
|
+
const slots: ListVirtualSlot<T, VirtualKey>[] = v.buildPlan(itemsToRender);
|
|
317
|
+
return slots.map(slot => {
|
|
318
|
+
if (!slot.real) {
|
|
319
|
+
const gapRef = slot.keys.length > 0 ? v.createObserverRef(slot.keys) : undefined;
|
|
320
|
+
return (
|
|
321
|
+
<React.Fragment key={slot.key}>
|
|
322
|
+
{virtualGapRenderer(slot.height, gapRef)}
|
|
323
|
+
</React.Fragment>
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const rowRef = isFixedHeight ? undefined : v.createObserverRef(slot.key);
|
|
328
|
+
return renderRow(slot.item, slot.index, slot.reactKey, rowRef);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
function mergeRefs<T>(...refs: Array<React.Ref<T> | undefined>) {
|
|
334
|
+
const activeRefs = refs.filter(Boolean) as React.Ref<T>[];
|
|
335
|
+
if (activeRefs.length === 0) {
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
if (activeRefs.length === 1) {
|
|
339
|
+
return activeRefs[0];
|
|
340
|
+
}
|
|
341
|
+
return (value: T | null) => {
|
|
342
|
+
for (const ref of activeRefs) {
|
|
343
|
+
if (typeof ref === 'function') {
|
|
344
|
+
ref(value);
|
|
345
|
+
} else {
|
|
346
|
+
(ref as React.MutableRefObject<T | null>).current = value;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export const ListCoreContext = createContext<ListCoreProps<any, any, any>>({ list: [] });
|
|
353
|
+
|
|
354
|
+
export const DefaultListItem = reactive(function DefaultListItem<T, ItemProps extends DefaultListItemProps>(
|
|
355
|
+
p: ListItemProps<T,unknown,ItemProps>
|
|
356
|
+
) {
|
|
357
|
+
const styles = useListCoreStyles();
|
|
358
|
+
let className = p.isSelected ? styles.selected : styles.unselected;
|
|
359
|
+
if (!p.itemProps?.noPadding)
|
|
360
|
+
className = cx(className, styles.padding);
|
|
361
|
+
const styling = classAndStyle(p.itemProps, className);
|
|
362
|
+
const { ref: itemRef, ...itemProps } = (p.itemProps ?? {}) as DefaultListItemProps;
|
|
363
|
+
|
|
364
|
+
const rendered = (p.renderItem ?? defaultRenderItem)(p.item, p);
|
|
365
|
+
let wrapped = (
|
|
366
|
+
<div ref={itemRef} {...itemProps} onClick={e => p.onClick(p.item, e)} {...styling}>
|
|
367
|
+
{rendered}
|
|
368
|
+
</div>);
|
|
369
|
+
|
|
370
|
+
if (p.readOnly) {
|
|
371
|
+
// Put a filter on top that grays out the contents of the row
|
|
372
|
+
wrapped = <div style={{ position: 'relative' }}>
|
|
373
|
+
{wrapped}
|
|
374
|
+
<div className={styles.readOnlyWrapper}/>
|
|
375
|
+
</div>;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return wrapped;
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
export function defaultRenderItem(item: any, props?: { isSelected: boolean|null }): React.ReactNode {
|
|
382
|
+
if (typeof item?.name === 'string')
|
|
383
|
+
return <span>{item.name}</span>;
|
|
384
|
+
else if (typeof item?.toString === 'function')
|
|
385
|
+
return <span>{item.toString()}</span>;
|
|
386
|
+
else
|
|
387
|
+
return <span>{item}</span>;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export interface ListBoxProps<T, K=T, ItemProps=Styles>
|
|
391
|
+
extends ListCoreProps<T,K,ItemProps>, Styles {
|
|
392
|
+
/** Theme for this list; overrides the ambient `TableConfig` theme. */
|
|
393
|
+
theme?: TableTheme | string | nully;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/** A list of items with keyboard and mouse interface for selecting items.
|
|
397
|
+
TODO: add the keyboard interface! (note: we already have a keyboard
|
|
398
|
+
interface in `DropDownSearchBox` after dropdown occurs) */
|
|
399
|
+
export function ListBox<T, K=T, ItemProps extends Styles = DefaultListItemProps>(p: ListBoxProps<T, K, ItemProps>) {
|
|
400
|
+
const styles = useListCoreStyles();
|
|
401
|
+
const theme = useThemeProps(p.theme);
|
|
402
|
+
return (
|
|
403
|
+
<div tabIndex={0} {...classAndStyle(p, cx(styles.outerDiv, theme.className), theme.style)}>
|
|
404
|
+
<ListCore<T, K, ItemProps> {...p}/>
|
|
405
|
+
</div>);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** The class names used to render list items (defined in datatable.css). These are stable,
|
|
409
|
+
* but exposed as an object so that code written against the earlier makeStyles-based API
|
|
410
|
+
* keeps working. Colors come from CSS variables; see `TableTheme`. */
|
|
411
|
+
export const listCoreClasses: Record<'outerDiv'|'selected'|'unselected'|'parentSelected'|'padding'|'readOnlyWrapper', string> = {
|
|
412
|
+
outerDiv: 'dtbl-list-outer',
|
|
413
|
+
selected: 'dtbl-selected',
|
|
414
|
+
unselected: 'dtbl-unselected',
|
|
415
|
+
// Used by TreeList.tsx and DataTable.tsx
|
|
416
|
+
parentSelected: 'dtbl-parent-selected',
|
|
417
|
+
padding: 'dtbl-item-padding',
|
|
418
|
+
readOnlyWrapper: 'dtbl-readonly-overlay',
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
export function useListCoreStyles() {
|
|
422
|
+
return listCoreClasses;
|
|
423
|
+
}
|