@react-fabric/data 1.0.0-beta.3 → 1.0.0-beta.31

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 (83) hide show
  1. package/dist/cjs/checklist/CheckList.js +143 -0
  2. package/dist/cjs/histogram/Histogram.js +16 -5
  3. package/dist/cjs/hooks/usePagination.js +7 -4
  4. package/dist/cjs/hooks/useSelectableList.js +54 -0
  5. package/dist/cjs/index.js +2 -0
  6. package/dist/cjs/json/JsonViewer.js +30 -23
  7. package/dist/cjs/table/BodyCell.js +38 -5
  8. package/dist/cjs/table/BodyRow.js +107 -0
  9. package/dist/cjs/table/CheckboxCell.js +1 -1
  10. package/dist/cjs/table/HeaderCell.js +29 -36
  11. package/dist/cjs/table/Table.js +104 -66
  12. package/dist/cjs/table/useResizer.js +4 -4
  13. package/dist/cjs/table/useVirtualTable.js +118 -17
  14. package/dist/cjs/tree/TreeNode.js +44 -24
  15. package/dist/cjs/tree/TreePanel.js +28 -10
  16. package/dist/cjs/tree/useTree.js +96 -14
  17. package/dist/cjs/tree/utils.js +20 -5
  18. package/dist/cjs/virtual/Gallery.js +43 -29
  19. package/dist/cjs/virtual/List.js +43 -31
  20. package/dist/esm/checklist/CheckList.js +143 -0
  21. package/dist/esm/histogram/Histogram.js +16 -5
  22. package/dist/esm/hooks/usePagination.js +7 -4
  23. package/dist/esm/hooks/useSelectableList.js +54 -0
  24. package/dist/esm/index.js +1 -0
  25. package/dist/esm/json/JsonViewer.js +31 -24
  26. package/dist/esm/table/BodyCell.js +38 -5
  27. package/dist/esm/table/BodyRow.js +107 -0
  28. package/dist/esm/table/CheckboxCell.js +1 -1
  29. package/dist/esm/table/HeaderCell.js +30 -37
  30. package/dist/esm/table/Table.js +106 -68
  31. package/dist/esm/table/useResizer.js +4 -4
  32. package/dist/esm/table/useVirtualTable.js +120 -19
  33. package/dist/esm/tree/TreeNode.js +46 -26
  34. package/dist/esm/tree/TreePanel.js +28 -10
  35. package/dist/esm/tree/useTree.js +98 -16
  36. package/dist/esm/tree/utils.js +20 -5
  37. package/dist/esm/virtual/Gallery.js +43 -29
  38. package/dist/esm/virtual/List.js +43 -31
  39. package/dist/types/checklist/CheckList.d.ts +30 -0
  40. package/dist/types/histogram/Histogram.d.ts +3 -3
  41. package/dist/types/hooks/useSelectableList.d.ts +28 -0
  42. package/dist/types/index.d.ts +2 -1
  43. package/dist/types/table/AddColumn.d.ts +2 -2
  44. package/dist/types/table/BodyCell.d.ts +4 -3
  45. package/dist/types/table/BodyRow.d.ts +20 -0
  46. package/dist/types/table/EmptyCell.d.ts +2 -2
  47. package/dist/types/table/HeaderCell.d.ts +2 -2
  48. package/dist/types/table/Table.d.ts +1 -1
  49. package/dist/types/table/types.d.ts +19 -8
  50. package/dist/types/table/useTableColumns.d.ts +6 -6
  51. package/dist/types/table/useVirtualTable.d.ts +20 -5
  52. package/dist/types/tree/TreeNode.d.ts +1 -1
  53. package/dist/types/tree/TreePanel.d.ts +1 -1
  54. package/dist/types/tree/types.d.ts +25 -5
  55. package/dist/types/tree/useTree.d.ts +2 -9
  56. package/dist/types/tree/utils.d.ts +4 -1
  57. package/dist/types/virtual/Gallery.d.ts +11 -3
  58. package/dist/types/virtual/List.d.ts +7 -3
  59. package/package.json +8 -7
  60. package/src/checklist/CheckList.tsx +222 -0
  61. package/src/histogram/Histogram.tsx +9 -2
  62. package/src/hooks/usePagination.tsx +13 -7
  63. package/src/hooks/useSelectableList.ts +94 -0
  64. package/src/index.ts +2 -1
  65. package/src/json/JsonViewer.tsx +31 -31
  66. package/src/table/AddColumn.tsx +2 -2
  67. package/src/table/BodyCell.tsx +34 -5
  68. package/src/table/BodyRow.tsx +143 -0
  69. package/src/table/CheckboxCell.tsx +1 -1
  70. package/src/table/EmptyCell.tsx +2 -2
  71. package/src/table/HeaderCell.tsx +27 -13
  72. package/src/table/Table.tsx +141 -73
  73. package/src/table/types.ts +22 -7
  74. package/src/table/useResizer.ts +5 -3
  75. package/src/table/useTableColumns.ts +7 -7
  76. package/src/table/useVirtualTable.ts +142 -27
  77. package/src/tree/TreeNode.tsx +90 -56
  78. package/src/tree/TreePanel.tsx +23 -3
  79. package/src/tree/types.ts +32 -4
  80. package/src/tree/useTree.ts +102 -23
  81. package/src/tree/utils.ts +23 -8
  82. package/src/virtual/Gallery.tsx +45 -34
  83. package/src/virtual/List.tsx +40 -27
