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
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React, { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import type { TableTheme } from './theme.js';
|
|
3
|
+
/**
|
|
4
|
+
* TableConfig
|
|
5
|
+
* -----------
|
|
6
|
+
*
|
|
7
|
+
* App-wide (or subtree-wide) defaults for the components in this package, so that
|
|
8
|
+
* individual call sites don't need to configure icons or themes. Provide it once near the
|
|
9
|
+
* root of your app:
|
|
10
|
+
*
|
|
11
|
+
* <TableConfig theme={darkTheme}
|
|
12
|
+
* icons={{ expanded: <MyChevronDown/>, collapsed: <MyChevronRight/> }}>
|
|
13
|
+
* <App/>
|
|
14
|
+
* </TableConfig>
|
|
15
|
+
*
|
|
16
|
+
* Providers nest: an inner `TableConfig` overrides only the fields it specifies. Anything
|
|
17
|
+
* not configured falls back to built-in defaults (unicode glyph icons, light theme).
|
|
18
|
+
*/
|
|
19
|
+
export interface TableIcons {
|
|
20
|
+
/** Sort indicator on a column header sorted ascending (default: ▲). */
|
|
21
|
+
sortAscending?: ReactNode;
|
|
22
|
+
/** Sort indicator on a column header sorted descending (default: ▼). */
|
|
23
|
+
sortDescending?: ReactNode;
|
|
24
|
+
/** Icon on the expander button of an expanded tree item (default: ▾). */
|
|
25
|
+
expanded?: ReactNode;
|
|
26
|
+
/** Icon on the expander button of a collapsed tree item (default: ▸). */
|
|
27
|
+
collapsed?: ReactNode;
|
|
28
|
+
}
|
|
29
|
+
/** Props passed to `TableConfigValue.renderIconButton`. */
|
|
30
|
+
export interface TableIconButtonProps {
|
|
31
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
32
|
+
style?: CSSProperties;
|
|
33
|
+
'aria-label'?: string;
|
|
34
|
+
children?: ReactNode;
|
|
35
|
+
}
|
|
36
|
+
export interface TableConfigValue {
|
|
37
|
+
icons?: TableIcons;
|
|
38
|
+
/** Renders the tree-expander button. The default renders a plain `<button
|
|
39
|
+
* className="dtbl-expander">`; override this to use your design system's icon button
|
|
40
|
+
* (e.g. MUI's `<IconButton size="large">`). */
|
|
41
|
+
renderIconButton?: (props: TableIconButtonProps) => React.ReactElement;
|
|
42
|
+
/** Default theme for tables/lists in this subtree (see `theme.ts`). A component's own
|
|
43
|
+
* `theme` prop takes precedence. */
|
|
44
|
+
theme?: TableTheme | string;
|
|
45
|
+
}
|
|
46
|
+
export declare const TableConfigContext: React.Context<TableConfigValue>;
|
|
47
|
+
/** Provides defaults (icons, theme, ...) to all list/tree/table components in a subtree.
|
|
48
|
+
* Nesting is supported; unspecified fields are inherited from the outer provider. */
|
|
49
|
+
export declare function TableConfig(p: TableConfigValue & {
|
|
50
|
+
children: ReactNode;
|
|
51
|
+
}): React.JSX.Element;
|
|
52
|
+
/** Returns the ambient `TableConfig` with all defaults filled in. */
|
|
53
|
+
export declare function useTableConfig(): {
|
|
54
|
+
icons: Required<TableIcons>;
|
|
55
|
+
} & Required<Pick<TableConfigValue, 'renderIconButton'>> & Pick<TableConfigValue, 'theme'>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useMemo } from 'react';
|
|
3
|
+
export const TableConfigContext = createContext({});
|
|
4
|
+
/** Provides defaults (icons, theme, ...) to all list/tree/table components in a subtree.
|
|
5
|
+
* Nesting is supported; unspecified fields are inherited from the outer provider. */
|
|
6
|
+
export function TableConfig(p) {
|
|
7
|
+
const outer = useContext(TableConfigContext);
|
|
8
|
+
const merged = useMemo(() => (Object.assign(Object.assign(Object.assign(Object.assign({}, outer), (p.theme !== undefined && { theme: p.theme })), (p.renderIconButton !== undefined && { renderIconButton: p.renderIconButton })), ((p.icons || outer.icons) && { icons: Object.assign(Object.assign({}, outer.icons), p.icons) }))), [outer, p.icons, p.renderIconButton, p.theme]);
|
|
9
|
+
return _jsx(TableConfigContext.Provider, { value: merged, children: p.children });
|
|
10
|
+
}
|
|
11
|
+
/** A chevron similar to typical icon-font arrows, but with no icon-font dependency. */
|
|
12
|
+
function chevron(points) {
|
|
13
|
+
return (_jsx("svg", { className: "dtbl-glyph-svg", viewBox: "0 0 24 24", width: "1.25em", height: "1.25em", "aria-hidden": "true", fill: "none", stroke: "currentColor", strokeWidth: "2.2", strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("polyline", { points: points }) }));
|
|
14
|
+
}
|
|
15
|
+
const defaultIcons = {
|
|
16
|
+
sortAscending: chevron('6.5 14.5 12 9 17.5 14.5'),
|
|
17
|
+
sortDescending: chevron('6.5 9.5 12 15 17.5 9.5'),
|
|
18
|
+
expanded: chevron('6.5 9.5 12 15 17.5 9.5'),
|
|
19
|
+
collapsed: chevron('9.5 6.5 15 12 9.5 17.5'),
|
|
20
|
+
};
|
|
21
|
+
function defaultRenderIconButton(props) {
|
|
22
|
+
return _jsx("button", Object.assign({ type: "button", className: "dtbl-expander" }, props));
|
|
23
|
+
}
|
|
24
|
+
/** Returns the ambient `TableConfig` with all defaults filled in. */
|
|
25
|
+
export function useTableConfig() {
|
|
26
|
+
var _a;
|
|
27
|
+
const config = useContext(TableConfigContext);
|
|
28
|
+
return {
|
|
29
|
+
icons: Object.assign(Object.assign({}, defaultIcons), config.icons),
|
|
30
|
+
renderIconButton: (_a = config.renderIconButton) !== null && _a !== void 0 ? _a : defaultRenderIconButton,
|
|
31
|
+
theme: config.theme,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/* datatable-tree structural stylesheet.
|
|
2
|
+
*
|
|
3
|
+
* All class names are stable and prefixed with `dtbl-`. Colors and spacing read CSS custom
|
|
4
|
+
* properties (--dtbl-*) whose fallback values equal the built-in default theme
|
|
5
|
+
* (`lightTheme`, except that the fallback text color is `inherit`); see theme.ts for
|
|
6
|
+
* the TableTheme type that sets these variables on a container element. */
|
|
7
|
+
|
|
8
|
+
/* Scrollable wrapper rendered by <DataTable> */
|
|
9
|
+
.dtbl-outer {
|
|
10
|
+
overflow: auto;
|
|
11
|
+
}
|
|
12
|
+
/* Scrollable wrapper rendered by <ListBox> and <TreeList> */
|
|
13
|
+
.dtbl-list-outer {
|
|
14
|
+
overflow-y: auto;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.dtbl-table {
|
|
18
|
+
width: 100%;
|
|
19
|
+
table-layout: fixed;
|
|
20
|
+
border-spacing: 0;
|
|
21
|
+
/* Line up digits vertically in numeric columns (fonts with proportional or old-style
|
|
22
|
+
* figures otherwise make number columns look ragged) */
|
|
23
|
+
font-variant-numeric: tabular-nums lining-nums;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* Table rows, including the header row. dtbl-row-padded is the default; dtbl-row is used
|
|
27
|
+
* when the `noPadding` item prop is set. */
|
|
28
|
+
.dtbl-row, .dtbl-row-padded {
|
|
29
|
+
text-align: left;
|
|
30
|
+
color: var(--dtbl-text, inherit);
|
|
31
|
+
background-color: var(--dtbl-row-bg, #ffffff);
|
|
32
|
+
}
|
|
33
|
+
.dtbl-row td, .dtbl-row th,
|
|
34
|
+
.dtbl-row-padded td, .dtbl-row-padded th {
|
|
35
|
+
display: table-cell;
|
|
36
|
+
vertical-align: middle;
|
|
37
|
+
border-bottom: var(--dtbl-row-border, 1px solid #d9e0dd);
|
|
38
|
+
}
|
|
39
|
+
.dtbl-row-padded td, .dtbl-row-padded th {
|
|
40
|
+
padding: var(--dtbl-cell-padding, 6px 6px);
|
|
41
|
+
}
|
|
42
|
+
.dtbl-row th, .dtbl-row-padded th {
|
|
43
|
+
position: sticky;
|
|
44
|
+
top: 0;
|
|
45
|
+
/* A background is required so that sticky headings appear on top of data rows.
|
|
46
|
+
* Without a background, data rows may be "mixed" with headings (drawn at the
|
|
47
|
+
* same location, which is ugly and perhaps unreadable.) */
|
|
48
|
+
background-color: var(--dtbl-header-bg, inherit);
|
|
49
|
+
/* While sticky headings are normally drawn on top of data cells, data with opacity
|
|
50
|
+
* less than 1 is drawn on top of headings by default. A z-index greater than 0
|
|
51
|
+
* fixes that. */
|
|
52
|
+
z-index: 1;
|
|
53
|
+
/* The default is bold for header cells, which doesn't quite fit our aesthetic */
|
|
54
|
+
font-weight: normal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* Base background and text color of every selectable row/item. List items need this too:
|
|
58
|
+
* without it (and with the state tints below replacing background-color, as they once did),
|
|
59
|
+
* translucent theme colors composited against the PAGE behind the table, so a dark table on
|
|
60
|
+
* a light page got light hovered rows under light text. */
|
|
61
|
+
.dtbl-selected, .dtbl-unselected, .dtbl-parent-selected {
|
|
62
|
+
color: var(--dtbl-text, inherit);
|
|
63
|
+
background-color: var(--dtbl-row-bg, #ffffff);
|
|
64
|
+
}
|
|
65
|
+
/* Optional striping of alternate table rows, starting with the first (--dtbl-stripe-bg;
|
|
66
|
+
* default: transparent, i.e. off). Drawn as a background-image layer like the state tints
|
|
67
|
+
* below; :where() keeps specificity at (0,1,0) so the (0,2,0) selection/hover rules always
|
|
68
|
+
* replace the stripe on their row. */
|
|
69
|
+
:where(tbody) > :where(.dtbl-row, .dtbl-row-padded):nth-child(odd) {
|
|
70
|
+
background-image: linear-gradient(var(--dtbl-stripe-bg, transparent),
|
|
71
|
+
var(--dtbl-stripe-bg, transparent));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* Selection/hover states, drawn as a background-image layer so the (often translucent)
|
|
75
|
+
* tint composites over the base row background above, never over the page. The class name
|
|
76
|
+
* is doubled to outrank single-class rules in app stylesheets regardless of rule order. */
|
|
77
|
+
.dtbl-selected.dtbl-selected {
|
|
78
|
+
background-image: linear-gradient(var(--dtbl-selected-bg, rgba(11, 58, 46, 0.22)),
|
|
79
|
+
var(--dtbl-selected-bg, rgba(11, 58, 46, 0.22)));
|
|
80
|
+
}
|
|
81
|
+
.dtbl-selected:hover {
|
|
82
|
+
background-image: linear-gradient(var(--dtbl-hover-selected-bg, rgba(11, 58, 46, 0.32)),
|
|
83
|
+
var(--dtbl-hover-selected-bg, rgba(11, 58, 46, 0.32)));
|
|
84
|
+
}
|
|
85
|
+
.dtbl-unselected:hover {
|
|
86
|
+
background-image: linear-gradient(var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08)),
|
|
87
|
+
var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08)));
|
|
88
|
+
}
|
|
89
|
+
/* Rows whose (grand)parent row is selected (see `highlightChildrenOfSelectedParents`) */
|
|
90
|
+
.dtbl-parent-selected.dtbl-parent-selected {
|
|
91
|
+
background-image: linear-gradient(var(--dtbl-parent-selected-bg, rgba(11, 58, 46, 0.08)),
|
|
92
|
+
var(--dtbl-parent-selected-bg, rgba(11, 58, 46, 0.08)));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* Padding of list/tree items outside tables (unless the `noPadding` item prop is set) */
|
|
96
|
+
.dtbl-item-padding {
|
|
97
|
+
padding: var(--dtbl-item-padding, 6px 10px);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* Overlay that grays out a read-only row */
|
|
101
|
+
.dtbl-readonly-overlay {
|
|
102
|
+
position: absolute;
|
|
103
|
+
left: 0; top: 0; right: 0; bottom: 0;
|
|
104
|
+
pointer-events: none;
|
|
105
|
+
opacity: 0.15;
|
|
106
|
+
background: #888;
|
|
107
|
+
filter: grayscale(80%);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* Sort direction indicator in a column header */
|
|
111
|
+
.dtbl-sort-icon {
|
|
112
|
+
transform: translateY(-50%);
|
|
113
|
+
position: absolute;
|
|
114
|
+
top: 50%;
|
|
115
|
+
right: 0;
|
|
116
|
+
pointer-events: none;
|
|
117
|
+
display: inline-flex;
|
|
118
|
+
align-items: center;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* Invisible drag handle at the right edge of a resizable column header */
|
|
122
|
+
.dtbl-resize-handle {
|
|
123
|
+
position: absolute;
|
|
124
|
+
bottom: 0;
|
|
125
|
+
width: 8px;
|
|
126
|
+
top: 0;
|
|
127
|
+
/* The fallback is `defaultResizeCursor` from theme.ts; keep the two in sync. */
|
|
128
|
+
cursor: var(--dtbl-resize-cursor, url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAYAAABe3VzdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGJoAABiaAZsyYXAAAAEbSURBVFhH7ZSLDsIgDAA3jf//wUZnb4GlEsbaTmUmuwSpPLobMMahI9M0zfUoSHUllPKU9kdqp+pHErwR6kJ7lr/Mvx0RkXuqFylNd8EtTsG9nIJ7aV40ta8qI3fU3Cljmjks4xiTn8Xdl8cSh1ZQJ7TA2CzqxS3olctEJV2CUblMRNJ8Bmty0paiNrV50rZM1rl1H7FJUCf4FIXIkr8U3Nzib8gBOcmd/q6yKcjb8Cafhpx5pVqYthhqK2kVr83Tcjq37iM2f8VMYkIJ7a2Shi1oAQuua4bENUkrXjlwCUJUMiIHbkHwSkblICQIPNDyUOu4NcKCv+IU3Ev4bESQs5iid+Qjkq6dF3UvDiPIalFKDrHFa/zBFg/DC/as2Q7ADo1PAAAAAElFTkSuQmCC) 30 15, col-resize);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/* Default tree-expander button (see TableConfig.renderIconButton to replace it) */
|
|
132
|
+
.dtbl-expander {
|
|
133
|
+
background: none;
|
|
134
|
+
border: none;
|
|
135
|
+
padding: 0;
|
|
136
|
+
margin: 0;
|
|
137
|
+
color: inherit;
|
|
138
|
+
font: inherit;
|
|
139
|
+
cursor: pointer;
|
|
140
|
+
display: inline-flex;
|
|
141
|
+
align-items: center;
|
|
142
|
+
justify-content: center;
|
|
143
|
+
align-self: center;
|
|
144
|
+
height: 2em;
|
|
145
|
+
border-radius: 1em;
|
|
146
|
+
}
|
|
147
|
+
.dtbl-expander:hover {
|
|
148
|
+
background-color: var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* Default text glyph icons (see TableConfig.icons to replace them) */
|
|
152
|
+
.dtbl-glyph {
|
|
153
|
+
font-size: 0.72em;
|
|
154
|
+
line-height: 1;
|
|
155
|
+
user-select: none;
|
|
156
|
+
}
|
|
157
|
+
/* Default SVG chevron icons (sort direction, tree expander) */
|
|
158
|
+
.dtbl-glyph-svg {
|
|
159
|
+
display: block;
|
|
160
|
+
user-select: none;
|
|
161
|
+
}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Holder } from 'holders';
|
|
2
|
+
import { nully } from './util.js';
|
|
3
|
+
export declare function useForceUpdate(): () => void;
|
|
4
|
+
/** Returns an override if provided and `useState(initialState)[0]` otherwise. Typically
|
|
5
|
+
* `initialState` would be, or return, a collection type such as a Set. */
|
|
6
|
+
export declare function useStatefulObjectIfNotGivenBy<S>(override: S | nully, initialState: S | (() => S)): S;
|
|
7
|
+
export declare function useStatefulObjectIfNotGivenBy<S>(override: S | nully): S | undefined;
|
|
8
|
+
/** This is the same as the `??` operator except that the second argument is evaluated
|
|
9
|
+
* unconditionally. Since hooks must be evaluated unconditionally, this makes it useful
|
|
10
|
+
* for using a hook's output as a default, e.g.
|
|
11
|
+
*
|
|
12
|
+
* const value = coalesce(p.parentState, useHeldState(''));
|
|
13
|
+
**/
|
|
14
|
+
export declare function coalesce<T>(override: T | nully, defaultValue: T): T;
|
|
15
|
+
/** Wraps `useState` in a `Holder`, an object with `get` and `set` methods (see the
|
|
16
|
+
* `holders` package). */
|
|
17
|
+
export declare function useHeldState<S>(initialState: S | (() => S)): Holder<S>;
|
|
18
|
+
export declare function useHeldState<S>(): Holder<S | undefined>;
|
|
19
|
+
/** Returns `override` if provided, or a `Holder` backed by `useState` otherwise. */
|
|
20
|
+
export declare function useHeldStateIfNotGivenBy<S>(override: Holder<S> | nully, initialState: S | (() => S)): Holder<S>;
|
|
21
|
+
export declare function useHeldStateIfNotGivenBy<S>(override: Holder<S | undefined> | nully): Holder<S | undefined>;
|
|
22
|
+
/** When a component's `readOnly` prop is undefined, the list/table components fall back on
|
|
23
|
+
* this context, so an app can mark a whole panel read-only with a single provider. */
|
|
24
|
+
export declare const ReadOnlyContext: import("react").Context<boolean>;
|
|
25
|
+
export declare function useIsReadOnlyContext(): boolean;
|
|
26
|
+
export declare function useOneEffect(effect: () => (void | (() => void)), deps?: React.DependencyList): void;
|
|
27
|
+
export declare function useUnmountHandler(destructor: () => void, deps?: React.DependencyList): void;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useReducer, useRef, useState } from 'react';
|
|
2
|
+
export function useForceUpdate() {
|
|
3
|
+
return useReducer(i => i + 1, 0)[1];
|
|
4
|
+
}
|
|
5
|
+
export function useStatefulObjectIfNotGivenBy(override, initialState) {
|
|
6
|
+
// Since hooks cannot be called conditionally, we call `useState` even if there's an override
|
|
7
|
+
return coalesce(override, useState(initialState)[0]);
|
|
8
|
+
}
|
|
9
|
+
/** This is the same as the `??` operator except that the second argument is evaluated
|
|
10
|
+
* unconditionally. Since hooks must be evaluated unconditionally, this makes it useful
|
|
11
|
+
* for using a hook's output as a default, e.g.
|
|
12
|
+
*
|
|
13
|
+
* const value = coalesce(p.parentState, useHeldState(''));
|
|
14
|
+
**/
|
|
15
|
+
export function coalesce(override, defaultValue) {
|
|
16
|
+
return override !== null && override !== void 0 ? override : defaultValue;
|
|
17
|
+
}
|
|
18
|
+
export function useHeldState(initialState) {
|
|
19
|
+
let [state, setState] = useState(initialState);
|
|
20
|
+
return {
|
|
21
|
+
get() { return state; },
|
|
22
|
+
set(s) {
|
|
23
|
+
setState(typeof s === 'function' ? () => s : s);
|
|
24
|
+
state = s;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export function useHeldStateIfNotGivenBy(override, initialState) {
|
|
29
|
+
let [state, setState] = useState(initialState);
|
|
30
|
+
if (override) {
|
|
31
|
+
return override;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return {
|
|
35
|
+
get() { return state; },
|
|
36
|
+
set(s) {
|
|
37
|
+
setState(typeof s === 'function' ? () => s : s);
|
|
38
|
+
state = s;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** When a component's `readOnly` prop is undefined, the list/table components fall back on
|
|
44
|
+
* this context, so an app can mark a whole panel read-only with a single provider. */
|
|
45
|
+
export const ReadOnlyContext = createContext(false);
|
|
46
|
+
export function useIsReadOnlyContext() {
|
|
47
|
+
return useContext(ReadOnlyContext);
|
|
48
|
+
}
|
|
49
|
+
// WORKAROUND: React 18 StrictMode causes fake cleanup events when the component
|
|
50
|
+
// is still visible. Specifically, `useEffect` calls your function twice, and it
|
|
51
|
+
// calls the cleanup function returned from your function before it calls your
|
|
52
|
+
// function a second time. Supposedly this is because they plan to add a feature
|
|
53
|
+
// someday such that components can be "hidden" without being destroyed.
|
|
54
|
+
// Unfortunately React won't tell us whether the component is being destroyed or
|
|
55
|
+
// not, so I wrote this helper function to bypass the StrictMode behavior of
|
|
56
|
+
// useEffect so that you can have an effect and cleanup handler that are called
|
|
57
|
+
// only once.
|
|
58
|
+
//
|
|
59
|
+
// Needs testing. It should support the `deps` array, but it's meant for the case
|
|
60
|
+
// that deps is [].
|
|
61
|
+
//
|
|
62
|
+
// It works by setting a temporary state during the first effect call and asking
|
|
63
|
+
// for a callback using `setTimeout()`. The temporary state will prevent the
|
|
64
|
+
// second `useEffect` call from doing anything except returning the destructor
|
|
65
|
+
// returned earlier by the effect handler, if any (and recording the fact that
|
|
66
|
+
// the second call happened, by clearing the temporary state). And if the second
|
|
67
|
+
// effect call doesn't happen, we know StrictMode is off.
|
|
68
|
+
//
|
|
69
|
+
// There's a slight difference in timing when `StrictMode` is off: the destructor
|
|
70
|
+
// is called in `setTimeout` rather than immediately.
|
|
71
|
+
export function useOneEffect(effect, deps) {
|
|
72
|
+
let state = useRef(false);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (state.current === false) {
|
|
75
|
+
// This is the first call to the effect, and it is unknown whether
|
|
76
|
+
// there will be a second call (we don't know if StrictMode is on)
|
|
77
|
+
let destructor = effect();
|
|
78
|
+
if (destructor) {
|
|
79
|
+
state.current = destructor;
|
|
80
|
+
return () => {
|
|
81
|
+
// At this point a fake or real cleanup is happening, and we must
|
|
82
|
+
// figure out which. If it's fake, then `state.current` will be
|
|
83
|
+
// false by the time the `setTimeout` handler is called. If it's real,
|
|
84
|
+
// then `state.current` should still point to the same `destructor`.
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
let destructor = state.current;
|
|
87
|
+
if (typeof destructor === 'function') {
|
|
88
|
+
state.current = false;
|
|
89
|
+
destructor();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// Helps detect if there is a second call to this effect handler
|
|
96
|
+
state.current = true;
|
|
97
|
+
setTimeout(() => {
|
|
98
|
+
state.current = false;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// This is the second call to the effect. This time we want to return
|
|
104
|
+
// the destructor from the first effect call, if there was one.
|
|
105
|
+
let destructor = state.current;
|
|
106
|
+
state.current = false;
|
|
107
|
+
if (typeof destructor === 'function') {
|
|
108
|
+
return destructor;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}, deps);
|
|
112
|
+
}
|
|
113
|
+
export function useUnmountHandler(destructor, deps = []) {
|
|
114
|
+
return useOneEffect(() => destructor, deps);
|
|
115
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './config.js';
|
|
2
|
+
export * from './DataTable.js';
|
|
3
|
+
export * from './Draggable.js';
|
|
4
|
+
export * from './hooks.js';
|
|
5
|
+
export * from './interfaces.js';
|
|
6
|
+
export * from './ListBox.js';
|
|
7
|
+
export * from './ListVirtualizer.js';
|
|
8
|
+
export * from './reactivity.js';
|
|
9
|
+
export * from './styles.js';
|
|
10
|
+
export * from './theme.js';
|
|
11
|
+
export * from './TreeList.js';
|
|
12
|
+
export * from './util.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Note: the stylesheet is deliberately NOT imported here. A CSS import in the published
|
|
2
|
+
// entry point makes the package unloadable by anything without a CSS-aware bundler (Node,
|
|
3
|
+
// SSR, a consumer's vitest). Import 'datatable-tree/datatable.css' from your app instead.
|
|
4
|
+
export * from './config.js';
|
|
5
|
+
export * from './DataTable.js';
|
|
6
|
+
export * from './Draggable.js';
|
|
7
|
+
export * from './hooks.js';
|
|
8
|
+
export * from './interfaces.js';
|
|
9
|
+
export * from './ListBox.js';
|
|
10
|
+
export * from './ListVirtualizer.js';
|
|
11
|
+
export * from './reactivity.js';
|
|
12
|
+
export * from './styles.js';
|
|
13
|
+
export * from './theme.js';
|
|
14
|
+
export * from './TreeList.js';
|
|
15
|
+
export * from './util.js';
|