@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.
Files changed (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/histogram/Histogram.js +89 -0
  4. package/dist/cjs/histogram/Histogram.module.css +1 -0
  5. package/dist/cjs/hooks/useFilteredList.js +56 -0
  6. package/dist/cjs/hooks/usePagination.js +85 -0
  7. package/dist/cjs/i18n/ar.json.js +56 -0
  8. package/dist/cjs/i18n/en.json.js +56 -0
  9. package/dist/cjs/i18n/index.js +33 -0
  10. package/dist/cjs/index.js +24 -0
  11. package/dist/cjs/json/JsonViewer.js +246 -0
  12. package/dist/cjs/pagination/Pagination.js +100 -0
  13. package/dist/cjs/table/BodyCell.js +47 -0
  14. package/dist/cjs/table/HeaderCell.js +79 -0
  15. package/dist/cjs/table/Table.js +133 -0
  16. package/dist/cjs/table/useTableColumns.js +26 -0
  17. package/dist/cjs/table/useVirtualTable.js +56 -0
  18. package/dist/cjs/tree/TreeNode.js +107 -0
  19. package/dist/cjs/tree/TreePanel.js +136 -0
  20. package/dist/cjs/tree/types.js +30 -0
  21. package/dist/cjs/tree/useTree.js +181 -0
  22. package/dist/cjs/tree/utils.js +145 -0
  23. package/dist/cjs/virtual/Gallery.js +229 -0
  24. package/dist/cjs/virtual/List.js +218 -0
  25. package/dist/cjs/virtual/Scroller.js +77 -0
  26. package/dist/esm/histogram/Histogram.js +89 -0
  27. package/dist/esm/histogram/Histogram.module.css +1 -0
  28. package/dist/esm/hooks/useFilteredList.js +56 -0
  29. package/dist/esm/hooks/usePagination.js +85 -0
  30. package/dist/esm/i18n/ar.json.js +47 -0
  31. package/dist/esm/i18n/en.json.js +47 -0
  32. package/dist/esm/i18n/index.js +33 -0
  33. package/dist/esm/index.js +12 -0
  34. package/dist/esm/json/JsonViewer.js +246 -0
  35. package/dist/esm/pagination/Pagination.js +100 -0
  36. package/dist/esm/table/BodyCell.js +47 -0
  37. package/dist/esm/table/HeaderCell.js +79 -0
  38. package/dist/esm/table/Table.js +133 -0
  39. package/dist/esm/table/useTableColumns.js +26 -0
  40. package/dist/esm/table/useVirtualTable.js +56 -0
  41. package/dist/esm/tree/TreeNode.js +107 -0
  42. package/dist/esm/tree/TreePanel.js +136 -0
  43. package/dist/esm/tree/types.js +29 -0
  44. package/dist/esm/tree/useTree.js +181 -0
  45. package/dist/esm/tree/utils.js +140 -0
  46. package/dist/esm/virtual/Gallery.js +229 -0
  47. package/dist/esm/virtual/List.js +218 -0
  48. package/dist/esm/virtual/Scroller.js +77 -0
  49. package/dist/types/histogram/Histogram.d.ts +41 -0
  50. package/dist/types/hooks/useFilteredList.d.ts +7 -0
  51. package/dist/types/hooks/usePagination.d.ts +25 -0
  52. package/dist/types/i18n/index.d.ts +1 -0
  53. package/dist/types/index.d.ts +10 -0
  54. package/dist/types/json/JsonViewer.d.ts +59 -0
  55. package/dist/types/pagination/Pagination.d.ts +24 -0
  56. package/dist/types/table/BodyCell.d.ts +5 -0
  57. package/dist/types/table/HeaderCell.d.ts +2 -0
  58. package/dist/types/table/Table.d.ts +2 -0
  59. package/dist/types/table/types.d.ts +26 -0
  60. package/dist/types/table/useTableColumns.d.ts +8 -0
  61. package/dist/types/table/useVirtualTable.d.ts +8 -0
  62. package/dist/types/tree/TreeNode.d.ts +2 -0
  63. package/dist/types/tree/TreePanel.d.ts +2 -0
  64. package/dist/types/tree/types.d.ts +138 -0
  65. package/dist/types/tree/useTree.d.ts +20 -0
  66. package/dist/types/tree/utils.d.ts +10 -0
  67. package/dist/types/virtual/Gallery.d.ts +55 -0
  68. package/dist/types/virtual/List.d.ts +59 -0
  69. package/dist/types/virtual/Scroller.d.ts +8 -0
  70. package/package.json +55 -0
  71. package/src/histogram/Histogram.module.css +57 -0
  72. package/src/histogram/Histogram.tsx +176 -0
  73. package/src/hooks/useFilteredList.tsx +78 -0
  74. package/src/hooks/usePagination.tsx +135 -0
  75. package/src/i18n/ar.json +36 -0
  76. package/src/i18n/en.json +36 -0
  77. package/src/i18n/index.ts +28 -0
  78. package/src/index.ts +35 -0
  79. package/src/json/JsonViewer.tsx +381 -0
  80. package/src/pagination/Pagination.tsx +157 -0
  81. package/src/table/BodyCell.tsx +54 -0
  82. package/src/table/HeaderCell.tsx +70 -0
  83. package/src/table/Table.tsx +143 -0
  84. package/src/table/types.ts +65 -0
  85. package/src/table/useTableColumns.ts +52 -0
  86. package/src/table/useVirtualTable.ts +54 -0
  87. package/src/tree/TreeNode.tsx +163 -0
  88. package/src/tree/TreePanel.tsx +157 -0
  89. package/src/tree/types.ts +181 -0
  90. package/src/tree/useTree.ts +227 -0
  91. package/src/tree/utils.ts +185 -0
  92. package/src/virtual/Gallery.tsx +349 -0
  93. package/src/virtual/List.tsx +351 -0
  94. package/src/virtual/Scroller.tsx +81 -0
@@ -0,0 +1,351 @@
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
+ /**
25
+ * Pass list of items with `null` filled array to intialize list at a page > 0
26
+ *
27
+ * `initialScroll`: last item scroll iindex from state
28
+ * `items`: [...Array(last page * page count).fill(null), ...last page items from state]
29
+ */
30
+
31
+ import { Skeleton, useDebounce } from "@react-fabric/core";
32
+ import { debounce } from "@react-fabric/utilities";
33
+ import { useVirtualizer } from "@tanstack/react-virtual";
34
+ import classNames from "classnames";
35
+ import memoizeOne from "memoize-one";
36
+ import {
37
+ memo,
38
+ useCallback,
39
+ useDeferredValue,
40
+ useEffect,
41
+ useImperativeHandle,
42
+ useLayoutEffect,
43
+ useMemo,
44
+ useRef,
45
+ useState,
46
+ type ReactElement,
47
+ type Ref,
48
+ } from "react";
49
+ import { Scroller } from "./Scroller";
50
+
51
+ export interface VirtualListRef {
52
+ scrollToItem: (index: number) => void;
53
+ hilight: (index: number) => void;
54
+ unhilight: () => void;
55
+ }
56
+
57
+ export interface VirtualListProps<T> {
58
+ children: (props: {
59
+ data: T;
60
+ index: number;
61
+ isLast: boolean;
62
+ }) => ReactElement;
63
+ listRef?: Ref<VirtualListRef | undefined>;
64
+ /**
65
+ * default item height
66
+ */
67
+ defaultHeight?: number;
68
+ /**
69
+ * default item width
70
+ */
71
+ defaultWidth?: number;
72
+ /**
73
+ * full width list
74
+ */
75
+ fullWidth?: boolean;
76
+ /**
77
+ * padding
78
+ */
79
+ padding?: "sm" | "md" | "lg" | "none";
80
+ /**
81
+ * list orientation
82
+ */
83
+ orientation?: "vertical" | "horizontal";
84
+ /**
85
+ * data list
86
+ */
87
+ items: T[];
88
+ initialScroll?: number;
89
+ /**
90
+ * loading state
91
+ */
92
+ loading?: boolean;
93
+ /**
94
+ * hide scroll buttons
95
+ */
96
+ hideScroller?: boolean;
97
+ /**
98
+ * load more callback
99
+ */
100
+ onLoadMore?: (page?: number) => void;
101
+ /**
102
+ * scroll handler
103
+ */
104
+ onScroll?: (top: number) => void;
105
+ }
106
+
107
+ const createItemList = memoizeOne((items) => items);
108
+
109
+ const _VirtualList = <T extends KeyValue>({
110
+ items,
111
+ children,
112
+ fullWidth,
113
+ padding = "md",
114
+ orientation = "vertical",
115
+ defaultHeight: height = 48,
116
+ defaultWidth: width = 48,
117
+ hideScroller,
118
+ initialScroll,
119
+ loading,
120
+ onLoadMore,
121
+ onScroll,
122
+ listRef,
123
+ }: VirtualListProps<T>) => {
124
+ const count = useDeferredValue(items.length);
125
+ const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
126
+ const itemList: Array<{
127
+ index: number;
128
+ data: T;
129
+ isLast: boolean;
130
+ isSticky: boolean;
131
+ }> = createItemList(
132
+ items.map((item: AnyObject, idx) => {
133
+ return {
134
+ index: idx,
135
+ data: item,
136
+ isLast: idx + 1 === items.length,
137
+ };
138
+ }),
139
+ );
140
+
141
+ const scrollerRef = useRef<HTMLDivElement>(null);
142
+ const virtualizer = useVirtualizer({
143
+ count,
144
+ horizontal: orientation === "horizontal",
145
+ getScrollElement: () => scrollerRef.current,
146
+ estimateSize: () => (orientation === "horizontal" ? width : height),
147
+ });
148
+
149
+ const virtualItems = virtualizer.getVirtualItems();
150
+
151
+ const currentIndex = useRef(0);
152
+ useEffect(() => {
153
+ currentIndex.current = virtualItems[1]?.index ?? 0;
154
+ if (
155
+ scrollerRef.current &&
156
+ virtualItems[0] &&
157
+ scrollerRef.current.scrollTop < virtualItems[0].size
158
+ )
159
+ currentIndex.current = 0;
160
+ }, [virtualItems]);
161
+
162
+ const calculateActiveScroll = useCallback(() => {
163
+ const {
164
+ scrollTop = 0,
165
+ scrollLeft = 0,
166
+ scrollWidth = 0,
167
+ scrollHeight = 0,
168
+ offsetWidth = 0,
169
+ offsetHeight = 0,
170
+ } = (scrollerRef.current ?? {}) as HTMLElement;
171
+ if (orientation === "vertical") {
172
+ if (scrollHeight === offsetHeight) return 0;
173
+
174
+ if (scrollTop === 0) return 1;
175
+ if (scrollTop >= scrollHeight - offsetHeight - extraSize * 2) return 2;
176
+ }
177
+ if (orientation === "horizontal") {
178
+ if (scrollWidth === offsetWidth) return 0;
179
+
180
+ if (Math.abs(scrollLeft) === 0) return 1;
181
+ if (Math.abs(scrollLeft) >= scrollWidth - offsetWidth - extraSize * 2)
182
+ return 2;
183
+ }
184
+ return 3;
185
+ }, [orientation]);
186
+
187
+ const [scrollActive, setScrollActive] = useState(0);
188
+ const fireScrollEvent = useDebounce(
189
+ () => onScroll?.(currentIndex.current),
190
+ [onScroll],
191
+ );
192
+ const handleScroll = useCallback(() => {
193
+ setScrollActive(calculateActiveScroll());
194
+ fireScrollEvent();
195
+ }, [orientation, onLoadMore]);
196
+
197
+ useEffect(() => {
198
+ setScrollActive(calculateActiveScroll());
199
+ }, [itemList, calculateActiveScroll]);
200
+
201
+ useEffect(() => {
202
+ const db = debounce(() => {
203
+ const lastItem = virtualItems?.slice(-1).pop();
204
+ if (lastItem && !loading && lastItem.index === count - 1) onLoadMore?.();
205
+ });
206
+ db();
207
+ return () => db.cancel();
208
+ }, [onLoadMore, loading, itemList, virtualItems, count]);
209
+
210
+ useEffect(() => {
211
+ const firstItem = virtualItems[0]?.index;
212
+ if (onLoadMore && firstItem !== undefined && items[firstItem] === null) {
213
+ const db = debounce(onLoadMore);
214
+ db(firstItem);
215
+ return () => db.cancel();
216
+ }
217
+ }, [onLoadMore, items, virtualItems, count]);
218
+
219
+ useLayoutEffect(() => {
220
+ setTimeout(() => {
221
+ initialScroll &&
222
+ virtualizer.scrollToIndex(initialScroll, { align: "start" });
223
+ }, 100);
224
+ }, []);
225
+
226
+ useImperativeHandle(
227
+ listRef,
228
+ () => ({
229
+ scrollToItem(index) {
230
+ virtualizer.scrollToIndex(index, { align: "start" });
231
+ },
232
+ hilight(index) {
233
+ scrollerRef.current
234
+ ?.querySelector(".hilight")
235
+ ?.classList.remove("hilight", "ring-1");
236
+ virtualizer.scrollToIndex(Math.floor(index), {
237
+ align: "start",
238
+ });
239
+ setTimeout(() => {
240
+ scrollerRef.current
241
+ ?.querySelector(`[data-index="${index}"]`)
242
+ ?.classList.add("hilight", "ring-1");
243
+ }, 100);
244
+ },
245
+ unhilight() {
246
+ scrollerRef.current
247
+ ?.querySelector(".hilight")
248
+ ?.classList.remove("hilight", "ring-1");
249
+ },
250
+ }),
251
+ [virtualizer],
252
+ );
253
+
254
+ return (
255
+ <div
256
+ ref={scrollerRef}
257
+ onScroll={handleScroll}
258
+ className={classNames(
259
+ "area-content bg-base overflow-auto contain-strict",
260
+ )}
261
+ >
262
+ <div
263
+ className={classNames(
264
+ "inline-flex flex-nowrap gap-2 justify-center min-h-full min-w-full",
265
+ orientation === "horizontal" && "flex-col",
266
+ )}
267
+ >
268
+ <div
269
+ className={classNames(
270
+ "box-content inline-flex flex-nowrap",
271
+ orientation === "vertical" && "flex-col",
272
+ orientation === "horizontal" && "flex-row",
273
+ fullWidth && "flex-1",
274
+ padding === "sm" && "p-1",
275
+ padding === "md" && "p-2",
276
+ padding === "lg" && "p-4",
277
+ )}
278
+ >
279
+ <div
280
+ style={
281
+ orientation === "vertical"
282
+ ? { height: virtualizer.getTotalSize() }
283
+ : { width: virtualizer.getTotalSize() }
284
+ }
285
+ >
286
+ <div
287
+ style={
288
+ orientation === "vertical"
289
+ ? { height: virtualItems[0]?.start }
290
+ : { width: virtualItems[0]?.start, display: "inline-block" }
291
+ }
292
+ />
293
+ {virtualItems.map(({ key, index }) => (
294
+ <div
295
+ key={key}
296
+ data-index={index}
297
+ ref={virtualizer.measureElement}
298
+ className={classNames(
299
+ "rounded-capped",
300
+ orientation === "horizontal" ? "inline-grid" : "grid",
301
+ )}
302
+ >
303
+ {children(itemList[index] as AnyObject)}
304
+ </div>
305
+ ))}
306
+ </div>
307
+ {onLoadMore && (
308
+ <div className={orientation === "horizontal" ? "min-w-16" : "h-16"}>
309
+ {loading && <Skeleton />}
310
+ </div>
311
+ )}
312
+ </div>
313
+ {!hideScroller && (
314
+ <div
315
+ className={classNames(
316
+ orientation === "vertical" && "self-end sticky bottom-2",
317
+ orientation === "horizontal" && "self-end sticky end-2",
318
+ )}
319
+ >
320
+ <Scroller
321
+ vertical={orientation === "vertical"}
322
+ scrollActive={scrollActive}
323
+ onStart={() => virtualizer.scrollToIndex(0)}
324
+ onPrevious={() =>
325
+ scrollerRef.current &&
326
+ virtualizer.scrollBy(
327
+ -(orientation === "vertical"
328
+ ? scrollerRef.current.offsetHeight
329
+ : scrollerRef.current.offsetWidth),
330
+ )
331
+ }
332
+ onNext={() =>
333
+ scrollerRef.current &&
334
+ virtualizer.scrollBy(
335
+ orientation === "vertical"
336
+ ? scrollerRef.current.offsetHeight
337
+ : scrollerRef.current.offsetWidth,
338
+ )
339
+ }
340
+ onEnd={() => virtualizer.scrollToIndex(count - 1)}
341
+ />
342
+ </div>
343
+ )}
344
+ </div>
345
+ </div>
346
+ );
347
+ };
348
+ _VirtualList.displayName = "VirtualList";
349
+
350
+ const GenericMemo: <T>(c: T) => T = memo;
351
+ export const VirtualList = GenericMemo(_VirtualList);
@@ -0,0 +1,81 @@
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 { Button, ButtonGroup, CoreIcons } from "@react-fabric/core";
25
+
26
+ export const Scroller = ({
27
+ vertical = true,
28
+ scrollActive,
29
+ onStart,
30
+ onEnd,
31
+ onPrevious,
32
+ onNext,
33
+ }: {
34
+ scrollActive: number;
35
+ vertical: boolean;
36
+ onStart: () => void;
37
+ onEnd: () => void;
38
+ onPrevious: () => void;
39
+ onNext: () => void;
40
+ }) => {
41
+ return (
42
+ <ButtonGroup vertical={vertical} variant="link" className="outline">
43
+ <Button
44
+ rtlFlip
45
+ size="sm"
46
+ variant="link"
47
+ aria-label="scroll to top"
48
+ icon={vertical ? CoreIcons.chevronsUp : CoreIcons.chevronsLeft}
49
+ disabled={(scrollActive & 2) === 0}
50
+ onClick={onStart}
51
+ />
52
+ <Button
53
+ rtlFlip
54
+ size="sm"
55
+ variant="link"
56
+ aria-label="scroll up"
57
+ icon={vertical ? CoreIcons.chevronUp : CoreIcons.chevronLeft}
58
+ disabled={(scrollActive & 2) === 0}
59
+ onClick={onPrevious}
60
+ />
61
+ <Button
62
+ rtlFlip
63
+ size="sm"
64
+ variant="link"
65
+ aria-label="scroll down"
66
+ icon={vertical ? CoreIcons.chevronDown : CoreIcons.chevronRight}
67
+ disabled={scrollActive % 2 === 0}
68
+ onClick={onNext}
69
+ />
70
+ <Button
71
+ rtlFlip
72
+ size="sm"
73
+ variant="link"
74
+ aria-label="scroll to bottom"
75
+ icon={vertical ? CoreIcons.chevronsDown : CoreIcons.chevronsRight}
76
+ disabled={scrollActive % 2 === 0}
77
+ onClick={onEnd}
78
+ />
79
+ </ButtonGroup>
80
+ );
81
+ };