@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,70 @@
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
+ import { type ChildrenProp } from "@react-fabric/core/dist/types/types";
25
+ import {
26
+ createContext,
27
+ useCallback,
28
+ useContext,
29
+ useRef,
30
+ useState,
31
+ } from "react";
32
+ import { useResizer } from "./useResizer";
33
+
34
+ interface Context {
35
+ widths: Map<string, number>;
36
+ startResize: (col: HTMLElement) => void;
37
+ }
38
+
39
+ const TableContext = createContext<Context>({} as Context);
40
+
41
+ export const TableProvider = ({ children }: ChildrenProp) => {
42
+ const ghostRef = useRef<HTMLDivElement>(null);
43
+ const [widths, setWidths] = useState<Map<string, number>>(new Map());
44
+ const startResize = useCallback(
45
+ (col: HTMLElement) => {
46
+ if (col) {
47
+ ghostRef.current != null &&
48
+ useResizer(col, ghostRef.current, (width) => {
49
+ setWidths(new Map(widths.set(col.dataset.id ?? "", width)));
50
+ });
51
+ }
52
+ },
53
+ [widths],
54
+ );
55
+
56
+ return (
57
+ <TableContext.Provider value={{ startResize, widths }}>
58
+ {children}
59
+
60
+ <div
61
+ className="absolute inset-0 hidden z-20 cursor-col-resize"
62
+ ref={ghostRef}
63
+ >
64
+ <div className="absolute inset-y-0 bg-muted opacity-50 border-primary border-e-2" />
65
+ </div>
66
+ </TableContext.Provider>
67
+ );
68
+ };
69
+
70
+ export const useTableContext = () => useContext(TableContext);
@@ -0,0 +1,57 @@
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
+ import { useMemo } from "react";
25
+ import { useTableContext } from "./Context";
26
+ import {
27
+ COL_DEFAULT_WIDTH,
28
+ COL_MAX_WIDTH,
29
+ COL_MIN_WIDTH,
30
+ type TableColumn,
31
+ } from "./types";
32
+
33
+ export const EmptyCell = ({
34
+ id,
35
+ width: _width,
36
+ maxWidth,
37
+ minWidth,
38
+ }: TableColumn) => {
39
+ const { widths } = useTableContext();
40
+
41
+ const width = useMemo(
42
+ () => widths.get(id.toString()) ?? _width,
43
+ [widths, id, _width],
44
+ );
45
+
46
+ return (
47
+ <div
48
+ key={`blank:${id.toString()}`}
49
+ className="overflow-hidden border-e"
50
+ style={{
51
+ width: width ?? COL_DEFAULT_WIDTH,
52
+ minWidth: minWidth ?? COL_MIN_WIDTH,
53
+ maxWidth: maxWidth ?? COL_MAX_WIDTH,
54
+ }}
55
+ />
56
+ );
57
+ };
@@ -21,10 +21,26 @@
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 { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
25
- import { type ColumnType } from "./types";
24
+ import {
25
+ CoreIcons,
26
+ DropdownTool,
27
+ Icon,
28
+ Menu,
29
+ MenuItem,
30
+ Tooltip,
31
+ } from "@react-fabric/core";
32
+ import { useCallback, useMemo } from "react";
33
+ import { useTableContext } from "./Context";
34
+ import {
35
+ COL_DEFAULT_WIDTH,
36
+ COL_MAX_WIDTH,
37
+ COL_MIN_WIDTH,
38
+ type TableColumn,
39
+ type TableProps,
40
+ } from "./types";
26
41
 
27
42
  export const HeaderCell = ({
43
+ id,
28
44
  label,
29
45
  icon,
30
46
  iconBg,
@@ -32,38 +48,90 @@ export const HeaderCell = ({
32
48
  rtlFlip,
33
49
  actions,
34
50
  locked,
51
+ tooltip,
52
+ sortable,
35
53
  resizable,
36
- width,
54
+ width: _width,
37
55
  maxWidth,
38
56
  minWidth,
39
- }: ColumnType) => {
57
+ sort,
58
+ onSort,
59
+ }: TableColumn & Pick<TableProps, "sort" | "onSort">) => {
60
+ const { startResize, widths } = useTableContext();
61
+
62
+ const width = useMemo(
63
+ () => widths.get(id.toString()) ?? _width,
64
+ [widths, _width, id],
65
+ );
66
+
67
+ const handleSort = useCallback(() => {
68
+ sortable &&
69
+ onSort?.({
70
+ id: id.toString(),
71
+ order: sort?.id === id && sort.order === "asc" ? "desc" : "asc",
72
+ });
73
+ }, [onSort, name, sort, sortable]);
74
+
75
+ const menus = useMemo(() => {
76
+ const ret = [];
77
+ if (sortable) {
78
+ ret.push(
79
+ <MenuItem
80
+ key="sort-asc"
81
+ label="Sort ascending"
82
+ icon={CoreIcons.sortAsc}
83
+ />,
84
+ <MenuItem
85
+ key="sort-desc"
86
+ label="Sort descending"
87
+ icon={CoreIcons.sortDesc}
88
+ />,
89
+ );
90
+ }
91
+ return ret;
92
+ }, [sortable]);
93
+
40
94
  return (
41
95
  <div
42
- className="group font-medium border-e flex flex-nowrap text-start items-center"
96
+ role="none"
97
+ data-id={id}
98
+ onClick={handleSort}
99
+ className="group font-medium border-e flex flex-nowrap text-start items-center table-header-cell"
43
100
  style={{
44
- width: width ?? "12rem",
45
- minWidth: minWidth ?? "2rem",
46
- maxWidth: maxWidth ?? "32rem",
101
+ width: width ?? COL_DEFAULT_WIDTH,
102
+ minWidth: minWidth ?? COL_MIN_WIDTH,
103
+ maxWidth: maxWidth ?? COL_MAX_WIDTH,
47
104
  }}
48
105
  >
49
- <div className="flex-1 px-2 py-1 text-sm truncate sticky start-0">
50
- {icon && (
51
- <Icon icon={icon} bg={iconBg} color={iconColor} rtlFlip={rtlFlip} />
52
- )}
53
- <span>{label ?? " "}</span>
54
- </div>
55
- <Dropdown placement="bottom-start">
56
- <Icon
57
- icon={CoreIcons.caretDown}
58
- className="outline bg-base me-0.5 flex-content hidden group-hover:inline-block data-[dropdown-open]:inline-block"
59
- />
60
- <Menu className="text-sm">
61
- <MenuItem label="Sort ascending" icon={CoreIcons.sortAsc} />
62
- <MenuItem label="Sort descending" icon={CoreIcons.sortDesc} />
63
- </Menu>
64
- </Dropdown>
106
+ <Tooltip content={tooltip ?? label} disabled={!(tooltip ?? label)}>
107
+ <div className="flex-initial px-2 py-1 text-sm truncate sticky start-0">
108
+ {icon && (
109
+ <Icon icon={icon} bg={iconBg} color={iconColor} rtlFlip={rtlFlip} />
110
+ )}
111
+ <span>{label ?? " "}</span>
112
+ </div>
113
+ </Tooltip>
114
+ {sortable && (
115
+ <div className="text-sm text-tint-300">
116
+ {sort?.id === id && sort?.order === "desc" && <span>▼</span>}
117
+ {sort?.id === id && sort?.order === "asc" && <span>▲</span>}
118
+ </div>
119
+ )}
120
+ <div className="flex-1" />
121
+ {menus.length > 0 && (
122
+ <DropdownTool groupHover placement="bottom-end">
123
+ <Menu className="text-sm">{menus}</Menu>
124
+ </DropdownTool>
125
+ )}
65
126
  {resizable && (
66
- <div className="cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch" />
127
+ <div
128
+ role="none"
129
+ className="cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch"
130
+ onMouseDown={(e) =>
131
+ e.currentTarget.parentElement &&
132
+ startResize(e.currentTarget.parentElement)
133
+ }
134
+ />
67
135
  )}
68
136
  </div>
69
137
  );
@@ -21,21 +21,65 @@
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 { useCallback, useMemo } from "react";
25
- import { BodyCell } from "./BodyCell";
24
+ import { EmptyContent, Loading, useDebounce } from "@react-fabric/core";
25
+ import { debounce } from "@react-fabric/utilities";
26
+ import {
27
+ useEffect,
28
+ useImperativeHandle,
29
+ useLayoutEffect,
30
+ useMemo,
31
+ useRef,
32
+ } from "react";
33
+ import { useTranslation } from "react-i18next";
34
+ import { AddColumn } from "./AddColumn";
35
+ import { BodyRow } from "./BodyRow";
36
+ import { CheckboxCell } from "./CheckboxCell";
37
+ import { TableProvider } from "./Context";
38
+ import { EmptyCell } from "./EmptyCell";
26
39
  import { HeaderCell } from "./HeaderCell";
27
- import { type ColumnType, type TableProps } from "./types";
40
+ import { type TableProps } from "./types";
28
41
  import { useTableColumns } from "./useTableColumns";
29
42
  import { useVirtualTable } from "./useVirtualTable";
30
- import classNames from "classnames";
31
43
 
32
44
  export const Table = <T extends KeyValue = KeyValue>({
45
+ ref,
33
46
  data,
34
47
  columns,
48
+ keyProperty,
49
+ checkableRows,
50
+ initialScroll,
51
+ hideableColumns,
52
+ sort,
53
+ loading,
54
+ emptyDisplay,
55
+ canExpand,
56
+ children,
57
+ onSort,
58
+ onScroll,
59
+ onRowClick,
60
+ onCheckedChanged,
35
61
  }: TableProps<T>) => {
36
- const { columnMap } = useTableColumns(columns);
37
- const { scrollerRef, items, getData, totalSize, measureElement } =
38
- useVirtualTable(data);
62
+ const { t } = useTranslation("data");
63
+ const fireCheckChanged = useDebounce(
64
+ (checkedIds: Array<keyof T>) => onCheckedChanged?.(checkedIds),
65
+ [],
66
+ );
67
+
68
+ const { state, onHide } = useTableColumns(columns);
69
+ const {
70
+ scrollerRef,
71
+ items,
72
+ top,
73
+ checkState,
74
+ virtualizer,
75
+ toggleExpand,
76
+ toggleChecked,
77
+ toggleAllChecked,
78
+ getData,
79
+ totalSize,
80
+ measureElement,
81
+ } = useVirtualTable(data, keyProperty, fireCheckChanged);
82
+ const refBody = useRef<HTMLDivElement>(null);
39
83
 
40
84
  const wrapperStart = useMemo(
41
85
  () => "bg-inherit sticky start-0 flex flex-nowrap z-1",
@@ -45,99 +89,162 @@ export const Table = <T extends KeyValue = KeyValue>({
45
89
  () => "bg-inherit sticky end-0 flex flex-nowrap z-1 border-s -ms-px",
46
90
  [],
47
91
  );
48
- const emptyCol = useCallback(
49
- (col: ColumnType, idc: number) => (
50
- <div
51
- key={`blank:${idc}`}
52
- className="overflow-hidden border-e"
53
- style={{
54
- width: col.width ?? "12rem",
55
- minWidth: col.minWidth ?? "2rem",
56
- maxWidth: col.maxWidth ?? "32rem",
57
- }}
58
- />
59
- ),
92
+
93
+ useLayoutEffect(() => {
94
+ setTimeout(() => {
95
+ initialScroll &&
96
+ virtualizer.scrollToIndex(initialScroll, { align: "start" });
97
+ }, 100);
98
+ }, []);
99
+
100
+ useImperativeHandle(
101
+ ref,
102
+ () => ({
103
+ scrollTo: (row: number) => {
104
+ virtualizer.scrollToIndex(row, { align: "start" });
105
+ },
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);
118
+ },
119
+ unhilight() {
120
+ scrollerRef.current
121
+ ?.querySelector(".hilight")
122
+ ?.classList.remove("hilight", "ring-1");
123
+ },
124
+ }),
60
125
  [],
61
126
  );
62
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
+
63
138
  return (
64
- <div
65
- className="grid area-content overflow-auto relative"
66
- style={{
67
- gridTemplate: `"head" auto "content" 1fr "foot" auto/1fr`,
68
- }}
69
- ref={scrollerRef}
70
- >
71
- <div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
72
- <div className={wrapperStart}>
73
- {columnMap.start?.map((col, idx) => (
74
- <HeaderCell key={idx} {...col} />
75
- ))}
76
- </div>
77
- {columnMap.cols?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
78
- <div className={wrapperEnd}>
79
- {columnMap.end?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
80
- </div>
81
- </div>
139
+ <TableProvider>
82
140
  <div
83
- className="area-content flex flex-col flex-nowrap"
84
- style={{ height: totalSize() }}
141
+ className="grid area-content overflow-auto relative"
142
+ style={{
143
+ gridTemplate: `"head" auto "loader" auto "content" 1fr "foot" auto/1fr`,
144
+ }}
145
+ ref={scrollerRef}
85
146
  >
86
- <div style={{ height: items[0]?.start }} />
87
- {items.map(({ index, key }) => (
88
- <div
89
- key={key}
90
- className={classNames(
91
- "flex flex-nowrap",
92
- index % 2 ? "bg-even" : "bg-odd",
93
- )}
94
- data-index={index}
95
- ref={measureElement}
96
- >
97
- <div className={wrapperStart}>
98
- {columnMap.start?.map((col, ids) => (
99
- <BodyCell
100
- key={`${key}:${ids}`}
101
- column={col}
102
- item={getData(index)}
103
- />
104
- ))}
105
- </div>
106
- {columnMap.cols?.map((col, idc) => (
107
- <BodyCell
108
- key={`${key}:${idc}`}
109
- column={col}
110
- item={getData(index)}
147
+ <div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
148
+ <div className={wrapperStart}>
149
+ {canExpand && <div className="w-6" />}
150
+ {checkableRows && (
151
+ <CheckboxCell
152
+ state={checkState.allChecked}
153
+ onClick={toggleAllChecked}
111
154
  />
155
+ )}
156
+ {state.start?.map((col, idx) => (
157
+ <HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
112
158
  ))}
113
- <div className={wrapperEnd}>
114
- {columnMap.end?.map((col, ide) => (
115
- <BodyCell
116
- key={`${key}:${ide}`}
117
- column={col}
118
- item={getData(index)}
159
+ </div>
160
+ {state.cols?.map((col, idx) => (
161
+ <HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
162
+ ))}
163
+ <div className={wrapperEnd}>
164
+ {state.end?.map((col, idx) => (
165
+ <HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
166
+ ))}
167
+
168
+ {hideableColumns && (
169
+ <AddColumn
170
+ onClick={onHide}
171
+ columns={state.columns.filter((col) => col.hideable !== false)}
172
+ />
173
+ )}
174
+ </div>
175
+ </div>
176
+ {loading && <Loading />}
177
+ {data.length > 0 && (
178
+ <div
179
+ className="area-content flex flex-col flex-nowrap"
180
+ style={{ height: totalSize() }}
181
+ ref={refBody}
182
+ >
183
+ <div style={{ height: items[0]?.start }} />
184
+ {items.map(({ index, key }) => {
185
+ const data = getData(index);
186
+ return (
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
196
+ }
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}
119
214
  />
215
+ );
216
+ })}
217
+ <div className="flex-1 flex flex-nowrap">
218
+ <div className={wrapperStart}>
219
+ {canExpand && <div className="w-6" />}
220
+ {checkableRows && <div className="w-6" />}
221
+ {state.start?.map((col) => (
222
+ <EmptyCell key={String(col.id)} {...col} />
223
+ ))}
224
+ </div>
225
+ {state.cols?.map((col) => (
226
+ <EmptyCell key={String(col.id)} {...col} />
120
227
  ))}
228
+ <div className={wrapperEnd}>
229
+ {state.end?.map((col) => (
230
+ <EmptyCell key={String(col.id)} {...col} />
231
+ ))}
232
+
233
+ {hideableColumns && <div className="w-6" />}
234
+ </div>
121
235
  </div>
122
236
  </div>
123
- ))}
124
- <div className="flex-1 flex flex-nowrap">
125
- <div className={wrapperStart}>{columnMap.start?.map(emptyCol)}</div>
126
- {columnMap.cols?.map(emptyCol)}
127
- <div className={wrapperEnd}>{columnMap.end?.map(emptyCol)}</div>
128
- </div>
129
- </div>
130
- <div className="area-foot bg-dimmed border-t sticky bottom-0 flex flex-nowrap z-2">
131
- <div className={wrapperStart}>
132
- {columnMap.start?.map((col, idx) => (
133
- <HeaderCell key={idx} {...col} />
237
+ )}
238
+ {!loading &&
239
+ data.length === 0 &&
240
+ (emptyDisplay ?? (
241
+ <EmptyContent
242
+ size="sm"
243
+ className="bg-dimmed"
244
+ message={t("datagrid.empty")}
245
+ />
134
246
  ))}
135
- </div>
136
- {columnMap.cols?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
137
- <div className={wrapperEnd}>
138
- {columnMap.end?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
139
- </div>
140
247
  </div>
141
- </div>
248
+ </TableProvider>
142
249
  );
143
250
  };
@@ -23,17 +23,24 @@
23
23
 
24
24
  import {
25
25
  type CssProp,
26
+ type Elements,
26
27
  type IconProps,
28
+ type RefProp,
27
29
  type TestProps,
28
30
  } from "@react-fabric/core/dist/types/types";
29
31
  import { type ReactElement } from "react";
30
32
 
31
- export interface ColumnType<T = KeyValue>
33
+ export const COL_MIN_WIDTH = "3rem";
34
+ export const COL_MAX_WIDTH = "64rem";
35
+ export const COL_DEFAULT_WIDTH = "12rem";
36
+
37
+ export interface TableColumn<T = KeyValue>
32
38
  extends CssProp,
33
39
  IconProps,
34
40
  TestProps {
35
- id: keyof T;
41
+ id: keyof T | string;
36
42
  label?: string;
43
+ tooltip?: string;
37
44
  actions?: ReactElement;
38
45
  align?: "start" | "center" | "end";
39
46
  locked?: "start" | "end";
@@ -55,11 +62,40 @@ export interface ColumnType<T = KeyValue>
55
62
  | "time"
56
63
  | "unknown";
57
64
  format?: string;
58
- renderer?: (value: AnyObject, data: T) => void;
65
+ valueMap?: KeyValue;
66
+ renderer?: (value: AnyObject, data: T, index: number) => Elements<AnyObject>;
67
+ }
68
+
69
+ export interface TableRef {
70
+ hilight: (row: number) => void;
71
+ unhilight: () => void;
72
+ scrollTo: (row: number) => void;
59
73
  }
60
74
 
61
- export interface TableProps<T = KeyValue> {
62
- columns: Array<ColumnType<T>>;
75
+ export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
76
+ columns: Array<TableColumn<T>>;
63
77
  data: T[];
64
- autoColumns?: boolean;
78
+ keyProperty?: keyof T;
79
+ emptyDisplay?: ReactElement;
80
+ /**
81
+ * checkable rows
82
+ */
83
+ checkableRows?: boolean;
84
+ onCheckedChanged?: (rows: Array<keyof T>) => void;
85
+ /**
86
+ * sorting
87
+ */
88
+ sort?: { id: string; order: "asc" | "desc" };
89
+ onSort?: (sort: { id: string; order: "asc" | "desc" }) => void;
90
+
91
+ loading?: boolean;
92
+ hideableColumns?: boolean;
93
+
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>;
65
101
  }