@react-fabric/data 1.0.0-beta.0 → 1.0.0-beta.11

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 +14 -5
  3. package/dist/cjs/hooks/useSelectableList.js +54 -0
  4. package/dist/cjs/index.js +2 -0
  5. package/dist/cjs/table/AddColumn.js +56 -0
  6. package/dist/cjs/table/BodyCell.js +47 -6
  7. package/dist/cjs/table/BodyRow.js +107 -0
  8. package/dist/cjs/table/CheckboxCell.js +44 -0
  9. package/dist/cjs/table/Context.js +43 -0
  10. package/dist/cjs/table/EmptyCell.js +52 -0
  11. package/dist/cjs/table/HeaderCell.js +78 -32
  12. package/dist/cjs/table/Table.js +173 -96
  13. package/dist/cjs/table/types.js +32 -0
  14. package/dist/cjs/table/useResizer.js +65 -0
  15. package/dist/cjs/table/useTableColumns.js +45 -8
  16. package/dist/cjs/table/useVirtualTable.js +56 -25
  17. package/dist/cjs/tree/TreePanel.js +14 -2
  18. package/dist/cjs/tree/useTree.js +7 -0
  19. package/dist/cjs/virtual/Gallery.js +11 -4
  20. package/dist/cjs/virtual/List.js +11 -4
  21. package/dist/esm/checklist/CheckList.js +143 -0
  22. package/dist/esm/histogram/Histogram.js +14 -5
  23. package/dist/esm/hooks/useSelectableList.js +54 -0
  24. package/dist/esm/index.js +1 -0
  25. package/dist/esm/table/AddColumn.js +56 -0
  26. package/dist/esm/table/BodyCell.js +47 -6
  27. package/dist/esm/table/BodyRow.js +107 -0
  28. package/dist/esm/table/CheckboxCell.js +44 -0
  29. package/dist/esm/table/Context.js +42 -0
  30. package/dist/esm/table/EmptyCell.js +52 -0
  31. package/dist/esm/table/HeaderCell.js +80 -34
  32. package/dist/esm/table/Table.js +174 -97
  33. package/dist/esm/table/types.js +30 -0
  34. package/dist/esm/table/useResizer.js +65 -0
  35. package/dist/esm/table/useTableColumns.js +46 -9
  36. package/dist/esm/table/useVirtualTable.js +57 -26
  37. package/dist/esm/tree/TreePanel.js +15 -3
  38. package/dist/esm/tree/useTree.js +7 -0
  39. package/dist/esm/virtual/Gallery.js +11 -4
  40. package/dist/esm/virtual/List.js +11 -4
  41. package/dist/types/checklist/CheckList.d.ts +30 -0
  42. package/dist/types/histogram/Histogram.d.ts +3 -3
  43. package/dist/types/hooks/useSelectableList.d.ts +28 -0
  44. package/dist/types/index.d.ts +3 -0
  45. package/dist/types/table/AddColumn.d.ts +5 -0
  46. package/dist/types/table/BodyCell.d.ts +4 -3
  47. package/dist/types/table/BodyRow.d.ts +20 -0
  48. package/dist/types/table/CheckboxCell.d.ts +4 -0
  49. package/dist/types/table/Context.d.ts +8 -0
  50. package/dist/types/table/EmptyCell.d.ts +2 -0
  51. package/dist/types/table/HeaderCell.d.ts +2 -2
  52. package/dist/types/table/Table.d.ts +1 -1
  53. package/dist/types/table/types.d.ts +41 -7
  54. package/dist/types/table/useResizer.d.ts +1 -0
  55. package/dist/types/table/useTableColumns.d.ts +8 -6
  56. package/dist/types/table/useVirtualTable.d.ts +13 -1
  57. package/dist/types/tree/TreePanel.d.ts +1 -1
  58. package/dist/types/tree/types.d.ts +6 -2
  59. package/dist/types/tree/useTree.d.ts +1 -0
  60. package/dist/types/virtual/Gallery.d.ts +3 -2
  61. package/dist/types/virtual/List.d.ts +3 -2
  62. package/package.json +6 -5
  63. package/src/checklist/CheckList.tsx +222 -0
  64. package/src/histogram/Histogram.tsx +7 -2
  65. package/src/hooks/useSelectableList.ts +94 -0
  66. package/src/index.ts +3 -0
  67. package/src/table/AddColumn.tsx +55 -0
  68. package/src/table/BodyCell.tsx +47 -6
  69. package/src/table/BodyRow.tsx +142 -0
  70. package/src/table/CheckboxCell.tsx +48 -0
  71. package/src/table/Context.tsx +70 -0
  72. package/src/table/EmptyCell.tsx +57 -0
  73. package/src/table/HeaderCell.tsx +93 -25
  74. package/src/table/Table.tsx +194 -87
  75. package/src/table/types.ts +42 -6
  76. package/src/table/useResizer.ts +66 -0
  77. package/src/table/useTableColumns.ts +52 -19
  78. package/src/table/useVirtualTable.ts +61 -2
  79. package/src/tree/TreePanel.tsx +12 -1
  80. package/src/tree/types.ts +8 -1
  81. package/src/tree/useTree.ts +5 -0
  82. package/src/virtual/Gallery.tsx +4 -1
  83. package/src/virtual/List.tsx +4 -1
