@zhenliang/sheet 0.1.26 → 0.1.28

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.
@@ -176,6 +176,6 @@ var Cell = function Cell(props) {
176
176
  });
177
177
  };
178
178
  export default /*#__PURE__*/memo(Cell, function (pre, next) {
179
- var shouldUpdate = pre.col === next.col && pre.row === next.row && pre.cell.value === next.cell.value && pre.cell.dataEditor === next.cell.dataEditor && pre.cell.record === next.cell.record;
179
+ var shouldUpdate = pre.col === next.col && pre.row === next.row && pre.cell.value === next.cell.value && pre.cell.dataEditor === next.cell.dataEditor && pre.cell.className === next.cell.className && pre.cell.record === next.cell.record;
180
180
  return shouldUpdate;
181
181
  });
@@ -0,0 +1,10 @@
1
+ import React, { CSSProperties } from 'react';
2
+ interface ControlProps {
3
+ showBackEdit?: boolean;
4
+ startRowVisible: boolean;
5
+ handelClick: () => void;
6
+ backEditStyle?: Partial<CSSProperties>;
7
+ direction: 'up' | 'down';
8
+ }
9
+ export declare const Control: React.FC<ControlProps>;
10
+ export {};
@@ -0,0 +1,32 @@
1
+ import { ArrowDownOutlined, ArrowUpOutlined } from '@ant-design/icons';
2
+ import React from 'react';
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ import { jsxs as _jsxs } from "react/jsx-runtime";
5
+ export var Control = function Control(props) {
6
+ var showBackEdit = props.showBackEdit,
7
+ startRowVisible = props.startRowVisible,
8
+ handelClick = props.handelClick,
9
+ backEditStyle = props.backEditStyle,
10
+ direction = props.direction;
11
+ if (!showBackEdit || startRowVisible) return null;
12
+ var styles = backEditStyle || {
13
+ top: 0,
14
+ right: 0
15
+ };
16
+ var backIcon = direction === 'up' ? /*#__PURE__*/_jsx(ArrowUpOutlined, {
17
+ rev: undefined
18
+ }) : /*#__PURE__*/_jsx(ArrowDownOutlined, {
19
+ rev: undefined
20
+ });
21
+ return /*#__PURE__*/_jsxs("div", {
22
+ className: "back-edit",
23
+ onClick: handelClick,
24
+ style: styles,
25
+ children: [backIcon, /*#__PURE__*/_jsx("span", {
26
+ style: {
27
+ marginLeft: 0
28
+ },
29
+ children: "\u8FD4\u56DE\u7F16\u8F91\u884C"
30
+ })]
31
+ });
32
+ };
@@ -12,13 +12,7 @@ var DefaultRow = function DefaultRow(_ref) {
12
12
  return groupConfigToGroupMap(groupConfig);
13
13
  }, [groupConfig]);
14
14
  if (groupMap.get(row) && !((_groupMap$get = groupMap.get(row)) !== null && _groupMap$get !== void 0 && _groupMap$get.isStart) && !((_groupMap$get2 = groupMap.get(row)) !== null && _groupMap$get2 !== void 0 && _groupMap$get2.isOpen)) {
15
- return /*#__PURE__*/_jsx("tr", {
16
- className: rowClassName,
17
- style: {
18
- display: 'none'
19
- },
20
- children: children
21
- });
15
+ return null;
22
16
  }
