@react-fabric/data 1.0.0-beta.0 → 1.0.0-beta.3
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/table/AddColumn.js +56 -0
- package/dist/cjs/table/BodyCell.js +13 -4
- package/dist/cjs/table/CheckboxCell.js +44 -0
- package/dist/cjs/table/Context.js +43 -0
- package/dist/cjs/table/EmptyCell.js +52 -0
- package/dist/cjs/table/HeaderCell.js +77 -32
- package/dist/cjs/table/Table.js +168 -93
- package/dist/cjs/table/types.js +32 -0
- package/dist/cjs/table/useResizer.js +65 -0
- package/dist/cjs/table/useTableColumns.js +45 -8
- package/dist/cjs/table/useVirtualTable.js +42 -25
- package/dist/cjs/tree/TreePanel.js +14 -2
- package/dist/cjs/tree/useTree.js +7 -0
- package/dist/esm/table/AddColumn.js +56 -0
- package/dist/esm/table/BodyCell.js +13 -4
- package/dist/esm/table/CheckboxCell.js +44 -0
- package/dist/esm/table/Context.js +42 -0
- package/dist/esm/table/EmptyCell.js +52 -0
- package/dist/esm/table/HeaderCell.js +79 -34
- package/dist/esm/table/Table.js +169 -94
- package/dist/esm/table/types.js +30 -0
- package/dist/esm/table/useResizer.js +65 -0
- package/dist/esm/table/useTableColumns.js +46 -9
- package/dist/esm/table/useVirtualTable.js +43 -26
- package/dist/esm/tree/TreePanel.js +15 -3
- package/dist/esm/tree/useTree.js +7 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/table/AddColumn.d.ts +5 -0
- package/dist/types/table/CheckboxCell.d.ts +4 -0
- package/dist/types/table/Context.d.ts +8 -0
- package/dist/types/table/EmptyCell.d.ts +2 -0
- 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 +30 -3
- package/dist/types/table/useResizer.d.ts +1 -0
- package/dist/types/table/useTableColumns.d.ts +6 -4
- package/dist/types/table/useVirtualTable.d.ts +9 -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/package.json +5 -5
- package/src/index.ts +2 -0
- package/src/table/AddColumn.tsx +55 -0
- package/src/table/BodyCell.tsx +18 -4
- package/src/table/CheckboxCell.tsx +48 -0
- package/src/table/Context.tsx +70 -0
- package/src/table/EmptyCell.tsx +57 -0
- package/src/table/HeaderCell.tsx +92 -25
- package/src/table/Table.tsx +178 -88
- package/src/table/types.ts +28 -2
- package/src/table/useResizer.ts +66 -0
- package/src/table/useTableColumns.ts +51 -18
- package/src/table/useVirtualTable.ts +47 -2
- package/src/tree/TreePanel.tsx +12 -1
- package/src/tree/types.ts +8 -1
- package/src/tree/useTree.ts +5 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ChildrenProp } from "@react-fabric/core/dist/types/types";
|
|
2
|
+
interface Context {
|
|
3
|
+
widths: Map<string, number>;
|
|
4
|
+
startResize: (col: HTMLElement) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare const TableProvider: ({ children }: ChildrenProp) => import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare const useTableContext: () => Context;
|
|
8
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type ColumnType } from "./types";
|
|
2
|
-
export declare const HeaderCell: ({ label, icon, iconBg, iconColor, rtlFlip, actions, locked, resizable, width, maxWidth, minWidth, }: ColumnType) => import("react/jsx-runtime").JSX.Element;
|
|
1
|
+
import { type ColumnType, type TableProps } from "./types";
|
|
2
|
+
export declare const HeaderCell: ({ id, label, icon, iconBg, iconColor, rtlFlip, actions, locked, sortable, resizable, width: _width, maxWidth, minWidth, sort, onSort, }: ColumnType & Pick<TableProps, "sort" | "onSort">) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type TableProps } from "./types";
|
|
2
|
-
export declare const Table: <T extends KeyValue<any> = KeyValue<any>>({ data, columns, }: TableProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare const Table: <T extends KeyValue<any> = KeyValue<any>>({ ref, data, columns, keyProperty, checkableRows, hideableColumns, sort, loading, onSort, onRowClick, onCheckedChanged, }: TableProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { type CssProp, type IconProps, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
1
|
+
import { type CssProp, type IconProps, type RefProp, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
2
2
|
import { type ReactElement } from "react";
|
|
3
|
+
export declare const COL_MIN_WIDTH = "3rem";
|
|
4
|
+
export declare const COL_MAX_WIDTH = "64rem";
|
|
5
|
+
export declare const COL_DEFAULT_WIDTH = "12rem";
|
|
3
6
|
export interface ColumnType<T = KeyValue> extends CssProp, IconProps, TestProps {
|
|
4
7
|
id: keyof T;
|
|
5
8
|
label?: string;
|
|
@@ -19,8 +22,32 @@ export interface ColumnType<T = KeyValue> extends CssProp, IconProps, TestProps
|
|
|
19
22
|
format?: string;
|
|
20
23
|
renderer?: (value: AnyObject, data: T) => void;
|
|
21
24
|
}
|
|
22
|
-
export interface
|
|
25
|
+
export interface TableRef {
|
|
26
|
+
hilight: (row: number) => void;
|
|
27
|
+
unhilight: () => void;
|
|
28
|
+
scrollTo: (row: number) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
|
|
23
31
|
columns: Array<ColumnType<T>>;
|
|
24
32
|
data: T[];
|
|
25
|
-
|
|
33
|
+
keyProperty: keyof T;
|
|
34
|
+
/**
|
|
35
|
+
* checkable rows
|
|
36
|
+
*/
|
|
37
|
+
checkableRows?: boolean;
|
|
38
|
+
onCheckedChanged?: (rows: Array<keyof T>) => void;
|
|
39
|
+
/**
|
|
40
|
+
* sorting
|
|
41
|
+
*/
|
|
42
|
+
sort?: {
|
|
43
|
+
id: string;
|
|
44
|
+
order: "asc" | "desc";
|
|
45
|
+
};
|
|
46
|
+
onSort?: (sort: {
|
|
47
|
+
id: string;
|
|
48
|
+
order: "asc" | "desc";
|
|
49
|
+
}) => void;
|
|
50
|
+
loading?: boolean;
|
|
51
|
+
hideableColumns?: boolean;
|
|
52
|
+
onRowClick?: (data: T) => void;
|
|
26
53
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useResizer: (colEl: HTMLElement, ghostEl: HTMLElement, callback: (width: number) => void) => void;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type ColumnType } from "./types";
|
|
2
2
|
export declare const useTableColumns: (columns: ColumnType[]) => {
|
|
3
|
-
|
|
4
|
-
start: ColumnType[];
|
|
5
|
-
end: ColumnType[];
|
|
6
|
-
cols: ColumnType[];
|
|
3
|
+
state: {
|
|
4
|
+
start: ColumnType<KeyValue<any>>[];
|
|
5
|
+
end: ColumnType<KeyValue<any>>[];
|
|
6
|
+
cols: ColumnType<KeyValue<any>>[];
|
|
7
|
+
columns: ColumnType[];
|
|
7
8
|
};
|
|
9
|
+
onHide: (id: string) => void;
|
|
8
10
|
};
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
2
|
+
interface DataState {
|
|
3
|
+
checked: Key[];
|
|
4
|
+
allChecked: 0 | 1 | 2;
|
|
5
|
+
}
|
|
6
|
+
export declare const useVirtualTable: (data: KeyValue[], keyProperty: Key, onCheckChanged: (checked: Key[]) => void) => {
|
|
3
7
|
scrollerRef: import("react").RefObject<HTMLDivElement>;
|
|
4
8
|
items: import("@tanstack/virtual-core").VirtualItem<Element>[];
|
|
9
|
+
checkState: DataState;
|
|
5
10
|
getData: (index: number) => KeyValue<any>;
|
|
6
11
|
totalSize: () => number;
|
|
7
12
|
measureElement: (node: Element | null | undefined) => void;
|
|
13
|
+
toggleChecked: (key: Key) => void;
|
|
14
|
+
toggleAllChecked: () => void;
|
|
8
15
|
};
|
|
16
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type TreePanelProps } from "./types";
|
|
2
|
-
export declare const TreePanel: <T extends KeyValue<any>>({ items, selected, checked, checkable, noLines, renderer, searchable, selectable, matcher, filterPlaceholder, defaultLeafIcon, defaultNodeIcon, expander, onChecked, onLoad, onSelect, ...aria }: TreePanelProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare const TreePanel: <T extends KeyValue<any>>({ ref, items, selected, checked, checkable, noLines, renderer, searchable, selectable, matcher, filterPlaceholder, defaultLeafIcon, defaultNodeIcon, expander, onChecked, onLoad, onSelect, ...aria }: TreePanelProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { type BadgeType, type ChildProp, type CssProp, type Elements, type IconProps, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
2
|
+
import { type BadgeType, type ChildProp, type CssProp, type Elements, type IconProps, type RefProp, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
3
3
|
interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
|
|
4
4
|
id: string;
|
|
5
5
|
/**
|
|
@@ -70,7 +70,11 @@ export interface TreeNodeProps extends ChildProp {
|
|
|
70
70
|
onSelect: (id: string) => void;
|
|
71
71
|
onChecked: (id: string) => void;
|
|
72
72
|
}
|
|
73
|
-
export interface
|
|
73
|
+
export interface TreeRef {
|
|
74
|
+
select: (id: string) => void;
|
|
75
|
+
open: (id: string) => void;
|
|
76
|
+
}
|
|
77
|
+
export interface TreePanelProps<T extends KeyValue = KeyValue> extends TestProps, RefProp<TreeRef> {
|
|
74
78
|
items?: Array<TreeNodeType<T>>;
|
|
75
79
|
/**
|
|
76
80
|
* make tree searchable
|
|
@@ -13,6 +13,7 @@ export declare const useTree: <T extends KeyValue<any>>({ items, checked, select
|
|
|
13
13
|
collapseAll: () => void;
|
|
14
14
|
toggleCheck: (id: string) => void;
|
|
15
15
|
select: (id: string) => void;
|
|
16
|
+
expand: (id: string) => void;
|
|
16
17
|
onFilter: {
|
|
17
18
|
(...args: any): void;
|
|
18
19
|
cancel(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-fabric/data",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
4
|
"description": "RUF: Data package",
|
|
5
5
|
"author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"watch": "npm-run-all -p watch:rollup watch:dts"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@react-fabric/core": "^1.0.0-beta.
|
|
37
|
-
"@react-fabric/form": "^1.0.0-beta.
|
|
38
|
-
"@react-fabric/utilities": "^1.0.0-beta.
|
|
36
|
+
"@react-fabric/core": "^1.0.0-beta.3",
|
|
37
|
+
"@react-fabric/form": "^1.0.0-beta.3",
|
|
38
|
+
"@react-fabric/utilities": "^1.0.0-beta.1",
|
|
39
39
|
"@tanstack/react-virtual": "^3.8.6",
|
|
40
40
|
"classnames": "^2.5.1",
|
|
41
41
|
"color": "^4.2.3",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"node": ">=18.12.0",
|
|
52
52
|
"npm": ">=8.19.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "2ce2a968f48d6ffc338f5da3a0cf67232d0c6864"
|
|
55
55
|
}
|
package/src/index.ts
CHANGED
|
@@ -27,7 +27,9 @@ export { Histogram } from "./histogram/Histogram";
|
|
|
27
27
|
export { JsonViewer } from "./json/JsonViewer";
|
|
28
28
|
export { Pagination } from "./pagination/Pagination";
|
|
29
29
|
export { Table } from "./table/Table";
|
|
30
|
+
export { type TableRef } from "./table/types";
|
|
30
31
|
export { TreePanel } from "./tree/TreePanel";
|
|
32
|
+
export { type TreeNodeType as TreeNode } from "./tree/types";
|
|
31
33
|
export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
|
|
32
34
|
export { VirtualList, type VirtualListRef } from "./virtual/List";
|
|
33
35
|
|
|
@@ -0,0 +1,55 @@
|
|
|
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 { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
|
|
25
|
+
import { type ColumnType } from "./types";
|
|
26
|
+
|
|
27
|
+
export const AddColumn = ({
|
|
28
|
+
columns,
|
|
29
|
+
onClick,
|
|
30
|
+
}: {
|
|
31
|
+
columns: ColumnType[];
|
|
32
|
+
onClick: (id: string) => void;
|
|
33
|
+
}) => {
|
|
34
|
+
return (
|
|
35
|
+
<div className="group font-medium w-6 flex flex-nowrap text-start items-end">
|
|
36
|
+
<Dropdown placement="bottom-end">
|
|
37
|
+
<Icon
|
|
38
|
+
icon={CoreIcons.insert}
|
|
39
|
+
className="p-1 rounded-t rounded-b-none hover:outline"
|
|
40
|
+
/>
|
|
41
|
+
<Menu className="text-sm" onClick={onClick}>
|
|
42
|
+
{columns.map((col) => (
|
|
43
|
+
<MenuItem
|
|
44
|
+
key={String(col.id)}
|
|
45
|
+
id={String(col.id)}
|
|
46
|
+
data-dropdown-dismiss="false"
|
|
47
|
+
label={`${col.label ?? String(col.id)}`}
|
|
48
|
+
icon={!col.hidden ? CoreIcons.tick : ""}
|
|
49
|
+
/>
|
|
50
|
+
))}
|
|
51
|
+
</Menu>
|
|
52
|
+
</Dropdown>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
};
|
package/src/table/BodyCell.tsx
CHANGED
|
@@ -22,7 +22,14 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import classNames from "classnames";
|
|
25
|
-
import {
|
|
25
|
+
import { useMemo } from "react";
|
|
26
|
+
import { useTableContext } from "./Context";
|
|
27
|
+
import {
|
|
28
|
+
COL_DEFAULT_WIDTH,
|
|
29
|
+
COL_MAX_WIDTH,
|
|
30
|
+
COL_MIN_WIDTH,
|
|
31
|
+
type ColumnType,
|
|
32
|
+
} from "./types";
|
|
26
33
|
|
|
27
34
|
export const BodyCell = ({
|
|
28
35
|
column,
|
|
@@ -31,13 +38,20 @@ export const BodyCell = ({
|
|
|
31
38
|
column: ColumnType;
|
|
32
39
|
item: KeyValue;
|
|
33
40
|
}) => {
|
|
41
|
+
const { widths } = useTableContext();
|
|
42
|
+
|
|
43
|
+
const width = useMemo(
|
|
44
|
+
() => widths.get(column.id.toString()) ?? column.width,
|
|
45
|
+
[widths, column],
|
|
46
|
+
);
|
|
47
|
+
|
|
34
48
|
return (
|
|
35
49
|
<div
|
|
36
50
|
className="overflow-hidden bg-inherit start-0"
|
|
37
51
|
style={{
|
|
38
|
-
width:
|
|
39
|
-
minWidth: column.minWidth ??
|
|
40
|
-
maxWidth: column.maxWidth ??
|
|
52
|
+
width: width ?? COL_DEFAULT_WIDTH,
|
|
53
|
+
minWidth: column.minWidth ?? COL_MIN_WIDTH,
|
|
54
|
+
maxWidth: column.maxWidth ?? COL_MAX_WIDTH,
|
|
41
55
|
}}
|
|
42
56
|
>
|
|
43
57
|
<div
|
|
@@ -0,0 +1,48 @@
|
|
|
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 { CoreIcons, Icon } from "@react-fabric/core";
|
|
25
|
+
|
|
26
|
+
export const CheckboxCell = ({
|
|
27
|
+
state,
|
|
28
|
+
onClick,
|
|
29
|
+
}: {
|
|
30
|
+
state: 0 | 1 | 2;
|
|
31
|
+
onClick?: () => void;
|
|
32
|
+
}) => {
|
|
33
|
+
return (
|
|
34
|
+
<div className="group font-medium border-e w-6 flex flex-nowrap text-start items-center">
|
|
35
|
+
<Icon
|
|
36
|
+
icon={
|
|
37
|
+
state === 2
|
|
38
|
+
? CoreIcons.checkboxInt
|
|
39
|
+
: state === 1
|
|
40
|
+
? CoreIcons.checkboxOn
|
|
41
|
+
: CoreIcons.checkboxOff
|
|
42
|
+
}
|
|
43
|
+
className="p-1 text-tint-700 hover:text-primary-600"
|
|
44
|
+
onClick={(e) => [onClick?.(), e.stopPropagation()]}
|
|
45
|
+
/>
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
@@ -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 ColumnType,
|
|
31
|
+
} from "./types";
|
|
32
|
+
|
|
33
|
+
export const EmptyCell = ({
|
|
34
|
+
id,
|
|
35
|
+
width: _width,
|
|
36
|
+
maxWidth,
|
|
37
|
+
minWidth,
|
|
38
|
+
}: ColumnType) => {
|
|
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
|
+
};
|
package/src/table/HeaderCell.tsx
CHANGED
|
@@ -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 {
|
|
25
|
-
|
|
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 ColumnType,
|
|
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,89 @@ export const HeaderCell = ({
|
|
|
32
48
|
rtlFlip,
|
|
33
49
|
actions,
|
|
34
50
|
locked,
|
|
51
|
+
sortable,
|
|
35
52
|
resizable,
|
|
36
|
-
width,
|
|
53
|
+
width: _width,
|
|
37
54
|
maxWidth,
|
|
38
55
|
minWidth,
|
|
39
|
-
|
|
56
|
+
sort,
|
|
57
|
+
onSort,
|
|
58
|
+
}: ColumnType & Pick<TableProps, "sort" | "onSort">) => {
|
|
59
|
+
const { startResize, widths } = useTableContext();
|
|
60
|
+
|
|
61
|
+
const width = useMemo(
|
|
62
|
+
() => widths.get(id.toString()) ?? _width,
|
|
63
|
+
[widths, _width, id],
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const handleSort = useCallback(() => {
|
|
67
|
+
sortable &&
|
|
68
|
+
onSort?.({
|
|
69
|
+
id: id.toString(),
|
|
70
|
+
order: sort?.id === id && sort.order === "asc" ? "desc" : "asc",
|
|
71
|
+
});
|
|
72
|
+
}, [onSort, name, sort, sortable]);
|
|
73
|
+
|
|
74
|
+
const menus = useMemo(() => {
|
|
75
|
+
const ret = [];
|
|
76
|
+
if (sortable) {
|
|
77
|
+
ret.push(
|
|
78
|
+
<MenuItem
|
|
79
|
+
key="sort-asc"
|
|
80
|
+
label="Sort ascending"
|
|
81
|
+
icon={CoreIcons.sortAsc}
|
|
82
|
+
/>,
|
|
83
|
+
<MenuItem
|
|
84
|
+
key="sort-desc"
|
|
85
|
+
label="Sort descending"
|
|
86
|
+
icon={CoreIcons.sortDesc}
|
|
87
|
+
/>,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return ret;
|
|
91
|
+
}, [sortable]);
|
|
92
|
+
|
|
40
93
|
return (
|
|
41
94
|
<div
|
|
42
|
-
|
|
95
|
+
role="none"
|
|
96
|
+
data-id={id}
|
|
97
|
+
onClick={handleSort}
|
|
98
|
+
className="group font-medium border-e flex flex-nowrap text-start items-center table-header-cell"
|
|
43
99
|
style={{
|
|
44
|
-
width: width ??
|
|
45
|
-
minWidth: minWidth ??
|
|
46
|
-
maxWidth: maxWidth ??
|
|
100
|
+
width: width ?? COL_DEFAULT_WIDTH,
|
|
101
|
+
minWidth: minWidth ?? COL_MIN_WIDTH,
|
|
102
|
+
maxWidth: maxWidth ?? COL_MAX_WIDTH,
|
|
47
103
|
}}
|
|
48
104
|
>
|
|
49
|
-
<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
105
|
+
<Tooltip content={label} disabled={!label}>
|
|
106
|
+
<div className="flex-initial px-2 py-1 text-sm truncate sticky start-0">
|
|
107
|
+
{icon && (
|
|
108
|
+
<Icon icon={icon} bg={iconBg} color={iconColor} rtlFlip={rtlFlip} />
|
|
109
|
+
)}
|
|
110
|
+
<span>{label ?? " "}</span>
|
|
111
|
+
</div>
|
|
112
|
+
</Tooltip>
|
|
113
|
+
{sortable && (
|
|
114
|
+
<div className="text-sm text-tint-300">
|
|
115
|
+
{sort?.id === id && sort?.order === "desc" && <span>▼</span>}
|
|
116
|
+
{sort?.id === id && sort?.order === "asc" && <span>▲</span>}
|
|
117
|
+
</div>
|
|
118
|
+
)}
|
|
119
|
+
<div className="flex-1" />
|
|
120
|
+
{menus.length > 0 && (
|
|
121
|
+
<DropdownTool groupHover placement="bottom-end">
|
|
122
|
+
<Menu className="text-sm">{menus}</Menu>
|
|
123
|
+
</DropdownTool>
|
|
124
|
+
)}
|
|
65
125
|
{resizable && (
|
|
66
|
-
<div
|
|
126
|
+
<div
|
|
127
|
+
role="none"
|
|
128
|
+
className="cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch"
|
|
129
|
+
onMouseDown={(e) =>
|
|
130
|
+
e.currentTarget.parentElement &&
|
|
131
|
+
startResize(e.currentTarget.parentElement)
|
|
132
|
+
}
|
|
133
|
+
/>
|
|
67
134
|
)}
|
|
68
135
|
</div>
|
|
69
136
|
);
|