@@ -0,0 +1,66 @@
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
+ export const useResizer = (
25
+ colEl: HTMLElement,
26
+ ghostEl: HTMLElement,
27
+ callback: (width: number) => void,
28
+ ) => {
29
+ const placeholder = ghostEl.firstElementChild as HTMLElement;
30
+ const isRtl = getComputedStyle(colEl).direction === "rtl";
31
+
32
+ const onResize = (evt: MouseEvent) => {
33
+ /** ***************** check if reverse enabled of RTL *******************/
34
+ const box = colEl.getBoundingClientRect();
35
+ const x = isRtl ? box.left - evt.clientX : evt.clientX - box.right;
36
+ placeholder.style.width = `${colEl.offsetWidth + x}px`;
37
+ };
38
+
39
+ /** ***************** dettach handlers on mouseup *******************/
40
+ const onResizeEnd = () => {
41
+ callback(placeholder.offsetWidth);
42
+ ghostEl.style.display = "none";
43
+ document.removeEventListener("mousemove", onResize);
44
+ document.removeEventListener("mouseup", onResizeEnd);
45
+ };
46
+
47
+ /** ***************** attach handlers *******************/
48
+ const box = colEl.getBoundingClientRect();
49
+ if (colEl.parentElement != null) {
50
+ const parentBox = colEl.parentElement?.getBoundingClientRect();
51
+ if (isRtl) {
52
+ placeholder.style.left = "unset";
53
+ placeholder.style.right = `${box.right - parentBox.right}px`;
54
+ } else {
55
+ placeholder.style.right = "unset";
56
+ placeholder.style.left = `${box.left - parentBox.left}px`;
57
+ }
58
+ }
59
+ placeholder.style.width = `${colEl.offsetWidth}px`;
60
+ placeholder.style.minWidth = colEl.style.minWidth || "48px";
61
+ placeholder.style.maxWidth = colEl.style.maxWidth || "75vw";
62
+ ghostEl.style.display = "block";
63
+
64
+ document.addEventListener("mousemove", onResize);
65
+ document.addEventListener("mouseup", onResizeEnd);
66
+ };
@@ -22,31 +22,64 @@
22
22
  */
23
23
 
24
24
  import { groupBy } from "@react-fabric/utilities";
25
- import { useEffect, useState } from "react";
26
- import { type ColumnType } from "./types";
25
+ import { useEffect, useReducer } from "react";
26
+ import { type TableColumn } from "./types";
27
27
 
