@react-fabric/data 1.0.0-beta.4 → 1.0.0-beta.40

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 (81) 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 +25 -16
  4. package/dist/cjs/histogram/Histogram.js +20 -10
  5. package/dist/cjs/hooks/usePagination.js +7 -4
  6. package/dist/cjs/json/JsonViewer.js +40 -27
  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 +105 -67
  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 +27 -18
  21. package/dist/esm/histogram/Histogram.js +21 -11
  22. package/dist/esm/hooks/usePagination.js +7 -4
  23. package/dist/esm/json/JsonViewer.js +41 -28
  24. package/dist/esm/table/BodyCell.js +38 -5
  25. package/dist/esm/table/BodyRow.js +107 -0
  26. package/dist/esm/table/CheckboxCell.js +1 -1
  27. package/dist/esm/table/HeaderCell.js +30 -37
  28. package/dist/esm/table/Table.js +107 -69
  29. package/dist/esm/table/useResizer.js +4 -4
  30. package/dist/esm/table/useVirtualTable.js +120 -19
  31. package/dist/esm/tree/TreeNode.js +46 -26
  32. package/dist/esm/tree/TreePanel.js +28 -10
  33. package/dist/esm/tree/useTree.js +98 -16
  34. package/dist/esm/tree/utils.js +20 -5
  35. package/dist/esm/virtual/Gallery.js +43 -29
  36. package/dist/esm/virtual/List.js +43 -31
  37. package/dist/types/checklist/CheckList.d.ts +2 -2
  38. package/dist/types/histogram/Histogram.d.ts +3 -3
  39. package/dist/types/index.d.ts +1 -1
  40. package/dist/types/table/AddColumn.d.ts +2 -2
  41. package/dist/types/table/BodyCell.d.ts +4 -3
  42. package/dist/types/table/BodyRow.d.ts +20 -0
  43. package/dist/types/table/EmptyCell.d.ts +2 -2
  44. package/dist/types/table/HeaderCell.d.ts +2 -2
  45. package/dist/types/table/Table.d.ts +1 -1
  46. package/dist/types/table/types.d.ts +17 -6
  47. package/dist/types/table/useTableColumns.d.ts +6 -6
  48. package/dist/types/table/useVirtualTable.d.ts +20 -5
  49. package/dist/types/tree/TreeNode.d.ts +1 -1
  50. package/dist/types/tree/TreePanel.d.ts +1 -1
  51. package/dist/types/tree/types.d.ts +25 -5
  52. package/dist/types/tree/useTree.d.ts +2 -9
  53. package/dist/types/tree/utils.d.ts +4 -1
  54. package/dist/types/virtual/Gallery.d.ts +12 -4
  55. package/dist/types/virtual/List.d.ts +7 -3
  56. package/package.json +10 -7
  57. package/src/checklist/CheckList.tsx +81 -77
  58. package/src/histogram/Histogram.tsx +17 -11
  59. package/src/hooks/usePagination.tsx +13 -7
  60. package/src/index.ts +1 -1
  61. package/src/json/JsonViewer.tsx +45 -35
  62. package/src/table/AddColumn.tsx +2 -2
  63. package/src/table/BodyCell.tsx +34 -5
  64. package/src/table/BodyRow.tsx +143 -0
  65. package/src/table/CheckboxCell.tsx +1 -1
  66. package/src/table/EmptyCell.tsx +2 -2
  67. package/src/table/HeaderCell.tsx +27 -13
  68. package/src/table/Table.tsx +141 -73
  69. package/src/table/types.ts +20 -5
  70. package/src/table/useResizer.ts +5 -3
  71. package/src/table/useTableColumns.ts +7 -7
  72. package/src/table/useVirtualTable.ts +143 -28
  73. package/src/tree/TreeNode.tsx +90 -56
  74. package/src/tree/TreePanel.tsx +23 -3
  75. package/src/tree/types.ts +32 -4
  76. package/src/tree/useTree.ts +102 -23
  77. package/src/tree/utils.ts +23 -8
  78. package/src/virtual/Gallery.tsx +46 -35
  79. package/src/virtual/List.tsx +40 -27
  80. package/dist/cjs/histogram/Histogram.module.css +0 -1
  81. package/dist/esm/histogram/Histogram.module.css +0 -1
