@react-fabric/data 1.0.0-beta.8 → 1.0.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 (141) hide show
  1. package/{src/histogram/Histogram.module.css → css/_histogram.css} +16 -13
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/checklist/CheckList.js +59 -33
  4. package/dist/cjs/histogram/Histogram.js +41 -26
  5. package/dist/cjs/hooks/useFilteredList.js +100 -17
  6. package/dist/cjs/hooks/usePagination.js +29 -8
  7. package/dist/cjs/hooks/useSelectableList.js +30 -5
  8. package/dist/cjs/i18n/{ar.json.js → ar/data.json.js} +8 -1
  9. package/dist/cjs/i18n/{en.json.js → en/data.json.js} +8 -1
  10. package/dist/cjs/i18n/index.js +4 -4
  11. package/dist/cjs/index.js +30 -0
  12. package/dist/cjs/json/JsonViewer.js +80 -43
  13. package/dist/cjs/pagination/Pagination.js +24 -15
  14. package/dist/cjs/table/AddColumn.js +5 -5
  15. package/dist/cjs/table/BodyCell.js +20 -13
  16. package/dist/cjs/table/BodyRow.js +41 -28
  17. package/dist/cjs/table/CheckboxCell.js +10 -9
  18. package/dist/cjs/table/ColumnFilters.js +100 -0
  19. package/dist/cjs/table/EmptyCell.js +4 -4
  20. package/dist/cjs/table/HeaderCell.js +79 -53
  21. package/dist/cjs/table/HtmlTable.js +84 -0
  22. package/dist/cjs/table/{Context.js → Provider.js} +8 -10
  23. package/dist/cjs/table/Table.js +99 -33
  24. package/dist/cjs/{tree/types.js → table/context.js} +9 -5
  25. package/dist/cjs/table/types.js +29 -0
  26. package/dist/cjs/table/useResizer.js +10 -10
  27. package/dist/cjs/table/useTableColumns.js +4 -4
  28. package/dist/cjs/table/useVirtualTable.js +129 -22
  29. package/dist/cjs/tree/TreeNode.js +66 -46
  30. package/dist/cjs/tree/TreePanel.js +60 -24
  31. package/dist/cjs/tree/useTree.js +212 -47
  32. package/dist/cjs/tree/utils.js +63 -34
  33. package/dist/cjs/utils/getColor.js +58 -0
  34. package/dist/cjs/virtual/Gallery.js +119 -61
  35. package/dist/cjs/virtual/List.js +116 -55
  36. package/dist/cjs/virtual/Scroller.js +16 -8
  37. package/dist/esm/checklist/CheckList.js +61 -35
  38. package/dist/esm/histogram/Histogram.js +43 -28
  39. package/dist/esm/hooks/useFilteredList.js +101 -18
  40. package/dist/esm/hooks/usePagination.js +29 -8
  41. package/dist/esm/hooks/useSelectableList.js +30 -5
  42. package/dist/esm/i18n/{ar.json.js → ar/data.json.js} +8 -2
  43. package/dist/esm/i18n/{en.json.js → en/data.json.js} +8 -2
  44. package/dist/esm/i18n/index.js +2 -2
  45. package/dist/esm/index.js +31 -0
  46. package/dist/esm/json/JsonViewer.js +82 -45
  47. package/dist/esm/pagination/Pagination.js +25 -16
  48. package/dist/esm/table/AddColumn.js +6 -6
  49. package/dist/esm/table/BodyCell.js +21 -14
  50. package/dist/esm/table/BodyRow.js +41 -28
  51. package/dist/esm/table/CheckboxCell.js +11 -10
  52. package/dist/esm/table/ColumnFilters.js +100 -0
  53. package/dist/esm/table/EmptyCell.js +4 -4
  54. package/dist/esm/table/HeaderCell.js +80 -54
  55. package/dist/esm/table/HtmlTable.js +84 -0
  56. package/dist/esm/table/{Context.js → Provider.js} +11 -12
  57. package/dist/esm/table/Table.js +100 -34
  58. package/dist/esm/{tree/types.js → table/context.js} +8 -4
  59. package/dist/esm/table/types.js +29 -0
  60. package/dist/esm/table/useResizer.js +10 -10
  61. package/dist/esm/table/useTableColumns.js +4 -4
  62. package/dist/esm/table/useVirtualTable.js +131 -24
  63. package/dist/esm/tree/TreeNode.js +69 -49
  64. package/dist/esm/tree/TreePanel.js +62 -26
  65. package/dist/esm/tree/useTree.js +215 -50
  66. package/dist/esm/tree/utils.js +64 -35
  67. package/dist/esm/utils/getColor.js +58 -0
  68. package/dist/esm/virtual/Gallery.js +121 -63
  69. package/dist/esm/virtual/List.js +118 -57
  70. package/dist/esm/virtual/Scroller.js +17 -9
  71. package/dist/types/checklist/CheckList.d.ts +25 -6
  72. package/dist/types/histogram/Histogram.d.ts +28 -6
  73. package/dist/types/hooks/useFilteredList.d.ts +62 -5
  74. package/dist/types/hooks/usePagination.d.ts +18 -2
  75. package/dist/types/hooks/useSelectableList.d.ts +24 -1
  76. package/dist/types/index.d.ts +27 -0
  77. package/dist/types/json/JsonViewer.d.ts +23 -3
  78. package/dist/types/pagination/Pagination.d.ts +8 -2
  79. package/dist/types/table/AddColumn.d.ts +2 -2
  80. package/dist/types/table/BodyCell.d.ts +2 -2
  81. package/dist/types/table/BodyRow.d.ts +9 -9
  82. package/dist/types/table/CheckboxCell.d.ts +3 -3
  83. package/dist/types/table/ColumnFilters.d.ts +12 -0
  84. package/dist/types/table/EmptyCell.d.ts +1 -1
  85. package/dist/types/table/HeaderCell.d.ts +1 -1
  86. package/dist/types/table/HtmlTable.d.ts +21 -0
  87. package/dist/types/table/Provider.d.ts +2 -0
  88. package/dist/types/table/Table.d.ts +10 -1
  89. package/dist/types/table/context.d.ts +7 -0
  90. package/dist/types/table/types.d.ts +52 -10
  91. package/dist/types/table/useResizer.d.ts +1 -1
  92. package/dist/types/table/useTableColumns.d.ts +1 -1
  93. package/dist/types/table/useVirtualTable.d.ts +31 -12
  94. package/dist/types/tree/TreeNode.d.ts +1 -1
  95. package/dist/types/tree/TreePanel.d.ts +36 -1
  96. package/dist/types/tree/types.d.ts +75 -28
  97. package/dist/types/tree/useTree.d.ts +8 -12
  98. package/dist/types/tree/utils.d.ts +15 -8
  99. package/dist/types/utils/getColor.d.ts +43 -0
  100. package/dist/types/virtual/Gallery.d.ts +37 -6
  101. package/dist/types/virtual/List.d.ts +38 -6
  102. package/dist/types/virtual/Scroller.d.ts +1 -1
  103. package/package.json +17 -19
  104. package/src/checklist/CheckList.tsx +68 -71
  105. package/src/histogram/Histogram.tsx +47 -48
  106. package/src/hooks/useFilteredList.tsx +111 -28
  107. package/src/hooks/usePagination.tsx +39 -39
  108. package/src/hooks/useSelectableList.ts +45 -33
  109. package/src/i18n/{ar.json → ar/data.json} +5 -0
  110. package/src/i18n/{en.json → en/data.json} +5 -0
  111. package/src/i18n/index.ts +2 -2
  112. package/src/index.ts +23 -17
  113. package/src/json/JsonViewer.tsx +89 -120
  114. package/src/pagination/Pagination.tsx +104 -105
  115. package/src/table/AddColumn.tsx +6 -15
  116. package/src/table/BodyCell.tsx +27 -36
  117. package/src/table/BodyRow.tsx +36 -59
  118. package/src/table/CheckboxCell.tsx +12 -15
  119. package/src/table/ColumnFilters.tsx +93 -0
  120. package/src/table/EmptyCell.tsx +5 -18
  121. package/src/table/HeaderCell.tsx +72 -53
  122. package/src/table/HtmlTable.tsx +106 -0
  123. package/src/table/{Context.tsx → Provider.tsx} +11 -28
  124. package/src/table/Table.tsx +98 -83
  125. package/src/table/context.ts +33 -0
  126. package/src/table/types.ts +53 -27
  127. package/src/table/useResizer.ts +5 -9
  128. package/src/table/useTableColumns.ts +4 -6
  129. package/src/table/useVirtualTable.ts +136 -37
  130. package/src/tree/TreeNode.tsx +91 -86
  131. package/src/tree/TreePanel.tsx +87 -51
  132. package/src/tree/types.ts +85 -46
  133. package/src/tree/useTree.ts +210 -65
  134. package/src/tree/utils.ts +81 -62
  135. package/src/utils/getColor.ts +55 -0
  136. package/src/virtual/Gallery.tsx +129 -120
  137. package/src/virtual/List.tsx +124 -91
  138. package/src/virtual/Scroller.tsx +25 -9
  139. package/dist/cjs/histogram/Histogram.module.css +0 -1
  140. package/dist/esm/histogram/Histogram.module.css +0 -1
  141. package/dist/types/table/Context.d.ts +0 -8