@@ -22,6 +22,7 @@
22
22
  */
23
23
 
24
24
  import { Skeleton, useDebounce, useEffectDebugger } from "@react-fabric/core";
25
+ import { type TestProps } from "@react-fabric/core/dist/types/types";
25
26
  import { debounce } from "@react-fabric/utilities";
26
27
  import { useVirtualizer } from "@tanstack/react-virtual";
27
28
  import classNames from "classnames";
@@ -47,9 +48,9 @@ export interface VirtualGalleryRef {
47
48
  unhilight: () => void;
48
49
  }
49
50
 
50
- export interface VirtualGalleryProps<T> {
51
+ export interface VirtualGalleryProps<T> extends TestProps {
51
52
  children: (props: {
52
- data: T;
53
+ item: T;
53
54
  index: number;
54
55
  isLast: boolean;
55
56
  }) => ReactElement;
@@ -66,6 +67,12 @@ export interface VirtualGalleryProps<T> {
66
67
  * default item width
67
68
  */
68
69
  defaultWidth?: number;
70
+ /**
71
+ * full width list
72
+ */
73
+ fullWidth?: boolean;
74
+ maxWidth?: string | number;
75
+ minWidth?: string | number;
69
76
  /**
70
77
  * padding
71
78
  */
@@ -74,6 +81,7 @@ export interface VirtualGalleryProps<T> {
74
81
  * data list
75
82
  */
76
83
  items: T[];
84
+ total?: number;
77
85
  /**
78
86
  * loading state
79
87
  */
@@ -99,6 +107,10 @@ const _VirtualGallery = <T extends KeyValue>({
99
107
  items,
100
108
  children,
101
109
  columns,
110
+ fullWidth,
111
+ maxWidth,
112
+ minWidth,
113
+ total = 0,
102
114
  padding = "md",
103
115
  defaultHeight: height = 48,
104
116
  defaultWidth: width = 48,
@@ -108,20 +120,21 @@ const _VirtualGallery = <T extends KeyValue>({
108
120
  onLoadMore,
109
121
  onScroll,
110
122
  galleryRef,
123
+ ...aria
111
124
  }: VirtualGalleryProps<T>) => {
112
125
  const count = useDeferredValue(items.length);
113
- const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
126
+ const extraSize = useMemo(() => (onLoadMore ? 8 : 0), [onLoadMore]);
114
127
  const itemList: Array<{
115
128
  index: number;
116
- data: T;
129
+ item: T;
117
130
  isLast: boolean;
118
131
  isSticky: boolean;
119
132
  }> = createItemList(
120
- items.map((item: AnyObject, idx) => {
133
+ items.map((item: AnyObject, index) => {
121
134
  return {
122
- index: idx,
123
- data: item,
124
- isLast: idx + 1 === items.length,
135
+ index,
136
+ item,
137
+ isLast: index + 1 === items.length,
125
138
  };
126
139
  }),
127
140
  );
@@ -203,31 +216,23 @@ const _VirtualGallery = <T extends KeyValue>({
203
216
  }, [count, calculateActiveScroll]);
204
217
 
205
218
  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
- ) {
219
+ if (onLoadMore) {
220
+ const firstItem = virtualItems[0]?.index;
221
+ const lastItem = virtualItems?.slice(-1).pop()?.index;
222
+ const lastIndex = (lastItem ?? 0) * columnCount + columnCount;
223
+ const canLoadPrevPage =
224
+ firstItem !== undefined && items[firstItem * columnCount] === null;
225
+ const canLoadNextPage =
226
+ lastItem !== undefined && lastIndex >= count - 1 && count < total;
226
227
  const db = debounce(onLoadMore);
227
- db(firstItem * columnCount);
228
+ if (canLoadPrevPage) {
229
+ db(firstItem);
230
+ } else if (!loading && canLoadNextPage) {
231
+ db(lastItem + 1);
232
+ }
228
233
  return () => db.cancel();
229
234
  }
230
- }, [onLoadMore, items, columnCount, virtualItems, count]);
235
+ }, [onLoadMore, virtualItems]);
231
236
 
232
237
  useLayoutEffect(() => {
233
238
  setTimeout(() => {
@@ -243,7 +248,7 @@ const _VirtualGallery = <T extends KeyValue>({
243
248
  () => ({
244
249
  scrollToItem(index) {
245
250
  virtualizer.scrollToIndex(Math.floor(index / columnCount), {
246
- align: "start",
251
+ align: "auto",
247
252
  });
248
253
  },
249
254
  hilight(index) {
@@ -251,7 +256,7 @@ const _VirtualGallery = <T extends KeyValue>({
251
256
  ?.querySelector(".hilight")
252
257
  ?.classList.remove("hilight", "ring-1");
253
258
  virtualizer.scrollToIndex(Math.floor(index / columnCount), {
254
- align: "start",
259
+ align: "auto",
255
260
  });
256
261
  setTimeout(() => {
257
262
  scrollerRef.current
@@ -275,6 +280,7 @@ const _VirtualGallery = <T extends KeyValue>({
275
280
  className={classNames(
276
281
  "area-content bg-base overflow-auto contain-strict",
277
282
  )}
283
+ {...aria}
278
284
  >
279
285
  <div
280
286
  className={classNames(
@@ -284,20 +290,25 @@ const _VirtualGallery = <T extends KeyValue>({
284
290
  <div
285
291
  className={classNames(
286
292
  "box-content",
293
+ fullWidth && "flex-1",
287
294
  padding === "sm" && "p-1",
288
295
  padding === "md" && "p-2",
289
296
  padding === "lg" && "p-4",
290
297
  )}
298
+ style={{
299
+ minWidth,
300
+ maxWidth,
301
+ }}
291
302
  >
292
303
  <div
293
304
  style={{
294
- height: virtualizer.getTotalSize(),
305
+ height: virtualizer.getTotalSize() + extraSize,
295
306
  }}
296
307
  >
297
308
  <div style={{ height: virtualItems[0]?.start }} />
298
309
  {virtualItems.map(({ key, index, measureElement }) => (
299
310
  <div
300
- className="flex"
311
+ className="flex justify-start"
301
312
  key={key}
302
313
  data-index={index}
303
314
  ref={measureElement}
@@ -29,6 +29,7 @@
29
29
  */
30
30
 
31
31
  import { Skeleton, useDebounce } from "@react-fabric/core";
32
+ import { type TestProps } from "@react-fabric/core/dist/types/types";
32
33
  import { debounce } from "@react-fabric/utilities";
33
34
  import { useVirtualizer } from "@tanstack/react-virtual";
34
35
  import classNames from "classnames";
@@ -54,9 +55,9 @@ export interface VirtualListRef {
54
55
  unhilight: () => void;
55
56
  }
56
57
 
57
- export interface VirtualListProps<T> {
58
+ export interface VirtualListProps<T> extends TestProps {
58
59
  children: (props: {
59
- data: T;
60
+ item: T;
60
61
  index: number;
61
62
  isLast: boolean;
62
63
  }) => ReactElement;
@@ -73,6 +74,8 @@ export interface VirtualListProps<T> {
73
74
  * full width list
74
75
  */
75
76
  fullWidth?: boolean;
77
+ maxWidth?: string | number;
78
+ minWidth?: string | number;
76
79
  /**
77
80
  * padding
78
81
  */
@@ -85,6 +88,7 @@ export interface VirtualListProps<T> {
85
88
  * data list
86
89
  */
87
90
  items: T[];
91
+ total?: number;
88
92
  initialScroll?: number;
89
93
  /**
90
94
  * loading state
@@ -108,8 +112,11 @@ const createItemList = memoizeOne((items) => items);
108
112
 
109
113
  const _VirtualList = <T extends KeyValue>({
110
114
  items,
115
+ total = 0,
111
116
  children,
112
117
  fullWidth,
118
+ maxWidth,
119
+ minWidth,
113
120
  padding = "md",
114
121
  orientation = "vertical",
115
122
  defaultHeight: height = 48,
@@ -120,20 +127,21 @@ const _VirtualList = <T extends KeyValue>({
120
127
  onLoadMore,
121
128
  onScroll,
122
129
  listRef,
130
+ ...aria
123
131
  }: VirtualListProps<T>) => {
124
132
  const count = useDeferredValue(items.length);
125
- const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
133
+ const extraSize = useMemo(() => (onLoadMore ? 8 : 0), [onLoadMore]);
126
134
  const itemList: Array<{
127
135
  index: number;
128
136
  data: T;
129
137
  isLast: boolean;
130
138
  isSticky: boolean;
131
139
  }> = createItemList(
132
- items.map((item: AnyObject, idx) => {
140
+ items.map((item: AnyObject, index) => {
133
141
  return {
134
- index: idx,
135
- data: item,
136
- isLast: idx + 1 === items.length,
142
+ index,
143
+ item,
144
+ isLast: index + 1 === items.length,
137
145
  };
138
146
  }),
139
147
  );
@@ -199,22 +207,22 @@ const _VirtualList = <T extends KeyValue>({
199
207
  }, [itemList, calculateActiveScroll]);
200
208
 
201
209
  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) {
210
+ if (onLoadMore) {
211
+ const firstItem = virtualItems[0]?.index;
212
+ const lastItem = virtualItems?.slice(-1).pop()?.index;
213
+ const canLoadPrevPage =
214
+ firstItem !== undefined && items[firstItem] === null;
215
+ const canLoadNextPage =
216
+ lastItem !== undefined && lastItem === count - 1 && count < total;
213
217
  const db = debounce(onLoadMore);
214
- db(firstItem);
218
+ if (canLoadPrevPage) {
219
+ db(firstItem);
220
+ } else if (!loading && canLoadNextPage) {
221
+ db(lastItem + 1);
222
+ }
215
223
  return () => db.cancel();
216
224
  }
217
- }, [onLoadMore, items, virtualItems, count]);
225
+ }, [onLoadMore, virtualItems]);
218
226
 
219
227
  useLayoutEffect(() => {
220
228
  setTimeout(() => {
@@ -227,14 +235,14 @@ const _VirtualList = <T extends KeyValue>({
227
235
  listRef,
228
236
  () => ({
229
237
  scrollToItem(index) {
230
- virtualizer.scrollToIndex(index, { align: "start" });
238
+ virtualizer.scrollToIndex(index, { align: "auto" });
231
239
  },
232
240
  hilight(index) {
233
241
  scrollerRef.current
234
242
  ?.querySelector(".hilight")
235
243
  ?.classList.remove("hilight", "ring-1");
236
244
  virtualizer.scrollToIndex(Math.floor(index), {
237
- align: "start",
245
+ align: "auto",
238
246
  });
239
247
  setTimeout(() => {
240
248
  scrollerRef.current
@@ -258,6 +266,7 @@ const _VirtualList = <T extends KeyValue>({
258
266
  className={classNames(
259
267
  "area-content bg-base overflow-auto contain-strict",
260
268
  )}
269
+ {...aria}
261
270
  >
262
271
  <div
263
272
  className={classNames(
@@ -275,12 +284,16 @@ const _VirtualList = <T extends KeyValue>({
275
284
  padding === "md" && "p-2",
276
285
  padding === "lg" && "p-4",
277
286
  )}
287
+ style={{
288
+ minWidth,
289
+ maxWidth,
290
+ }}
278
291
  >
279
292
  <div
280
293
  style={
281
294
  orientation === "vertical"
282
- ? { height: virtualizer.getTotalSize() }
283
- : { width: virtualizer.getTotalSize() }
295
+ ? { height: virtualizer.getTotalSize() + extraSize }
296
+ : { width: virtualizer.getTotalSize() + extraSize }
284
297
  }
285
298
  >
286
299
  <div
@@ -304,9 +317,9 @@ const _VirtualList = <T extends KeyValue>({
304
317
  </div>
305
318
  ))}
306
319
  </div>
307
- {onLoadMore && (
308
- <div className={orientation === "horizontal" ? "min-w-16" : "h-16"}>
309
- {loading && <Skeleton />}
320
+ {loading && (
321
+ <div className={orientation === "horizontal" ? "min-w-16" : ""}>
322
+ <Skeleton />
310
323
  </div>
311
324
  )}
312
325
  </div>