@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.
Files changed (56) 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 +168 -93
  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/cjs/tree/TreePanel.js +14 -2
  13. package/dist/cjs/tree/useTree.js +7 -0
  14. package/dist/esm/table/AddColumn.js +56 -0
  15. package/dist/esm/table/BodyCell.js +13 -4
  16. package/dist/esm/table/CheckboxCell.js +44 -0
  17. package/dist/esm/table/Context.js +42 -0
  18. package/dist/esm/table/EmptyCell.js +52 -0
  19. package/dist/esm/table/HeaderCell.js +79 -34
  20. package/dist/esm/table/Table.js +169 -94
  21. package/dist/esm/table/types.js +30 -0
  22. package/dist/esm/table/useResizer.js +65 -0
  23. package/dist/esm/table/useTableColumns.js +46 -9
  24. package/dist/esm/table/useVirtualTable.js +43 -26
  25. package/dist/esm/tree/TreePanel.js +15 -3
  26. package/dist/esm/tree/useTree.js +7 -0
  27. package/dist/types/index.d.ts +2 -0
  28. package/dist/types/table/AddColumn.d.ts +5 -0
  29. package/dist/types/table/CheckboxCell.d.ts +4 -0
  30. package/dist/types/table/Context.d.ts +8 -0
  31. package/dist/types/table/EmptyCell.d.ts +2 -0
  32. package/dist/types/table/HeaderCell.d.ts +2 -2
  33. package/dist/types/table/Table.d.ts +1 -1
  34. package/dist/types/table/types.d.ts +30 -3
  35. package/dist/types/table/useResizer.d.ts +1 -0
  36. package/dist/types/table/useTableColumns.d.ts +6 -4
  37. package/dist/types/table/useVirtualTable.d.ts +9 -1
  38. package/dist/types/tree/TreePanel.d.ts +1 -1
  39. package/dist/types/tree/types.d.ts +6 -2
  40. package/dist/types/tree/useTree.d.ts +1 -0
  41. package/package.json +5 -5
  42. package/src/index.ts +2 -0
  43. package/src/table/AddColumn.tsx +55 -0
  44. package/src/table/BodyCell.tsx +18 -4
  45. package/src/table/CheckboxCell.tsx +48 -0
  46. package/src/table/Context.tsx +70 -0
  47. package/src/table/EmptyCell.tsx +57 -0
  48. package/src/table/HeaderCell.tsx +92 -25
  49. package/src/table/Table.tsx +178 -88
  50. package/src/table/types.ts +28 -2
  51. package/src/table/useResizer.ts +66 -0
  52. package/src/table/useTableColumns.ts +51 -18
  53. package/src/table/useVirtualTable.ts +47 -2
  54. package/src/tree/TreePanel.tsx +12 -1
  55. package/src/tree/types.ts +8 -1
  56. package/src/tree/useTree.ts +5 -0
@@ -0,0 +1,56 @@
1
+ 'use client';
2
+
3
+ import { Dropdown, Icon, CoreIcons, Menu, MenuItem } from '@react-fabric/core';
4
+ import { jsx, jsxs } from 'react/jsx-runtime';
5
+
6
+ /*
7
+ * React Fabric
8
+ * @version: 1.0.0
9
+ *
10
+ *
11
+ * The MIT License (MIT)
12
+ * Copyright (c) 2024 Adarsh Pastakia
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
15
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
16
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
21
+ * portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
24
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ */
28
+
29
+ var AddColumn = function AddColumn(_ref) {
30
+ var columns = _ref.columns,
31
+ onClick = _ref.onClick;
32
+ return /*#__PURE__*/jsx("div", {
33
+ className: "group font-medium w-6 flex flex-nowrap text-start items-end",
34
+ children: /*#__PURE__*/jsxs(Dropdown, {
35
+ placement: "bottom-end",
36
+ children: [/*#__PURE__*/jsx(Icon, {
37
+ icon: CoreIcons.insert,
38
+ className: "p-1 rounded-t rounded-b-none hover:outline"
39
+ }), /*#__PURE__*/jsx(Menu, {
40
+ className: "text-sm",
41
+ onClick: onClick,
42
+ children: columns.map(function (col) {
43
+ var _col$label;
44
+ return /*#__PURE__*/jsx(MenuItem, {
45
+ id: String(col.id),
46
+ "data-dropdown-dismiss": "false",
47
+ label: "".concat((_col$label = col.label) !== null && _col$label !== void 0 ? _col$label : String(col.id)),
48
+ icon: !col.hidden ? CoreIcons.tick : ""
49
+ }, String(col.id));
50
+ })
51
+ })]
52
+ })
53
+ });
54
+ };
55
+
56
+ export { AddColumn };
@@ -1,6 +1,9 @@
1
1
  'use client';
