datatable-tree 0.0.0-stage → 0.1.1

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 +251 -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 +143 -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,234 @@
1
+ import { RefObject } from 'react';
2
+
3
+ export interface VirtualizationSettings<T> {
4
+ /** Per-row virtualization height in pixels, or a function that returns a height for a
5
+ * given row item. The virtualizer renders groups of off-screen rows as empty placeholders
6
+ * based on this height until they come into view. If this is a function that returns
7
+ * `false` for an item, the item is left unvirtualized (always rendered). A height of 0
8
+ * is not supported and acts like false. **/
9
+ height: number | ((item: T) => number | false);
10
+ /** Vertical buffer margin that makes rows "real" before they enter the viewport,
11
+ * to reduce pop-in and allow a wider range of Ctrl+F searching. */
12
+ margin?: number;
13
+ /** This is only necessary when using virtualization with `DataTableCore` (not
14
+ * `DataTable`). It's the ref of the scrolling container in which the `DataTableCore`
15
+ * is placed, which is used as the root element for row virtualization. */
16
+ containerRef?: RefObject<HTMLElement> | RefObject<HTMLDivElement>;
17
+ }
18
+
19
+ export type ListVirtualItem<T, K> = {
20
+ item: T;
21
+ key: K;
22
+ index: number;
23
+ reactKey: string | number;
24
+ };
25
+
26
+ export type ListVirtualSlot<T, K> =
27
+ | { real: true; item: T; key: K; index: number; reactKey: string | number }
28
+ | { real: false; height: number; key: string; keys: K[] };
29
+
30
+ export type ListVirtualizerOptions<T> = VirtualizationSettings<T> & { onVisibilityChange: () => void; };
31
+
32
+ export class ListVirtualizer<T, K> {
33
+ itemHeight: VirtualizationSettings<T>['height'];
34
+ visible = new Set<K>();
35
+
36
+ private readonly containerRef?: RefObject<HTMLElement> | RefObject<HTMLDivElement>;
37
+ private readonly margin: number;
38
+ private readonly onVisibilityChange: () => void;
39
+ private readonly nodeToKeys = new Map<Element, K[]>();
40
+ private readonly observer?: IntersectionObserver;
41
+
42
+ static isSupportedByBrowser() {
43
+ return typeof window !== 'undefined' && !!window.IntersectionObserver;
44
+ }
45
+
46
+ constructor(options: ListVirtualizerOptions<T>) {
47
+ this.itemHeight = options.height;
48
+ this.containerRef = options.containerRef;
49
+ this.margin = options.margin ?? 400;
50
+ this.onVisibilityChange = options.onVisibilityChange;
51
+ if (ListVirtualizer.isSupportedByBrowser()) {
52
+ this.observer = new IntersectionObserver(this.onIntersect, {
53
+ root: options.containerRef?.current ?? undefined,
54
+ rootMargin: this.margin + 'px 0px',
55
+ });
56
+ }
57
+ }
58
+
59
+ disconnect() {
60
+ this.observer?.disconnect();
61
+ this.nodeToKeys.clear();
62
+ }
63
+
64
+ createObserverRef(keys: K | K[]) {
65
+ if (!this.observer) {
66
+ return undefined;
67
+ }
68
+
69
+ const keyList = Array.isArray(keys) ? keys : [keys];
70
+ let lastElement: Element | null = null;
71
+ return (element: Element | null) => {
72
+ if (!this.observer) {
73
+ return;
74
+ }
75
+ if (lastElement) {
76
+ this.unobserve(lastElement);
77
+ }
78
+ if (element) {
79
+ this.observe(keyList, element);
80
+ }
81
+ lastElement = element;
82
+ };
83
+ }
84
+
85
+ buildPlan(items: ListVirtualItem<T, K>[]): ListVirtualSlot<T, K>[] {
86
+ const slots: ListVirtualSlot<T, K>[] = [];
87
+ if (this.visible.size > items.length) {
88
+ const currentKeys = new Set(items.map(item => item.key));
89
+ for (const key of this.visible) {
90
+ if (!currentKeys.has(key)) {
91
+ this.visible.delete(key);
92
+ }
93
+ }
94
+ }
95
+ const fixedHeight = this.getDefaultRowHeight();
96
+ if (fixedHeight) {
97
+ this.markVisibleRange(items, fixedHeight);
98
+ }
99
+
100
+ let gapKeys: K[] = [];
101
+ let gapHeight = 0;
102
+ for (const item of items) {
103
+ const height = this.getItemHeight(item.item);
104
+ const canVirtualize = height != null && height > 0;
105
+ if (!canVirtualize || this.visible.has(item.key)) {
106
+ if (gapHeight > 0) {
107
+ slots.push(this.buildGap(gapKeys, gapHeight));
108
+ gapKeys = [];
109
+ gapHeight = 0;
110
+ }
111
+ slots.push({
112
+ real: true,
113
+ item: item.item,
114
+ key: item.key,
115
+ index: item.index,
116
+ reactKey: item.reactKey,
117
+ });
118
+ continue;
119
+ }
120
+
121
+ gapKeys.push(item.key);
122
+ gapHeight += height;
123
+ }
124
+
125
+ if (gapHeight > 0) {
126
+ slots.push(this.buildGap(gapKeys, gapHeight));
127
+ }
128
+ return slots;
129
+ }
130
+
131
+ private buildGap(keys: K[], height: number): ListVirtualSlot<T, K> {
132
+ const firstKey = keys[0];
133
+ const gapKey = `gap-${String(firstKey)}-${keys.length}`;
134
+ return { real: false, height, key: gapKey, keys };
135
+ }
136
+
137
+ private getItemHeight(item: T): number | undefined {
138
+ const height = typeof this.itemHeight === 'function'
139
+ ? this.itemHeight(item)
140
+ : this.itemHeight;
141
+ if (height === false) {
142
+ return undefined;
143
+ }
144
+ if (typeof height !== 'number' || !Number.isFinite(height) || height <= 0) {
145
+ return undefined;
146
+ }
147
+ return height;
148
+ }
149
+
150
+ private observe(keys: K[], element: Element) {
151
+ if (!this.observer) {
152
+ return;
153
+ }
154
+ this.nodeToKeys.set(element, keys);
155
+ this.observer.observe(element);
156
+ }
157
+
158
+ private unobserve(element: Element) {
159
+ if (!this.observer) {
160
+ return;
161
+ }
162
+ this.nodeToKeys.delete(element);
163
+ this.observer.unobserve(element);
164
+ }
165
+
166
+ private onIntersect = (entries: IntersectionObserverEntry[]) => {
167
+ let changed = false;
168
+ for (const entry of entries) {
169
+ if (!entry.isIntersecting) {
170
+ continue;
171
+ }
172
+ const keys = this.nodeToKeys.get(entry.target);
173
+ if (!keys) {
174
+ continue;
175
+ }
176
+ const maxToAdd = keys.length === 1 ? 1 : this.getChunkSize(entry);
177
+ let added = 0;
178
+ for (const key of keys) {
179
+ if (!this.visible.has(key)) {
180
+ this.visible.add(key);
181
+ changed = true;
182
+ added++;
183
+ if (added >= maxToAdd) {
184
+ break;
185
+ }
186
+ }
187
+ }
188
+ if (keys.length > 1 && added > 0) {
189
+ if (added >= keys.length) {
190
+ this.unobserve(entry.target);
191
+ } else {
192
+ keys.splice(0, added);
193
+ }
194
+ }
195
+ }
196
+ if (changed) {
197
+ this.onVisibilityChange();
198
+ }
199
+ };
200
+
201
+ private getChunkSize(entry: IntersectionObserverEntry) {
202
+ const rowHeight = this.getDefaultRowHeight() ?? 50;
203
+ const rootHeight = entry.rootBounds?.height
204
+ ?? this.containerRef?.current?.clientHeight
205
+ ?? 600;
206
+ const targetHeight = rootHeight + this.margin * 2;
207
+ return Math.max(1, Math.ceil(targetHeight / rowHeight));
208
+ }
209
+
210
+ private getDefaultRowHeight(): number | undefined {
211
+ if (typeof this.itemHeight === 'number' && Number.isFinite(this.itemHeight) && this.itemHeight > 0) {
212
+ return this.itemHeight;
213
+ }
214
+ return undefined;
215
+ }
216
+
217
+ private markVisibleRange(items: ListVirtualItem<T, K>[], rowHeight: number) {
218
+ const container = this.containerRef?.current;
219
+ if (!container) {
220
+ return;
221
+ }
222
+ const margin = this.margin;
223
+ const scrollTop = container.scrollTop ?? 0;
224
+ const viewportHeight = container.clientHeight ?? 0;
225
+ const start = Math.max(0, Math.floor((scrollTop - margin) / rowHeight));
226
+ const end = Math.min(
227
+ items.length,
228
+ Math.ceil((scrollTop + viewportHeight + margin) / rowHeight)
229
+ );
230
+ for (let i = start; i < end; i++) {
231
+ this.visible.add(items[i].key);
232
+ }
233
+ }
234
+ }