package/src/tree/utils.ts CHANGED
@@ -21,40 +21,44 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- import { compareValues, isString, matchString } from "@react-fabric/utilities";
25
- import { type InternalNode, type TreeNodeType } from "./types";
24
+ import { compareValues, isArray, isString, matchString } from "@react-fabric/utilities";
25
+ import type { InternalNode, TreeNodeType } from "./types";
26
26
 
27
- const sorter = (a: TreeNodeType, b: TreeNodeType) => {
27
+ function defaultSorter<T extends KeyValue>(a: TreeNodeType<T>, b: TreeNodeType<T>) {
28
28
  if (!!a.leaf !== !!b.leaf) return a.leaf ? 1 : -1;
29
29
  return compareValues()(a.label, b.label);
30
- };
30
+ }
31
31
 
32
- const getChildren = (list: InternalNode[] = []): InternalNode[] => {
32
+ function getChildren<T extends KeyValue>(list: InternalNode<T>[] = []): InternalNode<T>[] {
33
33
  return list
34
- .map<InternalNode[]>((item) => [item, ...getChildren(item.children)])
34
+ .map<InternalNode<T>[]>((item) => [item, ...getChildren(item.children)])
35
35
  .flat(99)
36
36
  .filter(Boolean);
37
- };
37
+ }
38
38
 