28
- export const useTableColumns = (columns: ColumnType[]) => {
29
- const [columnMap, setColumnMap] = useState<{
30
- start: ColumnType[];
31
- end: ColumnType[];
32
- cols: ColumnType[];
33
- }>({
34
- start: [],
35
- end: [],
36
- cols: [],
37
- });
28
+ interface ColumnState {
29
+ columns: TableColumn[];
30
+ start: TableColumn[];
31
+ end: TableColumn[];
32
+ cols: TableColumn[];
33
+ }
38
34
 
39
- useEffect(() => {
40
- setColumnMap(
41
- groupBy(
42
- columns.filter((col) => !(col.hideable && col.hidden)),
35
+ type ColumnActions =
36
+ | { type: "columns"; columns: TableColumn[] }
37
+ | { type: "hide"; id: string };
38
+
39
+ export const useTableColumns = (columns: TableColumn[]) => {
40
+ const [state, dispatch] = useReducer(
41
+ (state: ColumnState, action: ColumnActions) => {
42
+ if (action.type === "columns") {
43
+ state.columns = action.columns;
44
+ }
45
+ if (action.type === "hide") {
46
+ const col = state.columns.find((col) => col.id === action.id);
47
+ col &&
48
+ state.columns.splice(state.columns.indexOf(col), 1, {
49
+ ...col,
50
+ hidden: col.hidden !== true,
51
+ });
52
+ }
53
+ const {
54
+ start = [],
55
+ end = [],
56
+ cols = [],
57
+ } = groupBy(
58
+ state.columns.filter((col) => col.hidden !== true),
43
59
  "locked",
44
60
  "cols",
45
- ) as AnyObject,
46
- );
61
+ );
62
+ return {
63
+ ...state,
64
+ start,
65
+ end,
66
+ cols,
67
+ };
68
+ },
69
+ {
70
+ columns: [],
71
+ start: [],
72
+ end: [],
73
+ cols: [],
74
+ },
75
+ );
76
+
77
+ useEffect(() => {
78
+ dispatch({ type: "columns", columns });
47
79
  }, [columns]);
48
80
 
49
81
  return {
50
- columnMap,
82
+ state,
83
+ onHide: (id: string) => dispatch({ type: "hide", id }),
51
84
  };
52
85
  };
@@ -22,9 +22,24 @@
22
22
  */
23
23
 
24
24
  import { useVirtualizer } from "@tanstack/react-virtual";
25
- import { useCallback, useRef } from "react";
25
+ import { useCallback, useReducer, useRef } from "react";
26
26
 
27
- export const useVirtualTable = (data: KeyValue[]) => {
27
+ interface DataState {
28
+ expanded: Key[];
29
+ checked: Key[];
30
+ allChecked: 0 | 1 | 2;
31
+ }
32
+
33
+ type DataAction =
34
+ | { type: "expand"; key: Key }
35
+ | { type: "check"; key: Key }
36
+ | { type: "checkall" };
37
+
38
+ export const useVirtualTable = (
39
+ data: KeyValue[],
40
+ keyProperty?: Key,
41
+ onCheckChanged?: (checked: Key[]) => void,
42
+ ) => {
28
43
  // TODO: refactor data into item list, apply grouping if groupColumn provided
29
44
  // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
30
45
 
@@ -35,6 +50,44 @@ export const useVirtualTable = (data: KeyValue[]) => {
35
50
  estimateSize: () => 24,
36
51
  });
37
52
 
53
+ const [state, dispatch] = useReducer(
54
+ (state: DataState, action: DataAction) => {
55
+ if (action.type === "expand") {
56
+ if (state.expanded.includes(action.key))
57
+ state.expanded.splice(state.expanded.indexOf(action.key), 1);
58
+ else state.expanded.push(action.key);
59
+ }
60
+ if (action.type === "check") {
61
+ if (state.checked.includes(action.key))
62
+ state.checked.splice(state.checked.indexOf(action.key), 1);
63
+ else state.checked.push(action.key);
64
+ onCheckChanged?.(state.checked);
65
+ }
66
+ if (action.type === "checkall") {
67
+ if (state.allChecked !== 0) {
68
+ state.checked = [];
69
+ } else if (keyProperty) {
70
+ state.checked = data.map((item) => item[keyProperty]);
71
+ }
72
+ onCheckChanged?.(state.checked);
73
+ }
74
+ return {
75
+ ...state,
76
+ allChecked:
77
+ state.checked.length === data.length
78
+ ? 1
79
+ : state.checked.length
80
+ ? 2
81
+ : 0,
82
+ } as DataState;
83
+ },
84
+ {
85
+ checked: [],
86
+ expanded: [],
87
+ allChecked: 0,
88
+ },
89
+ );
90
+
38
91
  const virtualItems = virtualizer.getVirtualItems();
39
92
 
40
93
  const getData = useCallback(
@@ -47,8 +100,14 @@ export const useVirtualTable = (data: KeyValue[]) => {
47
100
  return {
48
101
  scrollerRef,
49
102
  items: virtualItems,
103
+ checkState: state,
50
104
  getData,
105
+ virtualizer,
106
+ top: virtualizer.range?.startIndex,
51
107
  totalSize: virtualizer.getTotalSize,
52
108
  measureElement: virtualizer.measureElement,
109
+ toggleExpand: (key: Key) => dispatch({ type: "expand", key }),
110
+ toggleChecked: (key: Key) => dispatch({ type: "check", key }),
111
+ toggleAllChecked: () => dispatch({ type: "checkall" }),
53
112
  };
54
113
  };
@@ -31,7 +31,7 @@ import {
31
31
  } from "@react-fabric/core";
32
32
  import { Search } from "@react-fabric/form";
33
33
  import { useVirtualizer } from "@tanstack/react-virtual";
34
- import { useMemo, useRef } from "react";
34
+ import { useImperativeHandle, useMemo, useRef } from "react";
35
35
  import { TreeNode } from "./TreeNode";
36
36
  import { iconCollapseAll, iconExpandAll, type TreePanelProps } from "./types";
37
37
  import { useTree } from "./useTree";
@@ -39,6 +39,7 @@ import { useTree } from "./useTree";
39
39
  // TODO: implement keyboard handler for navigating tree
40
40
 
41
41
  export const TreePanel = <T extends KeyValue>({
42
+ ref,
42
43
  items,
43
44
  selected,
44
45
  checked,
@@ -64,6 +65,7 @@ export const TreePanel = <T extends KeyValue>({
64
65
  expandAll,
65
66
  collapseAll,
66
67
  select,
68
+ expand,
67
69
  onFilter,
68
70
  } = useTree({
69
71
  items,
@@ -93,6 +95,15 @@ export const TreePanel = <T extends KeyValue>({
93
95
  return [CoreIcons.expand, CoreIcons.collapse];
94
96
  }, [expander]);
95
97
 
98
+ useImperativeHandle(
99
+ ref,
100
+ () => ({
101
+ open: (id: string) => expand(id),
102
+ select: (id: string) => expand(id),
103
+ }),
104
+ [],
105
+ );
106
+
96
107
  return (
97
108
  <Section>
98
109
  <Header
package/src/tree/types.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  type CssProp,
28
28
  type Elements,
29
29
  type IconProps,
30
+ type RefProp,
30
31
  type TestProps,
31
32
  } from "@react-fabric/core/dist/types/types";
32
33
 
@@ -105,8 +106,14 @@ export interface TreeNodeProps extends ChildProp {
105
106
  onChecked: (id: string) => void;
106
107
  }
107
108
 
109
+ export interface TreeRef {
110
+ select: (id: string) => void;
111
+ open: (id: string) => void;
112
+ }
113
+
108
114
  export interface TreePanelProps<T extends KeyValue = KeyValue>
109
- extends TestProps {
115
+ extends TestProps,
116
+ RefProp<TreeRef> {
110
117
  items?: Array<TreeNodeType<T>>;
111
118
  /**
112
119
  * make tree searchable
@@ -203,6 +203,10 @@ export const useTree = <T extends KeyValue>({
203
203
  dispatch({ type: "select", id });
204
204
  }, []);
205
205
 
206
+ const expand = useCallback((id: string) => {
207
+ dispatch({ type: "expand", id });
208
+ }, []);
209
+
206
210
  const expandAll = useCallback(() => {
207
211
  dispatch({ type: "expandAll" });
208
212
  }, []);
@@ -222,6 +226,7 @@ export const useTree = <T extends KeyValue>({
222
226
  collapseAll,
223
227
  toggleCheck,
224
228
  select,
229
+ expand,
225
230
  onFilter,
226
231
  };
227
232
  };
@@ -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,7 +48,7 @@ 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
53
  data: T;
53
54
  index: number;
@@ -108,6 +109,7 @@ const _VirtualGallery = <T extends KeyValue>({
108
109
  onLoadMore,
109
110
  onScroll,
110
111
  galleryRef,
112
+ ...aria
111
113
  }: VirtualGalleryProps<T>) => {
112
114
  const count = useDeferredValue(items.length);
113
115
  const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
@@ -275,6 +277,7 @@ const _VirtualGallery = <T extends KeyValue>({
275
277
  className={classNames(
276
278
  "area-content bg-base overflow-auto contain-strict",
277
279
  )}
280
+ {...aria}
278
281
  >
279
282
  <div
280
283
  className={classNames(
@@ -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,7 +55,7 @@ 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
60
  data: T;
60
61
  index: number;
@@ -120,6 +121,7 @@ const _VirtualList = <T extends KeyValue>({
120
121
  onLoadMore,
121
122
  onScroll,
122
123
  listRef,
124
+ ...aria
123
125
  }: VirtualListProps<T>) => {
124
126
  const count = useDeferredValue(items.length);
125
127
  const extraSize = useMemo(() => (onLoadMore ? 64 : 0), [onLoadMore]);
@@ -258,6 +260,7 @@ const _VirtualList = <T extends KeyValue>({
258
260
  className={classNames(
259
261
  "area-content bg-base overflow-auto contain-strict",
260
262
  )}
263
+ {...aria}
261
264
  >
262
265
  <div
263
266
  className={classNames(