23
17
  return /*#__PURE__*/_jsx("tr", {
24
18
  className: rowClassName,
@@ -0,0 +1,6 @@
1
+ import { MenuRenderProps } from "../../type/sheet";
2
+ import React from 'react';
3
+ export declare const Menu: React.ForwardRefExoticComponent<MenuRenderProps & {
4
+ contextMenu?: React.FC<MenuRenderProps> | undefined;
5
+ showMenu: boolean;
6
+ } & React.RefAttributes<HTMLDivElement>>;
@@ -0,0 +1,21 @@
1
+ import React, { forwardRef } from 'react';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ export var Menu = /*#__PURE__*/forwardRef(function (props, ref) {
4
+ var showMenu = props.showMenu,
5
+ position = props.position,
6
+ cell = props.cell,
7
+ onContextMenu = props.onContextMenu,
8
+ ContextMenu = props.contextMenu;
9
+ var isMenuShow = !!ContextMenu && showMenu;
10
+ return /*#__PURE__*/_jsx("div", {
11
+ ref: ref,
12
+ style: {
13
+ display: isMenuShow ? '' : 'none'
14
+ },
15
+ children: isMenuShow && /*#__PURE__*/_jsx(ContextMenu, {
16
+ position: position,
17
+ cell: cell,
18
+ onContextMenu: onContextMenu
19
+ })
20
+ });
21
+ });
@@ -4,29 +4,31 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
4
4
  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; }
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
- import { useEffect, useMemo, useRef } from 'react';
7
+ import { useEffect, useImperativeHandle, useMemo, useRef } from 'react';
8
8
  // import ReduxLogger from 'redux-logger';
9
9
  import ReduxThunk from 'redux-thunk';
10
10
  import DefaultRow from "./DefaultRow";
11
11
  import DefaultShell from "./DefaultShell";
12
- import { ArrowDownOutlined, ArrowUpOutlined } from '@ant-design/icons';
12
+ import { useGroup } from "../../hooks/useGroupConfig";
13
13
  import { Empty } from 'antd';
14
14
  import { isEmpty, isNil, isNumber } from 'lodash';
15
15
  import { SheetEventContext, useEventBus, useMiddlewareReducer } from "../../hooks";
16
16
  import sheetReducer from "../reducers";
17
17
  import { classNames, getRowHeight, rowToActualRow } from "../util";
18
+ import { Control } from "./Control";
18
19
  import { DefaultRowMapper } from "./DefaultRowMapper";
20
+ import { Menu } from "./Menu";
19
21
  import "./index.less";
20
22
  import { useCellEvent } from "./useCellEvent";
21
23
  import { useContextMenu } from "./useContextMenu";
22
24
  import { useKeyBoardEvent } from "./useKeyBoardEvent";
23
25
  import { useMouseEvent } from "./useMouseEvent";
24
26
  import { useSelectVisible } from "./useSelectVisible";
25
- import { useVirtualList } from "./useVirtualList";
27
+ import { VirtualizeEnd, VirtualizeStart, useVirtualList } from "./useVirtualList";
26
28
  import { jsx as _jsx } from "react/jsx-runtime";
27
29
  import { jsxs as _jsxs } from "react/jsx-runtime";
28
30
  var Sheet = function Sheet(props) {
29
- var _state$data$length, _state$data3, _scroll$y, _scroll$x;
31
+ var _visibleData$length, _scroll$y, _scroll$x;
30
32
  var _props$sheetInstance = props.sheetInstance,
31
33
  sheetInstance = _props$sheetInstance === void 0 ? {
32
34
  current: null
@@ -46,8 +48,6 @@ var Sheet = function Sheet(props) {
46
48
  data = props.data,
47
49
  _props$freePaste = props.freePaste,
48
50
  freePaste = _props$freePaste === void 0 ? false : _props$freePaste,
49
- _props$groupConfig = props.groupConfig,
50
- groupConfig = _props$groupConfig === void 0 ? undefined : _props$groupConfig,
51
51
  onCellsChanged = props.onCellsChanged,
52
52
  onContextMenu = props.onContextMenu,
53
53
  rowClassName = props.rowClassName,
@@ -58,6 +58,8 @@ var Sheet = function Sheet(props) {
58
58
  var sheetWrapperRef = useRef(null);
59
59
  var contextMenuRef = useRef(null);
60
60
  var eventBus = useEventBus();
61
+ var _useGroup = useGroup(),
62
+ groupConfig = _useGroup.config;
61
63
  var _useMiddlewareReducer = useMiddlewareReducer(sheetReducer, {
62
64
  data: data,
63
65
  mouseDown: false,
@@ -71,8 +73,8 @@ var Sheet = function Sheet(props) {
71
73
  _useMiddlewareReducer2 = _slicedToArray(_useMiddlewareReducer, 2),
72
74
  state = _useMiddlewareReducer2[0],
73
75
  dispatch = _useMiddlewareReducer2[1];
74
- useEffect(function () {
75
- sheetInstance.current = {
76
+ useImperativeHandle(sheetInstance, function () {
77
+ return {
76
78
  zoomTo: function zoomTo(row) {
77
79
  // 给定 row 回到行
78
80
  // 不给定 row 默认回到编辑行和列
@@ -134,7 +136,7 @@ var Sheet = function Sheet(props) {
134
136
  return history !== null && history !== void 0 && history.length ? history === null || history === void 0 ? void 0 : history[history.length - 1] : {};
135
137
  }
136
138
  };
137
- }, [sheetWrapperRef.current, state.history]);
139
+ }, [state.history]);
138
140
  useEffect(function () {
139
141
  // 同步必要的状态
140
142
  dispatch({
@@ -164,7 +166,20 @@ var Sheet = function Sheet(props) {
164
166
  }, 1);
165
167
  }
166
168
  }, [state.editing, state.start]);
167
- var _useVirtualList = useVirtualList(sheetWrapperRef, state.data, groupConfig, virtualized),
169
+ var visibleData = useMemo(function () {
170
+ var _groupConfig$groups, _state$data;
171
+ if (!(groupConfig !== null && groupConfig !== void 0 && (_groupConfig$groups = groupConfig.groups) !== null && _groupConfig$groups !== void 0 && _groupConfig$groups.length)) {
172
+ return state.data;
173
+ }
174
+ return (_state$data = state.data) === null || _state$data === void 0 ? void 0 : _state$data.filter(function (item, index) {
175
+ if (!(groupConfig !== null && groupConfig !== void 0 && groupConfig.configMap.has(index))) {
176
+ return true;
177
+ }
178
+ var rowConfig = groupConfig.configMap.get(index);
179
+ return (rowConfig === null || rowConfig === void 0 ? void 0 : rowConfig.isOpen) || (rowConfig === null || rowConfig === void 0 ? void 0 : rowConfig.isStart);
180
+ });
181
+ }, [state.data, groupConfig]);
182
+ var _useVirtualList = useVirtualList(sheetWrapperRef, visibleData, virtualized),
168
183
  virtualStart = _useVirtualList.virtualStart,
169
184
  virtualEnd = _useVirtualList.virtualEnd,
170
185
  paddingTop = _useVirtualList.paddingTop,
@@ -178,15 +193,14 @@ var Sheet = function Sheet(props) {
178
193
  });
179
194
  }, [groupConfig]);
180
195
  var rowElements = useMemo(function () {
181
- var _state$data;
182
- return state === null || state === void 0 || (_state$data = state.data) === null || _state$data === void 0 || (_state$data = _state$data.slice(virtualStart, virtualEnd)) === null || _state$data === void 0 ? void 0 : _state$data.map(function (rowData) {
183
- var _state$data2, _rowData;
184
- var row = (state === null || state === void 0 || (_state$data2 = state.data) === null || _state$data2 === void 0 ? void 0 : _state$data2.indexOf(rowData)) || 0;
196
+ var _visibleData$slice;
197
+ return visibleData === null || visibleData === void 0 || (_visibleData$slice = visibleData.slice(virtualStart, virtualEnd)) === null || _visibleData$slice === void 0 ? void 0 : _visibleData$slice.map(function (rowData) {
198
+ var _rowData$, _rowData;
199
+ var row = ((_rowData$ = rowData[0]) === null || _rowData$ === void 0 ? void 0 : _rowData$.row) || 0;
185
200
  var rowCN = rowClassName instanceof Function ? rowClassName === null || rowClassName === void 0 ? void 0 : rowClassName(rowData === null || rowData === void 0 || (_rowData = rowData[rowData.length - 1]) === null || _rowData === void 0 ? void 0 : _rowData.record, row) : rowClassName;
186
201
  return /*#__PURE__*/_jsx(Row, {
187
202
  row: row,
188
203
  cells: rowData,
189
- groupConfig: groupConfig,
190
204
  rowClassName: rowCN,
191
205
  children: /*#__PURE__*/_jsx(DefaultRowMapper, {
192
206
  rowData: rowData,
@@ -194,12 +208,25 @@ var Sheet = function Sheet(props) {
194
208
  })
195
209
  }, row);
196
210
  });
197
- }, [state.data, groupConfig, virtualStart, virtualEnd, rowClassName]);
198
- var memoHeight = Math.min(((_state$data$length = state === null || state === void 0 || (_state$data3 = state.data) === null || _state$data3 === void 0 ? void 0 : _state$data3.length) !== null && _state$data$length !== void 0 ? _state$data$length : 0) + 1, 10) * 42 + 43;
211
+ }, [visibleData, groupConfig, virtualStart, virtualEnd, rowClassName]);
212
+ var memoHeight = Math.min(((_visibleData$length = visibleData === null || visibleData === void 0 ? void 0 : visibleData.length) !== null && _visibleData$length !== void 0 ? _visibleData$length : 0) + 1, 10) * 42 + 43;
199
213
  var _useSelectVisible = useSelectVisible(sheetWrapperRef, state.start),
200
214
  _useSelectVisible2 = _slicedToArray(_useSelectVisible, 2),
201
215
  startRowVisible = _useSelectVisible2[0],
202
216
  direction = _useSelectVisible2[1];
217
+ var isEmptyData = isEmpty(state.data);
218
+ var EmptyElement = useMemo(function () {
219
+ if (isEmptyData) {
220
+ return /*#__PURE__*/_jsx("div", {
221
+ style: {
222
+ marginTop: 16,
223
+ marginBottom: 16
224
+ },
225
+ children: emptyRenderer
226
+ });
227
+ }
228
+ return null;
229
+ }, [isEmptyData, emptyRenderer]);
203
230
  return /*#__PURE__*/_jsx(SheetEventContext.Provider, {
204
231
  value: eventBus,
205
232
  children: /*#__PURE__*/_jsxs("span", {
@@ -213,59 +240,33 @@ var Sheet = function Sheet(props) {
213
240
  },
214
241
  children: [/*#__PURE__*/_jsxs(SheetShell, {
215
242
  className: classNames('harvest-sheet', className),
216
- children: [virtualized && paddingTop > 0 && /*#__PURE__*/_jsx("tr", {
217
- style: {
218
- height: 0,
219
- paddingBottom: paddingTop,
220
- display: 'block'
221
- }
222
- }), rowElements, virtualized && paddingBottom > 0 && /*#__PURE__*/_jsx("tr", {
223
- style: {
224
- height: 0,
225
- paddingBottom: paddingBottom,
226
- display: 'block'
227
- }
243
+ children: [/*#__PURE__*/_jsx(VirtualizeStart, {
244
+ virtualized: virtualized,
245
+ paddingTop: paddingTop
246
+ }), rowElements, /*#__PURE__*/_jsx(VirtualizeEnd, {
247
+ virtualized: virtualized,
248
+ paddingBottom: paddingBottom
228
249
  })]
229
- }, "sheet"), ContextMenu ? /*#__PURE__*/_jsx("div", {
250
+ }, "sheet"), /*#__PURE__*/_jsx(Menu, {
230
251
  ref: contextMenuRef,
231
- style: {
232
- display: menu.showMenu ? '' : 'none'
233
- },
234
- children: /*#__PURE__*/_jsx(ContextMenu, {
235
- position: menu.position,
236
- cell: menu.cellPosition,
237
- onContextMenu: onContextMenu
238
- })
239
- }) : null]
240
- }), isEmpty(state.data) || isNil(state.data) ? /*#__PURE__*/_jsx("div", {
241
- style: {
242
- marginTop: 16,
243
- marginBottom: 16
244
- },
245
- children: emptyRenderer
246
- }) : null, /*#__PURE__*/_jsxs("div", {
252
+ position: menu.position,
253
+ cell: menu.cellPosition,
254
+ showMenu: menu.showMenu,
255
+ contextMenu: ContextMenu,
256
+ onContextMenu: onContextMenu
257
+ })]
258
+ }), EmptyElement, /*#__PURE__*/_jsxs("div", {
247
259
  className: "harvest-sheet-control",
248
- children: [children, showBackEdit && !startRowVisible ? /*#__PURE__*/_jsxs("div", {
249
- className: "back-edit",
250
- onClick: function onClick() {
260
+ children: [children, /*#__PURE__*/_jsx(Control, {
261
+ showBackEdit: showBackEdit,
262
+ startRowVisible: startRowVisible,
263
+ handelClick: function handelClick() {
251
264
  var _sheetInstance$curren;
252
265
  return sheetInstance === null || sheetInstance === void 0 || (_sheetInstance$curren = sheetInstance.current) === null || _sheetInstance$curren === void 0 ? void 0 : _sheetInstance$curren.zoomTo();
253
266
  },
254
- style: !backEditStyle ? {
255
- top: 0,
256
- right: 0
257
- } : backEditStyle,
258
- children: [direction === 'up' ? /*#__PURE__*/_jsx(ArrowUpOutlined, {
259
- rev: undefined
260
- }) : /*#__PURE__*/_jsx(ArrowDownOutlined, {
261
- rev: undefined
262
- }), /*#__PURE__*/_jsx("span", {
263
- style: {
264
- marginLeft: 0
265
- },
266
- children: "\u8FD4\u56DE\u7F16\u8F91\u884C"
267
- })]
268
- }) : null]
267
+ direction: direction,
268
+ backEditStyle: backEditStyle
269
+ })]
269
270
  })]
270
271
  })
271
272
  });
@@ -1,7 +1,16 @@
1
+ /// <reference types="react" />
1
2
  import type { SheetType } from "../../type";
2
- export declare const useVirtualList: (elementRef: React.RefObject<SheetType.refAssertion>, data?: SheetType.Cell[][], groupConfig?: SheetType.RowGroupConfig, virtualized?: boolean) => {
3
+ export declare const useVirtualList: (elementRef: React.RefObject<SheetType.refAssertion>, data?: SheetType.Cell[][], virtualized?: boolean) => {
3
4
  virtualStart: number;
4
5
  virtualEnd: number;
5
6
  paddingTop: number;
6
7
  paddingBottom: number;
7
8
  };
9
+ export declare const VirtualizeStart: React.FC<{
10
+ virtualized: boolean;
11
+ paddingTop: number;
12
+ }>;
13
+ export declare const VirtualizeEnd: React.FC<{
14
+ virtualized: boolean;
15
+ paddingBottom: number;
16
+ }>;
@@ -4,15 +4,17 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
4
4
  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; }
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
- import { isEqual, throttle } from 'lodash';
8
- import { useEffect, useRef, useState } from 'react';
9
- import { getRowHeight, rowToActualRow, rowToCountRow } from "../util";
7
+ /* eslint-disable @typescript-eslint/no-unused-vars */
8
+ import { useGroup } from "../../hooks/useGroupConfig";
9
+ import { throttle } from 'lodash';
10
+ import { useEffect, useState } from 'react';
11
+ import { getRowHeight } from "../util";
12
+ import { jsx as _jsx } from "react/jsx-runtime";
10
13
  var extra = 20;
11
14
  // 虚拟列表
12
15
  export var useVirtualList = function useVirtualList(elementRef) {
13
16
  var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
14
- var groupConfig = arguments.length > 2 ? arguments[2] : undefined;
15
- var virtualized = arguments.length > 3 ? arguments[3] : undefined;
17
+ var virtualized = arguments.length > 2 ? arguments[2] : undefined;
16
18
  var _useState = useState({
17
19
  virtualStart: 0,
18
20
  virtualEnd: virtualized ? Math.min(data.length - 1, 2 * extra) : data.length - 1,
@@ -22,17 +24,17 @@ export var useVirtualList = function useVirtualList(elementRef) {
22
24
  _useState2 = _slicedToArray(_useState, 2),
23
25
  state = _useState2[0],
24
26
  setState = _useState2[1];
27
+ var _useGroup = useGroup(),
28
+ groupConfig = _useGroup.config;
25
29
  var virtualStart = state.virtualStart,
26
30
  virtualEnd = state.virtualEnd,
27
31
  paddingTop = state.paddingTop,
28
32
  paddingBottom = state.paddingBottom;
29
- var virtualRef = useRef();
30
- var groupConfigRef = useRef(groupConfig);
33
+ // const virtualRef = useRef<VirtualConfig | null>();
31
34
  useEffect(function () {
32
35
  var _elementRef$current;
33
- groupConfigRef.current = groupConfig;
34
36
  (_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 || _elementRef$current.scrollBy({
35
- top: 1
37
+ top: 0
36
38
  });
37
39
  }, [groupConfig]);
38
40
  useEffect(function () {
@@ -47,36 +49,15 @@ export var useVirtualList = function useVirtualList(elementRef) {
47
49
  clientHeight = _ref.clientHeight;
48
50
  var start = Math.floor(scrollTop / itemHeight) - extra;
49
51
  var end = Math.ceil((scrollTop + clientHeight) / itemHeight) + extra;
50
- if (!groupConfigRef.current) {
51
- var newConfig = {
52
- virtualStart: start,
53
- virtualEnd: end,
54
- paddingTop: start * itemHeight,
55
- paddingBottom: (data.length - end) * itemHeight
56
- };
57
- if (isEqual(newConfig, virtualRef.current)) {
58
- return;
59
- }
60
- virtualRef.current = newConfig;
61
- setState(newConfig);
62
- return;
63
- }
64
- var actualStart = rowToCountRow(start, groupConfigRef.current, data.length);
65
- var actualEnd = rowToCountRow(end, groupConfigRef.current, data.length);
66
- var maxEnd = rowToActualRow(data.length, groupConfigRef.current, data.length);
67
- var invisibleTop = rowToActualRow(actualStart, groupConfigRef.current, data.length);
68
- var invisibleBottom = maxEnd > end ? maxEnd - end : 0;
69
- var updateVirtualConfig = {
70
- virtualStart: actualStart,
71
- virtualEnd: actualEnd,
72
- paddingTop: invisibleTop * itemHeight,
73
- paddingBottom: invisibleBottom * itemHeight
52
+ var newConfig = {
53
+ virtualStart: start,
54
+ virtualEnd: end,
55
+ paddingTop: start * itemHeight,
56
+ paddingBottom: (data.length - end) * itemHeight
74
57
  };
75
- if (isEqual(updateVirtualConfig, virtualRef.current)) {
76
- return;
77
- }
78
- setState(updateVirtualConfig);
79
- virtualRef.current = updateVirtualConfig;
58
+ // virtualRef.current = newConfig;
59
+ setState(newConfig);
60
+ return;
80
61
  }, 50);
81
62
  // data.length change 的时候用 handleScroll 重设 virtual params
82
63
  handleScroll();
@@ -100,4 +81,23 @@ export var useVirtualList = function useVirtualList(elementRef) {
100
81
  paddingTop: Math.max(0, paddingTop),
101
82
  paddingBottom: data.length - 1 <= virtualEnd ? 0 : paddingBottom
102
83
  };
84
+ };
85
+ export var VirtualizeStart = function VirtualizeStart(_ref2) {
86
+ var virtualized = _ref2.virtualized,
87
+ paddingTop = _ref2.paddingTop;
88
+ return virtualized && paddingTop > 0 ? /*#__PURE__*/_jsx("tr", {
89
+ style: {
90
+ height: 0,
91
+ paddingBottom: paddingTop,
92
+ display: 'block'
93
+ }
94
+ }) : null;
95
+ };
96
+ export var VirtualizeEnd = function VirtualizeEnd(_ref3) {
97
+ var virtualized = _ref3.virtualized,
98
+ paddingBottom = _ref3.paddingBottom;
99
+ return /*#__PURE__*/_jsx(VirtualizeStart, {
100
+ virtualized: virtualized,
101
+ paddingTop: paddingBottom
102
+ });
103
103
  };
@@ -7,9 +7,7 @@ export declare const SelectionEvent: FC<{
7
7
  }>;
8
8
  export declare const GroupEvent: FC<{
9
9
  hasChildren: boolean;
10
- rowGroupConfig: SheetType.RowGroupConfig;
11
10
  data: SheetType.Cell[][];
12
11
  sheetInstance: SheetType.SheetInstance | null;
13
- onGroupChange: (value: SheetType.RowGroupConfig) => void;
14
12
  onGridChange: (value: SheetType.Cell[][]) => void;
15
13
  }>;
@@ -10,6 +10,8 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
10
10
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
11
11
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
12
12
  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; }
13
+ /* eslint-disable @typescript-eslint/no-unused-vars */
14
+ import { useGroup } from "../../hooks/useGroupConfig";
13
15
  import { SheetEvent } from "../sheet/Event";
14
16
  import { jsx as _jsx } from "react/jsx-runtime";
15
17
  import { Fragment as _Fragment } from "react/jsx-runtime";
@@ -31,44 +33,34 @@ export var SelectionEvent = function SelectionEvent(props) {
31
33
  };
32
34
  export var GroupEvent = function GroupEvent(props) {
33
35
  var hasChildren = props.hasChildren,
34
- rowGroupConfig = props.rowGroupConfig,
35
- data = props.data,
36
- sheetInstance = props.sheetInstance,
37
- onGroupChange = props.onGroupChange,
38
- onGridChange = props.onGridChange;
36
+ sheetInstance = props.sheetInstance;
37
+ var _useGroup = useGroup(),
38
+ _useGroup$config = _useGroup.config,
39
+ rowGroupConfig = _useGroup$config === void 0 ? {} : _useGroup$config,
40
+ onGroupChange = _useGroup.onChange;
39
41
  if (!hasChildren) return null;
40
- var _ref = rowGroupConfig || {},
41
- groups = _ref.groups;
42
+ var groups = rowGroupConfig.groups;
42
43
  return /*#__PURE__*/_jsxs(_Fragment, {
43
44
  children: [/*#__PURE__*/_jsx(SheetEvent, {
44
45
  name: "group-open",
45
46
  handler: function handler(e) {
46
- var _ref2 = e,
47
- row = _ref2.row;
48
- var index = groups.findIndex(function (item) {
47
+ var _ref = e,
48
+ row = _ref.row;
49
+ var index = groups === null || groups === void 0 ? void 0 : groups.findIndex(function (item) {
49
50
  return item.groupStart === row;
50
51
  });
51
52
  if (index >= 0) {
52
- var groupOpen = _toConsumableArray(rowGroupConfig.groupOpen);
53
- groupOpen[index] = !rowGroupConfig.groupOpen[index];
53
+ var groupOpen = _toConsumableArray(rowGroupConfig === null || rowGroupConfig === void 0 ? void 0 : rowGroupConfig.groupOpen);
54
+ groupOpen[index] = !(rowGroupConfig !== null && rowGroupConfig !== void 0 && rowGroupConfig.groupOpen[index]);
54
55
  onGroupChange && onGroupChange(_objectSpread(_objectSpread({}, rowGroupConfig), {}, {
55
56
  groupOpen: groupOpen
56
57
  }));
57
- var newGrid = _toConsumableArray(data);
58
- newGrid[row] = _toConsumableArray(newGrid[row]);
59
- newGrid[row][0] = _objectSpread(_objectSpread({}, newGrid[row][0]), {}, {
60
- record: {
61
- open: !!groupOpen[index]
62
- }
63
- });
64
- onGridChange && onGridChange(newGrid);
65
58
  sheetInstance === null || sheetInstance === void 0 || sheetInstance.pushToHistory({
66
59
  type: 'Custom',
67
60
  changes: [],
68
61
  extraInfo: {
69
62
  extraType: 'group',
70
- groupConfig: rowGroupConfig,
71
- data: data
63
+ groupConfig: rowGroupConfig
72
64
  }
73
65
  });
74
66
  }
@@ -85,8 +77,7 @@ export var GroupEvent = function GroupEvent(props) {
85
77
  changes: [],
86
78
  extraInfo: {
87
79
  extraType: 'group',
88
- groupConfig: rowGroupConfig,
89
- data: data
80
+ groupConfig: rowGroupConfig
90
81
  }
91
82
  });
92
83
  }
@@ -1,9 +1,5 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
2
  var _excluded = ["sheetInstance", "columns", "dataSource", "rowKey", "rowSelection", "groupConfig", "onChange", "handleAdd", "draggable", "eventHandler"];
3
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4
- 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."); }
5
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
7
3
  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; }
8
4
  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; }
9
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; }
@@ -18,22 +14,19 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
18
14
  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
15
  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
16
  import { Sheet, useSetState } from "../..";
17
+ import { GroupContext } from "../../hooks/useGroupConfig";
21
18
  import { WidthContext } from "../../hooks/useWidthConfig";
22
19
  import { Button } from 'antd';
23
20
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
24
21
  import { SheetEvent } from "../sheet/Event";
25
22
  import { DraggableShell } from "../shell/draggableShell";
26
23
  import { TableShell } from "../shell/tableShell";
27
- import { groupConfigToGroupMap } from "../util";
28
- import { CheckViewer } from "../viewer/checkViewer";
29
- import { GroupViewer } from "../viewer/groupViewer";
30
24
  import { GroupEvent, SelectionEvent } from "./events";
31
- import { useGroupConfig } from "./useGroupConfig";
32
- import { useRowSelection } from "./useRowSelection";
25
+ import { formatGroupData, useGroupConfig } from "./useGroupConfig";
26
+ import { formatSelectionData, useRowSelection } from "./useRowSelection";
33
27
  import { jsx as _jsx } from "react/jsx-runtime";
34
28
  import { jsxs as _jsxs } from "react/jsx-runtime";
35
29
  var Table = function Table(_ref) {
36
- var _rowGroupConfig$group, _rowGroupConfig$group2;
37
30
  var sheetRef = _ref.sheetInstance,
38
31
  columns = _ref.columns,
39
32
  dataSource = _ref.dataSource,
@@ -72,119 +65,26 @@ var Table = function Table(_ref) {
72
65
  _useGroupConfig2 = _slicedToArray(_useGroupConfig, 2),
73
66
  rowGroupConfig = _useGroupConfig2[0],
74
67
  setGroupConfig = _useGroupConfig2[1];
75
- var _ref2 = rowGroupConfig || {},
76
- groups = _ref2.groups,
77
- groupOpen = _ref2.groupOpen;
78
68
  useEffect(function () {
79
69
  if (!hasChildren) return;
80
70
  if (!dataSource || !columns) return;
81
- var data = [];
82
- var groupMap = groupConfigToGroupMap({
83
- groups: groups,
84
- groupOpen: groupOpen
85
- });
86
- var currentIndex = 0;
87
- dataSource.forEach(function (item, row) {
88
- var groupList = [item];
89
- if (item.children) {
90
- groupList = [item].concat(_toConsumableArray(item.children));
91
- }
92
- groupList.forEach(function (itemRow) {
93
- var _columns$, _columns$2, _columns$3, _columns$4, _groupMap$get, _groupMap$get2;
94
- var dataRow = [];
95
- var rowId = itemRow.id || itemRow.key || String(currentIndex);
96
- if (rowKey) {
97
- if (rowKey instanceof Function) {
98
- rowId = rowKey(itemRow, row);
99
- } else {
100
- rowId = itemRow[rowKey];
101
- }
102
- }
103
- dataRow.push({
104
- id: rowId,
105
- row: currentIndex,
106
- col: -1,
107
- editable: !((columns === null || columns === void 0 ? void 0 : columns[0].editable) instanceof Function) ? columns === null || columns === void 0 || (_columns$ = columns[0]) === null || _columns$ === void 0 ? void 0 : _columns$.editable : columns === null || columns === void 0 || (_columns$2 = columns[0]) === null || _columns$2 === void 0 ? void 0 : _columns$2.editable('', itemRow, currentIndex),
108
- readonly: !((columns === null || columns === void 0 ? void 0 : columns[0].readonly) instanceof Function) ? columns === null || columns === void 0 || (_columns$3 = columns[0]) === null || _columns$3 === void 0 ? void 0 : _columns$3.readonly : columns === null || columns === void 0 || (_columns$4 = columns[0]) === null || _columns$4 === void 0 ? void 0 : _columns$4.readonly('', itemRow, currentIndex),
109
- align: 'center',
110
- fixed: 'unset',
111
- value: groupMap.get(currentIndex) && ((_groupMap$get = groupMap.get(currentIndex)) === null || _groupMap$get === void 0 ? void 0 : _groupMap$get.isStart),
112
- record: {
113
- open: groupMap.get(currentIndex) && ((_groupMap$get2 = groupMap.get(currentIndex)) === null || _groupMap$get2 === void 0 ? void 0 : _groupMap$get2.isOpen)
114
- },
115
- valueViewer: GroupViewer,
116
- className: 'sheet-control'
117
- });
118
- columns.forEach(function (colInfo, col) {
119
- var _colInfo$cellConfig, _colInfo$cellConfig2, _colInfo$cellConfig3;
120
- var value = itemRow[colInfo.dataIndex || ''];
121
- dataRow.push({
122
- id: rowId,
123
- value: value,
124
- record: itemRow,
125
- readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, itemRow, currentIndex, col),
126
- align: colInfo.align,
127
- fixed: colInfo.fixed,
128
- editable: !(colInfo.editable instanceof Function) ? colInfo.editable : colInfo.editable(value, itemRow, currentIndex, col),
129
- valueViewer: colInfo.render ? colInfo.render : undefined,
130
- dataEditor: colInfo.editor ? colInfo.editor : undefined,
131
- row: currentIndex,
132
- className: !(((_colInfo$cellConfig = colInfo.cellConfig) === null || _colInfo$cellConfig === void 0 ? void 0 : _colInfo$cellConfig.className) instanceof Function) ? (_colInfo$cellConfig2 = colInfo.cellConfig) === null || _colInfo$cellConfig2 === void 0 ? void 0 : _colInfo$cellConfig2.className : (_colInfo$cellConfig3 = colInfo.cellConfig) === null || _colInfo$cellConfig3 === void 0 ? void 0 : _colInfo$cellConfig3.className(value, itemRow, currentIndex),
133
- col: col
134
- });
135
- });
136
- data.push(dataRow);
137
- currentIndex++;
138
- });
139
- });
140
- setData(data);
141
- }, [dataSource, columns, groups, hasChildren, rowGroupConfig]);
71
+ setData(formatGroupData({
72
+ dataSource: dataSource,
73
+ columns: columns,
74
+ rowKey: rowKey
75
+ }));
76
+ }, [dataSource, columns, hasChildren, rowKey]);
142
77
  useEffect(function () {
143
78
  if (hasChildren) return;
144
79
  if (!dataSource || !columns) return;
145
- setData(dataSource.map(function (item, row) {
146
- var rowId = item.id || item.key || String(row);
147
- if (rowKey) {
148
- if (rowKey instanceof Function) {
149
- rowId = rowKey(item, row);
150
- } else {
151
- rowId = item[rowKey];
152
- }
153
- }
154
- var rows = [];
155
- if (rowSelection) {
156
- rows.push({
157
- id: rowId,
158
- row: row,
159
- col: -1,
160
- readonly: true,
161
- align: 'center',
162
- value: checkedRow[row],
163
- valueViewer: CheckViewer,
164
- className: 'sheet-control'
165
- });
166
- }
167
- columns.forEach(function (colInfo, col) {
168
- var _colInfo$cellConfig4, _colInfo$cellConfig5, _colInfo$cellConfig6;
169
- var value = item[colInfo.dataIndex || ''];
170
- rows.push({
171
- id: rowId,
172
- value: value,
173
- record: item,
174
- readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, item, row, col),
175
- align: colInfo.align,
176
- fixed: colInfo.fixed,
177
- editable: !(colInfo.editable instanceof Function) ? colInfo.editable : colInfo.editable(value, item, row),
178
- valueViewer: colInfo.render ? colInfo.render : undefined,
179
- dataEditor: colInfo.editor ? colInfo.editor : undefined,
180
- className: !(((_colInfo$cellConfig4 = colInfo.cellConfig) === null || _colInfo$cellConfig4 === void 0 ? void 0 : _colInfo$cellConfig4.className) instanceof Function) ? (_colInfo$cellConfig5 = colInfo.cellConfig) === null || _colInfo$cellConfig5 === void 0 ? void 0 : _colInfo$cellConfig5.className : (_colInfo$cellConfig6 = colInfo.cellConfig) === null || _colInfo$cellConfig6 === void 0 ? void 0 : _colInfo$cellConfig6.className(value, item, row),
181
- row: row,
182
- col: col
183
- });
184
- });
185
- return rows;
80
+ setData(formatSelectionData({
81
+ dataSource: dataSource,
82
+ columns: columns,
83
+ checked: checkedRow,
84
+ rowKey: rowKey,
85
+ rowSelection: rowSelection
186
86
  }));
187
- }, [dataSource, columns, checkedRow, hasChildren]);
87
+ }, [dataSource, columns, checkedRow, hasChildren, rowKey, rowSelection]);
188
88
  useEffect(function () {
189
89
  if (!dataSource || !columns) {
190
90
  setData([[]]);
@@ -210,17 +110,15 @@ var Table = function Table(_ref) {
210
110
  }));
211
111
  }, [columns, onChange, hasControl]);
212
112
  var handleReverse = useCallback(function (value) {
213
- var _ref3 = value,
214
- type = _ref3.type,
215
- extraInfo = _ref3.extraInfo;
113
+ var _ref2 = value,
114
+ type = _ref2.type,
115
+ extraInfo = _ref2.extraInfo;
216
116
  if (type === 'Custom') {
217
- var _ref4 = extraInfo,
218
- _groupConfig = _ref4.groupConfig,
219
- extraType = _ref4.extraType,
220
- lastData = _ref4.data;
117
+ var _ref3 = extraInfo,
118
+ _groupConfig = _ref3.groupConfig,
119
+ extraType = _ref3.extraType;
221
120
  if (extraType === 'group') {
222
121
  setGroupConfig(_groupConfig);
223
- setData(lastData);
224
122
  }
225
123
  }
226
124
  }, [data, groupConfig]);
@@ -232,9 +130,6 @@ var Table = function Table(_ref) {
232
130
  newChecked[currentRow] = !newChecked[currentRow];
233
131
  setCheckedRow(newChecked);
234
132
  }, [sheetInstance, checkedRow]);
235
- var headGroupOpen = !(rowGroupConfig !== null && rowGroupConfig !== void 0 && (_rowGroupConfig$group = rowGroupConfig.groupOpen) !== null && _rowGroupConfig$group !== void 0 && _rowGroupConfig$group.length) ? !!(rowGroupConfig !== null && rowGroupConfig !== void 0 && rowGroupConfig.defaultOpen) : !(rowGroupConfig !== null && rowGroupConfig !== void 0 && (_rowGroupConfig$group2 = rowGroupConfig.groupOpen) !== null && _rowGroupConfig$group2 !== void 0 && _rowGroupConfig$group2.some(function (value) {
236
- return !value;
237
- }));
238
133
  var headSelection = !!rowSelection;
239
134
  var WrappedTableShell = useMemo(function () {
240
135
  if (draggable) {
@@ -244,9 +139,6 @@ var Table = function Table(_ref) {
244
139
  showGroup: hasChildren,
245
140
  showSelect: !!rowSelection,
246
141
  controlProps: {
247
- group: {
248
- open: headGroupOpen
249
- },
250
142
  check: {
251
143
  checked: false
252
144
  }
@@ -259,55 +151,55 @@ var Table = function Table(_ref) {
259
151
  showGroup: hasChildren,
260
152
  showSelect: !!rowSelection,
261
153
  controlProps: {
262
- group: {
263
- open: headGroupOpen
264
- },
265
154
  check: {
266
155
  checked: false
267
156
  }
268
157
  }
269
158
  });
270
- }, [columns.length, draggable, headSelection, hasChildren, headGroupOpen]);
159
+ }, [columns.length, draggable, headSelection, hasChildren]);
271
160
  return /*#__PURE__*/_jsx(WidthContext.Provider, {
272
161
  value: {
273
162
  widths: widths,
274
163
  onChange: setWidth
275
164
  },
276
- children: /*#__PURE__*/_jsxs(Sheet, _objectSpread(_objectSpread({}, args), {}, {
277
- sheetInstance: sheetInstance,
278
- sheetRenderer: WrappedTableShell,
279
- groupConfig: rowGroupConfig,
280
- data: data,
281
- onCellsChanged: handleChanges,
282
- children: [/*#__PURE__*/_jsx(SelectionEvent, {
283
- hasChildren: hasChildren,
284
- rowSelection: rowSelection,
285
- onChange: handleRowSelect
286
- }), /*#__PURE__*/_jsx(GroupEvent, {
287
- hasChildren: hasChildren,
165
+ children: /*#__PURE__*/_jsx(GroupContext.Provider, {
166
+ value: {
167
+ config: rowGroupConfig,
168
+ onChange: setGroupConfig
169
+ },
170
+ children: /*#__PURE__*/_jsxs(Sheet, _objectSpread(_objectSpread({}, args), {}, {
171
+ sheetInstance: sheetInstance,
172
+ sheetRenderer: WrappedTableShell,
288
173
  data: data,
289
- rowGroupConfig: rowGroupConfig,
290
- sheetInstance: sheetInstance.current,
291
- onGridChange: setData,
292
- onGroupChange: setGroupConfig
293
- }), /*#__PURE__*/_jsx(SheetEvent, {
294
- name: "reverse",
295
- handler: handleReverse
296
- }, "_reverse"), Object.keys(eventHandler || {}).map(function (key) {
297
- return /*#__PURE__*/_jsx(SheetEvent, {
298
- name: key,
299
- handler: eventHandler === null || eventHandler === void 0 ? void 0 : eventHandler[key]
300
- }, key);
301
- }), handleAdd ? /*#__PURE__*/_jsx(Button, {
302
- type: "dashed",
303
- style: {
304
- width: '100%',
305
- height: 32
306
- },
307
- onClick: handleAdd,
308
- children: "+ \u6DFB\u52A0"
309
- }) : null]
310
- }))
174
+ onCellsChanged: handleChanges,
175
+ children: [/*#__PURE__*/_jsx(SelectionEvent, {
176
+ hasChildren: hasChildren,
177
+ rowSelection: rowSelection,
178
+ onChange: handleRowSelect
179
+ }), /*#__PURE__*/_jsx(GroupEvent, {
180
+ hasChildren: hasChildren,
181
+ data: data,
182
+ sheetInstance: sheetInstance.current,
183
+ onGridChange: setData
184
+ }), /*#__PURE__*/_jsx(SheetEvent, {
185
+ name: "reverse",
186
+ handler: handleReverse
187
+ }, "_reverse"), Object.keys(eventHandler || {}).map(function (key) {
188
+ return /*#__PURE__*/_jsx(SheetEvent, {
189
+ name: key,
190
+ handler: eventHandler === null || eventHandler === void 0 ? void 0 : eventHandler[key]
191
+ }, key);
192
+ }), handleAdd ? /*#__PURE__*/_jsx(Button, {
193
+ type: "dashed",
194
+ style: {
195
+ width: '100%',
196
+ height: 32
197
+ },
198
+ onClick: handleAdd,
199
+ children: "+ \u6DFB\u52A0"
200
+ }) : null]
201
+ }))
202
+ })
311
203
  });
312
204
  };
313
205
  export default Table;
@@ -1,2 +1,5 @@
1
1
  import type { SheetTableType, SheetType } from "../../type";
2
- export declare const useGroupConfig: (dataSource: Record<string, unknown>[], tableGroupConfig?: SheetTableType.TableGroupConfig, hasChildren?: boolean) => [SheetType.RowGroupConfig, (value: SheetType.RowGroupConfig) => void];
2
+ export declare const useGroupConfig: (dataSource: Record<string, unknown>[], tableGroupConfig?: SheetTableType.TableGroupConfig, hasChildren?: boolean) => [SheetType.RowGroupConfig & {
3
+ configMap: SheetType.GroupMap;
4
+ }, (value: SheetType.RowGroupConfig) => void];
5
+ export declare const formatGroupData: (param: Pick<SheetTableType.TableProps, 'dataSource' | 'columns' | 'rowKey'>) => SheetType.Cell[][];
@@ -1,3 +1,13 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
3
+ 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."); }
4
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
5
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
6
+ 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; }
7
+ 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; }
8
+ 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; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
10
+ 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); }
1
11
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
12
  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."); }
3
13
  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); }
@@ -6,6 +16,8 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
6
16
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
17
  import { flatten, isNil } from 'lodash';
8
18
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
19
+ import { groupConfigToGroupMap } from "../util";
20
+ import { GroupViewer } from "../viewer";
9
21
  import { dataSourceToRowConfig } from "./util";
10
22
  export var useGroupConfig = function useGroupConfig(dataSource, tableGroupConfig, hasChildren) {
11
23
  var _useState = useState(),
@@ -61,5 +73,70 @@ export var useGroupConfig = function useGroupConfig(dataSource, tableGroupConfig
61
73
  setGroupConfig(value);
62
74
  groupConfigRef.current = value;
63
75
  }, [setGroupConfig]);
64
- return [groupConfig, handleGroupChange];
76
+ var groupMap = useMemo(function () {
77
+ return groupConfigToGroupMap({
78
+ groups: groupConfig === null || groupConfig === void 0 ? void 0 : groupConfig.groups,
79
+ groupOpen: groupConfig === null || groupConfig === void 0 ? void 0 : groupConfig.groupOpen
80
+ });
81
+ }, [groupConfig]);
82
+ return [_objectSpread(_objectSpread({}, groupConfig), {}, {
83
+ configMap: groupMap
84
+ }), handleGroupChange];
85
+ };
86
+ export var formatGroupData = function formatGroupData(param) {
87
+ var dataSource = param.dataSource,
88
+ columns = param.columns,
89
+ rowKey = param.rowKey;
90
+ var data = [];
91
+ var currentIndex = 0;
92
+ dataSource.forEach(function (item, row) {
93
+ var groupList = [item];
94
+ if (item.children) {
95
+ groupList = [item].concat(_toConsumableArray(item.children));
96
+ }
97
+ groupList.forEach(function (itemRow) {
98
+ var _columns$, _columns$2, _columns$3, _columns$4;
99
+ var dataRow = [];
100
+ var rowId = itemRow.id || itemRow.key || String(currentIndex);
101
+ if (rowKey) {
102
+ if (rowKey instanceof Function) {
103
+ rowId = rowKey(itemRow, row);
104
+ } else {
105
+ rowId = itemRow[rowKey];
106
+ }
107
+ }
108
+ dataRow.push({
109
+ id: rowId,
110
+ row: currentIndex,
111
+ col: -1,
112
+ editable: !((columns === null || columns === void 0 ? void 0 : columns[0].editable) instanceof Function) ? columns === null || columns === void 0 || (_columns$ = columns[0]) === null || _columns$ === void 0 ? void 0 : _columns$.editable : columns === null || columns === void 0 || (_columns$2 = columns[0]) === null || _columns$2 === void 0 ? void 0 : _columns$2.editable('', itemRow, currentIndex),
113
+ readonly: !((columns === null || columns === void 0 ? void 0 : columns[0].readonly) instanceof Function) ? columns === null || columns === void 0 || (_columns$3 = columns[0]) === null || _columns$3 === void 0 ? void 0 : _columns$3.readonly : columns === null || columns === void 0 || (_columns$4 = columns[0]) === null || _columns$4 === void 0 ? void 0 : _columns$4.readonly('', itemRow, currentIndex),
114
+ align: 'center',
115
+ fixed: 'unset',
116
+ valueViewer: GroupViewer,
117
+ className: 'sheet-control'
118
+ });
119
+ columns.forEach(function (colInfo, col) {
120
+ var _colInfo$cellConfig, _colInfo$cellConfig2, _colInfo$cellConfig3;
121
+ var value = itemRow[colInfo.dataIndex || ''];
122
+ dataRow.push({
123
+ id: rowId,
124
+ value: value,
125
+ record: itemRow,
126
+ readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, itemRow, currentIndex, col),
127
+ align: colInfo.align,
128
+ fixed: colInfo.fixed,
129
+ editable: !(colInfo.editable instanceof Function) ? colInfo.editable : colInfo.editable(value, itemRow, currentIndex, col),
130
+ valueViewer: colInfo.render ? colInfo.render : undefined,
131
+ dataEditor: colInfo.editor ? colInfo.editor : undefined,
132
+ row: currentIndex,
133
+ className: !(((_colInfo$cellConfig = colInfo.cellConfig) === null || _colInfo$cellConfig === void 0 ? void 0 : _colInfo$cellConfig.className) instanceof Function) ? (_colInfo$cellConfig2 = colInfo.cellConfig) === null || _colInfo$cellConfig2 === void 0 ? void 0 : _colInfo$cellConfig2.className : (_colInfo$cellConfig3 = colInfo.cellConfig) === null || _colInfo$cellConfig3 === void 0 ? void 0 : _colInfo$cellConfig3.className(value, itemRow, currentIndex),
134
+ col: col
135
+ });
136
+ });
137
+ data.push(dataRow);
138
+ currentIndex++;
139
+ });
140
+ });
141
+ return data;
65
142
  };
@@ -1,2 +1,14 @@
1
- import { SheetTableType } from "../..";
1
+ import { SheetTableType, SheetType } from "../..";
2
2
  export declare const useRowSelection: (dataSource: Record<string, unknown>[], rowSelection?: SheetTableType.TableRowSelection, hasChildren?: boolean) => [boolean[], (value: boolean[]) => void];
3
+ export declare const formatSelectionData: (param: Pick<SheetTableType.TableProps, "columns" | "dataSource" | "rowKey" | "rowSelection"> & {
4
+ checked: boolean[];
5
+ }) => {
6
+ id: string;
7
+ row: number;
8
+ col: number;
9
+ readonly: boolean;
10
+ align: SheetType.CellAlign;
11
+ value: string;
12
+ valueViewer: SheetType.CellViewer;
13
+ className: string;
14
+ }[][];
@@ -4,6 +4,7 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
4
4
  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; }
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { CheckViewer } from "../..";
7
8
  import { useEffect, useRef, useState } from 'react';
8
9
  export var useRowSelection = function useRowSelection(dataSource, rowSelection, hasChildren) {
9
10
  var _dataSource$length;
@@ -21,4 +22,53 @@ export var useRowSelection = function useRowSelection(dataSource, rowSelection,
21
22
  checkedRowRef.current = currentEmpty;
22
23
  }, [dataSource.length, hasChildren, rowSelection]);
23
24
  return [checkedRow, setCheckedRow];
25
+ };
26
+ export var formatSelectionData = function formatSelectionData(param) {
27
+ var dataSource = param.dataSource,
28
+ columns = param.columns,
29
+ checked = param.checked,
30
+ rowKey = param.rowKey,
31
+ rowSelection = param.rowSelection;
32
+ return dataSource.map(function (item, row) {
33
+ var rowId = item.id || item.key || String(row);
34
+ if (rowKey) {
35
+ if (rowKey instanceof Function) {
36
+ rowId = rowKey(item, row);
37
+ } else {
38
+ rowId = item[rowKey];
39
+ }
40
+ }
41
+ var rows = [];
42
+ if (rowSelection) {
43
+ rows.push({
44
+ id: rowId,
45
+ row: row,
46
+ col: -1,
47
+ readonly: true,
48
+ align: 'center',
49
+ value: checked[row],
50
+ valueViewer: CheckViewer,
51
+ className: 'sheet-control'
52
+ });
53
+ }
54
+ columns.forEach(function (colInfo, col) {
55
+ var _colInfo$cellConfig, _colInfo$cellConfig2, _colInfo$cellConfig3;
56
+ var value = item[colInfo.dataIndex || ''];
57
+ rows.push({
58
+ id: rowId,
59
+ value: value,
60
+ record: item,
61
+ readonly: !(colInfo.readonly instanceof Function) ? colInfo.readonly : colInfo.readonly(value, item, row, col),
62
+ align: colInfo.align,
63
+ fixed: colInfo.fixed,
64
+ editable: !(colInfo.editable instanceof Function) ? colInfo.editable : colInfo.editable(value, item, row),
65
+ valueViewer: colInfo.render ? colInfo.render : undefined,
66
+ dataEditor: colInfo.editor ? colInfo.editor : undefined,
67
+ className: !(((_colInfo$cellConfig = colInfo.cellConfig) === null || _colInfo$cellConfig === void 0 ? void 0 : _colInfo$cellConfig.className) instanceof Function) ? (_colInfo$cellConfig2 = colInfo.cellConfig) === null || _colInfo$cellConfig2 === void 0 ? void 0 : _colInfo$cellConfig2.className : (_colInfo$cellConfig3 = colInfo.cellConfig) === null || _colInfo$cellConfig3 === void 0 ? void 0 : _colInfo$cellConfig3.className(value, item, row),
68
+ row: row,
69
+ col: col
70
+ });
71
+ });
72
+ return rows;
73
+ });
24
74
  };
@@ -32,7 +32,7 @@ export declare function renderValue(cell: SheetType.Cell): string | number;
32
32
  export declare const optionsToValuesFromLabelOrValue: (options: SheetType.OptionsType[], val: string) => string[];
33
33
  export declare const optionsTransferToValue: (options: SheetType.OptionsType[], val: string, key?: string) => string[];
34
34
  export declare const valuesTransferToLabel: (options?: SheetType.OptionsType[], value?: string) => string | null;
35
- export declare const groupConfigToGroupMap: (rowGroupConfig?: SheetType.RowGroupConfig) => Map<number, SheetType.RowGroup & {
35
+ export declare const groupConfigToGroupMap: (rowGroupConfig?: Partial<SheetType.RowGroupConfig>) => Map<number, SheetType.RowGroup & {
36
36
  isStart: boolean;
37
37
  isOpen: boolean;
38
38
  }>;
package/dist/core/util.js CHANGED
@@ -308,9 +308,10 @@ export var groupConfigToGroupMap = function groupConfigToGroupMap(rowGroupConfig
308
308
  var groupStart = group.groupStart,
309
309
  groupEnd = group.groupEnd;
310
310
  for (var i = groupStart; i <= groupEnd; i++) {
311
+ var _rowGroupConfig$group2;
311
312
  groupMap.set(i, _objectSpread(_objectSpread({}, group), {}, {
312
313
  isStart: i === groupStart,
313
- isOpen: rowGroupConfig.groupOpen[index]
314
+ isOpen: !!(rowGroupConfig !== null && rowGroupConfig !== void 0 && (_rowGroupConfig$group2 = rowGroupConfig.groupOpen) !== null && _rowGroupConfig$group2 !== void 0 && _rowGroupConfig$group2[index])
314
315
  }));
315
316
  }
316
317
  });
@@ -1,24 +1,36 @@
1
1
  import { MinusSquareOutlined, PlusSquareOutlined } from '@ant-design/icons';
2
2
  import { useSheetEvent } from "../../..";
3
+ import { useGroup } from "../../../hooks/useGroupConfig";
3
4
  import { useCallback } from 'react';
4
5
  import { jsx as _jsx } from "react/jsx-runtime";
5
6
  export var GroupViewer = function GroupViewer(props) {
6
- var value = props.value,
7
- row = props.row,
7
+ var _config$configMap, _config$configMap2, _config$groupOpen;
8
+ var _props$row = props.row,
9
+ row = _props$row === void 0 ? 0 : _props$row,
8
10
  record = props.record;
11
+ var _ref = record || {},
12
+ isHeader = _ref.isHeader;
13
+ var _useGroup = useGroup(),
14
+ config = _useGroup.config;
9
15
  var eventBus = useSheetEvent();
16
+ var isStart = config === null || config === void 0 || (_config$configMap = config.configMap) === null || _config$configMap === void 0 || (_config$configMap = _config$configMap.get(row)) === null || _config$configMap === void 0 ? void 0 : _config$configMap.isStart;
17
+ var isOpen = config === null || config === void 0 || (_config$configMap2 = config.configMap) === null || _config$configMap2 === void 0 || (_config$configMap2 = _config$configMap2.get(row)) === null || _config$configMap2 === void 0 ? void 0 : _config$configMap2.isOpen;
18
+ var allOpen = !(config !== null && config !== void 0 && (_config$groupOpen = config.groupOpen) !== null && _config$groupOpen !== void 0 && _config$groupOpen.some(function (value) {
19
+ return !value;
20
+ }));
10
21
  var handleChange = useCallback(function () {
11
22
  if (!eventBus) return;
12
- if (record !== null && record !== void 0 && record.isHeader) {
13
- eventBus.emit('group-open-title', !(record !== null && record !== void 0 && record.open));
23
+ if (isHeader) {
24
+ eventBus.emit('group-open-title', !allOpen);
14
25
  } else {
15
26
  eventBus.emit('group-open', {
16
27
  row: row,
17
- open: record === null || record === void 0 ? void 0 : record.open
28
+ open: !isOpen
18
29
  });
19
30
  }
20
- }, [eventBus, row, record === null || record === void 0 ? void 0 : record.open]);
21
- if (value) {
31
+ }, [eventBus, row, isOpen, allOpen]);
32
+ if (isStart || isHeader) {
33
+ var currentOpen = record !== null && record !== void 0 && record.isHeader ? allOpen : isOpen;
22
34
  return /*#__PURE__*/_jsx("span", {
23
35
  style: {
24
36
  cursor: 'pointer'
@@ -27,7 +39,7 @@ export var GroupViewer = function GroupViewer(props) {
27
39
  return e.stopPropagation();
28
40
  },
29
41
  onClick: handleChange,
30
- children: !(record !== null && record !== void 0 && record.open) ? /*#__PURE__*/_jsx(PlusSquareOutlined, {
42
+ children: !currentOpen ? /*#__PURE__*/_jsx(PlusSquareOutlined, {
31
43
  rev: undefined
32
44
  }) : /*#__PURE__*/_jsx(MinusSquareOutlined, {
33
45
  rev: undefined
@@ -329,7 +329,7 @@ var BasicSheet = function BasicSheet() {
329
329
  groupConfig: groupConfig,
330
330
  scroll: {
331
331
  x: '100%',
332
- y: '100vh'
332
+ y: 'calc(100vh - 290px)'
333
333
  },
334
334
  sheetRenderer: WrappedTableShell,
335
335
  menuRenderer: MenuRender,
@@ -5,4 +5,4 @@ export type { Dispatch, FunctionAction, NormalAction, } from './useMiddlewareRed
5
5
  export { useMouse } from './useMouse';
6
6
  export { useSetState } from './useSetState';
7
7
  export { SheetEventContext, useSheetEvent } from './useSheetEvent';
8
- export { useWidth, WidthContext } from './useWidthConfig';
8
+ export { WidthContext, useWidth } from './useWidthConfig';
@@ -4,4 +4,4 @@ export { useMiddlewareReducer } from "./useMiddlewareReducer";
4
4
  export { useMouse } from "./useMouse";
5
5
  export { useSetState } from "./useSetState";
6
6
  export { SheetEventContext, useSheetEvent } from "./useSheetEvent";
7
- export { useWidth, WidthContext } from "./useWidthConfig";
7
+ export { WidthContext, useWidth } from "./useWidthConfig";
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import { GroupConfigContext } from '../type/sheet';
3
+ export declare const GroupContext: import("react").Context<GroupConfigContext>;
4
+ export declare function useGroup(): GroupConfigContext;
@@ -0,0 +1,5 @@
1
+ import { createContext, useContext } from 'react';
2
+ export var GroupContext = /*#__PURE__*/createContext({});
3
+ export function useGroup() {
4
+ return useContext(GroupContext);
5
+ }
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
- import { WidthConfig } from '../type/sheet';
3
- export declare const WidthContext: import("react").Context<WidthConfig>;
4
- export declare function useWidth(): WidthConfig;
2
+ import { WidthConfigContext } from '../type/sheet';
3
+ export declare const WidthContext: import("react").Context<WidthConfigContext>;
4
+ export declare function useWidth(): WidthConfigContext;
@@ -1,6 +1,6 @@
1
- /// <reference types="react" />
2
1
  /// <reference types="node" />
3
2
  import type { EventEmitter } from 'events';
3
+ import { CSSProperties } from 'react';
4
4
  import { SheetTableType, SheetType } from '.';
5
5
  export declare enum CellAlign {
6
6
  left = "left",
@@ -10,6 +10,8 @@ export declare enum CellAlign {
10
10
  }
11
11
  export type Cell = {
12
12
  id: string;
13
+ row?: number;
14
+ col?: number;
13
15
  key?: string;
14
16
  readonly?: boolean;
15
17
  component?: CellViewer;
@@ -68,6 +70,10 @@ export type RowGroupConfig = {
68
70
  groups: RowGroup[];
69
71
  groupOpen: boolean[];
70
72
  };
73
+ export type GroupMap = Map<number, SheetType.RowGroup & {
74
+ isStart: boolean;
75
+ isOpen: boolean;
76
+ }>;
71
77
  export type MenuRenderProps = {
72
78
  position?: {
73
79
  top: number;
@@ -96,7 +102,7 @@ export type SheetProps = {
96
102
  freePaste?: boolean;
97
103
  virtualized?: boolean;
98
104
  showBackEdit?: boolean;
99
- backEditStyle?: Partial<CSSStyleDeclaration>;
105
+ backEditStyle?: Partial<CSSProperties>;
100
106
  sticky?: boolean;
101
107
  groupConfig?: RowGroupConfig;
102
108
  onCellsChanged?: CellChangeHandler;
@@ -109,10 +115,16 @@ export type SheetProps = {
109
115
  rowClassName?: string | ((record: Record<string, unknown>, index: number) => string);
110
116
  children?: any[];
111
117
  };
112
- export type WidthConfig = {
118
+ export type WidthConfigContext = {
113
119
  onChange?: (value: Record<number | string, number>) => void;
114
120
  widths?: Record<number | string, number>;
115
121
  };
122
+ export type GroupConfigContext = {
123
+ onChange?: (value: RowGroupConfig) => void;
124
+ config?: RowGroupConfig & {
125
+ configMap: GroupMap;
126
+ };
127
+ };
116
128
  export type SheetShell = Pick<SheetTableType.TableProps, 'columns'> & {
117
129
  className?: string;
118
130
  showGroup?: boolean;
@@ -1,4 +1,4 @@
1
- import { ReactNode } from 'react';
1
+ import { CSSProperties, ReactNode } from 'react';
2
2
  import { SheetType } from '.';
3
3
  export type refAssertion = {
4
4
  contains?: (target: EventTarget | null) => boolean;
@@ -79,7 +79,7 @@ export type TableProps = {
79
79
  draggable?: boolean;
80
80
  freePaste?: boolean;
81
81
  showBackEdit?: boolean;
82
- backEditStyle?: Partial<CSSStyleDeclaration>;
82
+ backEditStyle?: Partial<CSSProperties>;
83
83
  rowSelection?: TableRowSelection;
84
84
  groupConfig?: TableGroupConfig;
85
85
  menuRenderer?: React.FC<SheetType.MenuRenderProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.1.26",
3
+ "version": "0.1.28",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",