39
- const getIntermediate = (list: AnyObject[] = []) => {
39
+ function getIntermediate<T extends KeyValue>(list: InternalNode<T>[] = []) {
40
40
  const checks = list.map((item) => item.checked);
41
41
  const someOff = checks.includes(0);
42
42
  const someInt = checks.includes(2);
43
43
  const someOn = checks.includes(1);
44
44
 
45
45
  return (someOff && someOn) || someInt ? 2 : someOn ? 1 : 0;
46
- };
46
+ }
47
47
 
48
48
  // convert node item to internal node used by the tree
49
- export const refactorTree = (
50
- nodes: TreeNodeType[],
49
+ export function refactorTree<T extends KeyValue>(
50
+ nodes: TreeNodeType<T>[],
51
+ {
52
+ sorter = defaultSorter<T>,
53
+ defaultExpanded = [],
54
+ }: { sorter?: false | ((a: TreeNodeType<T>, b: TreeNodeType<T>) => number); defaultExpanded?: string[] },
51
55
  options: { level: number; parent?: string } = { level: 0, parent: undefined },
52
- ) => {
53
- const list: InternalNode[] = [];
54
-
55
- nodes.sort(sorter).forEach((node) => {
56
+ ) {
57
+ const list: InternalNode<T>[] = [];
58
+ if (sorter !== false) nodes = nodes.sort(sorter);
59
+ nodes.forEach((node) => {
56
60
  list.push({
57
- open: false,
61
+ open: defaultExpanded.includes(node.id) && !!node.children?.length,
58
62
  level: options.level,
59
63
  parent: options.parent,
60
64
  checked: 0,
@@ -66,93 +70,106 @@ export const refactorTree = (
66
70
  leaf: !!node.leaf,
67
71
  children:
68
72
  node.children &&
69
- refactorTree(node.children, {
70
- level: options.level + 1,
71
- parent: node.id,
72
- }),
73
+ refactorTree<T>(
74
+ node.children,
75
+ { sorter, defaultExpanded },
76
+ {
77
+ level: options.level + 1,
78
+ parent: node.id,
79
+ },
80
+ ),
73
81
  });
74
82
  });
75
83
 
76
84
  return list;
77
- };
85
+ }
78
86
 
79
- export const makeTreeMap = (nodes: InternalNode[], map = new Map()) => {
87
+ export function makeTreeMap<T extends KeyValue>(nodes: InternalNode<T>[], map = new Map<string, InternalNode<T>>()) {
80
88
  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
- );
89
+ if (map.has(node.id)) throw new Error(`Node with id [${node.id}] already exists, please make sure all ids are unique`);
85
90
  map.set(node.id, node);
86
- node.children && makeTreeMap(node.children, map);
91
+ if (node.children) makeTreeMap<T>(node.children, map);
87
92
  });
88
93
  return map;
89
- };
94
+ }
90
95
 