@@ -23,6 +23,7 @@
23
23
 
24
24
  import {
25
25
  type CssProp,
26
+ type Elements,
26
27
  type IconProps,
27
28
  type RefProp,
28
29
  type TestProps,
@@ -33,16 +34,18 @@ export const COL_MIN_WIDTH = "3rem";
33
34
  export const COL_MAX_WIDTH = "64rem";
34
35
  export const COL_DEFAULT_WIDTH = "12rem";
35
36
 
36
- export interface ColumnType<T = KeyValue>
37
+ export interface TableColumn<T = KeyValue>
37
38
  extends CssProp,
38
39
  IconProps,
39
40
  TestProps {
40
41
  id: keyof T | string;
41
42
  label?: string;
42
- actions?: ReactElement;
43
+ tooltip?: string;
44
+ actions?: ReactElement[];
43
45
  align?: "start" | "center" | "end";
44
46
  locked?: "start" | "end";
45
47
  hidden?: boolean;
48
+ truncate?: boolean;
46
49
  hideable?: boolean;
47
50
  resizable?: boolean;
48
51
  sortable?: boolean;
@@ -60,7 +63,8 @@ export interface ColumnType<T = KeyValue>
60
63
  | "time"
61
64
  | "unknown";
62
65
  format?: string;
63
- renderer?: (value: AnyObject, data: T) => void;
66
+ valueMap?: KeyValue;
67
+ renderer?: (value: AnyObject, data: T, index: number) => Elements<AnyObject>;
64
68
  }
65
69
 
66
70
  export interface TableRef {
@@ -70,14 +74,19 @@ export interface TableRef {
70
74
  }
71
75
 
72
76
  export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
73
- columns: Array<ColumnType<T>>;
77
+ columns: Array<TableColumn<T>>;
74
78
  data: T[];
75
79
  keyProperty?: keyof T;
80
+ groupProperty?: keyof T;
81
+ groupRenderer?: (key: AnyObject) => ReactElement;
82
+ emptyDisplay?: ReactElement;
83
+
76
84
  /**
77
85
  * checkable rows
78
86
  */
87
+ checked?: AnyObject[];
79
88
  checkableRows?: boolean;
80
- onCheckedChanged?: (rows: Array<keyof T>) => void;
89
+ onCheckedChanged?: (rows: AnyObject[]) => void;
81
90
  /**
82
91
  * sorting
83
92
  */
@@ -88,4 +97,10 @@ export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
88
97
  hideableColumns?: boolean;
89
98
 
90
99
  onRowClick?: (data: T) => void;
100
+
101
+ initialScroll?: number;
102
+ onScroll?: (firstItem: number) => void;
103
+
104
+ canExpand?: (record: T) => boolean;
105
+ children?: (record: T) => Elements<AnyObject>;
91
106
  }
@@ -47,13 +47,15 @@ export const useResizer = (
47
47
  /** ***************** attach handlers *******************/
48
48
  const box = colEl.getBoundingClientRect();
49
49
  if (colEl.parentElement != null) {
50
- const parentBox = colEl.parentElement?.getBoundingClientRect();
50
+ const parentBox = colEl
51
+ .closest("[data-ref='tableScroller']")
52
+ ?.getBoundingClientRect() as unknown as DOMRect;
51
53
  if (isRtl) {
52
54
  placeholder.style.left = "unset";
53
- placeholder.style.right = `${box.right - parentBox.right}px`;
55
+ placeholder.style.right = `${parentBox.width - box.right}px`;
54
56
  } else {
55
57
  placeholder.style.right = "unset";
56
- placeholder.style.left = `${box.left - parentBox.left}px`;
58
+ placeholder.style.left = `${box.left}px`;
57
59
  }
58
60
  }
59
61
  placeholder.style.width = `${colEl.offsetWidth}px`;
@@ -23,20 +23,20 @@
23
23
 
24
24
  import { groupBy } from "@react-fabric/utilities";
25
25
  import { useEffect, useReducer } from "react";
26
- import { type ColumnType } from "./types";
26
+ import { type TableColumn } from "./types";
27
27
 
28
28
  interface ColumnState {
29
- columns: ColumnType[];
30
- start: ColumnType[];
31
- end: ColumnType[];
32
- cols: ColumnType[];
29
+ columns: TableColumn[];
30
+ start: TableColumn[];
31
+ end: TableColumn[];
32
+ cols: TableColumn[];
33
33
  }
34
34
 
35
35
  type ColumnActions =
36
- | { type: "columns"; columns: ColumnType[] }
36
+ | { type: "columns"; columns: TableColumn[] }
37
37
  | { type: "hide"; id: string };
38
38
 
39
- export const useTableColumns = (columns: ColumnType[]) => {
39
+ export const useTableColumns = (columns: TableColumn[]) => {
40
40
  const [state, dispatch] = useReducer(
41
41
  (state: ColumnState, action: ColumnActions) => {
42
42
  if (action.type === "columns") {
@@ -21,78 +21,193 @@
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 { useVirtualizer } from "@tanstack/react-virtual";
25
- import { useCallback, useReducer, useRef } from "react";
24
+ import { compareValues, groupBy } from "@react-fabric/utilities";
25
+ import {
26
+ defaultRangeExtractor,
27
+ type Range,
28
+ useVirtualizer,
29
+ } from "@tanstack/react-virtual";
30
+ import {
31
+ useCallback,
32
+ useEffect,
33
+ useMemo,
34
+ useReducer,
35
+ useRef,
36
+ useState,
37
+ } from "react";
26
38
 
27
- interface DataState {
28
- checked: Key[];
39
+ interface DataState<T = KeyValue> {
40
+ items: T[];
41
+ grouped: KeyValue<T[]>;
42
+ groups: AnyObject[];
43
+ expanded: AnyObject[];
44
+ checked: AnyObject[];
29
45
  allChecked: 0 | 1 | 2;
30
46
  }
31
47
 
32
- type DataAction = { type: "check"; key: Key } | { type: "checkall" };
48
+ type DataAction =
49
+ | { type: "updateState"; checked: AnyObject[] }
50
+ | { type: "groupExpand"; key: AnyObject }
51
+ | { type: "grouped"; key: AnyObject }
52
+ | { type: "expand"; key: AnyObject }
53
+ | { type: "check"; key: AnyObject }
54
+ | { type: "checkall" };
33
55
 
34
- export const useVirtualTable = (
35
- data: KeyValue[],
36
- keyProperty?: Key,
37
- onCheckChanged?: (checked: Key[]) => void,
56
+ export const useVirtualTable = <T extends KeyValue = KeyValue>(
57
+ data: T[],
58
+ options: {
59
+ checked?: AnyObject[];
60
+ keyProperty: keyof T;
61
+ groupProperty?: keyof T;
62
+ onCheckChanged?: (checked: AnyObject[]) => void;
63
+ },
38
64
  ) => {
39
65
  // TODO: refactor data into item list, apply grouping if groupColumn provided
40
66
  // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
41
67
 
42
- const scrollerRef = useRef<HTMLDivElement>(null);
43
- const virtualizer = useVirtualizer({
44
- count: data.length,
45
- getScrollElement: () => scrollerRef.current,
46
- estimateSize: () => 24,
47
- });
48
-
49
68
  const [state, dispatch] = useReducer(
50
- (state: DataState, action: DataAction) => {
69
+ (state: DataState<T>, action: DataAction) => {
70
+ if (action.type === "updateState") {
71
+ state.checked = action.checked;
72
+ }
73
+ if (action.type === "expand") {
74
+ if (state.expanded.includes(action.key))
75
+ state.expanded.splice(state.expanded.indexOf(action.key), 1);
76
+ else state.expanded.push(action.key);
77
+ }
51
78
  if (action.type === "check") {
52
79
  if (state.checked.includes(action.key))
53
80
  state.checked.splice(state.checked.indexOf(action.key), 1);
54
81
  else state.checked.push(action.key);
82
+ options.onCheckChanged?.(state.checked);
55
83
  }
56
84
  if (action.type === "checkall") {
57
85
  if (state.allChecked !== 0) {
58
86
  state.checked = [];
59
- } else if (keyProperty) {
60
- state.checked = data.map((item) => item[keyProperty]);
87
+ } else if (options.keyProperty) {
88
+ state.checked = data.map((item) => item[options.keyProperty]);
89
+ }
90
+ options.onCheckChanged?.(state.checked);
91
+ }
92
+ const checkList = data.filter((hit) =>
93
+ state.checked.includes(hit[options.keyProperty]),
94
+ );
95
+ if (action.type === "groupExpand") {
96
+ const group = state.groups.find((group) => group.key === action.key);
97
+ if (group) {
98
+ group.open = !group.open;
99
+ state.groups = state.groups.slice();
100
+ state.items = state.groups
101
+ .map((group) => [
102
+ group,
103
+ group.open ? state.grouped[group.key] : undefined,
104
+ ])
105
+ .flat(2)
106
+ .filter(Boolean) as any;
107
+ }
108
+ }
109
+ if (action.type === "grouped") {
110
+ if (action.key) {
111
+ state.grouped = groupBy<T>(data, action.key);
112
+ state.groups = Object.keys(state.grouped)
113
+ .sort(compareValues("asc"))
114
+ .map((group) => ({
115
+ __GROUP__: true,
116
+ open: true,
117
+ key: group,
118
+ items: state.grouped[group],
119
+ itemCount: state.grouped[group].length,
120
+ }));
121
+ state.items = state.groups
122
+ .map((group) => [group, ...state.grouped[group.key]])
123
+ .flat() as any;
124
+ } else {
125
+ state.items = data;
61
126
  }
62
127
  }
63
- onCheckChanged?.(state.checked);
64
128
  return {
65
129
  ...state,
66
130
  allChecked:
67
- state.checked.length === data.length
68
- ? 1
69
- : state.checked.length
70
- ? 2
71
- : 0,
72
- } as DataState;
131
+ checkList.length === data.length ? 1 : state.checked.length ? 2 : 0,
132
+ } as DataState<T>;
73
133
  },
74
134
  {
135
+ items: data,
136
+ grouped: {},
137
+ groups: [],
75
138
  checked: [],
139
+ expanded: [],
76
140
  allChecked: 0,
77
141
  },
78
142
  );
79
143
 
144
+ const activeStickyIndexRef = useRef(0);
145
+ const stickyIndexes = useMemo<number[]>(
146
+ () =>
147
+ state.items
148
+ .map((hit, idx) => ("__GROUP__" in hit ? idx : undefined))
149
+ .filter(Boolean) as number[],
150
+ [state.items],
151
+ );
152
+ const isActiveSticky = (index: number) =>
153
+ activeStickyIndexRef.current === index;
154
+
155
+ const [hasActiveSticky, setHasActiveSticky] = useState(false);
156
+ const scrollerRef = useRef<HTMLDivElement>(null);
157
+ const virtualizer = useVirtualizer({
158
+ count: state.items.length,
159
+ getScrollElement: () => scrollerRef.current,
160
+ estimateSize: () => 32,
161
+ rangeExtractor: useCallback(
162
+ (range: Range) => {
163
+ activeStickyIndexRef.current =
164
+ [...stickyIndexes]
165
+ .reverse()
166
+ .find((index) => range.startIndex > index) ?? 0;
167
+
168
+ setHasActiveSticky(range.startIndex !== activeStickyIndexRef.current);
169
+
170
+ const next = new Set([
171
+ activeStickyIndexRef.current,
172
+ ...defaultRangeExtractor(range),
173
+ ]);
174
+
175
+ return [...next];
176
+ },
177
+ [stickyIndexes],
178
+ ),
179
+ });
180
+
80
181
  const virtualItems = virtualizer.getVirtualItems();
81
182
 
82
183
  const getData = useCallback(
83
184
  (index: number) => {
84
- return data[index];
185
+ return state.items[index];
85
186
  },
86
- [data],
187
+ [state.items],
87
188
  );
88
189
 
190
+ useEffect(() => {
191
+ dispatch({ type: "grouped", key: options.groupProperty });
192
+ }, [options.groupProperty]);
193
+
194
+ useEffect(() => {
195
+ dispatch({ type: "updateState", checked: options.checked ?? [] });
196
+ }, [options.checked]);
197
+
89
198
  return {
90
199
  scrollerRef,
91
200
  items: virtualItems,
92
201
  checkState: state,
93
202
  getData,
203
+ virtualizer,
204
+ hasActiveSticky,
205
+ isActiveSticky,
206
+ top: virtualizer.range?.startIndex,
94
207
  totalSize: virtualizer.getTotalSize,
95
208
  measureElement: virtualizer.measureElement,
209
+ toggleGroupExpand: (key: Key) => dispatch({ type: "groupExpand", key }),
210
+ toggleExpand: (key: Key) => dispatch({ type: "expand", key }),
96
211
  toggleChecked: (key: Key) => dispatch({ type: "check", key }),
97
212
  toggleAllChecked: () => dispatch({ type: "checkall" }),
98
213
  };
@@ -22,8 +22,10 @@
22
22
  */
23
23
 
24
24
  import { Badge, CoreIcons, getBadgeProps, Icon } from "@react-fabric/core";
25
+ import { isEmpty, isString } from "@react-fabric/utilities";
25
26
  import classNames from "classnames";
26
- import { useMemo } from "react";
27
+ import { isValidElement, useMemo } from "react";
28
+ import { useTranslation } from "react-i18next";
27
29
  import { type TreeNodeProps } from "./types";
28
30
 
29
31
  export const TreeNode = ({
@@ -35,30 +37,43 @@ export const TreeNode = ({
35
37
  defaultLeafIcon,
36
38
  defaultNodeIcon,
37
39
  onToggle,
40
+ onClick,
38
41
  onSelect,
39
42
  onChecked,
40
43
  children,
44
+ leafClassName,
45
+ nodeClassName,
41
46
  }: TreeNodeProps) => {
47
+ const { t } = useTranslation("data");
42
48
  const badgeProps = useMemo(() => {
43
49
  return getBadgeProps(node.badge);
44
50
  }, [node.badge]);
45
51
 
46
52
  const filler = useMemo(() => {
47
- return node.lines.slice(noLines ? 1 : 0).map((line, idx) => (
48
- <svg
53
+ return node.lines.slice(1).map((line, idx) => (
54
+ <div
49
55
  key={idx}
50
- viewBox="0 0 24 24"
51
- className="flex-initial w-6 fill-tint-200"
52
- >
53
- {line === 0 && <path d="" />}
54
- {!noLines && line === 1 && <path d="M7.45 26.1v-28.2h1.1v28.2h-1.1z" />}
55
- {!noLines && line === 2 && (
56
- <path d="M14.989 12.389l0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v14.1l0.161 0.389 0.389 0.161h6.6z" />
57
- )}
58
- {!noLines && line === 3 && (
59
- <path d="M8.55 12.55h6.050l0.389-0.161 0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v28.2h1.1z" />
56
+ className={classNames(
57
+ "flex-initial self-stretch flex items-center w-6 relative",
58
+ !noLines &&
59
+ line > 0 &&
60
+ "before:absolute before:top-0 before:start-[7px] before:bg-tint-200 before:min-h-[50%] before:w-[2px] before:scale-x-[.625]",
61
+ !noLines && line !== 2 && "before:h-full",
60
62
  )}
61
- </svg>
63
+ >
64
+ <svg viewBox="0 0 24 24" className="fill-tint-200 rtl:-scale-x-100">
65
+ {line === 0 && <path d="" />}
66
+ {!noLines && line === 1 && (
67
+ <path d="M7.45 26.1v-28.2h1.1v28.2h-1.1z" />
68
+ )}
69
+ {!noLines && line === 2 && (
70
+ <path d="M14.989 12.389l0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v14.1l0.161 0.389 0.389 0.161h6.6z" />
71
+ )}
72
+ {!noLines && line === 3 && (
73
+ <path d="M8.55 12.55h6.050l0.389-0.161 0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v28.2h1.1z" />
74
+ )}
75
+ </svg>
76
+ </div>
62
77
  ));
63
78
  }, [noLines, node.lines]);
64
79
 
@@ -82,12 +97,20 @@ export const TreeNode = ({
82
97
  <div
83
98
  role="treeitem"
84
99
  aria-selected={node.selected}
85
- className="flex flex-nowrap"
100
+ className={classNames(
101
+ "flex flex-nowrap items-center",
102
+ node.leaf ? leafClassName : nodeClassName,
103
+ )}
86
104
  data-testid={node["data-testid"]}
87
105
  data-test-value={node["data-test-value"]}
88
106
  >
89
107
  {filler}
90
- {!node.leaf && !node.childFiltered && (
108
+ {node.loading && (
109
+ <div className="flex-initial w-6">
110
+ <Icon icon={CoreIcons.spinner} className="animate-spin text-muted" />
111
+ </div>
112
+ )}
113
+ {!node.leaf && !node.childFiltered && !node.loading && (
91
114
  <div className="flex-initial w-6">
92
115
  <Icon
93
116
  rtlFlip
@@ -116,48 +139,59 @@ export const TreeNode = ({
116
139
  />
117
140
  </div>
118
141
  )}
119
- <div
120
- role="none"
121
- className={classNames(
122
- "group data-[selected]:bg-primary-100 flex flex-nowrap flex-1 items-center overflow-hidden select-none",
123
- node.childSelected && "font-medium",
124
- canSelect && "hover:bg-primary-50 cursor-pointer",
125
- )}
126
- data-selected={node.selected}
127
- onClick={() =>
128
- canSelect
129
- ? onSelect(node.id)
130
- : canCheck
131
- ? onChecked(node.id)
132
- : onToggle(node.id)
133
- }
134
- >
135
- {nodeIcon && (
136
- <div className="flex-content w-6">
137
- <Icon
138
- icon={nodeIcon}
139
- bg={node.iconBg}
140
- color={node.iconColor}
141
- rtlFlip={node.rtlFlip}
142
- />
142
+ {!node.empty && (
143
+ <div
144
+ role="none"
145
+ className={classNames(
146
+ "group/tool data-[selected]:bg-primary-100 flex flex-nowrap flex-1 overflow-hidden select-none",
147
+ node.childSelected && "font-medium",
148
+ (canSelect || onClick) && "hover:bg-primary-50 cursor-pointer",
149
+ )}
150
+ data-selected={node.selected}
151
+ onClick={() => {
152
+ onClick?.(node.id, node.data);
153
+ canSelect
154
+ ? onSelect(node.id)
155
+ : canCheck
156
+ ? onChecked(node.id)
157
+ : onToggle(node.id);
158
+ }}
159
+ >
160
+ {nodeIcon && (
161
+ <div className="flex-content w-6 self-center leading-none">
162
+ {isValidElement(nodeIcon) && nodeIcon}
163
+ {isString(nodeIcon) && (
164
+ <Icon
165
+ icon={nodeIcon}
166
+ bg={node.iconBg}
167
+ color={node.iconColor}
168
+ rtlFlip={node.rtlFlip}
169
+ />
170
+ )}
171
+ </div>
172
+ )}
173
+ {node.childSelected && !node.open && (
174
+ <span className=" flex-content text-primary-500">•</span>
175
+ )}
176
+ <div role="none" className="flex-1 truncate">
177
+ {children}
143
178
  </div>
144
- )}
145
- {node.childSelected && !node.open && (
146
- <span className=" flex-content text-primary-500">•</span>
147
- )}
148
- <div role="none" className="flex-1 truncate">
149
- {children}
179
+ {!isEmpty(node.badge) && (
180
+ <Badge
181
+ {...badgeProps}
182
+ className={classNames(
183
+ "me-1 self-center flex-content bg-opacity-50",
184
+ badgeProps.className,
185
+ )}
186
+ />
187
+ )}
150
188
  </div>
151
- {node.badge && (
152
- <Badge
153
- {...badgeProps}
154
- className={classNames(
155
- "me-1 self-center flex-content bg-opacity-50",
156
- badgeProps.className,
157
- )}
158
- />
159
- )}
160
- </div>
189
+ )}
190
+ {node.empty && (
191
+ <div className="text-muted text-sm select-none">
192
+ {t("tree.noItems")}
193
+ </div>
194
+ )}
161
195
  </div>
162
196
  );
163
197
  };
@@ -55,7 +55,16 @@ export const TreePanel = <T extends KeyValue>({
55
55
  expander = "box",
56
56
  onChecked,
57
57
  onLoad,
58
+ onQuery,
58
59
  onSelect,
60
+ onClick,
61
+ sorter,
62
+ makeLabel,
63
+ makeIcon,
64
+ defaultExpanded,
65
+ onExpandToggle,
66
+ leafClassName,
67
+ nodeClassName,
59
68
  ...aria
60
69
  }: TreePanelProps<T>) => {
61
70
  const {
@@ -72,8 +81,13 @@ export const TreePanel = <T extends KeyValue>({
72
81
  checked,
73
82
  selected,
74
83
  matcher,
84
+ sorter,
85
+ onLoad,
86
+ onQuery,
75
87
  onSelect,
76
88
  onChecked,
89
+ defaultExpanded,
90
+ onExpandToggle,
77
91
  });
78
92
 
79
93
  const scrollerRef = useRef<HTMLDivElement>(null);
@@ -99,7 +113,7 @@ export const TreePanel = <T extends KeyValue>({
99
113
  ref,
100
114
  () => ({
101
115
  open: (id: string) => expand(id),
102
- select: (id: string) => expand(id),
116
+ select: (id: string) => select(id),
103
117
  }),
104
118
  [],
105
119
  );
@@ -151,12 +165,18 @@ export const TreePanel = <T extends KeyValue>({
151
165
  checkable={checkable}
152
166
  selectable={selectable}
153
167
  defaultLeafIcon={defaultLeafIcon}
154
- defaultNodeIcon={defaultNodeIcon}
168
+ defaultNodeIcon={
169
+ makeIcon?.(tree[index].data) ?? defaultNodeIcon
170
+ }
155
171
  onToggle={toggleExpand}
156
172
  onChecked={toggleCheck}
157
173
  onSelect={select}
174
+ onClick={onClick}
175
+ leafClassName={leafClassName}
176
+ nodeClassName={nodeClassName}
158
177
  >
159
- {(tree[index].data && renderer?.(tree[index].data)) ||
178
+ {(tree[index].data && renderer?.(tree[index].data)) ??
179
+ makeLabel?.(tree[index].data) ??
160
180
  tree[index].label}
161
181
  </TreeNode>
162
182
  </div>
package/src/tree/types.ts CHANGED
@@ -30,8 +30,9 @@ import {
30
30
  type RefProp,
31
31
  type TestProps,
32
32
  } from "@react-fabric/core/dist/types/types";
33
+ import { type ElementType } from "react";
33
34
 
34
- interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
35
+ interface TreeBaseNode<T> extends Omit<IconProps, "icon">, CssProp, TestProps {
35
36
  id: string;
36
37
  /**
37
38
  * display label
@@ -54,20 +55,25 @@ interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
54
55
  */
55
56
  disabled?: boolean;
56
57
  data?: T;
58
+
59
+ icon?: ElementType<IconProps> | string;
57
60
  }
58
61
 
59
62
  export type TreeNodeType<T extends KeyValue = KeyValue> = TreeBaseNode<T> &
60
63
  (
61
64
  | { leaf: true; children?: never }
62
65
  | {
63
- leaf?: false;
66
+ leaf?: boolean;
64
67
  children?: Array<TreeNodeType<T>>;
65
68
  open?: boolean;
66
69
  iconOpen?: string;
67
70
  }
68
71
  );
69
72
 
70
- export interface InternalNode extends IconProps, CssProp, TestProps {
73
+ export interface InternalNode
74
+ extends Omit<IconProps, "icon">,
75
+ CssProp,
76
+ TestProps {
71
77
  id: string;
72
78
  label: Elements<JSX.Element>;
73
79
  queryable?: string;
@@ -84,6 +90,7 @@ export interface InternalNode extends IconProps, CssProp, TestProps {
84
90
  loaded: boolean;
85
91
  parent?: string;
86
92
  empty?: boolean;
93
+ icon?: ElementType<IconProps> | string;
87
94
  errored?: string;
88
95
  selected?: true;
89
96
  childSelected?: true;
@@ -101,9 +108,12 @@ export interface TreeNodeProps extends ChildProp {
101
108
  noLines?: boolean;
102
109
  selectable?: true | "leafOnly";
103
110
  checkable?: true | "leafOnly";
111
+ leafClassName?: string;
112
+ nodeClassName?: string;
104
113
  onToggle: (id: string) => void;
105
114
  onSelect: (id: string) => void;
106
115
  onChecked: (id: string) => void;
116
+ onClick?: (id: string, node: any) => void;
107
117
  }
108
118
 
109
119
  export interface TreeRef {
@@ -164,10 +174,18 @@ export interface TreePanelProps<T extends KeyValue = KeyValue>
164
174
  | Promise<Array<TreeNodeType<T>> | undefined>
165
175
  | Array<TreeNodeType<T>>
166
176
  | undefined;
177
+ /**
178
+ * callback to load tree using search query
179
+ */
180
+ onQuery?: (query: string) => void;
181
+ /**
182
+ * callback on click of tree node
183
+ */
184
+ onClick?: (id: string, data: T) => void;
167
185
  /**
168
186
  * callback on selection of tree node
169
187
  */
170
- onSelect?: (id: string) => void;
188
+ onSelect?: (id: string, data: T) => void;
171
189
  /**
172
190
  * callback on change of checked list
173
191
  */
@@ -180,6 +198,16 @@ export interface TreePanelProps<T extends KeyValue = KeyValue>
180
198
  * item filter matcher
181
199
  */
182
200
  matcher?: (data: T, query: string) => boolean;
201
+
202
+ sorter?: false | ((a: T, b: T) => number);
203
+ makeLabel?: (node: T) => string;
204
+ makeIcon?: (node: T) => string;
205
+
206
+ defaultExpanded?: string[];
207
+ onExpandToggle?: (id: string[]) => void;
208
+
209
+ leafClassName?: string;
210
+ nodeClassName?: string;
183
211
  }
184
212
 
185
213
  export const iconExpandAll =