@react-fabric/data 1.0.0-beta.5 → 1.0.0-beta.7

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 (47) hide show
  1. package/dist/cjs/checklist/CheckList.js +25 -16
  2. package/dist/cjs/histogram/Histogram.js +12 -4
  3. package/dist/cjs/table/BodyCell.js +35 -3
  4. package/dist/cjs/table/BodyRow.js +107 -0
  5. package/dist/cjs/table/HeaderCell.js +4 -3
  6. package/dist/cjs/table/Table.js +71 -69
  7. package/dist/cjs/table/useVirtualTable.js +15 -1
  8. package/dist/cjs/virtual/Gallery.js +11 -4
  9. package/dist/cjs/virtual/List.js +11 -4
  10. package/dist/esm/checklist/CheckList.js +27 -18
  11. package/dist/esm/histogram/Histogram.js +12 -4
  12. package/dist/esm/table/BodyCell.js +35 -3
  13. package/dist/esm/table/BodyRow.js +107 -0
  14. package/dist/esm/table/HeaderCell.js +4 -3
  15. package/dist/esm/table/Table.js +72 -70
  16. package/dist/esm/table/useVirtualTable.js +15 -1
  17. package/dist/esm/virtual/Gallery.js +11 -4
  18. package/dist/esm/virtual/List.js +11 -4
  19. package/dist/types/checklist/CheckList.d.ts +2 -2
  20. package/dist/types/histogram/Histogram.d.ts +3 -3
  21. package/dist/types/index.d.ts +1 -1
  22. package/dist/types/table/AddColumn.d.ts +2 -2
  23. package/dist/types/table/BodyCell.d.ts +4 -3
  24. package/dist/types/table/BodyRow.d.ts +20 -0
  25. package/dist/types/table/EmptyCell.d.ts +2 -2
  26. package/dist/types/table/HeaderCell.d.ts +2 -2
  27. package/dist/types/table/Table.d.ts +1 -1
  28. package/dist/types/table/types.d.ts +11 -4
  29. package/dist/types/table/useTableColumns.d.ts +6 -6
  30. package/dist/types/table/useVirtualTable.d.ts +4 -0
  31. package/dist/types/virtual/Gallery.d.ts +3 -2
  32. package/dist/types/virtual/List.d.ts +3 -2
  33. package/package.json +6 -5
  34. package/src/checklist/CheckList.tsx +81 -77
  35. package/src/histogram/Histogram.tsx +5 -1
  36. package/src/index.ts +1 -1
  37. package/src/table/AddColumn.tsx +2 -2
  38. package/src/table/BodyCell.tsx +30 -3
  39. package/src/table/BodyRow.tsx +142 -0
  40. package/src/table/EmptyCell.tsx +2 -2
  41. package/src/table/HeaderCell.tsx +4 -3
  42. package/src/table/Table.tsx +90 -73
  43. package/src/table/types.ts +13 -3
  44. package/src/table/useTableColumns.ts +7 -7
  45. package/src/table/useVirtualTable.ts +16 -2
  46. package/src/virtual/Gallery.tsx +4 -1
  47. package/src/virtual/List.tsx +4 -1
@@ -22,11 +22,17 @@
22
22
  */
23
23
 
24
24
  import { EmptyContent, Loading, useDebounce } from "@react-fabric/core";
25
- import classNames from "classnames";
26
- import { useImperativeHandle, useMemo, useRef } from "react";
25
+ import { debounce } from "@react-fabric/utilities";
26
+ import {
27
+ useEffect,
28
+ useImperativeHandle,
29
+ useLayoutEffect,
30
+ useMemo,
31
+ useRef,
32
+ } from "react";
27
33
  import { useTranslation } from "react-i18next";
28
34
  import { AddColumn } from "./AddColumn";
29
- import { BodyCell } from "./BodyCell";
35
+ import { BodyRow } from "./BodyRow";
30
36
  import { CheckboxCell } from "./CheckboxCell";
31
37
  import { TableProvider } from "./Context";
32
38
  import { EmptyCell } from "./EmptyCell";