91
96
  // 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
+ export function flattenTree<T extends KeyValue>(nodes: InternalNode<T>[], lines: Array<0 | 1 | 2 | 3> = []) {
98
+ const list: InternalNode<T>[] = [];
97
99
  const filtered = nodes.filter((node) => !node.filtered);
98
100
  filtered.forEach((node, idx) => {
99
101
  node.lines = [...lines, idx === filtered.length - 1 ? 2 : 3];
100
102
  list.push(node);
101
- (node.open || node.childFiltered) &&
102
- node.children &&
103
+ if ((node.open || node.childFiltered) && node.children)
103
104
  list.push(
104
105
  ...flattenTree(
105
106
  node.children.filter((child) => !child.filtered),
106
107
  [...lines, idx === filtered.length - 1 ? 0 : 1],
107
108
  ),
108
109
  );
110
+ if (node.open && node.children?.length === 0)
111
+ list.push({
112
+ empty: true,
113
+ leaf: true,
114
+ lines: [...lines, idx === filtered.length - 1 ? 0 : 1, 2],
115
+ } as InternalNode<T>);
109
116
  });
110
117
  return list;
111
- };
118
+ }
112
119
 
113
- export const updateSelection = (
114
- nodes: Map<string, InternalNode>,
115
- id: string,
116
- selected?: true,
117
- ) => {
118
- const node = nodes.get(id);
120
+ function selectNode<T extends KeyValue>(nodes: Map<string, InternalNode<T>>, id?: string) {
121
+ const node = nodes.get(id ?? "");
119
122
  if (node) {
120
123
  // change selection
121
- node.selected = selected;
124
+ node.selected = true;
122
125
  // update all parent childSelected flag
123
126
  let parent = node.parent;
124
127
  while (parent) {
125
128
  const parentNode = nodes.get(parent);
126
- parentNode && (parentNode.childSelected = selected);
127
- selected && parentNode && (parentNode.open = true);
129
+ if (parentNode) parentNode.childSelected = true;
130
+ // selected && parentNode && (parentNode.open = true);
128
131
  parent = parentNode?.parent;
129
132
  }
130
133
  }
131
- };
134
+ }
132
135
 
