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

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 (44) hide show
  1. package/dist/cjs/table/AddColumn.js +56 -0
  2. package/dist/cjs/table/BodyCell.js +13 -4
  3. package/dist/cjs/table/CheckboxCell.js +44 -0
  4. package/dist/cjs/table/Context.js +43 -0
  5. package/dist/cjs/table/EmptyCell.js +52 -0
  6. package/dist/cjs/table/HeaderCell.js +77 -32
  7. package/dist/cjs/table/Table.js +136 -94
  8. package/dist/cjs/table/types.js +32 -0
  9. package/dist/cjs/table/useResizer.js +65 -0
  10. package/dist/cjs/table/useTableColumns.js +45 -8
  11. package/dist/cjs/table/useVirtualTable.js +42 -25
  12. package/dist/esm/table/AddColumn.js +56 -0
  13. package/dist/esm/table/BodyCell.js +13 -4
  14. package/dist/esm/table/CheckboxCell.js +44 -0
  15. package/dist/esm/table/Context.js +42 -0
  16. package/dist/esm/table/EmptyCell.js +52 -0
  17. package/dist/esm/table/HeaderCell.js +79 -34
  18. package/dist/esm/table/Table.js +137 -95
  19. package/dist/esm/table/types.js +30 -0
  20. package/dist/esm/table/useResizer.js +65 -0
  21. package/dist/esm/table/useTableColumns.js +46 -9
  22. package/dist/esm/table/useVirtualTable.js +43 -26
  23. package/dist/types/table/AddColumn.d.ts +5 -0
  24. package/dist/types/table/CheckboxCell.d.ts +4 -0
  25. package/dist/types/table/Context.d.ts +8 -0
  26. package/dist/types/table/EmptyCell.d.ts +2 -0
  27. package/dist/types/table/HeaderCell.d.ts +2 -2
  28. package/dist/types/table/Table.d.ts +1 -1
  29. package/dist/types/table/types.d.ts +23 -1
  30. package/dist/types/table/useResizer.d.ts +1 -0
  31. package/dist/types/table/useTableColumns.d.ts +6 -4
  32. package/dist/types/table/useVirtualTable.d.ts +9 -1
  33. package/package.json +5 -5
  34. package/src/table/AddColumn.tsx +55 -0
  35. package/src/table/BodyCell.tsx +18 -4
  36. package/src/table/CheckboxCell.tsx +48 -0
  37. package/src/table/Context.tsx +70 -0
  38. package/src/table/EmptyCell.tsx +57 -0
  39. package/src/table/HeaderCell.tsx +92 -25
  40. package/src/table/Table.tsx +149 -89
  41. package/src/table/types.ts +20 -1
  42. package/src/table/useResizer.ts +66 -0
  43. package/src/table/useTableColumns.ts +51 -18
  44. package/src/table/useVirtualTable.ts +47 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/data",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.0-beta.1",
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.0",
37
- "@react-fabric/form": "^1.0.0-beta.0",
38
- "@react-fabric/utilities": "^1.0.0-beta.0",
36
+ "@react-fabric/core": "^1.0.0-beta.1",
37
+ "@react-fabric/form": "^1.0.0-beta.1",
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": "83fa79064b1d57e71149005432192d506810bbda"
54
+ "gitHead": "e0712d7ea2c945de876bef813b5d2dcec1218ed7"
55
55
  }
@@ -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
+ };
@@ -22,7 +22,14 @@
22
22
  */
23
23
 
24
24
  import classNames from "classnames";
25
- import { type ColumnType } from "./types";
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: column.width ?? "12rem",
39
- minWidth: column.minWidth ?? "2rem",
40
- maxWidth: column.maxWidth ?? "32rem",
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
+ };
@@ -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 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
- }: ColumnType) => {
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
- className="group font-medium border-e flex flex-nowrap text-start items-center"
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 ?? "12rem",
45
- minWidth: minWidth ?? "2rem",
46
- maxWidth: maxWidth ?? "32rem",
100
+ width: width ?? COL_DEFAULT_WIDTH,
101
+ minWidth: minWidth ?? COL_MIN_WIDTH,
102
+ maxWidth: maxWidth ?? COL_MAX_WIDTH,
47
103
  }}
48
104
  >
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>
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 className="cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch" />
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
  );