@react-fabric/data 1.0.0-beta.1 → 1.0.0-beta.12
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.
- package/dist/cjs/checklist/CheckList.js +143 -0
- package/dist/cjs/histogram/Histogram.js +14 -5
- package/dist/cjs/hooks/useSelectableList.js +54 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/table/BodyCell.js +35 -3
- package/dist/cjs/table/BodyRow.js +107 -0
- package/dist/cjs/table/HeaderCell.js +4 -3
- package/dist/cjs/table/Table.js +84 -49
- package/dist/cjs/table/useVirtualTable.js +16 -2
- package/dist/cjs/tree/TreePanel.js +14 -2
- package/dist/cjs/tree/useTree.js +7 -0
- package/dist/cjs/virtual/Gallery.js +11 -4
- package/dist/cjs/virtual/List.js +11 -4
- package/dist/esm/checklist/CheckList.js +143 -0
- package/dist/esm/histogram/Histogram.js +14 -5
- package/dist/esm/hooks/useSelectableList.js +54 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/table/BodyCell.js +35 -3
- package/dist/esm/table/BodyRow.js +107 -0
- package/dist/esm/table/HeaderCell.js +4 -3
- package/dist/esm/table/Table.js +85 -50
- package/dist/esm/table/useVirtualTable.js +16 -2
- package/dist/esm/tree/TreePanel.js +15 -3
- package/dist/esm/tree/useTree.js +7 -0
- package/dist/esm/virtual/Gallery.js +11 -4
- package/dist/esm/virtual/List.js +11 -4
- package/dist/types/checklist/CheckList.d.ts +30 -0
- package/dist/types/histogram/Histogram.d.ts +3 -3
- package/dist/types/hooks/useSelectableList.d.ts +28 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/table/AddColumn.d.ts +2 -2
- package/dist/types/table/BodyCell.d.ts +4 -3
- package/dist/types/table/BodyRow.d.ts +20 -0
- package/dist/types/table/EmptyCell.d.ts +2 -2
- package/dist/types/table/HeaderCell.d.ts +2 -2
- package/dist/types/table/Table.d.ts +1 -1
- package/dist/types/table/types.d.ts +19 -7
- package/dist/types/table/useTableColumns.d.ts +6 -6
- package/dist/types/table/useVirtualTable.d.ts +5 -1
- package/dist/types/tree/TreePanel.d.ts +1 -1
- package/dist/types/tree/types.d.ts +6 -2
- package/dist/types/tree/useTree.d.ts +1 -0
- package/dist/types/virtual/Gallery.d.ts +3 -2
- package/dist/types/virtual/List.d.ts +3 -2
- package/package.json +6 -5
- package/src/checklist/CheckList.tsx +222 -0
- package/src/histogram/Histogram.tsx +7 -2
- package/src/hooks/useSelectableList.ts +94 -0
- package/src/index.ts +3 -0
- package/src/table/AddColumn.tsx +2 -2
- package/src/table/BodyCell.tsx +30 -3
- package/src/table/BodyRow.tsx +142 -0
- package/src/table/EmptyCell.tsx +2 -2
- package/src/table/HeaderCell.tsx +4 -3
- package/src/table/Table.tsx +103 -56
- package/src/table/types.ts +23 -6
- package/src/table/useTableColumns.ts +7 -7
- package/src/table/useVirtualTable.ts +19 -5
- package/src/tree/TreePanel.tsx +12 -1
- package/src/tree/types.ts +8 -1
- package/src/tree/useTree.ts +5 -0
- package/src/virtual/Gallery.tsx +4 -1
- package/src/virtual/List.tsx +4 -1
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
import {
|
|
32
32
|
type ColorType,
|
|
33
33
|
type PaletteType,
|
|
34
|
+
type TestProps,
|
|
34
35
|
} from "@react-fabric/core/dist/types/types";
|
|
35
36
|
import { compareValues, Format } from "@react-fabric/utilities";
|
|
36
37
|
import classNames from "classnames";
|
|
@@ -49,7 +50,7 @@ interface HistogramItem {
|
|
|
49
50
|
disabled?: boolean;
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
export interface HistogramProps {
|
|
53
|
+
export interface HistogramProps extends TestProps {
|
|
53
54
|
/**
|
|
54
55
|
* histogram items list
|
|
55
56
|
*/
|
|
@@ -88,6 +89,7 @@ export const Histogram = ({
|
|
|
88
89
|
emptyTitle,
|
|
89
90
|
emptyMessage,
|
|
90
91
|
onClick,
|
|
92
|
+
...aria
|
|
91
93
|
}: HistogramProps) => {
|
|
92
94
|
const { t } = useTranslation("data");
|
|
93
95
|
const maxTotal = useMemo(() => {
|
|
@@ -104,6 +106,7 @@ export const Histogram = ({
|
|
|
104
106
|
return (
|
|
105
107
|
<div
|
|
106
108
|
className={classes.histogram}
|
|
109
|
+
{...aria}
|
|
107
110
|
style={
|
|
108
111
|
{
|
|
109
112
|
"--color": `var(--${color})`,
|
|
@@ -125,8 +128,10 @@ export const Histogram = ({
|
|
|
125
128
|
className={classNames(
|
|
126
129
|
"relative cursor-pointer flex flex-nowrap items-center select-none overflow-hidden min-h-6 my-px",
|
|
127
130
|
classes.histoLabel,
|
|
128
|
-
(item.count === 0 || item.disabled) &&
|
|
131
|
+
((item.total ?? item.count) === 0 || item.disabled) &&
|
|
132
|
+
"pointer-events-none",
|
|
129
133
|
)}
|
|
134
|
+
data-testid={item.id}
|
|
130
135
|
onClick={(e) => [onClick?.(item.id), e.stopPropagation()]}
|
|
131
136
|
>
|
|
132
137
|
<div
|
|
@@ -0,0 +1,94 @@
|
|
|
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 { useCallback, useEffect, useRef, useState, useTransition } from "react";
|
|
25
|
+
|
|
26
|
+
export interface SelectableProps {
|
|
27
|
+
/**
|
|
28
|
+
* list items
|
|
29
|
+
*/
|
|
30
|
+
items: Array<{ label: string } & KeyValue>;
|
|
31
|
+
/**
|
|
32
|
+
* selected item ids
|
|
33
|
+
*/
|
|
34
|
+
selected?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* selected negative item ids
|
|
37
|
+
*/
|
|
38
|
+
nonselected?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* change callback
|
|
41
|
+
*/
|
|
42
|
+
onChange?: (ids: string[], nonIds?: string[]) => void;
|
|
43
|
+
/**
|
|
44
|
+
* item click callback
|
|
45
|
+
*/
|
|
46
|
+
onClick?: (id: string, isNegative: boolean) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const useSelectableList = ({
|
|
50
|
+
items,
|
|
51
|
+
selected,
|
|
52
|
+
nonselected,
|
|
53
|
+
onChange,
|
|
54
|
+
onClick,
|
|
55
|
+
}: SelectableProps) => {
|
|
56
|
+
const selectionRef = useRef<KeyValue<number>>({});
|
|
57
|
+
const [selection, setSelection] = useState<KeyValue<number>>({});
|
|
58
|
+
const [, startTransition] = useTransition();
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
selectionRef.current = items.reduce(
|
|
61
|
+
(ret, { id }) => ({
|
|
62
|
+
...ret,
|
|
63
|
+
[id]: selected?.includes?.(id)
|
|
64
|
+
? 1
|
|
65
|
+
: nonselected?.includes?.(id)
|
|
66
|
+
? -1
|
|
67
|
+
: 0,
|
|
68
|
+
}),
|
|
69
|
+
{},
|
|
70
|
+
);
|
|
71
|
+
setSelection({ ...selectionRef.current });
|
|
72
|
+
}, [items, selected, nonselected]);
|
|
73
|
+
|
|
74
|
+
const toggleSelection = useCallback((id: string, isNegative = false) => {
|
|
75
|
+
const check = isNegative ? -1 : 1;
|
|
76
|
+
if (selectionRef.current[id] !== check) selectionRef.current[id] = check;
|
|
77
|
+
else if (selectionRef.current[id] === check) selectionRef.current[id] = 0;
|
|
78
|
+
const change: string[][] = [[], []];
|
|
79
|
+
Object.entries(selectionRef.current).forEach(([id, select]) =>
|
|
80
|
+
select === 1
|
|
81
|
+
? change[0].push(id)
|
|
82
|
+
: select === -1
|
|
83
|
+
? change[1].push(id)
|
|
84
|
+
: undefined,
|
|
85
|
+
);
|
|
86
|
+
setSelection({ ...selectionRef.current });
|
|
87
|
+
startTransition(() => {
|
|
88
|
+
onClick?.(id, isNegative);
|
|
89
|
+
onChange?.(change[0], change[1]);
|
|
90
|
+
});
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
return { selection, toggleSelection };
|
|
94
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -23,11 +23,14 @@
|
|
|
23
23
|
|
|
24
24
|
import "./i18n";
|
|
25
25
|
|
|
26
|
+
export { CheckList } from "./checklist/CheckList";
|
|
26
27
|
export { Histogram } from "./histogram/Histogram";
|
|
27
28
|
export { JsonViewer } from "./json/JsonViewer";
|
|
28
29
|
export { Pagination } from "./pagination/Pagination";
|
|
29
30
|
export { Table } from "./table/Table";
|
|
31
|
+
export { type TableColumn, type TableRef } from "./table/types";
|
|
30
32
|
export { TreePanel } from "./tree/TreePanel";
|
|
33
|
+
export { type TreeNodeType as TreeNode } from "./tree/types";
|
|
31
34
|
export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
|
|
32
35
|
export { VirtualList, type VirtualListRef } from "./virtual/List";
|
|
33
36
|
|
package/src/table/AddColumn.tsx
CHANGED
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
|
|
25
|
-
import { type
|
|
25
|
+
import { type TableColumn } from "./types";
|
|
26
26
|
|
|
27
27
|
export const AddColumn = ({
|
|
28
28
|
columns,
|
|
29
29
|
onClick,
|
|
30
30
|
}: {
|
|
31
|
-
columns:
|
|
31
|
+
columns: TableColumn[];
|
|
32
32
|
onClick: (id: string) => void;
|
|
33
33
|
}) => {
|
|
34
34
|
return (
|
package/src/table/BodyCell.tsx
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
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 { DateDisplay } from "@react-fabric/date";
|
|
25
|
+
import { Format, getByPath, isTrue } from "@react-fabric/utilities";
|
|
24
26
|
import classNames from "classnames";
|
|
25
27
|
import { useMemo } from "react";
|
|
26
28
|
import { useTableContext } from "./Context";
|
|
@@ -28,15 +30,17 @@ import {
|
|
|
28
30
|
COL_DEFAULT_WIDTH,
|
|
29
31
|
COL_MAX_WIDTH,
|
|
30
32
|
COL_MIN_WIDTH,
|
|
31
|
-
type
|
|
33
|
+
type TableColumn,
|
|
32
34
|
} from "./types";
|
|
33
35
|
|
|
34
36
|
export const BodyCell = ({
|
|
35
37
|
column,
|
|
36
38
|
item,
|
|
39
|
+
index,
|
|
37
40
|
}: {
|
|
38
|
-
column:
|
|
41
|
+
column: TableColumn;
|
|
39
42
|
item: KeyValue;
|
|
43
|
+
index: number;
|
|
40
44
|
}) => {
|
|
41
45
|
const { widths } = useTableContext();
|
|
42
46
|
|
|
@@ -45,6 +49,29 @@ export const BodyCell = ({
|
|
|
45
49
|
[widths, column],
|
|
46
50
|
);
|
|
47
51
|
|
|
52
|
+
const content = useMemo(() => {
|
|
53
|
+
const value = getByPath(item, column.id.toString());
|
|
54
|
+
if (column.renderer != null) {
|
|
55
|
+
return column.renderer(item[column.id], item, index);
|
|
56
|
+
}
|
|
57
|
+
if (column.dataType === "boolean") {
|
|
58
|
+
const map = column.valueMap ?? { true: "Yes", false: "no" };
|
|
59
|
+
return map[`${isTrue(value) ? "true" : "false"}`];
|
|
60
|
+
}
|
|
61
|
+
if (column.valueMap != null) {
|
|
62
|
+
if (column.dataType === "string" && value in column.valueMap) {
|
|
63
|
+
return column.valueMap[value];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (column.dataType === "date") {
|
|
67
|
+
return <DateDisplay date={value} format={column.format} />;
|
|
68
|
+
}
|
|
69
|
+
if (column.dataType === "number") {
|
|
70
|
+
return Format.number(value, column.format);
|
|
71
|
+
}
|
|
72
|
+
return value ?? "";
|
|
73
|
+
}, [item, column]);
|
|
74
|
+
|
|
48
75
|
return (
|
|
49
76
|
<div
|
|
50
77
|
className="overflow-hidden bg-inherit start-0"
|
|
@@ -61,7 +88,7 @@ export const BodyCell = ({
|
|
|
61
88
|
column.align === "end" && "text-end",
|
|
62
89
|
)}
|
|
63
90
|
>
|
|
64
|
-
{
|
|
91
|
+
{content}
|
|
65
92
|
</div>
|
|
66
93
|
</div>
|
|
67
94
|
);
|
|
@@ -0,0 +1,142 @@
|
|
|
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 classNames from "classnames";
|
|
25
|
+
import { BodyCell } from "./BodyCell";
|
|
26
|
+
import { CheckboxCell } from "./CheckboxCell";
|
|
27
|
+
import { type TableColumn } from "./types";
|
|
28
|
+
import { CoreIcons, Icon } from "@react-fabric/core";
|
|
29
|
+
|
|
30
|
+
export const BodyRow = ({
|
|
31
|
+
rowKey,
|
|
32
|
+
index,
|
|
33
|
+
data,
|
|
34
|
+
keyProperty,
|
|
35
|
+
measureElement,
|
|
36
|
+
state,
|
|
37
|
+
hideableColumns,
|
|
38
|
+
wrapperStart,
|
|
39
|
+
wrapperEnd,
|
|
40
|
+
onRowClick,
|
|
41
|
+
checkableRows,
|
|
42
|
+
checked,
|
|
43
|
+
toggleChecked,
|
|
44
|
+
canExpand,
|
|
45
|
+
expanded,
|
|
46
|
+
toggleExpand,
|
|
47
|
+
expandRow,
|
|
48
|
+
}: {
|
|
49
|
+
rowKey: Key;
|
|
50
|
+
index: number;
|
|
51
|
+
data: KeyValue;
|
|
52
|
+
keyProperty?: Key;
|
|
53
|
+
measureElement: AnyObject;
|
|
54
|
+
state: KeyValue<TableColumn[]>;
|
|
55
|
+
hideableColumns?: boolean;
|
|
56
|
+
wrapperStart: string;
|
|
57
|
+
wrapperEnd: string;
|
|
58
|
+
checkableRows?: boolean;
|
|
59
|
+
checked: 0 | 1;
|
|
60
|
+
onRowClick?: AnyObject;
|
|
61
|
+
canExpand?: AnyObject;
|
|
62
|
+
expandRow?: AnyObject;
|
|
63
|
+
expanded: boolean;
|
|
64
|
+
toggleExpand: (key: Key) => void;
|
|
65
|
+
toggleChecked: (key: Key) => void;
|
|
66
|
+
}) => {
|
|
67
|
+
return (
|
|
68
|
+
<div
|
|
69
|
+
ref={measureElement}
|
|
70
|
+
data-index={index}
|
|
71
|
+
className={index % 2 ? "bg-even" : "bg-odd"}
|
|
72
|
+
>
|
|
73
|
+
<div
|
|
74
|
+
role="none"
|
|
75
|
+
data-row={index}
|
|
76
|
+
className={classNames(
|
|
77
|
+
"flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500",
|
|
78
|
+
onRowClick && "hover:bg-primary-50 active:bg-primary-100",
|
|
79
|
+
)}
|
|
80
|
+
onClick={() =>
|
|
81
|
+
checkableRows && keyProperty && !onRowClick
|
|
82
|
+
? toggleChecked(data[keyProperty])
|
|
83
|
+
: onRowClick?.(data)
|
|
84
|
+
}
|
|
85
|
+
>
|
|
86
|
+
<div className={wrapperStart}>
|
|
87
|
+
{!!canExpand && (
|
|
88
|
+
<div
|
|
89
|
+
className={classNames(
|
|
90
|
+
"group font-medium w-6 flex flex-nowrap text-start items-center",
|
|
91
|
+
!canExpand(data) && "pointer-events-none opacity-35",
|
|
92
|
+
)}
|
|
93
|
+
>
|
|
94
|
+
<Icon
|
|
95
|
+
className="p-1 text-tint-700 hover:text-primary-600"
|
|
96
|
+
icon={expanded ? CoreIcons.collapse : CoreIcons.expand}
|
|
97
|
+
onClick={(e) => [
|
|
98
|
+
keyProperty && toggleExpand(data[keyProperty]),
|
|
99
|
+
e.stopPropagation(),
|
|
100
|
+
]}
|
|
101
|
+
/>
|
|
102
|
+
</div>
|
|
103
|
+
)}
|
|
104
|
+
{checkableRows && keyProperty && (
|
|
105
|
+
<CheckboxCell
|
|
106
|
+
onClick={() => toggleChecked(data[keyProperty])}
|
|
107
|
+
state={checked}
|
|
108
|
+
/>
|
|
109
|
+
)}
|
|
110
|
+
{state.start?.map((col, ids) => (
|
|
111
|
+
<BodyCell
|
|
112
|
+
key={`${String(rowKey)}:${ids}`}
|
|
113
|
+
column={col}
|
|
114
|
+
item={data}
|
|
115
|
+
index={ids}
|
|
116
|
+
/>
|
|
117
|
+
))}
|
|
118
|
+
</div>
|
|
119
|
+
{state.cols?.map((col, idc) => (
|
|
120
|
+
<BodyCell
|
|
121
|
+
key={`${String(rowKey)}:${idc}`}
|
|
122
|
+
column={col}
|
|
123
|
+
item={data}
|
|
124
|
+
index={idc}
|
|
125
|
+
/>
|
|
126
|
+
))}
|
|
127
|
+
<div className={wrapperEnd}>
|
|
128
|
+
{state.end?.map((col, ide) => (
|
|
129
|
+
<BodyCell
|
|
130
|
+
key={`${String(rowKey)}:${ide}`}
|
|
131
|
+
column={col}
|
|
132
|
+
item={data}
|
|
133
|
+
index={ide}
|
|
134
|
+
/>
|
|
135
|
+
))}
|
|
136
|
+
{hideableColumns && <div className="w-6" />}
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
{expanded && expandRow(data)}
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
};
|
package/src/table/EmptyCell.tsx
CHANGED
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
COL_DEFAULT_WIDTH,
|
|
28
28
|
COL_MAX_WIDTH,
|
|
29
29
|
COL_MIN_WIDTH,
|
|
30
|
-
type
|
|
30
|
+
type TableColumn,
|
|
31
31
|
} from "./types";
|
|
32
32
|
|
|
33
33
|
export const EmptyCell = ({
|
|
@@ -35,7 +35,7 @@ export const EmptyCell = ({
|
|
|
35
35
|
width: _width,
|
|
36
36
|
maxWidth,
|
|
37
37
|
minWidth,
|
|
38
|
-
}:
|
|
38
|
+
}: TableColumn) => {
|
|
39
39
|
const { widths } = useTableContext();
|
|
40
40
|
|
|
41
41
|
const width = useMemo(
|
package/src/table/HeaderCell.tsx
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
COL_DEFAULT_WIDTH,
|
|
36
36
|
COL_MAX_WIDTH,
|
|
37
37
|
COL_MIN_WIDTH,
|
|
38
|
-
type
|
|
38
|
+
type TableColumn,
|
|
39
39
|
type TableProps,
|
|
40
40
|
} from "./types";
|
|
41
41
|
|
|
@@ -48,6 +48,7 @@ export const HeaderCell = ({
|
|
|
48
48
|
rtlFlip,
|
|
49
49
|
actions,
|
|
50
50
|
locked,
|
|
51
|
+
tooltip,
|
|
51
52
|
sortable,
|
|
52
53
|
resizable,
|
|
53
54
|
width: _width,
|
|
@@ -55,7 +56,7 @@ export const HeaderCell = ({
|
|
|
55
56
|
minWidth,
|
|
56
57
|
sort,
|
|
57
58
|
onSort,
|
|
58
|
-
}:
|
|
59
|
+
}: TableColumn & Pick<TableProps, "sort" | "onSort">) => {
|
|
59
60
|
const { startResize, widths } = useTableContext();
|
|
60
61
|
|
|
61
62
|
const width = useMemo(
|
|
@@ -102,7 +103,7 @@ export const HeaderCell = ({
|
|
|
102
103
|
maxWidth: maxWidth ?? COL_MAX_WIDTH,
|
|
103
104
|
}}
|
|
104
105
|
>
|
|
105
|
-
<Tooltip content={label} disabled={!label}>
|
|
106
|
+
<Tooltip content={tooltip ?? label} disabled={!(tooltip ?? label)}>
|
|
106
107
|
<div className="flex-initial px-2 py-1 text-sm truncate sticky start-0">
|
|
107
108
|
{icon && (
|
|
108
109
|
<Icon icon={icon} bg={iconBg} color={iconColor} rtlFlip={rtlFlip} />
|
package/src/table/Table.tsx
CHANGED
|
@@ -22,11 +22,17 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { EmptyContent, Loading, useDebounce } from "@react-fabric/core";
|
|
25
|
-
import
|
|
26
|
-
import {
|
|
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 {
|
|
35
|
+
import { BodyRow } from "./BodyRow";
|
|
30
36
|
import { CheckboxCell } from "./CheckboxCell";
|
|
31
37
|
import { TableProvider } from "./Context";
|
|
32
38
|
import { EmptyCell } from "./EmptyCell";
|
|
@@ -36,14 +42,20 @@ import { useTableColumns } from "./useTableColumns";
|
|
|
36
42
|
import { useVirtualTable } from "./useVirtualTable";
|
|
37
43
|
|
|
38
44
|
export const Table = <T extends KeyValue = KeyValue>({
|
|
45
|
+
ref,
|
|
39
46
|
data,
|
|
40
47
|
columns,
|
|
41
48
|
keyProperty,
|
|
42
49
|
checkableRows,
|
|
50
|
+
initialScroll,
|
|
43
51
|
hideableColumns,
|
|
44
52
|
sort,
|
|
45
53
|
loading,
|
|
54
|
+
emptyDisplay,
|
|
55
|
+
canExpand,
|
|
56
|
+
children,
|
|
46
57
|
onSort,
|
|
58
|
+
onScroll,
|
|
47
59
|
onRowClick,
|
|
48
60
|
onCheckedChanged,
|
|
49
61
|
}: TableProps<T>) => {
|
|
@@ -57,13 +69,17 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
57
69
|
const {
|
|
58
70
|
scrollerRef,
|
|
59
71
|
items,
|
|
72
|
+
top,
|
|
60
73
|
checkState,
|
|
74
|
+
virtualizer,
|
|
75
|
+
toggleExpand,
|
|
61
76
|
toggleChecked,
|
|
62
77
|
toggleAllChecked,
|
|
63
78
|
getData,
|
|
64
79
|
totalSize,
|
|
65
80
|
measureElement,
|
|
66
81
|
} = useVirtualTable(data, keyProperty, fireCheckChanged);
|
|
82
|
+
const refBody = useRef<HTMLDivElement>(null);
|
|
67
83
|
|
|
68
84
|
const wrapperStart = useMemo(
|
|
69
85
|
() => "bg-inherit sticky start-0 flex flex-nowrap z-1",
|
|
@@ -74,6 +90,51 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
74
90
|
[],
|
|
75
91
|
);
|
|
76
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
|
+
}),
|
|
125
|
+
[],
|
|
126
|
+
);
|
|
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
|
+
|
|
77
138
|
return (
|
|
78
139
|
<TableProvider>
|
|
79
140
|
<div
|
|
@@ -85,6 +146,7 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
85
146
|
>
|
|
86
147
|
<div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
|
|
87
148
|
<div className={wrapperStart}>
|
|
149
|
+
{canExpand && <div className="w-6" />}
|
|
88
150
|
{checkableRows && (
|
|
89
151
|
<CheckboxCell
|
|
90
152
|
state={checkState.allChecked}
|
|
@@ -116,62 +178,45 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
116
178
|
<div
|
|
117
179
|
className="area-content flex flex-col flex-nowrap"
|
|
118
180
|
style={{ height: totalSize() }}
|
|
181
|
+
ref={refBody}
|
|
119
182
|
>
|
|
120
183
|
<div style={{ height: items[0]?.start }} />
|
|
121
184
|
{items.map(({ index, key }) => {
|
|
122
185
|
const data = getData(index);
|
|
123
186
|
return (
|
|
124
|
-
<
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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])
|
|
136
200
|
}
|
|
137
|
-
data
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
key={`${key}:${ids}`}
|
|
152
|
-
column={col}
|
|
153
|
-
item={data}
|
|
154
|
-
/>
|
|
155
|
-
))}
|
|
156
|
-
</div>
|
|
157
|
-
{state.cols?.map((col, idc) => (
|
|
158
|
-
<BodyCell key={`${key}:${idc}`} column={col} item={data} />
|
|
159
|
-
))}
|
|
160
|
-
<div className={wrapperEnd}>
|
|
161
|
-
{state.end?.map((col, ide) => (
|
|
162
|
-
<BodyCell
|
|
163
|
-
key={`${key}:${ide}`}
|
|
164
|
-
column={col}
|
|
165
|
-
item={data}
|
|
166
|
-
/>
|
|
167
|
-
))}
|
|
168
|
-
{hideableColumns && <div className="w-6" />}
|
|
169
|
-
</div>
|
|
170
|
-
</div>
|
|
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
|
+
/>
|
|
171
215
|
);
|
|
172
216
|
})}
|
|
173
217
|
<div className="flex-1 flex flex-nowrap">
|
|
174
218
|
<div className={wrapperStart}>
|
|
219
|
+
{canExpand && <div className="w-6" />}
|
|
175
220
|
{checkableRows && <div className="w-6" />}
|
|
176
221
|
{state.start?.map((col) => (
|
|
177
222
|
<EmptyCell key={String(col.id)} {...col} />
|
|
@@ -190,13 +235,15 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
190
235
|
</div>
|
|
191
236
|
</div>
|
|
192
237
|
)}
|
|
193
|
-
{!loading &&
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
238
|
+
{!loading &&
|
|
239
|
+
data.length === 0 &&
|
|
240
|
+
(emptyDisplay ?? (
|
|
241
|
+
<EmptyContent
|
|
242
|
+
size="sm"
|
|
243
|
+
className="bg-dimmed"
|
|
244
|
+
message={t("datagrid.empty")}
|
|
245
|
+
/>
|
|
246
|
+
))}
|
|
200
247
|
</div>
|
|
201
248
|
</TableProvider>
|
|
202
249
|
);
|