133
- export const updateChecked = (list: Map<string, AnyObject>, id: string) => {
136
+ export function updateSelection<T extends KeyValue>({
137
+ nodes,
138
+ multiple,
139
+ selected,
140
+ }: {
141
+ nodes: Map<string, InternalNode<T>>;
142
+ multiple?: boolean;
143
+ selected?: string | string[];
144
+ }) {
145
+ nodes.forEach((node) => {
146
+ node.selected = undefined;
147
+ node.childSelected = undefined;
148
+ });
149
+ if (multiple && isArray(selected)) selected?.forEach((id) => selectNode(nodes, id));
150
+ else selectNode(nodes, selected as string);
151
+ }
152
+
153
+ export function updateChecked<T extends KeyValue>(list: Map<string, InternalNode<T>>, id: string, checked?: 0 | 1 | 2) {
134
154
  const node = list.get(id);
135
155
  if (node) {
136
- node.checked = node.checked === 1 ? 0 : 1;
156
+ node.checked = (checked ?? node.checked === 0) ? 1 : 0;
137
157
  // check all children
138
- node.children &&
139
- getChildren(node.children).forEach(
140
- (child) => (child.checked = node.checked),
141
- );
158
+ if (node.children) getChildren(node.children).forEach((child) => (child.checked = node.checked));
142
159
  let parent = node.parent;
143
160
  while (parent) {
144
161
  const parentNode = list.get(parent);
145
- parentNode && (parentNode.checked = getIntermediate(parentNode.children));
162
+ if (parentNode) parentNode.checked = getIntermediate(parentNode.children);
146
163
  parent = parentNode?.parent;
147
164
  }
148
165
  }
149
- };
166
+ }
150
167
 
151
- export const filterTree = (
152
- nodes: Map<string, InternalNode>,
168
+ export function filterTree<T extends KeyValue>(
169
+ nodes: Map<string, InternalNode<T>>,
153
170
  query?: string,
154
- matcher?: AnyObject,
155
- ) => {
171
+ matcher?: (data: T, query: string) => boolean,
172
+ ) {
156
173
  nodes.forEach((node) => {
157
174
  if (query) {
158
175
  if (node.parentFiltered) return;
@@ -166,11 +183,13 @@ export const filterTree = (
166
183
  let parent = node.parent;
167
184
  while (parent) {
168
185
  const parentNode = nodes.get(parent);
169
- parentNode && (parentNode.filtered = false);
170
- parentNode && (parentNode.childFiltered = true);
186
+ if (parentNode) {
187
+ parentNode.filtered = false;
188
+ parentNode.childFiltered = true;
189
+ }
171
190
  parent = parentNode?.parent;
172
191
  }
173
- node.children &&
192
+ if (node.children)
174
193
  getChildren(node.children).forEach((child) => {
175
194
  child.parentFiltered = true;
176
195
  child.filtered = false;
@@ -182,4 +201,4 @@ export const filterTree = (
182
201
  node.parentFiltered = undefined;
183
202
  }
184
203
  });
185
- };
204
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ /* istanbul ignore file */
27
+
28
+ import { isColor } from "@react-fabric/utilities";
29
+
30
+ /**
31
+ * Resolves a color value to a CSS color string.
32
+ *
33
+ * If the input is a valid CSS color (e.g., `"#ff0000"`, `"rgb(255,0,0)"`), it is returned as-is.
34
+ * Otherwise, it is treated as a design token name and resolved to a CSS custom property
35
+ * (e.g., `"primary-500"` → `"var(--color-primary-500)"`).
36
+ *
37
+ * @param {ColorUnion} color - The color value to resolve. Accepts CSS color strings or design token names.
38
+ * @returns The resolved CSS color string, or `undefined` if color is falsy.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * getColor("primary-500"); // "var(--color-primary-500)"
43
+ * getColor("#ff0000"); // "#ff0000"
44
+ * getColor("red"); // "var(--color-red)"
45
+ * getColor(undefined); // undefined
46
+ * ```
47
+ */
48
+ export function getColor(color?: string) {
49
+ if (!color) return undefined;
50
+ if (color && isColor(color)) {
51
+ return color;
52
+ }
53
+ if (color.startsWith("--")) return `var(${color})`;
54
+ return `var(--color-${color})`;
55
+ }