@react-fabric/data 1.0.0-beta.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 +10 -0
- package/dist/cjs/histogram/Histogram.js +89 -0
- package/dist/cjs/histogram/Histogram.module.css +1 -0
- package/dist/cjs/hooks/useFilteredList.js +56 -0
- package/dist/cjs/hooks/usePagination.js +85 -0
- package/dist/cjs/i18n/ar.json.js +56 -0
- package/dist/cjs/i18n/en.json.js +56 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/json/JsonViewer.js +246 -0
- package/dist/cjs/pagination/Pagination.js +100 -0
- package/dist/cjs/table/BodyCell.js +47 -0
- package/dist/cjs/table/HeaderCell.js +79 -0
- package/dist/cjs/table/Table.js +133 -0
- package/dist/cjs/table/useTableColumns.js +26 -0
- package/dist/cjs/table/useVirtualTable.js +56 -0
- package/dist/cjs/tree/TreeNode.js +107 -0
- package/dist/cjs/tree/TreePanel.js +136 -0
- package/dist/cjs/tree/types.js +30 -0
- package/dist/cjs/tree/useTree.js +181 -0
- package/dist/cjs/tree/utils.js +145 -0
- package/dist/cjs/virtual/Gallery.js +229 -0
- package/dist/cjs/virtual/List.js +218 -0
- package/dist/cjs/virtual/Scroller.js +77 -0
- package/dist/esm/histogram/Histogram.js +89 -0
- package/dist/esm/histogram/Histogram.module.css +1 -0
- package/dist/esm/hooks/useFilteredList.js +56 -0
- package/dist/esm/hooks/usePagination.js +85 -0
- package/dist/esm/i18n/ar.json.js +47 -0
- package/dist/esm/i18n/en.json.js +47 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/json/JsonViewer.js +246 -0
- package/dist/esm/pagination/Pagination.js +100 -0
- package/dist/esm/table/BodyCell.js +47 -0
- package/dist/esm/table/HeaderCell.js +79 -0
- package/dist/esm/table/Table.js +133 -0
- package/dist/esm/table/useTableColumns.js +26 -0
- package/dist/esm/table/useVirtualTable.js +56 -0
- package/dist/esm/tree/TreeNode.js +107 -0
- package/dist/esm/tree/TreePanel.js +136 -0
- package/dist/esm/tree/types.js +29 -0
- package/dist/esm/tree/useTree.js +181 -0
- package/dist/esm/tree/utils.js +140 -0
- package/dist/esm/virtual/Gallery.js +229 -0
- package/dist/esm/virtual/List.js +218 -0
- package/dist/esm/virtual/Scroller.js +77 -0
- package/dist/types/histogram/Histogram.d.ts +41 -0
- package/dist/types/hooks/useFilteredList.d.ts +7 -0
- package/dist/types/hooks/usePagination.d.ts +25 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/json/JsonViewer.d.ts +59 -0
- package/dist/types/pagination/Pagination.d.ts +24 -0
- package/dist/types/table/BodyCell.d.ts +5 -0
- package/dist/types/table/HeaderCell.d.ts +2 -0
- package/dist/types/table/Table.d.ts +2 -0
- package/dist/types/table/types.d.ts +26 -0
- package/dist/types/table/useTableColumns.d.ts +8 -0
- package/dist/types/table/useVirtualTable.d.ts +8 -0
- package/dist/types/tree/TreeNode.d.ts +2 -0
- package/dist/types/tree/TreePanel.d.ts +2 -0
- package/dist/types/tree/types.d.ts +138 -0
- package/dist/types/tree/useTree.d.ts +20 -0
- package/dist/types/tree/utils.d.ts +10 -0
- package/dist/types/virtual/Gallery.d.ts +55 -0
- package/dist/types/virtual/List.d.ts +59 -0
- package/dist/types/virtual/Scroller.d.ts +8 -0
- package/package.json +55 -0
- package/src/histogram/Histogram.module.css +57 -0
- package/src/histogram/Histogram.tsx +176 -0
- package/src/hooks/useFilteredList.tsx +78 -0
- package/src/hooks/usePagination.tsx +135 -0
- package/src/i18n/ar.json +36 -0
- package/src/i18n/en.json +36 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +35 -0
- package/src/json/JsonViewer.tsx +381 -0
- package/src/pagination/Pagination.tsx +157 -0
- package/src/table/BodyCell.tsx +54 -0
- package/src/table/HeaderCell.tsx +70 -0
- package/src/table/Table.tsx +143 -0
- package/src/table/types.ts +65 -0
- package/src/table/useTableColumns.ts +52 -0
- package/src/table/useVirtualTable.ts +54 -0
- package/src/tree/TreeNode.tsx +163 -0
- package/src/tree/TreePanel.tsx +157 -0
- package/src/tree/types.ts +181 -0
- package/src/tree/useTree.ts +227 -0
- package/src/tree/utils.ts +185 -0
- package/src/virtual/Gallery.tsx +349 -0
- package/src/virtual/List.tsx +351 -0
- package/src/virtual/Scroller.tsx +81 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { compareValues, isString, matchString } from "@react-fabric/utilities";
|
|
25
|
+
import { type InternalNode, type TreeNodeType } from "./types";
|
|
26
|
+
|
|
27
|
+
const sorter = (a: TreeNodeType, b: TreeNodeType) => {
|
|
28
|
+
if (!!a.leaf !== !!b.leaf) return a.leaf ? 1 : -1;
|
|
29
|
+
return compareValues()(a.label, b.label);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const getChildren = (list: InternalNode[] = []): InternalNode[] => {
|
|
33
|
+
return list
|
|
34
|
+
.map<InternalNode[]>((item) => [item, ...getChildren(item.children)])
|
|
35
|
+
.flat(99)
|
|
36
|
+
.filter(Boolean);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const getIntermediate = (list: AnyObject[] = []) => {
|
|
40
|
+
const checks = list.map((item) => item.checked);
|
|
41
|
+
const someOff = checks.includes(0);
|
|
42
|
+
const someInt = checks.includes(2);
|
|
43
|
+
const someOn = checks.includes(1);
|
|
44
|
+
|
|
45
|
+
return (someOff && someOn) || someInt ? 2 : someOn ? 1 : 0;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// convert node item to internal node used by the tree
|
|
49
|
+
export const refactorTree = (
|
|
50
|
+
nodes: TreeNodeType[],
|
|
51
|
+
options: { level: number; parent?: string } = { level: 0, parent: undefined },
|
|
52
|
+
) => {
|
|
53
|
+
const list: InternalNode[] = [];
|
|
54
|
+
|
|
55
|
+
nodes.sort(sorter).forEach((node) => {
|
|
56
|
+
list.push({
|
|
57
|
+
open: false,
|
|
58
|
+
level: options.level,
|
|
59
|
+
parent: options.parent,
|
|
60
|
+
checked: 0,
|
|
61
|
+
loaded: false,
|
|
62
|
+
loading: false,
|
|
63
|
+
filtered: false,
|
|
64
|
+
lines: [],
|
|
65
|
+
...node,
|
|
66
|
+
leaf: !!node.leaf,
|
|
67
|
+
children:
|
|
68
|
+
node.children &&
|
|
69
|
+
refactorTree(node.children, {
|
|
70
|
+
level: options.level + 1,
|
|
71
|
+
parent: node.id,
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return list;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const makeTreeMap = (nodes: InternalNode[], map = new Map()) => {
|
|
80
|
+
nodes.forEach((node) => {
|
|
81
|
+
if (map.has(node.id))
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Node with id [${node.id}] already exists, please make sure all ids are unique`,
|
|
84
|
+
);
|
|
85
|
+
map.set(node.id, node);
|
|
86
|
+
node.children && makeTreeMap(node.children, map);
|
|
87
|
+
});
|
|
88
|
+
return map;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// make flat list to be rendered by virtual list
|
|
92
|
+
export const flattenTree = (
|
|
93
|
+
nodes: InternalNode[],
|
|
94
|
+
lines: Array<0 | 1 | 2 | 3> = [],
|
|
95
|
+
) => {
|
|
96
|
+
const list: InternalNode[] = [];
|
|
97
|
+
const filtered = nodes.filter((node) => !node.filtered);
|
|
98
|
+
filtered.forEach((node, idx) => {
|
|
99
|
+
node.lines = [...lines, idx === filtered.length - 1 ? 2 : 3];
|
|
100
|
+
list.push(node);
|
|
101
|
+
(node.open || node.childFiltered) &&
|
|
102
|
+
node.children &&
|
|
103
|
+
list.push(
|
|
104
|
+
...flattenTree(
|
|
105
|
+
node.children.filter((child) => !child.filtered),
|
|
106
|
+
[...lines, idx === filtered.length - 1 ? 0 : 1],
|
|
107
|
+
),
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
return list;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const updateSelection = (
|
|
114
|
+
nodes: Map<string, InternalNode>,
|
|
115
|
+
id: string,
|
|
116
|
+
selected?: true,
|
|
117
|
+
) => {
|
|
118
|
+
const node = nodes.get(id);
|
|
119
|
+
if (node) {
|
|
120
|
+
// change selection
|
|
121
|
+
node.selected = selected;
|
|
122
|
+
// update all parent childSelected flag
|
|
123
|
+
let parent = node.parent;
|
|
124
|
+
while (parent) {
|
|
125
|
+
const parentNode = nodes.get(parent);
|
|
126
|
+
parentNode && (parentNode.childSelected = selected);
|
|
127
|
+
selected && parentNode && (parentNode.open = true);
|
|
128
|
+
parent = parentNode?.parent;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const updateChecked = (list: Map<string, AnyObject>, id: string) => {
|
|
134
|
+
const node = list.get(id);
|
|
135
|
+
if (node) {
|
|
136
|
+
node.checked = node.checked === 1 ? 0 : 1;
|
|
137
|
+
// check all children
|
|
138
|
+
node.children &&
|
|
139
|
+
getChildren(node.children).forEach(
|
|
140
|
+
(child) => (child.checked = node.checked),
|
|
141
|
+
);
|
|
142
|
+
let parent = node.parent;
|
|
143
|
+
while (parent) {
|
|
144
|
+
const parentNode = list.get(parent);
|
|
145
|
+
parentNode && (parentNode.checked = getIntermediate(parentNode.children));
|
|
146
|
+
parent = parentNode?.parent;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const filterTree = (
|
|
152
|
+
nodes: Map<string, InternalNode>,
|
|
153
|
+
query?: string,
|
|
154
|
+
matcher?: AnyObject,
|
|
155
|
+
) => {
|
|
156
|
+
nodes.forEach((node) => {
|
|
157
|
+
if (query) {
|
|
158
|
+
if (node.parentFiltered) return;
|
|
159
|
+
node.filtered = !(matcher
|
|
160
|
+
? matcher(node.data, query)
|
|
161
|
+
: isString(node.queryable)
|
|
162
|
+
? matchString(node.queryable, query)
|
|
163
|
+
: isString(node.label) && matchString(node.label, query));
|
|
164
|
+
|
|
165
|
+
if (!node.filtered) {
|
|
166
|
+
let parent = node.parent;
|
|
167
|
+
while (parent) {
|
|
168
|
+
const parentNode = nodes.get(parent);
|
|
169
|
+
parentNode && (parentNode.filtered = false);
|
|
170
|
+
parentNode && (parentNode.childFiltered = true);
|
|
171
|
+
parent = parentNode?.parent;
|
|
172
|
+
}
|
|
173
|
+
node.children &&
|
|
174
|
+
getChildren(node.children).forEach((child) => {
|
|
175
|
+
child.parentFiltered = true;
|
|
176
|
+
child.filtered = false;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
node.filtered = false;
|
|
181
|
+
node.childFiltered = undefined;
|
|
182
|
+
node.parentFiltered = undefined;
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
};
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Skeleton, useDebounce, useEffectDebugger } from "@react-fabric/core";
|
|
25
|
+
import { debounce } from "@react-fabric/utilities";
|
|
26
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
27
|
+
import classNames from "classnames";
|
|
28
|
+
import memoize from "memoize-one";
|
|
29
|
+
import {
|
|
30
|
+
memo,
|
|
31
|
+
useCallback,
|
|
32
|
+
useDeferredValue,
|
|
33
|
+
useEffect,
|
|
34
|
+
useImperativeHandle,
|
|
35
|
+
useLayoutEffect,
|
|
36
|
+
useMemo,
|
|
37
|
+
useRef,
|
|
38
|
+
useState,
|
|
39
|
+
type ReactElement,
|
|
40
|
+
type Ref,
|
|
41
|
+
} from "react";
|
|
42
|
+
import { Scroller } from "./Scroller";
|
|
43
|
+
|
|
44
|
+
export interface VirtualGalleryRef {
|
|
45
|
+
scrollToItem: (index: number) => void;
|
|
46
|
+
hilight: (index: number) => void;
|
|
47
|
+
unhilight: () => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface VirtualGalleryProps<T> {
|
|
51
|
+
children: (props: {
|
|
52
|
+
data: T;
|
|
53
|
+
index: number;
|
|
54
|
+
isLast: boolean;
|
|
55
|
+
}) => ReactElement;
|
|
56
|
+
galleryRef?: Ref<VirtualGalleryRef | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* fixed columns
|
|
59
|
+
*/
|
|
60
|
+
columns?: number;
|
|
61
|
+
/**
|
|
62
|
+
* default item height
|
|
63
|
+
*/
|
|
64
|
+
defaultHeight?: number;
|
|
65
|
+
/**
|
|
66
|
+
* default item width
|
|
67
|
+
*/
|
|
68
|
+
defaultWidth?: number;
|
|
69
|
+
/**
|
|
70
|
+
* padding
|
|
71
|
+
*/
|
|
72
|
+
padding?: "sm" | "md" | "lg" | "none";
|
|
73
|
+
/**
|
|
74
|
+
* data list
|
|
75
|
+
*/
|
|
76
|
+
items: T[];
|
|
77
|
+
/**
|
|
78
|
+
* loading state
|
|
79
|
+
*/
|
|
80
|
+
loading?: boolean;
|
|
81
|
+
initialScroll?: number;
|
|
82
|
+
/**
|
|
83
|
+
* hide scroll buttons
|
|
84
|
+
*/
|
|
85
|
+
hideScroller?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* load more callback
|
|
88
|
+
*/
|
|
89
|
+
onLoadMore?: () => void;
|
|
90
|
+
/**
|
|
91
|
+
* scroll handler
|
|
92
|
+
*/
|
|
93
|
+
onScroll?: (top: number) => void;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const createItemList = memoize((items) => items);
|
|
97
|
+
|
|
98
|
+
const _VirtualGallery = <T extends KeyValue>({
|
|
99
|
+
items,
|
|
100
|
+
children,
|
|
101
|
+
columns,
|
|
102
|
+
padding = "md",
|
|
103
|
+
defaultHeight: height = 48,
|
|
104
|
+
defaultWidth: width = 48,
|
|
105
|
+
hideScroller,
|
|
106
|
+
initialScroll,
|
|
107
|
+
loading,
|
|
108
|
+
onLoadMore,
|
|
109
|
+
onScroll,
|
|
110
|
+
galleryRef,
|
|
111
|
+
}: VirtualGalleryProps<T>) => {
|
|
112
|
+
const count = useDeferredValue(items.length);
|
|
113
|
+
const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
|
|
114
|
+
const itemList: Array<{
|
|
115
|
+
index: number;
|
|
116
|
+
data: T;
|
|
117
|
+
isLast: boolean;
|
|
118
|
+
isSticky: boolean;
|
|
119
|
+
}> = createItemList(
|
|
120
|
+
items.map((item: AnyObject, idx) => {
|
|
121
|
+
return {
|
|
122
|
+
index: idx,
|
|
123
|
+
data: item,
|
|
124
|
+
isLast: idx + 1 === items.length,
|
|
125
|
+
};
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
130
|
+
const [columnCount, setColumnCount] = useState(1);
|
|
131
|
+
const columnMap = useMemo(
|
|
132
|
+
() => new Array(columnCount).fill(0).map((_, idx) => idx),
|
|
133
|
+
[columnCount],
|
|
134
|
+
);
|
|
135
|
+
useEffectDebugger(
|
|
136
|
+
() => {
|
|
137
|
+
if (scrollerRef.current) {
|
|
138
|
+
const el = scrollerRef.current;
|
|
139
|
+
const handler = () => {
|
|
140
|
+
setColumnCount(
|
|
141
|
+
columns ??
|
|
142
|
+
Math.max(1, Math.floor(((el?.offsetWidth ?? 800) - 32) / width)),
|
|
143
|
+
);
|
|
144
|
+
};
|
|
145
|
+
const ob = new ResizeObserver(handler);
|
|
146
|
+
handler();
|
|
147
|
+
ob.observe(el);
|
|
148
|
+
|
|
149
|
+
return () => {
|
|
150
|
+
ob.unobserve(el);
|
|
151
|
+
ob.disconnect();
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[scrollerRef.current, columns, width],
|
|
156
|
+
"virtualGallery recalculate view count",
|
|
157
|
+
);
|
|
158
|
+
const virtualizer = useVirtualizer({
|
|
159
|
+
count: Math.ceil(count / columnCount),
|
|
160
|
+
getScrollElement: () => scrollerRef.current,
|
|
161
|
+
estimateSize: () => height,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const virtualItems = virtualizer.getVirtualItems();
|
|
165
|
+
|
|
166
|
+
const currentIndex = useRef(0);
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
currentIndex.current = virtualItems[1]?.index ?? 0;
|
|
169
|
+
if (
|
|
170
|
+
scrollerRef.current &&
|
|
171
|
+
virtualItems[0] &&
|
|
172
|
+
scrollerRef.current.scrollTop < virtualItems[0].size
|
|
173
|
+
)
|
|
174
|
+
currentIndex.current = 0;
|
|
175
|
+
}, [virtualItems]);
|
|
176
|
+
|
|
177
|
+
const calculateActiveScroll = useCallback(() => {
|
|
178
|
+
const {
|
|
179
|
+
scrollTop = 0,
|
|
180
|
+
scrollHeight = 0,
|
|
181
|
+
offsetHeight = 0,
|
|
182
|
+
} = (scrollerRef.current ?? {}) as HTMLElement;
|
|
183
|
+
if (scrollHeight === offsetHeight) return 0;
|
|
184
|
+
|
|
185
|
+
if (scrollTop === 0) return 1;
|
|
186
|
+
if (scrollTop >= scrollHeight - offsetHeight - extraSize * 2) return 2;
|
|
187
|
+
|
|
188
|
+
return 3;
|
|
189
|
+
}, []);
|
|
190
|
+
|
|
191
|
+
const [scrollActive, setScrollActive] = useState(0);
|
|
192
|
+
const fireScrollEvent = useDebounce(
|
|
193
|
+
(idx: number) => onScroll?.(currentIndex.current * idx),
|
|
194
|
+
[onScroll],
|
|
195
|
+
);
|
|
196
|
+
const handleScroll = useCallback(() => {
|
|
197
|
+
setScrollActive(calculateActiveScroll());
|
|
198
|
+
fireScrollEvent(columnCount);
|
|
199
|
+
}, [columnCount, onLoadMore]);
|
|
200
|
+
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
setScrollActive(calculateActiveScroll());
|
|
203
|
+
}, [count, calculateActiveScroll]);
|
|
204
|
+
|
|
205
|
+
useEffect(() => {
|
|
206
|
+
const db = debounce(() => {
|
|
207
|
+
const lastItem = virtualItems?.slice(-1).pop();
|
|
208
|
+
if (
|
|
209
|
+
lastItem &&
|
|
210
|
+
!loading &&
|
|
211
|
+
lastItem.index * columnCount + columnCount >= count - 1
|
|
212
|
+
)
|
|
213
|
+
onLoadMore?.();
|
|
214
|
+
});
|
|
215
|
+
db();
|
|
216
|
+
return () => db.cancel();
|
|
217
|
+
}, [onLoadMore, loading, itemList, virtualItems, count, columnCount]);
|
|
218
|
+
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
const firstItem = virtualItems[0]?.index;
|
|
221
|
+
if (
|
|
222
|
+
onLoadMore &&
|
|
223
|
+
firstItem !== undefined &&
|
|
224
|
+
items[firstItem * columnCount] === null
|
|
225
|
+
) {
|
|
226
|
+
const db = debounce(onLoadMore);
|
|
227
|
+
db(firstItem * columnCount);
|
|
228
|
+
return () => db.cancel();
|
|
229
|
+
}
|
|
230
|
+
}, [onLoadMore, items, columnCount, virtualItems, count]);
|
|
231
|
+
|
|
232
|
+
useLayoutEffect(() => {
|
|
233
|
+
setTimeout(() => {
|
|
234
|
+
initialScroll &&
|
|
235
|
+
virtualizer.scrollToIndex(Math.floor(initialScroll / columnCount), {
|
|
236
|
+
align: "start",
|
|
237
|
+
});
|
|
238
|
+
}, 100);
|
|
239
|
+
}, [columnCount]);
|
|
240
|
+
|
|
241
|
+
useImperativeHandle(
|
|
242
|
+
galleryRef,
|
|
243
|
+
() => ({
|
|
244
|
+
scrollToItem(index) {
|
|
245
|
+
virtualizer.scrollToIndex(Math.floor(index / columnCount), {
|
|
246
|
+
align: "start",
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
hilight(index) {
|
|
250
|
+
scrollerRef.current
|
|
251
|
+
?.querySelector(".hilight")
|
|
252
|
+
?.classList.remove("hilight", "ring-1");
|
|
253
|
+
virtualizer.scrollToIndex(Math.floor(index / columnCount), {
|
|
254
|
+
align: "start",
|
|
255
|
+
});
|
|
256
|
+
setTimeout(() => {
|
|
257
|
+
scrollerRef.current
|
|
258
|
+
?.querySelector(`[data-index="${index}"]`)
|
|
259
|
+
?.classList.add("hilight", "ring-1");
|
|
260
|
+
}, 100);
|
|
261
|
+
},
|
|
262
|
+
unhilight() {
|
|
263
|
+
scrollerRef.current
|
|
264
|
+
?.querySelector(".hilight")
|
|
265
|
+
?.classList.remove("hilight", "ring-1");
|
|
266
|
+
},
|
|
267
|
+
}),
|
|
268
|
+
[virtualizer, columnCount],
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<div
|
|
273
|
+
ref={scrollerRef}
|
|
274
|
+
onScroll={handleScroll}
|
|
275
|
+
className={classNames(
|
|
276
|
+
"area-content bg-base overflow-auto contain-strict",
|
|
277
|
+
)}
|
|
278
|
+
>
|
|
279
|
+
<div
|
|
280
|
+
className={classNames(
|
|
281
|
+
"inline-flex flex-nowrap gap-2 justify-center min-h-full min-w-full",
|
|
282
|
+
)}
|
|
283
|
+
>
|
|
284
|
+
<div
|
|
285
|
+
className={classNames(
|
|
286
|
+
"box-content",
|
|
287
|
+
padding === "sm" && "p-1",
|
|
288
|
+
padding === "md" && "p-2",
|
|
289
|
+
padding === "lg" && "p-4",
|
|
290
|
+
)}
|
|
291
|
+
>
|
|
292
|
+
<div
|
|
293
|
+
style={{
|
|
294
|
+
height: virtualizer.getTotalSize(),
|
|
295
|
+
}}
|
|
296
|
+
>
|
|
297
|
+
<div style={{ height: virtualItems[0]?.start }} />
|
|
298
|
+
{virtualItems.map(({ key, index, measureElement }) => (
|
|
299
|
+
<div
|
|
300
|
+
className="flex"
|
|
301
|
+
key={key}
|
|
302
|
+
data-index={index}
|
|
303
|
+
ref={measureElement}
|
|
304
|
+
>
|
|
305
|
+
{columnMap.map((col) => (
|
|
306
|
+
<div
|
|
307
|
+
key={`${key}:${col}`}
|
|
308
|
+
data-index={index * columnCount + col}
|
|
309
|
+
className={classNames("grid rounded-capped")}
|
|
310
|
+
>
|
|
311
|
+
{itemList[index * columnCount + col] &&
|
|
312
|
+
children(
|
|
313
|
+
itemList[index * columnCount + col] as AnyObject,
|
|
314
|
+
)}
|
|
315
|
+
</div>
|
|
316
|
+
))}
|
|
317
|
+
</div>
|
|
318
|
+
))}
|
|
319
|
+
{onLoadMore && (
|
|
320
|
+
<div className="h-16">{loading && <Skeleton />}</div>
|
|
321
|
+
)}
|
|
322
|
+
</div>
|
|
323
|
+
</div>
|
|
324
|
+
{!hideScroller && (
|
|
325
|
+
<div className={classNames("self-end sticky bottom-2")}>
|
|
326
|
+
<Scroller
|
|
327
|
+
vertical
|
|
328
|
+
scrollActive={scrollActive}
|
|
329
|
+
onStart={() => virtualizer.scrollToIndex(0)}
|
|
330
|
+
onPrevious={() =>
|
|
331
|
+
scrollerRef.current &&
|
|
332
|
+
virtualizer.scrollBy(-scrollerRef.current.offsetHeight)
|
|
333
|
+
}
|
|
334
|
+
onNext={() =>
|
|
335
|
+
scrollerRef.current &&
|
|
336
|
+
virtualizer.scrollBy(scrollerRef.current.offsetHeight)
|
|
337
|
+
}
|
|
338
|
+
onEnd={() => virtualizer.scrollToIndex(count - 1)}
|
|
339
|
+
/>
|
|
340
|
+
</div>
|
|
341
|
+
)}
|
|
342
|
+
</div>
|
|
343
|
+
</div>
|
|
344
|
+
);
|
|
345
|
+
};
|
|
346
|
+
_VirtualGallery.displayName = "VirtualGallery";
|
|
347
|
+
|
|
348
|
+
const GenericMemo: <T>(c: T) => T = memo;
|
|
349
|
+
export const VirtualGallery = GenericMemo(_VirtualGallery);
|