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

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.
@@ -23,7 +23,7 @@
23
23
 
24
24
  import { EmptyContent, Loading, useDebounce } from "@react-fabric/core";
25
25
  import classNames from "classnames";
26
- import { useMemo } from "react";
26
+ import { useImperativeHandle, useMemo, useRef } from "react";
27
27
  import { useTranslation } from "react-i18next";
28
28
  import { AddColumn } from "./AddColumn";
29
29
  import { BodyCell } from "./BodyCell";
@@ -36,6 +36,7 @@ import { useTableColumns } from "./useTableColumns";
36
36
  import { useVirtualTable } from "./useVirtualTable";
37
37
 
38
38
  export const Table = <T extends KeyValue = KeyValue>({
39
+ ref,
39
40
  data,
40
41
  columns,
41
42
  keyProperty,
@@ -64,6 +65,7 @@ export const Table = <T extends KeyValue = KeyValue>({
64
65
  totalSize,
65
66
  measureElement,
66
67
  } = useVirtualTable(data, keyProperty, fireCheckChanged);
68
+ const refBody = useRef<HTMLDivElement>(null);
67
69
 
68
70
  const wrapperStart = useMemo(
69
71
  () => "bg-inherit sticky start-0 flex flex-nowrap z-1",
@@ -74,6 +76,32 @@ export const Table = <T extends KeyValue = KeyValue>({
74
76
  [],
75
77
  );
76
78
 
79
+ useImperativeHandle(
80
+ ref,
81
+ () => ({
82
+ hilight: (row: number) => {
83
+ refBody.current
84
+ ?.querySelectorAll<HTMLElement>(`.datatable-row[data-hilight="true"]`)
85
+ .forEach((el) => el && (el.dataset.hilight = undefined));
86
+ const el = refBody.current?.querySelector<HTMLElement>(
87
+ `[data-row="${row}"]`,
88
+ );
89
+ el?.scrollIntoView({ behavior: "smooth", block: "center" });
90
+ el && (el.dataset.hilight = "true");
91
+ },
92
+ unhilight: () => {
93
+ refBody.current
94
+ ?.querySelectorAll<HTMLElement>(`.datatable-row[data-hilight="true"]`)
95
+ .forEach((el) => el && (el.dataset.hilight = undefined));
96
+ },
97
+ scrollTo: (row: number) => {
98
+ const el = refBody.current?.querySelector(`[data-row="${row}"]`);
99
+ el?.scrollIntoView({ behavior: "smooth" });
100
+ },
101
+ }),
102
+ [],
103
+ );
104
+
77
105
  return (
78
106
  <TableProvider>
79
107
  <div
@@ -116,6 +144,7 @@ export const Table = <T extends KeyValue = KeyValue>({
116
144
  <div
117
145
  className="area-content flex flex-col flex-nowrap"
118
146
  style={{ height: totalSize() }}
147
+ ref={refBody}
119
148
  >
120
149
  <div style={{ height: items[0]?.start }} />
121
150
  {items.map(({ index, key }) => {
@@ -124,13 +153,14 @@ export const Table = <T extends KeyValue = KeyValue>({
124
153
  <div
125
154
  key={key}
126
155
  role="none"
156
+ data-row={index}
127
157
  className={classNames(
128
- "flex flex-nowrap",
158
+ "flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500",
129
159
  index % 2 ? "bg-even" : "bg-odd",
130
160
  onRowClick && "hover:bg-primary-50 active:bg-primary-100",
131
161
  )}
132
162
  onClick={() =>
133
- checkableRows && !onRowClick
163
+ checkableRows && keyProperty && !onRowClick
134
164
  ? toggleChecked(data[keyProperty])
135
165
  : onRowClick?.(data)
136
166
  }
@@ -138,7 +168,7 @@ export const Table = <T extends KeyValue = KeyValue>({
138
168
  ref={measureElement}
139
169
  >
140
170
  <div className={wrapperStart}>
141
- {checkableRows && (
171
+ {checkableRows && keyProperty && (
142
172
  <CheckboxCell
143
173
  onClick={() => toggleChecked(data[keyProperty])}
144
174
  state={
@@ -24,6 +24,7 @@
24
24
  import {
25
25
  type CssProp,
26
26
  type IconProps,
27
+ type RefProp,
27
28
  type TestProps,
28
29
  } from "@react-fabric/core/dist/types/types";
29
30
  import { type ReactElement } from "react";
@@ -36,7 +37,7 @@ export interface ColumnType<T = KeyValue>
36
37
  extends CssProp,
37
38
  IconProps,
38
39
  TestProps {
39
- id: keyof T;
40
+ id: keyof T | string;
40
41
  label?: string;
41
42
  actions?: ReactElement;
42
43
  align?: "start" | "center" | "end";
@@ -62,10 +63,16 @@ export interface ColumnType<T = KeyValue>
62
63
  renderer?: (value: AnyObject, data: T) => void;
63
64
  }
64
65
 
65
- export interface TableProps<T = KeyValue> {
66
+ export interface TableRef {
67
+ hilight: (row: number) => void;
68
+ unhilight: () => void;
69
+ scrollTo: (row: number) => void;
70
+ }
71
+
72
+ export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
66
73
  columns: Array<ColumnType<T>>;
67
74
  data: T[];
68
- keyProperty: keyof T;
75
+ keyProperty?: keyof T;
69
76
  /**
70
77
  * checkable rows
71
78
  */
@@ -33,8 +33,8 @@ type DataAction = { type: "check"; key: Key } | { type: "checkall" };
33
33
 
34
34
  export const useVirtualTable = (
35
35
  data: KeyValue[],
36
- keyProperty: Key,
37
- onCheckChanged: (checked: Key[]) => void,
36
+ keyProperty?: Key,
37
+ onCheckChanged?: (checked: Key[]) => void,
38
38
  ) => {
39
39
  // TODO: refactor data into item list, apply grouping if groupColumn provided
40
40
  // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
@@ -56,11 +56,11 @@ export const useVirtualTable = (
56
56
  if (action.type === "checkall") {
57
57
  if (state.allChecked !== 0) {
58
58
  state.checked = [];
59
- } else {
59
+ } else if (keyProperty) {
60
60
  state.checked = data.map((item) => item[keyProperty]);
61
61
  }
62
62
  }
63
- onCheckChanged(state.checked);
63
+ onCheckChanged?.(state.checked);
64
64
  return {
65
65
  ...state,
66
66
  allChecked:
@@ -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
  };