@zhenliang/sheet 0.0.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 (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/core/config.js +13 -0
  4. package/dist/core/editor/cascaderEditor/index.js +63 -0
  5. package/dist/core/editor/cascaderEditor/index.less +24 -0
  6. package/dist/core/editor/dateEditor/index.js +42 -0
  7. package/dist/core/editor/dateEditor/index.less +7 -0
  8. package/dist/core/editor/numberEditor/index.js +25 -0
  9. package/dist/core/editor/numberEditor/index.less +17 -0
  10. package/dist/core/editor/selectEditor/index.js +44 -0
  11. package/dist/core/editor/selectEditor/index.less +41 -0
  12. package/dist/core/reducers/index.js +36 -0
  13. package/dist/core/reducers/keyboardReducer.js +91 -0
  14. package/dist/core/reducers/mouseReducer.js +151 -0
  15. package/dist/core/reducers/sideEffectReducer.js +324 -0
  16. package/dist/core/reducers/stateReducer.js +140 -0
  17. package/dist/core/sheet/Cell.js +160 -0
  18. package/dist/core/sheet/DataEditor.js +21 -0
  19. package/dist/core/sheet/DefaultCell.js +31 -0
  20. package/dist/core/sheet/DefaultRow.js +25 -0
  21. package/dist/core/sheet/DefaultRowMapper.js +15 -0
  22. package/dist/core/sheet/DefaultShell.js +12 -0
  23. package/dist/core/sheet/Event.js +17 -0
  24. package/dist/core/sheet/ValueViewer.js +9 -0
  25. package/dist/core/sheet/index.js +201 -0
  26. package/dist/core/sheet/index.less +261 -0
  27. package/dist/core/sheet/useCellEvent.js +22 -0
  28. package/dist/core/sheet/useContextMenu.js +86 -0
  29. package/dist/core/sheet/useKeyBoardEvent.js +44 -0
  30. package/dist/core/sheet/useMouseEvent.js +298 -0
  31. package/dist/core/sheet/useVirtualList.js +74 -0
  32. package/dist/core/sheet/var.less +41 -0
  33. package/dist/core/shell/draggableShell/index.js +182 -0
  34. package/dist/core/shell/draggableShell/index.less +16 -0
  35. package/dist/core/shell/resizeShell.js +57 -0
  36. package/dist/core/shell/tableShell.js +105 -0
  37. package/dist/core/table/index.js +311 -0
  38. package/dist/core/table/useGroupConfig.js +32 -0
  39. package/dist/core/table/useRowSelection.js +24 -0
  40. package/dist/core/table/util.js +29 -0
  41. package/dist/core/util.js +363 -0
  42. package/dist/core/viewer/checkViewer/index.js +23 -0
  43. package/dist/core/viewer/groupViewer/index.js +28 -0
  44. package/dist/example/basic.js +120 -0
  45. package/dist/example/draggable.js +0 -0
  46. package/dist/example/ellipsis.js +63 -0
  47. package/dist/example/fixed.js +88 -0
  48. package/dist/example/group.js +84 -0
  49. package/dist/example/index.less +5 -0
  50. package/dist/example/selection.js +85 -0
  51. package/dist/example/sheet.js +356 -0
  52. package/dist/hooks/index.js +6 -0
  53. package/dist/hooks/useEventBus.js +17 -0
  54. package/dist/hooks/useKeyboard.js +108 -0
  55. package/dist/hooks/useMiddlewareReducer.js +47 -0
  56. package/dist/hooks/useMouse.js +52 -0
  57. package/dist/hooks/useSetState.js +27 -0
  58. package/dist/hooks/useSheetEvent.js +5 -0
  59. package/dist/index.js +3 -0
  60. package/dist/standardUtils/index.js +32 -0
  61. package/dist/typings/index.d.ts +1 -0
  62. package/dist/typings/sheet.d.ts +209 -0
  63. package/dist/typings/table.js +0 -0
  64. package/package.json +84 -0
@@ -0,0 +1,182 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
3
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
4
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
5
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
10
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
11
+ import { useEffect, useMemo, useRef } from 'react';
12
+ import { useSetState } from "../../../hooks";
13
+ import { classNames } from "../../util";
14
+ import { CheckViewer } from "../../viewer/checkViewer";
15
+ import { GroupViewer } from "../../viewer/groupViewer";
16
+ import "./index.less";
17
+ import { jsx as _jsx } from "react/jsx-runtime";
18
+ import { jsxs as _jsxs } from "react/jsx-runtime";
19
+ import { Fragment as _Fragment } from "react/jsx-runtime";
20
+ export var DraggableShell = function DraggableShell(_ref) {
21
+ var columns = _ref.columns,
22
+ className = _ref.className,
23
+ showGroup = _ref.showGroup,
24
+ showSelect = _ref.showSelect,
25
+ controlProps = _ref.controlProps,
26
+ _ref$controlWidth = _ref.controlWidth,
27
+ controlWidth = _ref$controlWidth === void 0 ? 25 : _ref$controlWidth;
28
+ var TableShell = function TableShell(_ref2) {
29
+ var children = _ref2.children;
30
+ var headRef = useRef(null);
31
+ var downRef = useRef(null);
32
+ var _useSetState = useSetState({}),
33
+ _useSetState2 = _slicedToArray(_useSetState, 2),
34
+ widths = _useSetState2[0],
35
+ setWidth = _useSetState2[1];
36
+ var hasControl = showGroup || showSelect;
37
+ var thItems = useMemo(function () {
38
+ var ths = [];
39
+ if (hasControl) {
40
+ var _controlProps$group, _controlProps$check, _controlProps$check2, _controlProps$check3;
41
+ ths.push( /*#__PURE__*/_jsxs("th", {
42
+ className: "cell cell-title read-only sheet-control",
43
+ children: [showGroup && /*#__PURE__*/_jsx(GroupViewer, {
44
+ row: -1,
45
+ col: -1,
46
+ value: true,
47
+ record: {
48
+ open: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$group = controlProps.group) === null || _controlProps$group === void 0 ? void 0 : _controlProps$group.open,
49
+ isHeader: true
50
+ }
51
+ }), showSelect && /*#__PURE__*/_jsx(CheckViewer, {
52
+ row: -1,
53
+ col: -1,
54
+ value: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check = controlProps.check) === null || _controlProps$check === void 0 ? void 0 : _controlProps$check.checked,
55
+ record: {
56
+ open: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check2 = controlProps.check) === null || _controlProps$check2 === void 0 ? void 0 : _controlProps$check2.checked,
57
+ isHeader: true,
58
+ indeterminate: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check3 = controlProps.check) === null || _controlProps$check3 === void 0 ? void 0 : _controlProps$check3.indeterminate
59
+ }
60
+ })]
61
+ }, "-1"));
62
+ }
63
+ columns.forEach(function (item, index) {
64
+ var _item$dataIndex, _ref3;
65
+ ths.push( /*#__PURE__*/_jsx("th", {
66
+ className: classNames('cell', 'cell-title', 'read-only', item.fixed && 'fixed', item.fixed && "fixed-".concat(item.fixed)),
67
+ style: {
68
+ textAlign: (_ref3 = item.align) !== null && _ref3 !== void 0 ? _ref3 : 'unset',
69
+ left: item.fixed === 'left' ? 0 : 'unset',
70
+ right: item.fixed === 'right' ? 0 : 'unset'
71
+ },
72
+ onMouseDown: function onMouseDown(e) {
73
+ var target = e.target;
74
+ downRef.current = target;
75
+ if (e.nativeEvent.offsetX > target.offsetWidth - 10) {
76
+ downRef.current.mouseDown = true;
77
+ downRef.current.oldX = e.nativeEvent.x;
78
+ downRef.current.oldWidth = downRef.current.offsetWidth;
79
+ } else {
80
+ downRef.current = null;
81
+ }
82
+ },
83
+ onMouseMove: function onMouseMove(e) {
84
+ var target = e.target;
85
+ if (e.nativeEvent.offsetX > target.offsetWidth - 10) {
86
+ target.style.cursor = 'col-resize';
87
+ } else {
88
+ target.style.cursor = 'default';
89
+ }
90
+ //取出暂存的Table Cell
91
+ if (downRef.current === undefined) downRef.current = target;
92
+ //调整宽度
93
+ },
94
+ children: item.title
95
+ }, (_item$dataIndex = item.dataIndex) !== null && _item$dataIndex !== void 0 ? _item$dataIndex : index));
96
+ });
97
+ return ths;
98
+ }, [columns]);
99
+ var colItems = useMemo(function () {
100
+ var cols = [];
101
+ if (hasControl) {
102
+ cols.push( /*#__PURE__*/_jsx("col", {
103
+ className: classNames('sheet-control'),
104
+ style: {
105
+ width: controlWidth
106
+ }
107
+ }, "sheet-control"));
108
+ }
109
+ columns.forEach(function (item, index) {
110
+ var _item$dataIndex2;
111
+ cols.push( /*#__PURE__*/_jsx("col", {
112
+ className: classNames('cell'),
113
+ style: {
114
+ width: widths[index] || item.width || 'unset'
115
+ }
116
+ }, (_item$dataIndex2 = item.dataIndex) !== null && _item$dataIndex2 !== void 0 ? _item$dataIndex2 : index));
117
+ });
118
+ return cols;
119
+ }, [widths, hasControl, columns]);
120
+ useEffect(function () {
121
+ var handleMouseUp = function handleMouseUp() {
122
+ //结束宽度调整
123
+ if (downRef.current) {
124
+ downRef.current.mouseDown = false;
125
+ downRef.current.style.cursor = 'default';
126
+ }
127
+ };
128
+ var handleMouseMove = function handleMouseMove(e) {
129
+ var _downRef$current, _downRef$current2;
130
+ if (!downRef.current || !downRef.current.mouseDown) return;
131
+ if (((_downRef$current = downRef.current) === null || _downRef$current === void 0 ? void 0 : _downRef$current.mouseDown) !== null && ((_downRef$current2 = downRef.current) === null || _downRef$current2 === void 0 ? void 0 : _downRef$current2.mouseDown) === true) {
132
+ var _downRef$current$oldW, _downRef$current$oldX;
133
+ downRef.current.style.cursor = 'default';
134
+ if (((_downRef$current$oldW = downRef.current.oldWidth) !== null && _downRef$current$oldW !== void 0 ? _downRef$current$oldW : 0) + (e.x - ((_downRef$current$oldX = downRef.current.oldX) !== null && _downRef$current$oldX !== void 0 ? _downRef$current$oldX : 0)) > 0) {
135
+ var _downRef$current$oldW2, _downRef$current$oldX2, _headRef$current;
136
+ var newWidth = Math.max(Number(((_downRef$current$oldW2 = downRef.current.oldWidth) !== null && _downRef$current$oldW2 !== void 0 ? _downRef$current$oldW2 : 0) + (e.x - ((_downRef$current$oldX2 = downRef.current.oldX) !== null && _downRef$current$oldX2 !== void 0 ? _downRef$current$oldX2 : 0))), 50);
137
+ var cellList = [].slice.call((_headRef$current = headRef.current) === null || _headRef$current === void 0 ? void 0 : _headRef$current.cells);
138
+ var changeIndex = cellList.indexOf(downRef.current);
139
+ //调整该列中的每个Cell
140
+ var _widths = _defineProperty({
141
+ length: columns.length
142
+ }, hasControl ? changeIndex - 1 : changeIndex, "".concat(newWidth, "px"));
143
+ setWidth(_widths);
144
+ }
145
+ }
146
+ };
147
+ document.addEventListener('mouseup', handleMouseUp);
148
+ document.addEventListener('mousemove', handleMouseMove);
149
+ return function () {
150
+ document.removeEventListener('mouseup', handleMouseUp);
151
+ document.removeEventListener('mousemove', handleMouseMove);
152
+ };
153
+ }, [hasControl]);
154
+ return /*#__PURE__*/_jsxs(_Fragment, {
155
+ children: [/*#__PURE__*/_jsxs("table", {
156
+ className: classNames('header', 'harvest-sheet', className),
157
+ style: {
158
+ position: 'sticky',
159
+ top: 0,
160
+ zIndex: 3
161
+ },
162
+ children: [/*#__PURE__*/_jsx("colgroup", {
163
+ children: colItems
164
+ }), /*#__PURE__*/_jsx("thead", {
165
+ children: /*#__PURE__*/_jsx("tr", {
166
+ ref: headRef,
167
+ children: thItems
168
+ })
169
+ })]
170
+ }, "header"), /*#__PURE__*/_jsxs("table", {
171
+ className: classNames('body', 'harvest-sheet'),
172
+ children: [/*#__PURE__*/_jsx("colgroup", {
173
+ className: "header",
174
+ children: colItems
175
+ }), /*#__PURE__*/_jsx("tbody", {
176
+ children: children
177
+ }, "tbody")]
178
+ }, "body")]
179
+ });
180
+ };
181
+ return TableShell;
182
+ };
@@ -0,0 +1,16 @@
1
+ .left{
2
+ text-align: left;
3
+ }
4
+
5
+ .right{
6
+ text-align: right;
7
+ }
8
+
9
+ .center{
10
+ text-align: center;
11
+ }
12
+
13
+ .sheet-cell-fixed{
14
+ position: sticky;
15
+ z-index: 2;
16
+ }
@@ -0,0 +1,57 @@
1
+ import { useMemo } from 'react';
2
+ import { classNames } from "./util";
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ import { jsxs as _jsxs } from "react/jsx-runtime";
5
+ import { Fragment as _Fragment } from "react/jsx-runtime";
6
+ export var WrapperShell = function WrapperShell(_ref) {
7
+ var columns = _ref.columns,
8
+ className = _ref.className;
9
+ var TableShell = function TableShell(_ref2) {
10
+ var children = _ref2.children;
11
+ var _useMemo = useMemo(function () {
12
+ var thItems = columns.map(function (item) {
13
+ var _item$width;
14
+ return /*#__PURE__*/_jsx("th", {
15
+ className: "cell read-only",
16
+ style: {
17
+ width: (_item$width = item.width) !== null && _item$width !== void 0 ? _item$width : 'unset'
18
+ },
19
+ children: item.title
20
+ }, item.dataIndex);
21
+ });
22
+ var colItems = columns.map(function (item) {
23
+ var _item$width2;
24
+ return /*#__PURE__*/_jsx("col", {
25
+ className: "cell",
26
+ style: {
27
+ width: (_item$width2 = item.width) !== null && _item$width2 !== void 0 ? _item$width2 : 'unset'
28
+ }
29
+ }, item.dataIndex);
30
+ });
31
+ return {
32
+ thItems: thItems,
33
+ colItems: colItems
34
+ };
35
+ }, [columns]),
36
+ thItems = _useMemo.thItems,
37
+ colItems = _useMemo.colItems;
38
+ return /*#__PURE__*/_jsxs(_Fragment, {
39
+ children: [/*#__PURE__*/_jsx("table", {
40
+ className: classNames('header', 'harvest-sheet', className),
41
+ children: /*#__PURE__*/_jsx("thead", {
42
+ children: /*#__PURE__*/_jsx("tr", {
43
+ children: thItems
44
+ })
45
+ })
46
+ }, "header"), /*#__PURE__*/_jsxs("table", {
47
+ className: classNames('body', 'harvest-sheet'),
48
+ children: [/*#__PURE__*/_jsx("colgroup", {
49
+ children: colItems
50
+ }), /*#__PURE__*/_jsx("tbody", {
51
+ children: children
52
+ }, "tbody")]
53
+ }, "body")]
54
+ });
55
+ };
56
+ return TableShell;
57
+ };
@@ -0,0 +1,105 @@
1
+ import { useMemo } from 'react';
2
+ import { classNames } from "../util";
3
+ import { CheckViewer } from "../viewer/checkViewer";
4
+ import { GroupViewer } from "../viewer/groupViewer";
5
+ import "./draggableShell/index.less";
6
+ import { jsx as _jsx } from "react/jsx-runtime";
7
+ import { jsxs as _jsxs } from "react/jsx-runtime";
8
+ import { Fragment as _Fragment } from "react/jsx-runtime";
9
+ export var TableShell = function TableShell(_ref) {
10
+ var columns = _ref.columns,
11
+ className = _ref.className,
12
+ showGroup = _ref.showGroup,
13
+ showSelect = _ref.showSelect,
14
+ controlProps = _ref.controlProps,
15
+ _ref$controlWidth = _ref.controlWidth,
16
+ controlWidth = _ref$controlWidth === void 0 ? 25 : _ref$controlWidth;
17
+ var TableShell = function TableShell(_ref2) {
18
+ var children = _ref2.children;
19
+ var thItems = useMemo(function () {
20
+ var ths = [];
21
+ if (showGroup || showSelect) {
22
+ var _controlProps$group, _controlProps$check, _controlProps$check2, _controlProps$check3;
23
+ ths.push( /*#__PURE__*/_jsxs("th", {
24
+ className: "cell cell-title read-only sheet-control",
25
+ children: [showGroup && /*#__PURE__*/_jsx(GroupViewer, {
26
+ row: -1,
27
+ col: -1,
28
+ value: true,
29
+ record: {
30
+ open: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$group = controlProps.group) === null || _controlProps$group === void 0 ? void 0 : _controlProps$group.open,
31
+ isHeader: true
32
+ }
33
+ }), showSelect && /*#__PURE__*/_jsx(CheckViewer, {
34
+ row: -1,
35
+ col: -1,
36
+ value: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check = controlProps.check) === null || _controlProps$check === void 0 ? void 0 : _controlProps$check.checked,
37
+ record: {
38
+ open: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check2 = controlProps.check) === null || _controlProps$check2 === void 0 ? void 0 : _controlProps$check2.checked,
39
+ isHeader: true,
40
+ indeterminate: controlProps === null || controlProps === void 0 ? void 0 : (_controlProps$check3 = controlProps.check) === null || _controlProps$check3 === void 0 ? void 0 : _controlProps$check3.indeterminate
41
+ }
42
+ })]
43
+ }, "-1"));
44
+ }
45
+ columns.forEach(function (item, index) {
46
+ var _ref3;
47
+ ths.push( /*#__PURE__*/_jsx("th", {
48
+ className: classNames('cell', 'cell-title', 'read-only', item.fixed && 'fixed', item.fixed && "fixed-".concat(item.fixed)),
49
+ style: {
50
+ textAlign: (_ref3 = item.align) !== null && _ref3 !== void 0 ? _ref3 : 'unset',
51
+ left: item.fixed === 'left' ? 0 : 'unset',
52
+ right: item.fixed === 'right' ? 0 : 'unset'
53
+ },
54
+ children: item.title
55
+ }, item.dataIndex || index));
56
+ });
57
+ return ths;
58
+ }, [columns, showGroup || showSelect, controlProps]);
59
+ var colItems = useMemo(function () {
60
+ var cols = [];
61
+ if (showGroup || showSelect) {
62
+ cols.push( /*#__PURE__*/_jsx("col", {
63
+ className: classNames('sheet-control'),
64
+ style: {
65
+ width: controlWidth
66
+ }
67
+ }, "sheet-control"));
68
+ }
69
+ columns.forEach(function (item, index) {
70
+ cols.push( /*#__PURE__*/_jsx("col", {
71
+ className: classNames('cell'),
72
+ style: {
73
+ width: item.width || 'unset'
74
+ }
75
+ }, item.dataIndex || index));
76
+ });
77
+ return cols;
78
+ }, [columns, showGroup || showSelect, controlWidth]);
79
+ return /*#__PURE__*/_jsxs(_Fragment, {
80
+ children: [/*#__PURE__*/_jsxs("table", {
81
+ className: classNames('header', 'harvest-sheet', className),
82
+ style: {
83
+ position: 'sticky',
84
+ top: 0,
85
+ zIndex: 3
86
+ },
87
+ children: [/*#__PURE__*/_jsx("colgroup", {
88
+ children: colItems
89
+ }), /*#__PURE__*/_jsx("thead", {
90
+ children: /*#__PURE__*/_jsx("tr", {
91
+ children: thItems
92
+ })
93
+ })]
94
+ }, "header"), /*#__PURE__*/_jsxs("table", {
95
+ className: classNames('body', 'harvest-sheet'),
96
+ children: [/*#__PURE__*/_jsx("colgroup", {
97
+ children: colItems
98
+ }), /*#__PURE__*/_jsx("tbody", {
99
+ children: children
100
+ }, "tbody")]
101
+ }, "body")]
102
+ });
103
+ };
104
+ return TableShell;
105
+ };
@@ -0,0 +1,311 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ var _excluded = ["columns", "dataSource", "rowKey", "rowSelection", "groupConfig", "onChange", "draggable"];
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
7
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
8
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
9
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
10
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
11
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
12
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
13
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
16
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
17
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
18
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
19
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
20
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
21
+ import Sheet from "../sheet";
22
+ import { SheetEvent } from "../sheet/Event";
23
+ import { DraggableShell } from "../shell/draggableShell";
24
+ import { TableShell } from "../shell/tableShell";
25
+ import { groupConfigToGroupMap } from "../util";
26
+ import { CheckViewer } from "../viewer/checkViewer";
27
+ import { GroupViewer } from "../viewer/groupViewer";
28
+ import { useGroupConfig } from "./useGroupConfig";
29
+ import { useRowSelection } from "./useRowSelection";
30
+ import { jsx as _jsx } from "react/jsx-runtime";
31
+ import { jsxs as _jsxs } from "react/jsx-runtime";
32
+ var Table = function Table(_ref) {
33
+ var columns = _ref.columns,
34
+ dataSource = _ref.dataSource,
35
+ rowKey = _ref.rowKey,
36
+ rowSelection = _ref.rowSelection,
37
+ groupConfig = _ref.groupConfig,
38
+ onChange = _ref.onChange,
39
+ draggable = _ref.draggable,
40
+ args = _objectWithoutProperties(_ref, _excluded);
41
+ var _useState = useState([[]]),
42
+ _useState2 = _slicedToArray(_useState, 2),
43
+ data = _useState2[0],
44
+ setData = _useState2[1];
45
+ var sheetInstance = useRef(null);
46
+ var hasChildren = dataSource === null || dataSource === void 0 ? void 0 : dataSource.some(function (item) {
47
+ var _item$children;
48
+ return (item === null || item === void 0 ? void 0 : (_item$children = item.children) === null || _item$children === void 0 ? void 0 : _item$children.length) > 0;
49
+ });
50
+ var hasControl = hasChildren || rowSelection;
51
+ var _useRowSelection = useRowSelection(dataSource, rowSelection, hasChildren),
52
+ _useRowSelection2 = _slicedToArray(_useRowSelection, 2),
53
+ checkedRow = _useRowSelection2[0],
54
+ setCheckedRow = _useRowSelection2[1];
55
+ var _useGroupConfig = useGroupConfig(dataSource, {
56
+ defaultOpen: true
57
+ }, hasChildren),
58
+ _useGroupConfig2 = _slicedToArray(_useGroupConfig, 2),
59
+ rowGroupConfig = _useGroupConfig2[0],
60
+ setGroupConfig = _useGroupConfig2[1];
61
+ var _ref2 = rowGroupConfig || {},
62
+ groups = _ref2.groups,
63
+ groupOpen = _ref2.groupOpen;
64
+ useEffect(function () {
65
+ if (!hasChildren) return;
66
+ if (!dataSource || !columns) return;
67
+ var data = [];
68
+ var groupMap = groupConfigToGroupMap({
69
+ groups: groups,
70
+ groupOpen: groupOpen
71
+ });
72
+ var currentIndex = 0;
73
+ dataSource.forEach(function (item, row) {
74
+ var groupList = [item];
75
+ if (item.children) {
76
+ groupList = [item].concat(_toConsumableArray(item.children));
77
+ }
78
+ groupList.forEach(function (itemRow) {
79
+ var _groupMap$get, _groupMap$get2;
80
+ var dataRow = [];
81
+ var rowId = item.key || item.id || String(currentIndex);
82
+ if (rowKey) {
83
+ if (rowKey instanceof Function) {
84
+ rowId = rowKey(item, row);
85
+ } else {
86
+ rowId = item[rowKey];
87
+ }
88
+ }
89
+ dataRow.push({
90
+ id: rowId,
91
+ row: currentIndex,
92
+ col: -1,
93
+ readonly: true,
94
+ editable: false,
95
+ value: groupMap.get(currentIndex) && ((_groupMap$get = groupMap.get(currentIndex)) === null || _groupMap$get === void 0 ? void 0 : _groupMap$get.isStart),
96
+ record: {
97
+ open: groupMap.get(currentIndex) && ((_groupMap$get2 = groupMap.get(currentIndex)) === null || _groupMap$get2 === void 0 ? void 0 : _groupMap$get2.isOpen)
98
+ },
99
+ valueViewer: GroupViewer,
100
+ className: 'sheet-control'
101
+ });
102
+ columns.forEach(function (colInfo, col) {
103
+ var value = itemRow[colInfo.dataIndex || ''];
104
+ dataRow.push({
105
+ id: rowId,
106
+ value: value,
107
+ record: itemRow,
108
+ readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, itemRow, currentIndex),
109
+ align: colInfo.align,
110
+ fixed: colInfo.fixed,
111
+ editable: colInfo.editable,
112
+ valueViewer: colInfo.render ? colInfo.render : undefined,
113
+ dataEditor: colInfo.editor ? colInfo.editor : undefined,
114
+ row: currentIndex,
115
+ col: col
116
+ });
117
+ });
118
+ data.push(dataRow);
119
+ currentIndex++;
120
+ });
121
+ });
122
+ setData(data);
123
+ }, [dataSource, columns, groups, hasChildren, rowGroupConfig]);
124
+ useEffect(function () {
125
+ if (hasChildren) return;
126
+ if (!dataSource || !columns) return;
127
+ setData(dataSource.map(function (item, row) {
128
+ var rowId = String(row);
129
+ if (rowKey) {
130
+ if (rowKey instanceof Function) {
131
+ rowId = rowKey(item, row);
132
+ } else {
133
+ rowId = item[rowKey];
134
+ }
135
+ }
136
+ var rows = [];
137
+ if (rowSelection) {
138
+ rows.push({
139
+ id: rowId,
140
+ row: row,
141
+ col: -1,
142
+ readonly: true,
143
+ value: checkedRow[row],
144
+ valueViewer: CheckViewer,
145
+ className: 'sheet-control'
146
+ });
147
+ }
148
+ columns.forEach(function (colInfo, col) {
149
+ var value = item[colInfo.dataIndex || ''];
150
+ rows.push({
151
+ id: rowId,
152
+ value: value,
153
+ record: item,
154
+ readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, item, row),
155
+ align: colInfo.align,
156
+ fixed: colInfo.fixed,
157
+ editable: colInfo.editable,
158
+ valueViewer: colInfo.render ? colInfo.render : undefined,
159
+ dataEditor: colInfo.editor ? colInfo.editor : undefined,
160
+ row: row,
161
+ col: col
162
+ });
163
+ });
164
+ return rows;
165
+ }));
166
+ }, [dataSource, columns, checkedRow, hasChildren]);
167
+ useEffect(function () {
168
+ if (!dataSource || !columns) {
169
+ setData([[]]);
170
+ }
171
+ }, [dataSource, columns]);
172
+ var handleChanges = useCallback(function (changes) {
173
+ onChange && onChange(changes.map(function (item) {
174
+ return {
175
+ row: item.row,
176
+ id: item.id,
177
+ key: columns[hasControl ? item.col - 1 : item.col].dataIndex,
178
+ value: item.value
179
+ };
180
+ }));
181
+ }, [columns, onChange, hasControl]);
182
+ var handleReverse = useCallback(function (value) {
183
+ var _ref3 = value,
184
+ type = _ref3.type,
185
+ extraInfo = _ref3.extraInfo;
186
+ if (type === 'Custom') {
187
+ var _ref4 = extraInfo,
188
+ _groupConfig = _ref4.groupConfig,
189
+ extraType = _ref4.extraType,
190
+ lastData = _ref4.data;
191
+ if (extraType === 'group') {
192
+ setGroupConfig(_groupConfig);
193
+ setData(lastData);
194
+ }
195
+ }
196
+ }, [data, groupConfig]);
197
+ var handleRowSelect = useCallback(function (value) {
198
+ if (!sheetInstance.current) return;
199
+ // sheetInstance.current?.selectRow(value as number);
200
+ var newChecked = Array(checkedRow.length).fill(false);
201
+ newChecked[value] = !newChecked[value];
202
+ setCheckedRow(newChecked);
203
+ }, [sheetInstance, checkedRow]);
204
+ var WrappedTableShell = useMemo(function () {
205
+ var _rowGroupConfig$group2;
206
+ if (draggable) {
207
+ var _rowGroupConfig$group;
208
+ return DraggableShell({
209
+ columns: columns,
210
+ className: 'baseTable',
211
+ showGroup: hasChildren,
212
+ showSelect: !!rowSelection,
213
+ controlProps: {
214
+ group: {
215
+ open: !(rowGroupConfig !== null && rowGroupConfig !== void 0 && (_rowGroupConfig$group = rowGroupConfig.groupOpen) !== null && _rowGroupConfig$group !== void 0 && _rowGroupConfig$group.some(function (value) {
216
+ return !value;
217
+ }))
218
+ },
219
+ check: {
220
+ checked: false
221
+ }
222
+ }
223
+ });
224
+ }
225
+ return TableShell({
226
+ columns: columns,
227
+ className: 'baseTable',
228
+ showGroup: hasChildren,
229
+ showSelect: !!rowSelection,
230
+ controlProps: {
231
+ group: {
232
+ open: !(rowGroupConfig !== null && rowGroupConfig !== void 0 && (_rowGroupConfig$group2 = rowGroupConfig.groupOpen) !== null && _rowGroupConfig$group2 !== void 0 && _rowGroupConfig$group2.some(function (value) {
233
+ return !value;
234
+ }))
235
+ },
236
+ check: {
237
+ checked: false
238
+ }
239
+ }
240
+ });
241
+ }, [columns, draggable, rowSelection, hasChildren, rowGroupConfig]);
242
+ return /*#__PURE__*/_jsxs(Sheet, _objectSpread(_objectSpread({}, args), {}, {
243
+ sheetInstance: sheetInstance,
244
+ sheetRenderer: WrappedTableShell,
245
+ groupConfig: rowGroupConfig,
246
+ data: data,
247
+ onCellsChanged: handleChanges,
248
+ children: [!hasChildren && rowSelection ? [/*#__PURE__*/_jsx(SheetEvent, {
249
+ name: "row-select",
250
+ handler: handleRowSelect
251
+ }, "row-select"), /*#__PURE__*/_jsx(SheetEvent, {
252
+ name: "row-select-title",
253
+ handler: handleRowSelect
254
+ }, "row-select-title")] : null, hasChildren ? [/*#__PURE__*/_jsx(SheetEvent, {
255
+ name: "group-open",
256
+ handler: function handler(e) {
257
+ var _ref5 = e,
258
+ row = _ref5.row;
259
+ var index = groups.findIndex(function (item) {
260
+ return item.groupStart === row;
261
+ });
262
+ if (index >= 0) {
263
+ var _sheetInstance$curren;
264
+ var _groupOpen = _toConsumableArray(rowGroupConfig.groupOpen);
265
+ _groupOpen[index] = !rowGroupConfig.groupOpen[index];
266
+ setGroupConfig(_objectSpread(_objectSpread({}, rowGroupConfig), {}, {
267
+ groupOpen: _groupOpen
268
+ }));
269
+ var newGrid = _toConsumableArray(data);
270
+ newGrid[row] = _toConsumableArray(newGrid[row]);
271
+ newGrid[row][0] = _objectSpread(_objectSpread({}, newGrid[row][0]), {}, {
272
+ record: {
273
+ open: !!_groupOpen[index]
274
+ }
275
+ });
276
+ setData(newGrid);
277
+ (_sheetInstance$curren = sheetInstance.current) === null || _sheetInstance$curren === void 0 ? void 0 : _sheetInstance$curren.pushToHistory({
278
+ type: 'Custom',
279
+ changes: [],
280
+ extraInfo: {
281
+ extraType: 'group',
282
+ groupConfig: rowGroupConfig,
283
+ data: data
284
+ }
285
+ });
286
+ }
287
+ }
288
+ }, "group-open"), /*#__PURE__*/_jsx(SheetEvent, {
289
+ name: "group-open-title",
290
+ handler: function handler(value) {
291
+ var _sheetInstance$curren2;
292
+ setGroupConfig(_objectSpread(_objectSpread({}, rowGroupConfig), {}, {
293
+ groupOpen: Array(rowGroupConfig.groupOpen.length).fill(value)
294
+ }));
295
+ (_sheetInstance$curren2 = sheetInstance.current) === null || _sheetInstance$curren2 === void 0 ? void 0 : _sheetInstance$curren2.pushToHistory({
296
+ type: 'Custom',
297
+ changes: [],
298
+ extraInfo: {
299
+ extraType: 'group',
300
+ groupConfig: rowGroupConfig,
301
+ data: data
302
+ }
303
+ });
304
+ }
305
+ }, "group-open-title")] : null, /*#__PURE__*/_jsx(SheetEvent, {
306
+ name: "reverse",
307
+ handler: handleReverse
308
+ })]
309
+ }));
310
+ };
311
+ export default Table;