@@ -41,10 +47,15 @@ export const Table = <T extends KeyValue = KeyValue>({
41
47
  columns,
42
48
  keyProperty,
43
49
  checkableRows,
50
+ initialScroll,
44
51
  hideableColumns,
45
52
  sort,
46
53
  loading,
54
+ emptyDisplay,
55
+ canExpand,
56
+ children,
47
57
  onSort,
58
+ onScroll,
48
59
  onRowClick,
49
60
  onCheckedChanged,
50
61
  }: TableProps<T>) => {
@@ -58,7 +69,10 @@ export const Table = <T extends KeyValue = KeyValue>({
58
69
  const {
59
70
  scrollerRef,
60
71
  items,
72
+ top,
61
73
  checkState,
74
+ virtualizer,
75
+ toggleExpand,
62
76
  toggleChecked,
63
77
  toggleAllChecked,
64
78
  getData,
@@ -76,32 +90,51 @@ export const Table = <T extends KeyValue = KeyValue>({
76
90
  [],
77
91
  );
78
92
 
93
+ useLayoutEffect(() => {
94
+ setTimeout(() => {
95
+ initialScroll &&
96
+ virtualizer.scrollToIndex(initialScroll, { align: "start" });
97
+ }, 100);
98
+ }, []);
99
+
79
100
  useImperativeHandle(
80
101
  ref,
81
102
  () => ({
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");
103
+ scrollTo: (row: number) => {
104
+ virtualizer.scrollToIndex(row, { align: "start" });
91
105
  },
92
- unhilight: () => {
93
- refBody.current
94
- ?.querySelectorAll<HTMLElement>(`.datatable-row[data-hilight="true"]`)
95
- .forEach((el) => el && (el.dataset.hilight = undefined));
106
+ hilight(row) {
107
+ scrollerRef.current
108
+ ?.querySelector(".hilight")
109
+ ?.classList.remove("hilight", "ring-1");
110
+ virtualizer.scrollToIndex(Math.floor(row), {
111
+ align: "start",
112
+ });
113
+ setTimeout(() => {
114
+ scrollerRef.current
115
+ ?.querySelector(`[data-index="${row}"]`)
116
+ ?.classList.add("hilight", "ring-1");
117
+ }, 100);
96
118
  },
97
- scrollTo: (row: number) => {
98
- const el = refBody.current?.querySelector(`[data-row="${row}"]`);
99
- el?.scrollIntoView({ behavior: "smooth" });
119
+ unhilight() {
120
+ scrollerRef.current
121
+ ?.querySelector(".hilight")
122
+ ?.classList.remove("hilight", "ring-1");
100
123
  },
101
124
  }),
102
125
  [],
103
126
  );
104
127
 
128
+ useEffect(() => {
129
+ const handle = debounce((top: number) => {
130
+ onScroll?.(top ?? 0);
131
+ });
132
+ handle(top);
133
+ return () => {
134
+ handle.cancel();
135
+ };
136
+ }, [top]);
137
+
105
138
  return (
106
139
  <TableProvider>
107
140
  <div
@@ -113,6 +146,7 @@ export const Table = <T extends KeyValue = KeyValue>({
113
146
  >
114
147
  <div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
115
148
  <div className={wrapperStart}>
149
+ {canExpand && <div className="w-6" />}
116
150
  {checkableRows && (
117
151
  <CheckboxCell
118
152
  state={checkState.allChecked}
@@ -150,58 +184,39 @@ export const Table = <T extends KeyValue = KeyValue>({
150
184
  {items.map(({ index, key }) => {
151
185
  const data = getData(index);
152
186
  return (
153
- <div
154
- key={key}
155
- role="none"
156
- data-row={index}
157
- className={classNames(
158
- "flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500",
159
- index % 2 ? "bg-even" : "bg-odd",
160
- onRowClick && "hover:bg-primary-50 active:bg-primary-100",
161
- )}
162
- onClick={() =>
163
- checkableRows && keyProperty && !onRowClick
164
- ? toggleChecked(data[keyProperty])
165
- : onRowClick?.(data)
187
+ <BodyRow
188
+ rowKey={key}
189
+ index={index}
190
+ checkableRows={checkableRows}
191
+ checked={
192
+ keyProperty &&
193
+ checkState.checked.includes(data[keyProperty])
194
+ ? 1
195
+ : 0
166
196
  }
167
- data-index={index}
168
- ref={measureElement}
169
- >
170
- <div className={wrapperStart}>
171
- {checkableRows && keyProperty && (
172
- <CheckboxCell
173
- onClick={() => toggleChecked(data[keyProperty])}
174
- state={
175
- checkState.checked.includes(data[keyProperty]) ? 1 : 0
176
- }
177
- />
178
- )}
179
- {state.start?.map((col, ids) => (
180
- <BodyCell
181
- key={`${key}:${ids}`}
182
- column={col}
183
- item={data}
184
- />
185
- ))}
186
- </div>
187
- {state.cols?.map((col, idc) => (
188
- <BodyCell key={`${key}:${idc}`} column={col} item={data} />
189
- ))}
190
- <div className={wrapperEnd}>
191
- {state.end?.map((col, ide) => (
192
- <BodyCell
193
- key={`${key}:${ide}`}
194
- column={col}
195
- item={data}
196
- />
197
- ))}
198
- {hideableColumns && <div className="w-6" />}
199
- </div>
200
- </div>
197
+ expanded={
198
+ !!keyProperty &&
199
+ checkState.expanded.includes(data[keyProperty])
200
+ }
201
+ data={data}
202
+ hideableColumns={hideableColumns}
203
+ keyProperty={keyProperty}
204
+ measureElement={measureElement}
205
+ onRowClick={onRowClick}
206
+ state={state}
207
+ canExpand={canExpand}
208
+ toggleExpand={toggleExpand}
209
+ expandRow={children}
210
+ toggleChecked={toggleChecked}
211
+ wrapperEnd={wrapperEnd}
212
+ wrapperStart={wrapperStart}
213
+ key={key}
214
+ />
201
215
  );
202
216
  })}
203
217
  <div className="flex-1 flex flex-nowrap">
204
218
  <div className={wrapperStart}>
219
+ {canExpand && <div className="w-6" />}
205
220
  {checkableRows && <div className="w-6" />}
206
221
  {state.start?.map((col) => (
207
222
  <EmptyCell key={String(col.id)} {...col} />
@@ -220,13 +235,15 @@ export const Table = <T extends KeyValue = KeyValue>({
220
235
  </div>
221
236
  </div>
222
237
  )}
223
- {!loading && data.length === 0 && (
224
- <EmptyContent
225
- size="sm"
226
- className="bg-dimmed"
227
- message={t("datagrid.empty")}
228
- />
229
- )}
238
+ {!loading &&
239
+ data.length === 0 &&
240
+ (emptyDisplay ?? (
241
+ <EmptyContent
242
+ size="sm"
243
+ className="bg-dimmed"
244
+ message={t("datagrid.empty")}
245
+ />
246
+ ))}
230
247
  </div>
231
248
  </TableProvider>
232
249
  );
@@ -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,12 +34,13 @@ 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;
43
+ tooltip?: string;
42
44
  actions?: ReactElement;
43
45
  align?: "start" | "center" | "end";
44
46
  locked?: "start" | "end";
@@ -60,7 +62,8 @@ export interface ColumnType<T = KeyValue>
60
62
  | "time"
61
63
  | "unknown";
62
64
  format?: string;
63
- renderer?: (value: AnyObject, data: T) => void;
65
+ valueMap?: KeyValue;
66
+ renderer?: (value: AnyObject, data: T, index: number) => Elements<AnyObject>;
64
67
  }
65
68
 
66
69
  export interface TableRef {
@@ -70,9 +73,10 @@ export interface TableRef {
70
73
  }
71
74
 
72
75
  export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
73
- columns: Array<ColumnType<T>>;
76
+ columns: Array<TableColumn<T>>;
74
77
  data: T[];
75
78
  keyProperty?: keyof T;
79
+ emptyDisplay?: ReactElement;
76
80
  /**
77
81
  * checkable rows
78
82
  */
@@ -88,4 +92,10 @@ export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
88
92
  hideableColumns?: boolean;
89
93
 
90
94
  onRowClick?: (data: T) => void;
95
+
96
+ initialScroll?: number;
97
+ onScroll?: (firstItem: number) => void;
98
+
99
+ canExpand?: (record: T) => boolean;
100
+ children?: (record: T) => Elements<AnyObject>;
91
101
  }
@@ -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") {
@@ -25,11 +25,15 @@ import { useVirtualizer } from "@tanstack/react-virtual";
25
25
  import { useCallback, useReducer, useRef } from "react";
26
26
 
27
27
  interface DataState {
28
+ expanded: Key[];
28
29
  checked: Key[];
29
30
  allChecked: 0 | 1 | 2;
30
31
  }
31
32
 
32
- type DataAction = { type: "check"; key: Key } | { type: "checkall" };
33
+ type DataAction =
34
+ | { type: "expand"; key: Key }
35
+ | { type: "check"; key: Key }
36
+ | { type: "checkall" };
33
37
 
34
38
  export const useVirtualTable = (
35
39
  data: KeyValue[],
@@ -48,10 +52,16 @@ export const useVirtualTable = (
48
52
 
49
53
  const [state, dispatch] = useReducer(
50
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
+ }
51
60
  if (action.type === "check") {
52
61
  if (state.checked.includes(action.key))
53
62
  state.checked.splice(state.checked.indexOf(action.key), 1);
54
63
  else state.checked.push(action.key);
64
+ onCheckChanged?.(state.checked);
55
65
  }
56
66
  if (action.type === "checkall") {
57
67
  if (state.allChecked !== 0) {
@@ -59,8 +69,8 @@ export const useVirtualTable = (
59
69
  } else if (keyProperty) {
60
70
  state.checked = data.map((item) => item[keyProperty]);
61
71
  }
72
+ onCheckChanged?.(state.checked);
62
73
  }
63
- onCheckChanged?.(state.checked);
64
74
  return {
65
75
  ...state,
66
76
  allChecked:
@@ -73,6 +83,7 @@ export const useVirtualTable = (
73
83
  },
74
84
  {
75
85
  checked: [],
86
+ expanded: [],
76
87
  allChecked: 0,
77
88
  },
78
89
  );
@@ -91,8 +102,11 @@ export const useVirtualTable = (
91
102
  items: virtualItems,
92
103
  checkState: state,
93
104
  getData,
105
+ virtualizer,
106
+ top: virtualizer.range?.startIndex,
94
107
  totalSize: virtualizer.getTotalSize,
95
108
  measureElement: virtualizer.measureElement,
109
+ toggleExpand: (key: Key) => dispatch({ type: "expand", key }),
96
110
  toggleChecked: (key: Key) => dispatch({ type: "check", key }),
97
111
  toggleAllChecked: () => dispatch({ type: "checkall" }),
98
112
  };
@@ -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(