2
2
 
3
3
  import classNames from 'classnames';
4
+ import { useMemo } from 'react';
5
+ import { useTableContext } from './Context.js';
6
+ import { COL_DEFAULT_WIDTH, COL_MIN_WIDTH, COL_MAX_WIDTH } from './types.js';
4
7
  import { jsx } from 'react/jsx-runtime';
5
8
 
6
9
  /*
@@ -27,15 +30,21 @@ import { jsx } from 'react/jsx-runtime';
27
30
  */
28
31
 
29
32
  var BodyCell = function BodyCell(_ref) {
30
- var _column$width, _column$minWidth, _column$maxWidth, _column$renderer, _column$renderer2;
33
+ var _column$minWidth, _column$maxWidth, _column$renderer, _column$renderer2;
31
34
  var column = _ref.column,
32
35
  item = _ref.item;
36
+ var _useTableContext = useTableContext(),
37
+ widths = _useTableContext.widths;
38
+ var width = useMemo(function () {
39
+ var _widths$get;
40
+ return (_widths$get = widths.get(column.id.toString())) !== null && _widths$get !== void 0 ? _widths$get : column.width;
41
+ }, [widths, column]);
33
42
  return /*#__PURE__*/jsx("div", {
34
43
  className: "overflow-hidden bg-inherit start-0",
35
44
  style: {
36
- width: (_column$width = column.width) !== null && _column$width !== void 0 ? _column$width : "12rem",
37
- minWidth: (_column$minWidth = column.minWidth) !== null && _column$minWidth !== void 0 ? _column$minWidth : "2rem",
38
- maxWidth: (_column$maxWidth = column.maxWidth) !== null && _column$maxWidth !== void 0 ? _column$maxWidth : "32rem"
45
+ width: width !== null && width !== void 0 ? width : COL_DEFAULT_WIDTH,
46
+ minWidth: (_column$minWidth = column.minWidth) !== null && _column$minWidth !== void 0 ? _column$minWidth : COL_MIN_WIDTH,
47
+ maxWidth: (_column$maxWidth = column.maxWidth) !== null && _column$maxWidth !== void 0 ? _column$maxWidth : COL_MAX_WIDTH
39
48
  },
40
49
  children: /*#__PURE__*/jsx("div", {
41
50
  className: classNames("px-2 py-1 border-e truncate", column.align === "center" && "text-center", column.align === "end" && "text-end"),
@@ -0,0 +1,44 @@
1
+ 'use client';
2
+
3
+ import { Icon, CoreIcons } from '@react-fabric/core';
4
+ import { jsx } from 'react/jsx-runtime';
5
+
6
+ /*
7
+ * React Fabric
8
+ * @version: 1.0.0
9
+ *
10
+ *
11
+ * The MIT License (MIT)
12
+ * Copyright (c) 2024 Adarsh Pastakia
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
15
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
16
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
21
+ * portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
24
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ */
28
+
29
+ var CheckboxCell = function CheckboxCell(_ref) {
30
+ var state = _ref.state,
31
+ _onClick = _ref.onClick;
32
+ return /*#__PURE__*/jsx("div", {
33
+ className: "group font-medium border-e w-6 flex flex-nowrap text-start items-center",
34
+ children: /*#__PURE__*/jsx(Icon, {
35
+ icon: state === 2 ? CoreIcons.checkboxInt : state === 1 ? CoreIcons.checkboxOn : CoreIcons.checkboxOff,
36
+ className: "p-1 text-tint-700 hover:text-primary-600",
37
+ onClick: function onClick(e) {
38
+ return [_onClick === null || _onClick === void 0 ? void 0 : _onClick(), e.stopPropagation()];
39
+ }
40
+ })
41
+ });
42
+ };
43
+
44
+ export { CheckboxCell };
@@ -0,0 +1,42 @@
1
+ 'use client';
2
+
3
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
+ import { useRef, useState, useCallback, createContext, useContext } from 'react';
5
+ import { useResizer } from './useResizer.js';
6
+ import { jsxs, jsx } from 'react/jsx-runtime';
7
+
8
+ var TableContext = /*#__PURE__*/createContext({});
9
+ var TableProvider = function TableProvider(_ref) {
10
+ var children = _ref.children;
11
+ var ghostRef = useRef(null);
12
+ var _useState = useState(new Map()),
13
+ _useState2 = _slicedToArray(_useState, 2),
14
+ widths = _useState2[0],
15
+ setWidths = _useState2[1];
16
+ var startResize = useCallback(function (col) {
17
+ if (col) {
18
+ ghostRef.current != null && useResizer(col, ghostRef.current, function (width) {
19
+ var _col$dataset$id;
20
+ setWidths(new Map(widths.set((_col$dataset$id = col.dataset.id) !== null && _col$dataset$id !== void 0 ? _col$dataset$id : "", width)));
21
+ });
22
+ }
23
+ }, [widths]);
24
+ return /*#__PURE__*/jsxs(TableContext.Provider, {
25
+ value: {
26
+ startResize: startResize,
27
+ widths: widths
28
+ },
29
+ children: [children, /*#__PURE__*/jsx("div", {
30
+ className: "absolute inset-0 hidden z-20 cursor-col-resize",
31
+ ref: ghostRef,
32
+ children: /*#__PURE__*/jsx("div", {
33
+ className: "absolute inset-y-0 bg-muted opacity-50 border-primary border-e-2"
34
+ })
35
+ })]
36
+ });
37
+ };
38
+ var useTableContext = function useTableContext() {
39
+ return useContext(TableContext);
40
+ };
41
+
42
+ export { TableProvider, useTableContext };
@@ -0,0 +1,52 @@
1
+ 'use client';
2
+
3
+ import { useMemo } from 'react';
4
+ import { useTableContext } from './Context.js';
5
+ import { COL_DEFAULT_WIDTH, COL_MIN_WIDTH, COL_MAX_WIDTH } from './types.js';
6
+ import { jsx } from 'react/jsx-runtime';
7
+
8
+ /*
9
+ * React Fabric
10
+ * @version: 1.0.0
11
+ *
12
+ *
13
+ * The MIT License (MIT)
14
+ * Copyright (c) 2024 Adarsh Pastakia
15
+ *
16
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
17
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
18
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
19
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
20
+ * subject to the following conditions:
21
+ *
22
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
23
+ * portions of the Software.
24
+ *
25
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
26
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+
31
+ var EmptyCell = function EmptyCell(_ref) {
32
+ var id = _ref.id,
33
+ _width = _ref.width,
34
+ maxWidth = _ref.maxWidth,
35
+ minWidth = _ref.minWidth;
36
+ var _useTableContext = useTableContext(),
37
+ widths = _useTableContext.widths;
38
+ var width = useMemo(function () {
39
+ var _widths$get;
40
+ return (_widths$get = widths.get(id.toString())) !== null && _widths$get !== void 0 ? _widths$get : _width;
41
+ }, [widths, id, _width]);
42
+ return /*#__PURE__*/jsx("div", {
43
+ className: "overflow-hidden border-e",
44
+ style: {
45
+ width: width !== null && width !== void 0 ? width : COL_DEFAULT_WIDTH,
46
+ minWidth: minWidth !== null && minWidth !== void 0 ? minWidth : COL_MIN_WIDTH,
47
+ maxWidth: maxWidth !== null && maxWidth !== void 0 ? maxWidth : COL_MAX_WIDTH
48
+ }
49
+ }, "blank:".concat(id.toString()));
50
+ };
51
+
52
+ export { EmptyCell };
@@ -1,7 +1,10 @@
1
1
  'use client';
2
2
 
3
- import { Icon, Dropdown, CoreIcons, Menu, MenuItem } from '@react-fabric/core';
4
- import { jsxs, jsx } from 'react/jsx-runtime';
3
+ import { MenuItem, CoreIcons, Tooltip, Icon, DropdownTool, Menu } from '@react-fabric/core';
4
+ import { useMemo, useCallback } from 'react';
5
+ import { useTableContext } from './Context.js';
6
+ import { COL_DEFAULT_WIDTH, COL_MIN_WIDTH, COL_MAX_WIDTH } from './types.js';
7
+ import { jsx, jsxs } from 'react/jsx-runtime';
5
8
 
6
9
  /*
7
10
  * React Fabric
@@ -27,51 +30,93 @@ import { jsxs, jsx } from 'react/jsx-runtime';
27
30
  */
28
31
 
29
32
  var HeaderCell = function HeaderCell(_ref) {
30
- var label = _ref.label,
33
+ var id = _ref.id,
34
+ label = _ref.label,
31
35
  icon = _ref.icon,
32
36
  iconBg = _ref.iconBg,
33
37
  iconColor = _ref.iconColor,
34
38
  rtlFlip = _ref.rtlFlip;
35
39
  _ref.actions;
36
40
  _ref.locked;
37
- var resizable = _ref.resizable,
38
- width = _ref.width,
41
+ var sortable = _ref.sortable,
42
+ resizable = _ref.resizable,
43
+ _width = _ref.width,
39
44
  maxWidth = _ref.maxWidth,
40
- minWidth = _ref.minWidth;
45
+ minWidth = _ref.minWidth,
46
+ sort = _ref.sort,
47
+ onSort = _ref.onSort;
48
+ var _useTableContext = useTableContext(),
49
+ startResize = _useTableContext.startResize,
50
+ widths = _useTableContext.widths;
51
+ var width = useMemo(function () {
52
+ var _widths$get;
53
+ return (_widths$get = widths.get(id.toString())) !== null && _widths$get !== void 0 ? _widths$get : _width;
54
+ }, [widths, _width, id]);
55
+ var handleSort = useCallback(function () {
56
+ sortable && (onSort === null || onSort === void 0 ? void 0 : onSort({
57
+ id: id.toString(),
58
+ order: (sort === null || sort === void 0 ? void 0 : sort.id) === id && sort.order === "asc" ? "desc" : "asc"
59
+ }));
60
+ }, [onSort, name, sort, sortable]);
61
+ var menus = useMemo(function () {
62
+ var ret = [];
63
+ if (sortable) {
64
+ ret.push( /*#__PURE__*/jsx(MenuItem, {
65
+ label: "Sort ascending",
66
+ icon: CoreIcons.sortAsc
67
+ }, "sort-asc"), /*#__PURE__*/jsx(MenuItem, {
68
+ label: "Sort descending",
69
+ icon: CoreIcons.sortDesc
70
+ }, "sort-desc"));
71
+ }
72
+ return ret;
73
+ }, [sortable]);
41
74
  return /*#__PURE__*/jsxs("div", {
42
- className: "group font-medium border-e flex flex-nowrap text-start items-center",
75
+ role: "none",
76
+ "data-id": id,
77
+ onClick: handleSort,
78
+ className: "group font-medium border-e flex flex-nowrap text-start items-center table-header-cell",
43
79
  style: {
44
- width: width !== null && width !== void 0 ? width : "12rem",
45
- minWidth: minWidth !== null && minWidth !== void 0 ? minWidth : "2rem",
46
- maxWidth: maxWidth !== null && maxWidth !== void 0 ? maxWidth : "32rem"
80
+ width: width !== null && width !== void 0 ? width : COL_DEFAULT_WIDTH,
81
+ minWidth: minWidth !== null && minWidth !== void 0 ? minWidth : COL_MIN_WIDTH,
82
+ maxWidth: maxWidth !== null && maxWidth !== void 0 ? maxWidth : COL_MAX_WIDTH
47
83
  },
48
- children: [/*#__PURE__*/jsxs("div", {
49
- className: "flex-1 px-2 py-1 text-sm truncate sticky start-0",
50
- children: [icon && /*#__PURE__*/jsx(Icon, {
51
- icon: icon,
52
- bg: iconBg,
53
- color: iconColor,
54
- rtlFlip: rtlFlip
55
- }), /*#__PURE__*/jsx("span", {
56
- children: label !== null && label !== void 0 ? label : " "
57
- })]
58
- }), /*#__PURE__*/jsxs(Dropdown, {
59
- placement: "bottom-start",
60
- children: [/*#__PURE__*/jsx(Icon, {
61
- icon: CoreIcons.caretDown,
62
- className: "outline bg-base me-0.5 flex-content hidden group-hover:inline-block data-[dropdown-open]:inline-block"
63
- }), /*#__PURE__*/jsxs(Menu, {
64
- className: "text-sm",
65
- children: [/*#__PURE__*/jsx(MenuItem, {
66
- label: "Sort ascending",
67
- icon: CoreIcons.sortAsc
68
- }), /*#__PURE__*/jsx(MenuItem, {
69
- label: "Sort descending",
70
- icon: CoreIcons.sortDesc
84
+ children: [/*#__PURE__*/jsx(Tooltip, {
85
+ content: label,
86
+ disabled: !label,
87
+ children: /*#__PURE__*/jsxs("div", {
88
+ className: "flex-initial px-2 py-1 text-sm truncate sticky start-0",
89
+ children: [icon && /*#__PURE__*/jsx(Icon, {
90
+ icon: icon,
91
+ bg: iconBg,
92
+ color: iconColor,
93
+ rtlFlip: rtlFlip
94
+ }), /*#__PURE__*/jsx("span", {
95
+ children: label !== null && label !== void 0 ? label : " "
71
96
  })]
97
+ })
98
+ }), sortable && /*#__PURE__*/jsxs("div", {
99
+ className: "text-sm text-tint-300",
100
+ children: [(sort === null || sort === void 0 ? void 0 : sort.id) === id && (sort === null || sort === void 0 ? void 0 : sort.order) === "desc" && /*#__PURE__*/jsx("span", {
101
+ children: "\u25BC"
102
+ }), (sort === null || sort === void 0 ? void 0 : sort.id) === id && (sort === null || sort === void 0 ? void 0 : sort.order) === "asc" && /*#__PURE__*/jsx("span", {
103
+ children: "\u25B2"
72
104
  })]
105
+ }), /*#__PURE__*/jsx("div", {
106
+ className: "flex-1"
107
+ }), menus.length > 0 && /*#__PURE__*/jsx(DropdownTool, {
108
+ groupHover: true,
109
+ placement: "bottom-end",
110
+ children: /*#__PURE__*/jsx(Menu, {
111
+ className: "text-sm",
112
+ children: menus
113
+ })
73
114
  }), resizable && /*#__PURE__*/jsx("div", {
74
- className: "cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch"
115
+ role: "none",
116
+ className: "cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch",
117
+ onMouseDown: function onMouseDown(e) {
118
+ return e.currentTarget.parentElement && startResize(e.currentTarget.parentElement);
119
+ }
75
120
  })]
76
121
  });
77
122
  };