@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
@@ -1,25 +1,48 @@
1
1
  'use client';
2
2
 
3
3
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
- import { useMemo, useCallback } from 'react';
4
+ import { useDebounce, Loading, EmptyContent } from '@react-fabric/core';
5
+ import classNames from 'classnames';
6
+ import { useMemo } from 'react';
7
+ import { useTranslation } from 'react-i18next';
8
+ import { AddColumn } from './AddColumn.js';
5
9
  import { BodyCell } from './BodyCell.js';
10
+ import { CheckboxCell } from './CheckboxCell.js';
11
+ import { TableProvider } from './Context.js';
12
+ import { EmptyCell } from './EmptyCell.js';
6
13
  import { HeaderCell } from './HeaderCell.js';
7
14
  import { useTableColumns } from './useTableColumns.js';
8
15
  import { useVirtualTable } from './useVirtualTable.js';
9
- import classNames from 'classnames';
10
16
  import { jsx, jsxs } from 'react/jsx-runtime';
11
17
 
12
18
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
19
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
20
  var Table = function Table(_ref) {
15
- var _columnMap$start, _columnMap$cols, _columnMap$end, _items$, _columnMap$start3, _columnMap$cols3, _columnMap$end3, _columnMap$start4, _columnMap$cols4, _columnMap$end4;
21
+ var _state$start, _state$cols, _state$end, _items$, _state$start3, _state$cols3, _state$end3;
16
22
  var data = _ref.data,
17
- columns = _ref.columns;
23
+ columns = _ref.columns,
24
+ keyProperty = _ref.keyProperty,
25
+ checkableRows = _ref.checkableRows,
26
+ hideableColumns = _ref.hideableColumns,
27
+ sort = _ref.sort,
28
+ loading = _ref.loading,
29
+ onSort = _ref.onSort,
30
+ onRowClick = _ref.onRowClick,
31
+ onCheckedChanged = _ref.onCheckedChanged;
32
+ var _useTranslation = useTranslation("data"),
33
+ t = _useTranslation.t;
34
+ var fireCheckChanged = useDebounce(function (checkedIds) {
35
+ return onCheckedChanged === null || onCheckedChanged === void 0 ? void 0 : onCheckedChanged(checkedIds);
36
+ }, []);
18
37
  var _useTableColumns = useTableColumns(columns),
19
- columnMap = _useTableColumns.columnMap;
20
- var _useVirtualTable = useVirtualTable(data),
38
+ state = _useTableColumns.state,
39
+ onHide = _useTableColumns.onHide;
40
+ var _useVirtualTable = useVirtualTable(data, keyProperty, fireCheckChanged),
21
41
  scrollerRef = _useVirtualTable.scrollerRef,
22
42
  items = _useVirtualTable.items,
43
+ checkState = _useVirtualTable.checkState,
44
+ toggleChecked = _useVirtualTable.toggleChecked,
45
+ toggleAllChecked = _useVirtualTable.toggleAllChecked,
23
46
  getData = _useVirtualTable.getData,
24
47
  totalSize = _useVirtualTable.totalSize,
25
48
  measureElement = _useVirtualTable.measureElement;
@@ -29,104 +52,123 @@ var Table = function Table(_ref) {
29
52
  var wrapperEnd = useMemo(function () {
30
53
  return "bg-inherit sticky end-0 flex flex-nowrap z-1 border-s -ms-px";
31
54
  }, []);
32
- var emptyCol = useCallback(function (col, idc) {
33
- var _col$width, _col$minWidth, _col$maxWidth;
34
- return /*#__PURE__*/jsx("div", {
35
- className: "overflow-hidden border-e",
36
- style: {
37
- width: (_col$width = col.width) !== null && _col$width !== void 0 ? _col$width : "12rem",
38
- minWidth: (_col$minWidth = col.minWidth) !== null && _col$minWidth !== void 0 ? _col$minWidth : "2rem",
39
- maxWidth: (_col$maxWidth = col.maxWidth) !== null && _col$maxWidth !== void 0 ? _col$maxWidth : "32rem"
40
- }
41
- }, "blank:".concat(idc));
42
- }, []);
43
- return /*#__PURE__*/jsxs("div", {
44
- className: "grid area-content overflow-auto relative",
45
- style: {
46
- gridTemplate: "\"head\" auto \"content\" 1fr \"foot\" auto/1fr"
47
- },
48
- ref: scrollerRef,
49
- children: [/*#__PURE__*/jsxs("div", {
50
- className: "bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2",
51
- children: [/*#__PURE__*/jsx("div", {
52
- className: wrapperStart,
53
- children: (_columnMap$start = columnMap.start) === null || _columnMap$start === void 0 ? void 0 : _columnMap$start.map(function (col, idx) {
54
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
55
- })
56
- }), (_columnMap$cols = columnMap.cols) === null || _columnMap$cols === void 0 ? void 0 : _columnMap$cols.map(function (col, idx) {
57
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
58
- }), /*#__PURE__*/jsx("div", {
59
- className: wrapperEnd,
60
- children: (_columnMap$end = columnMap.end) === null || _columnMap$end === void 0 ? void 0 : _columnMap$end.map(function (col, idx) {
61
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
62
- })
63
- })]
64
- }), /*#__PURE__*/jsxs("div", {
65
- className: "area-content flex flex-col flex-nowrap",
55
+ return /*#__PURE__*/jsx(TableProvider, {
56
+ children: /*#__PURE__*/jsxs("div", {
57
+ className: "grid area-content overflow-auto relative",
66
58
  style: {
67
- height: totalSize()
59
+ gridTemplate: "\"head\" auto \"loader\" auto \"content\" 1fr \"foot\" auto/1fr"
68
60
  },
69
- children: [/*#__PURE__*/jsx("div", {
61
+ ref: scrollerRef,
62
+ children: [/*#__PURE__*/jsxs("div", {
63
+ className: "bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2",
64
+ children: [/*#__PURE__*/jsxs("div", {
65
+ className: wrapperStart,
66
+ children: [checkableRows && /*#__PURE__*/jsx(CheckboxCell, {
67
+ state: checkState.allChecked,
68
+ onClick: toggleAllChecked
69
+ }), (_state$start = state.start) === null || _state$start === void 0 ? void 0 : _state$start.map(function (col, idx) {
70
+ return /*#__PURE__*/jsx(HeaderCell, _objectSpread(_objectSpread({}, col), {}, {
71
+ sort: sort,
72
+ onSort: onSort
73
+ }), idx);
74
+ })]
75
+ }), (_state$cols = state.cols) === null || _state$cols === void 0 ? void 0 : _state$cols.map(function (col, idx) {
76
+ return /*#__PURE__*/jsx(HeaderCell, _objectSpread(_objectSpread({}, col), {}, {
77
+ sort: sort,
78
+ onSort: onSort
79
+ }), idx);
80
+ }), /*#__PURE__*/jsxs("div", {
81
+ className: wrapperEnd,
82
+ children: [(_state$end = state.end) === null || _state$end === void 0 ? void 0 : _state$end.map(function (col, idx) {
83
+ return /*#__PURE__*/jsx(HeaderCell, _objectSpread(_objectSpread({}, col), {}, {
84
+ sort: sort,
85
+ onSort: onSort
86
+ }), idx);
87
+ }), hideableColumns && /*#__PURE__*/jsx(AddColumn, {
88
+ onClick: onHide,
89
+ columns: state.columns.filter(function (col) {
90
+ return col.hideable !== false;
91
+ })
92
+ })]
93
+ })]
94
+ }), loading && /*#__PURE__*/jsx(Loading, {}), data.length > 0 && /*#__PURE__*/jsxs("div", {
95
+ className: "area-content flex flex-col flex-nowrap",
70
96
  style: {
71
- height: (_items$ = items[0]) === null || _items$ === void 0 ? void 0 : _items$.start
72
- }
73
- }), items.map(function (_ref2) {
74
- var _columnMap$start2, _columnMap$cols2, _columnMap$end2;
75
- var index = _ref2.index,
76
- key = _ref2.key;
77
- return /*#__PURE__*/jsxs("div", {
78
- className: classNames("flex flex-nowrap", index % 2 ? "bg-even" : "bg-odd"),
79
- "data-index": index,
80
- ref: measureElement,
81
- children: [/*#__PURE__*/jsx("div", {
82
- className: wrapperStart,
83
- children: (_columnMap$start2 = columnMap.start) === null || _columnMap$start2 === void 0 ? void 0 : _columnMap$start2.map(function (col, ids) {
97
+ height: totalSize()
98
+ },
99
+ children: [/*#__PURE__*/jsx("div", {
100
+ style: {
101
+ height: (_items$ = items[0]) === null || _items$ === void 0 ? void 0 : _items$.start
102
+ }
103
+ }), items.map(function (_ref2) {
104
+ var _state$start2, _state$cols2, _state$end2;
105
+ var index = _ref2.index,
106
+ key = _ref2.key;
107
+ var data = getData(index);
108
+ return /*#__PURE__*/jsxs("div", {
109
+ role: "none",
110
+ className: classNames("flex flex-nowrap", index % 2 ? "bg-even" : "bg-odd", onRowClick && "hover:bg-primary-50 active:bg-primary-100"),
111
+ onClick: function onClick() {
112
+ return checkableRows && !onRowClick ? toggleChecked(data[keyProperty]) : onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(data);
113
+ },
114
+ "data-index": index,
115
+ ref: measureElement,
116
+ children: [/*#__PURE__*/jsxs("div", {
117
+ className: wrapperStart,
118
+ children: [checkableRows && /*#__PURE__*/jsx(CheckboxCell, {
119
+ onClick: function onClick() {
120
+ return toggleChecked(data[keyProperty]);
121
+ },
122
+ state: checkState.checked.includes(data[keyProperty]) ? 1 : 0
123
+ }), (_state$start2 = state.start) === null || _state$start2 === void 0 ? void 0 : _state$start2.map(function (col, ids) {
124
+ return /*#__PURE__*/jsx(BodyCell, {
125
+ column: col,
126
+ item: data
127
+ }, "".concat(key, ":").concat(ids));
128
+ })]
129
+ }), (_state$cols2 = state.cols) === null || _state$cols2 === void 0 ? void 0 : _state$cols2.map(function (col, idc) {
84
130
  return /*#__PURE__*/jsx(BodyCell, {
85
131
  column: col,
86
- item: getData(index)
87
- }, "".concat(key, ":").concat(ids));
88
- })
89
- }), (_columnMap$cols2 = columnMap.cols) === null || _columnMap$cols2 === void 0 ? void 0 : _columnMap$cols2.map(function (col, idc) {
90
- return /*#__PURE__*/jsx(BodyCell, {
91
- column: col,
92
- item: getData(index)
93
- }, "".concat(key, ":").concat(idc));
94
- }), /*#__PURE__*/jsx("div", {
132
+ item: data
133
+ }, "".concat(key, ":").concat(idc));
134
+ }), /*#__PURE__*/jsxs("div", {
135
+ className: wrapperEnd,
136
+ children: [(_state$end2 = state.end) === null || _state$end2 === void 0 ? void 0 : _state$end2.map(function (col, ide) {
137
+ return /*#__PURE__*/jsx(BodyCell, {
138
+ column: col,
139
+ item: data
140
+ }, "".concat(key, ":").concat(ide));
141
+ }), hideableColumns && /*#__PURE__*/jsx("div", {
142
+ className: "w-6"
143
+ })]
144
+ })]
145
+ }, key);
146
+ }), /*#__PURE__*/jsxs("div", {
147
+ className: "flex-1 flex flex-nowrap",
148
+ children: [/*#__PURE__*/jsxs("div", {
149
+ className: wrapperStart,
150
+ children: [checkableRows && /*#__PURE__*/jsx("div", {
151
+ className: "w-6"
152
+ }), (_state$start3 = state.start) === null || _state$start3 === void 0 ? void 0 : _state$start3.map(function (col) {
153
+ return /*#__PURE__*/jsx(EmptyCell, _objectSpread({}, col), String(col.id));
154
+ })]
155
+ }), (_state$cols3 = state.cols) === null || _state$cols3 === void 0 ? void 0 : _state$cols3.map(function (col) {
156
+ return /*#__PURE__*/jsx(EmptyCell, _objectSpread({}, col), String(col.id));
157
+ }), /*#__PURE__*/jsxs("div", {
95
158
  className: wrapperEnd,
96
- children: (_columnMap$end2 = columnMap.end) === null || _columnMap$end2 === void 0 ? void 0 : _columnMap$end2.map(function (col, ide) {
97
- return /*#__PURE__*/jsx(BodyCell, {
98
- column: col,
99
- item: getData(index)
100
- }, "".concat(key, ":").concat(ide));
101
- })
159
+ children: [(_state$end3 = state.end) === null || _state$end3 === void 0 ? void 0 : _state$end3.map(function (col) {
160
+ return /*#__PURE__*/jsx(EmptyCell, _objectSpread({}, col), String(col.id));
161
+ }), hideableColumns && /*#__PURE__*/jsx("div", {
162
+ className: "w-6"
163
+ })]
102
164
  })]
103
- }, key);
104
- }), /*#__PURE__*/jsxs("div", {
105
- className: "flex-1 flex flex-nowrap",
106
- children: [/*#__PURE__*/jsx("div", {
107
- className: wrapperStart,
108
- children: (_columnMap$start3 = columnMap.start) === null || _columnMap$start3 === void 0 ? void 0 : _columnMap$start3.map(emptyCol)
109
- }), (_columnMap$cols3 = columnMap.cols) === null || _columnMap$cols3 === void 0 ? void 0 : _columnMap$cols3.map(emptyCol), /*#__PURE__*/jsx("div", {
110
- className: wrapperEnd,
111
- children: (_columnMap$end3 = columnMap.end) === null || _columnMap$end3 === void 0 ? void 0 : _columnMap$end3.map(emptyCol)
112
165
  })]
166
+ }), !loading && data.length === 0 && /*#__PURE__*/jsx(EmptyContent, {
167
+ size: "sm",
168
+ className: "bg-dimmed",
169
+ message: t("datagrid.empty")
113
170
  })]
114
- }), /*#__PURE__*/jsxs("div", {
115
- className: "area-foot bg-dimmed border-t sticky bottom-0 flex flex-nowrap z-2",
116
- children: [/*#__PURE__*/jsx("div", {
117
- className: wrapperStart,
118
- children: (_columnMap$start4 = columnMap.start) === null || _columnMap$start4 === void 0 ? void 0 : _columnMap$start4.map(function (col, idx) {
119
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
120
- })
121
- }), (_columnMap$cols4 = columnMap.cols) === null || _columnMap$cols4 === void 0 ? void 0 : _columnMap$cols4.map(function (col, idx) {
122
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
123
- }), /*#__PURE__*/jsx("div", {
124
- className: wrapperEnd,
125
- children: (_columnMap$end4 = columnMap.end) === null || _columnMap$end4 === void 0 ? void 0 : _columnMap$end4.map(function (col, idx) {
126
- return /*#__PURE__*/jsx(HeaderCell, _objectSpread({}, col), idx);
127
- })
128
- })]
129
- })]
171
+ })
130
172
  });
131
173
  };
132
174
 
@@ -0,0 +1,30 @@
1
+ 'use client';
2
+
3
+ /*
4
+ * React Fabric
5
+ * @version: 1.0.0
6
+ *
7
+ *
8
+ * The MIT License (MIT)
9
+ * Copyright (c) 2024 Adarsh Pastakia
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
12
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
13
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
15
+ * subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
18
+ * portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
21
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ var COL_MIN_WIDTH = "3rem";
27
+ var COL_MAX_WIDTH = "64rem";
28
+ var COL_DEFAULT_WIDTH = "12rem";
29
+
30
+ export { COL_DEFAULT_WIDTH, COL_MAX_WIDTH, COL_MIN_WIDTH };
@@ -0,0 +1,65 @@
1
+ 'use client';
2
+
3
+ /*
4
+ * React Fabric
5
+ * @version: 1.0.0
6
+ *
7
+ *
8
+ * The MIT License (MIT)
9
+ * Copyright (c) 2024 Adarsh Pastakia
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
12
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
13
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
15
+ * subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
18
+ * portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
21
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ var useResizer = function useResizer(colEl, ghostEl, callback) {
27
+ var placeholder = ghostEl.firstElementChild;
28
+ var isRtl = getComputedStyle(colEl).direction === "rtl";
29
+ var onResize = function onResize(evt) {
30
+ /** ***************** check if reverse enabled of RTL *******************/
31
+ var box = colEl.getBoundingClientRect();
32
+ var x = isRtl ? box.left - evt.clientX : evt.clientX - box.right;
33
+ placeholder.style.width = "".concat(colEl.offsetWidth + x, "px");
34
+ };
35
+
36
+ /** ***************** dettach handlers on mouseup *******************/
37
+ var onResizeEnd = function onResizeEnd() {
38
+ callback(placeholder.offsetWidth);
39
+ ghostEl.style.display = "none";
40
+ document.removeEventListener("mousemove", onResize);
41
+ document.removeEventListener("mouseup", onResizeEnd);
42
+ };
43
+
44
+ /** ***************** attach handlers *******************/
45
+ var box = colEl.getBoundingClientRect();
46
+ if (colEl.parentElement != null) {
47
+ var _colEl$parentElement;
48
+ var parentBox = (_colEl$parentElement = colEl.parentElement) === null || _colEl$parentElement === void 0 ? void 0 : _colEl$parentElement.getBoundingClientRect();
49
+ if (isRtl) {
50
+ placeholder.style.left = "unset";
51
+ placeholder.style.right = "".concat(box.right - parentBox.right, "px");
52
+ } else {
53
+ placeholder.style.right = "unset";
54
+ placeholder.style.left = "".concat(box.left - parentBox.left, "px");
55
+ }
56
+ }
57
+ placeholder.style.width = "".concat(colEl.offsetWidth, "px");
58
+ placeholder.style.minWidth = colEl.style.minWidth || "48px";
59
+ placeholder.style.maxWidth = colEl.style.maxWidth || "75vw";
60
+ ghostEl.style.display = "block";
61
+ document.addEventListener("mousemove", onResize);
62
+ document.addEventListener("mouseup", onResizeEnd);
63
+ };
64
+
65
+ export { useResizer };
@@ -1,25 +1,62 @@
1
1
  'use client';
2
2
 
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
4
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
5
  import { groupBy } from '@react-fabric/utilities';
5
- import { useState, useEffect } from 'react';
6
+ import { useReducer, useEffect } from 'react';
6
7
 
8
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
10
  var useTableColumns = function useTableColumns(columns) {
8
- var _useState = useState({
11
+ var _useReducer = useReducer(function (state, action) {
12
+ if (action.type === "columns") {
13
+ state.columns = action.columns;
14
+ }
15
+ if (action.type === "hide") {
16
+ var col = state.columns.find(function (col) {
17
+ return col.id === action.id;
18
+ });
19
+ col && state.columns.splice(state.columns.indexOf(col), 1, _objectSpread(_objectSpread({}, col), {}, {
20
+ hidden: col.hidden !== true
21
+ }));
22
+ }
23
+ var _groupBy = groupBy(state.columns.filter(function (col) {
24
+ return col.hidden !== true;
25
+ }), "locked", "cols"),
26
+ _groupBy$start = _groupBy.start,
27
+ start = _groupBy$start === void 0 ? [] : _groupBy$start,
28
+ _groupBy$end = _groupBy.end,
29
+ end = _groupBy$end === void 0 ? [] : _groupBy$end,
30
+ _groupBy$cols = _groupBy.cols,
31
+ cols = _groupBy$cols === void 0 ? [] : _groupBy$cols;
32
+ return _objectSpread(_objectSpread({}, state), {}, {
33
+ start: start,
34
+ end: end,
35
+ cols: cols
36
+ });
37
+ }, {
38
+ columns: [],
9
39
  start: [],
10
40
  end: [],
11
41
  cols: []
12
42
  }),
13
- _useState2 = _slicedToArray(_useState, 2),
14
- columnMap = _useState2[0],
15
- setColumnMap = _useState2[1];
43
+ _useReducer2 = _slicedToArray(_useReducer, 2),
44
+ state = _useReducer2[0],
45
+ dispatch = _useReducer2[1];
16
46
  useEffect(function () {
17
- setColumnMap(groupBy(columns.filter(function (col) {
18
- return !(col.hideable && col.hidden);
19
- }), "locked", "cols"));
47
+ dispatch({
48
+ type: "columns",
49
+ columns: columns
50
+ });
20
51
  }, [columns]);
21
52
  return {
22
- columnMap: columnMap
53
+ state: state,
54
+ onHide: function onHide(id) {
55
+ return dispatch({
56
+ type: "hide",
57
+ id: id
58
+ });
59
+ }
23
60
  };
24
61
  };
25
62
 
@@ -1,32 +1,13 @@
1
1
  'use client';
2
2
 
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
3
5
  import { useVirtualizer } from '@tanstack/react-virtual';
4
- import { useRef, useCallback } from 'react';
6
+ import { useRef, useReducer, useCallback } from 'react';
5
7
 
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 useVirtualTable = function useVirtualTable(data) {
8
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
+ var useVirtualTable = function useVirtualTable(data, keyProperty, onCheckChanged) {
30
11
  // TODO: refactor data into item list, apply grouping if groupColumn provided
31
12
  // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
32
13
 
@@ -40,6 +21,30 @@ var useVirtualTable = function useVirtualTable(data) {
40
21
  return 24;
41
22
  }
42
23
  });
24
+ var _useReducer = useReducer(function (state, action) {
25
+ if (action.type === "check") {
26
+ if (state.checked.includes(action.key)) state.checked.splice(state.checked.indexOf(action.key), 1);else state.checked.push(action.key);
27
+ }
28
+ if (action.type === "checkall") {
29
+ if (state.allChecked !== 0) {
30
+ state.checked = [];
31
+ } else {
32
+ state.checked = data.map(function (item) {
33
+ return item[keyProperty];
34
+ });
35
+ }
36
+ }
37
+ onCheckChanged(state.checked);
38
+ return _objectSpread(_objectSpread({}, state), {}, {
39
+ allChecked: state.checked.length === data.length ? 1 : state.checked.length ? 2 : 0
40
+ });
41
+ }, {
42
+ checked: [],
43
+ allChecked: 0
44
+ }),
45
+ _useReducer2 = _slicedToArray(_useReducer, 2),
46
+ state = _useReducer2[0],
47
+ dispatch = _useReducer2[1];
43
48
  var virtualItems = virtualizer.getVirtualItems();
44
49
  var getData = useCallback(function (index) {
45
50
  return data[index];
@@ -47,9 +52,21 @@ var useVirtualTable = function useVirtualTable(data) {
47
52
  return {
48
53
  scrollerRef: scrollerRef,
49
54
  items: virtualItems,
55
+ checkState: state,
50
56
  getData: getData,
51
57
  totalSize: virtualizer.getTotalSize,
52
- measureElement: virtualizer.measureElement
58
+ measureElement: virtualizer.measureElement,
59
+ toggleChecked: function toggleChecked(key) {
60
+ return dispatch({
61
+ type: "check",
62
+ key: key
63
+ });
64
+ },
65
+ toggleAllChecked: function toggleAllChecked() {
66
+ return dispatch({
67
+ type: "checkall"
68
+ });
69
+ }
53
70
  };
54
71
  };
55
72
 
@@ -0,0 +1,5 @@
1
+ import { type ColumnType } from "./types";
2
+ export declare const AddColumn: ({ columns, onClick, }: {
3
+ columns: ColumnType[];
4
+ onClick: (id: string) => void;
5
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
1
+ export declare const CheckboxCell: ({ state, onClick, }: {
2
+ state: 0 | 1 | 2;
3
+ onClick?: () => void;
4
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -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 {};
@@ -0,0 +1,2 @@
1
+ import { type ColumnType } from "./types";
2
+ export declare const EmptyCell: ({ id, width: _width, maxWidth, minWidth, }: ColumnType) => import("react/jsx-runtime").JSX.Element;
@@ -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>>({ data, columns, keyProperty, checkableRows, hideableColumns, sort, loading, onSort, onRowClick, onCheckedChanged, }: TableProps<T>) => import("react/jsx-runtime").JSX.Element;
@@ -1,5 +1,8 @@
1
1
  import { type CssProp, type IconProps, 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;
@@ -22,5 +25,24 @@ export interface ColumnType<T = KeyValue> extends CssProp, IconProps, TestProps
22
25
  export interface TableProps<T = KeyValue> {
23
26
  columns: Array<ColumnType<T>>;
24
27
  data: T[];
25
- autoColumns?: boolean;
28
+ keyProperty: keyof T;
29
+ /**
30
+ * checkable rows
31
+ */
32
+ checkableRows?: boolean;
33
+ onCheckedChanged?: (rows: Array<keyof T>) => void;
34
+ /**
35
+ * sorting
36
+ */
37
+ sort?: {
38
+ id: string;
39
+ order: "asc" | "desc";
40
+ };
41
+ onSort?: (sort: {
42
+ id: string;
43
+ order: "asc" | "desc";
44
+ }) => void;
45
+ loading?: boolean;
46
+ hideableColumns?: boolean;
47
+ onRowClick?: (data: T) => void;
26
48
  }
@@ -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
- columnMap: {
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
- export declare const useVirtualTable: (data: KeyValue[]) => {
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 {};