@zhenliang/sheet 0.1.89-beta.0 → 0.1.89-beta.2

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.
@@ -4,24 +4,26 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
4
4
  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; }
5
5
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
6
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
- import { isNil } from 'lodash';
8
- import { getNextVisibleRow, groupConfigToGroupMap, stripRowIndex, getRowHeight, getColWidth } from "../util";
7
+ import { get, isNil } from 'lodash';
8
+ import { getNextVisibleRow, groupConfigToGroupMap, stripRowIndex } from "../util";
9
9
  export var keyboardReducer = {
10
10
  move: function move(state, payload) {
11
11
  var _state$start, _state$start2, _data;
12
12
  var _ref = payload,
13
13
  optRow = _ref.row,
14
- optCol = _ref.col;
14
+ optCol = _ref.col,
15
+ elementRef = _ref.elementRef;
15
16
  var groupConfig = state.groupConfig,
16
- elementRef = state.elementRef,
17
17
  _state$data = state.data,
18
- data = _state$data === void 0 ? [] : _state$data;
18
+ data = _state$data === void 0 ? [] : _state$data,
19
+ rowHeight = state.rowHeight,
20
+ cloWidthArray = state.cloWidthArray;
19
21
  var newRow = (((_state$start = state.start) === null || _state$start === void 0 ? void 0 : _state$start.row) || 0) + optRow;
20
22
  var newCol = (((_state$start2 = state.start) === null || _state$start2 === void 0 ? void 0 : _state$start2.col) || 0) + optCol;
21
23
  // 越界判断
22
24
  var maxRow = (data === null || data === void 0 || (_data = data[data.length - 1]) === null || _data === void 0 ? void 0 : _data[0].row) || data.length - 1;
23
25
  var maxCol = data[0].length - 1;
24
- var minCol = -1;
26
+ var minCol = 0;
25
27
  data[0].forEach(function (item) {
26
28
  var itemCol = item.col;
27
29
  if (itemCol < 0) {
@@ -34,21 +36,21 @@ export var keyboardReducer = {
34
36
  // 跟随滚动
35
37
  if (optCol !== 0) {
36
38
  var _elementRef$current;
37
- var colWidth = getColWidth(elementRef.current, newCol);
39
+ var colWidth = get(cloWidthArray.current, newCol);
38
40
  elementRef === null || elementRef === void 0 || (_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 || _elementRef$current.scrollBy({
39
41
  left: colWidth * optCol
40
42
  });
41
43
  }
42
44
  if (optRow !== 0) {
43
45
  var _elementRef$current2;
44
- var rowHeight = getRowHeight(elementRef.current);
45
- var itemHeight = rowHeight;
46
+ var itemHeight = rowHeight.current;
46
47
  elementRef === null || elementRef === void 0 || (_elementRef$current2 = elementRef.current) === null || _elementRef$current2 === void 0 || _elementRef$current2.scrollBy({
47
48
  top: itemHeight * optRow
48
49
  });
49
50
  }
50
51
  if (groupConfig && optRow !== 0) {
51
- newRow = getNextVisibleRow(newRow, maxRow, groupConfigToGroupMap(groupConfig), optRow);
52
+ var _getNextVisibleRow;
53
+ newRow = (_getNextVisibleRow = getNextVisibleRow(newRow, maxRow, groupConfigToGroupMap(groupConfig), optRow)) !== null && _getNextVisibleRow !== void 0 ? _getNextVisibleRow : 0;
52
54
  }
53
55
  var currentPos = {
54
56
  row: newRow,
@@ -21,7 +21,7 @@ import { isEmpty, isNil, isNumber } from 'lodash';
21
21
  import { SheetEventContext, useEventBus, useMiddlewareReducer } from "../../hooks";
22
22
  import sheetReducer from "../reducers";
23
23
  import { sideEffectReducer } from "../reducers/sideEffectReducer";
24
- import { classNames, getRowHeight, rowToActualRow } from "../util";
24
+ import { classNames, rowToActualRow } from "../util";
25
25
  import { Control } from "./Control";
26
26
  import { DefaultRowMapper } from "./DefaultRowMapper";
27
27
  import { Menu } from "./Menu";
@@ -87,6 +87,8 @@ var Sheet = function Sheet(props) {
87
87
  _useState4 = _slicedToArray(_useState3, 2),
88
88
  isScrolledToEnd = _useState4[0],
89
89
  setIsScrolledToEnd = _useState4[1];
90
+ var rowHeight = useRef(30);
91
+ var cloWidthArray = useRef([]);
90
92
  var sheetWrapperRef = useRef(null);
91
93
  var contextMenuRef = useRef(null);
92
94
  var eventBus = useEventBus();
@@ -100,7 +102,8 @@ var Sheet = function Sheet(props) {
100
102
  groupConfig: groupConfig,
101
103
  eventBus: eventBus,
102
104
  fixedInfo: [],
103
- elementRef: sheetWrapperRef
105
+ rowHeight: rowHeight,
106
+ cloWidthArray: cloWidthArray
104
107
  },
105
108
  // [ReduxThunk, ReduxLogger],
106
109
  [ReduxThunk]),
@@ -121,10 +124,9 @@ var Sheet = function Sheet(props) {
121
124
  var container = sheetWrapperRef.current;
122
125
  if (!start && isNil(row)) return;
123
126
  var actual = rowToActualRow((_ref = row) !== null && _ref !== void 0 ? _ref : start === null || start === void 0 ? void 0 : start.row, groupConfig, data.length);
124
- var rowHeight = getRowHeight(container);
125
127
  var firstRowCell = container.querySelector("td.cell[data-col='".concat(start === null || start === void 0 ? void 0 : start.col, "']"));
126
128
  var colPosition = firstRowCell ? firstRowCell.offsetLeft - firstRowCell.clientWidth : 0;
127
- var scrollHeight = actual * rowHeight;
129
+ var scrollHeight = actual * rowHeight.current;
128
130
  (_sheetWrapperRef$curr = sheetWrapperRef.current) === null || _sheetWrapperRef$curr === void 0 || _sheetWrapperRef$curr.scrollTo(isNumber(row) ? 0 : colPosition, scrollHeight);
129
131
 
130
132
  // 最后一行的bug暂时用 scroll end 事件来处理
@@ -154,10 +156,9 @@ var Sheet = function Sheet(props) {
154
156
  var container = sheetWrapperRef.current;
155
157
  if (isNil(row) || isNil(col)) return;
156
158
  var actual = rowToActualRow(row, groupConfig, data.length);
157
- var rowHeight = getRowHeight(container);
158
159
  var firstRowCell = container.querySelector("td.cell[data-col='".concat(col, "']"));
159
160
  var colPosition = firstRowCell ? firstRowCell.offsetLeft - firstRowCell.clientWidth : 0;
160
- var scrollHeight = actual * rowHeight;
161
+ var scrollHeight = actual * rowHeight.current;
161
162
  if (nextLoop) {
162
163
  setTimeout(function () {
163
164
  var _sheetWrapperRef$curr4;
@@ -245,6 +246,16 @@ var Sheet = function Sheet(props) {
245
246
  }
246
247
  });
247
248
  }, [onCellsChanged, data, freePaste, groupConfig, freeze]);
249
+ useEffect(function () {
250
+ var _sheetWrapperRef$curr6, _sheetWrapperRef$curr7, _sheetWrapperRef$curr8;
251
+ if (!sheetWrapperRef.current) {
252
+ return;
253
+ }
254
+ rowHeight.current = ((_sheetWrapperRef$curr6 = sheetWrapperRef.current) === null || _sheetWrapperRef$curr6 === void 0 || (_sheetWrapperRef$curr6 = _sheetWrapperRef$curr6.getElementsByTagName('thead')[0]) === null || _sheetWrapperRef$curr6 === void 0 ? void 0 : _sheetWrapperRef$curr6.clientHeight) || 30;
255
+ cloWidthArray.current = Array.from((_sheetWrapperRef$curr7 = (_sheetWrapperRef$curr8 = sheetWrapperRef.current) === null || _sheetWrapperRef$curr8 === void 0 ? void 0 : _sheetWrapperRef$curr8.getElementsByTagName('col')) !== null && _sheetWrapperRef$curr7 !== void 0 ? _sheetWrapperRef$curr7 : []).map(function (item) {
256
+ return item.clientWidth;
257
+ });
258
+ }, [sheetWrapperRef.current]);
248
259
  useCellEvent(dispatch, state);
249
260
  useMouseEvent(dispatch, sheetWrapperRef);
250
261
  useKeyBoardEvent(dispatch, sheetWrapperRef);
@@ -271,9 +282,9 @@ var Sheet = function Sheet(props) {
271
282
  useEffect(function () {
272
283
  if (!state.editing && state.start) {
273
284
  setTimeout(function () {
274
- var _sheetWrapperRef$curr6;
285
+ var _sheetWrapperRef$curr9;
275
286
  // 表格获取焦点 + 接收keyboard event
276
- (_sheetWrapperRef$curr6 = sheetWrapperRef.current) === null || _sheetWrapperRef$curr6 === void 0 || _sheetWrapperRef$curr6.focus({
287
+ (_sheetWrapperRef$curr9 = sheetWrapperRef.current) === null || _sheetWrapperRef$curr9 === void 0 || _sheetWrapperRef$curr9.focus({
277
288
  preventScroll: true
278
289
  });
279
290
  }, 1);
@@ -1,3 +1,9 @@
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 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; }
3
+ 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; }
4
+ 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; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
1
7
  import { useKeyBoard } from "../..";
2
8
  import { sideEffectReducer } from "../reducers/sideEffectReducer";
3
9
  import { ensureFocus, isSearchElement } from "../util";
@@ -7,7 +13,9 @@ export var useKeyBoardEvent = function useKeyBoardEvent(dispatch, elementRef) {
7
13
  e.preventDefault();
8
14
  dispatch({
9
15
  type: 'move',
10
- payload: value
16
+ payload: _objectSpread(_objectSpread({}, value), {}, {
17
+ elementRef: elementRef
18
+ })
11
19
  });
12
20
  },
13
21
  selectAll: function selectAll(e) {
@@ -43,7 +43,7 @@ export declare const changeGroupConfig: (rowGroupConfig: SheetType.RowGroupConfi
43
43
  export declare const rowToActualRow: (row: number, groupConfig?: SheetType.RowGroupConfig, max?: number) => number;
44
44
  export declare const rowToCountRow: (row: number, groupConfig: SheetType.RowGroupConfig, max: number) => number;
45
45
  export declare const getRowHeight: (container: HTMLSpanElement) => number;
46
- export declare const getColWidth: (container: HTMLSpanElement, col: number) => any;
46
+ export declare const getColWidth: (container: HTMLSpanElement, col: number) => number;
47
47
  export declare const getNextVisibleRow: (row: number, maxRow: number, groupMap?: Map<number, SheetType.RowGroup & {
48
48
  isStart: boolean;
49
49
  isOpen: boolean;
package/dist/core/util.js CHANGED
@@ -16,7 +16,6 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
16
16
 
17
17
  import { message } from 'antd';
18
18
  import { cloneDeep, get, isNil, range } from 'lodash';
19
- var colElements;
20
19
  export function findParentTd(el) {
21
20
  if (!el) return null;
22
21
  if (el.tagName === 'TD') return el;
@@ -418,16 +417,13 @@ export var getRowHeight = function getRowHeight(container) {
418
417
  return h;
419
418
  };
420
419
  export var getColWidth = function getColWidth(container, col) {
421
- if (isNil(colElements)) {
422
- colElements = container.getElementsByTagName('col');
423
- }
420
+ var colElements = container.getElementsByTagName('col');
424
421
  if (colElements[col]) {
425
422
  return colElements[col].clientWidth;
426
423
  }
427
424
  return 0;
428
425
  };
429
426
  export var getNextVisibleRow = function getNextVisibleRow(row, maxRow, groupMap) {
430
- var _groupMap$get15, _groupMap$get16;
431
427
  var up = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
432
428
  if (!(groupMap !== null && groupMap !== void 0 && groupMap.size)) {
433
429
  return row;
@@ -435,17 +431,20 @@ export var getNextVisibleRow = function getNextVisibleRow(row, maxRow, groupMap)
435
431
  if (row > maxRow || row < 0) {
436
432
  return null;
437
433
  }
438
- var isClose = !((_groupMap$get15 = groupMap.get(row)) !== null && _groupMap$get15 !== void 0 && _groupMap$get15.isOpen);
439
- var isChild = !((_groupMap$get16 = groupMap.get(row)) !== null && _groupMap$get16 !== void 0 && _groupMap$get16.isStart);
434
+ var rowInfo = groupMap.get(row);
435
+ var isClose = !(rowInfo !== null && rowInfo !== void 0 && rowInfo.isOpen);
436
+ var isChild = !(rowInfo !== null && rowInfo !== void 0 && rowInfo.isStart);
440
437
  // 向下 如果遇到了一个关着的 并且 是子行 直接返回他的最后一行+1
441
- if (groupMap.get(row) && isClose && isChild && up > 0) {
442
- var _groupMap$get17;
443
- return ((_groupMap$get17 = groupMap.get(row)) === null || _groupMap$get17 === void 0 ? void 0 : _groupMap$get17.groupEnd) + 1;
438
+ if (rowInfo && isClose && up > 0 && isChild) {
439
+ var newRow = rowInfo.groupEnd + 1;
440
+ if (rowInfo.groupEnd === maxRow) {
441
+ newRow = rowInfo.groupStart;
442
+ }
443
+ return newRow;
444
444
  }
445
445
  // 向上 如果遇到了一个关着的直接返回他的主行
446
- if (groupMap.get(row) && isClose) {
447
- var _groupMap$get18;
448
- return (_groupMap$get18 = groupMap.get(row)) === null || _groupMap$get18 === void 0 ? void 0 : _groupMap$get18.groupStart;
446
+ if (rowInfo && isClose) {
447
+ return rowInfo.groupStart;
449
448
  }
450
449
  return row;
451
450
  };
@@ -154,6 +154,21 @@ var data = [{
154
154
  name: 'Not Expandable',
155
155
  age: 29,
156
156
  address: 'Jiangsu No. 1 Lake Park'
157
+ }, {
158
+ key: 9,
159
+ id: 9,
160
+ name: 'Jim Green',
161
+ age: 42,
162
+ address: 'London No. 1 Lake Park',
163
+ children: [{
164
+ id: 91,
165
+ key: '9-1',
166
+ parentId: 9
167
+ }, {
168
+ id: 92,
169
+ key: '9-2',
170
+ parentId: 9
171
+ }]
157
172
  }];
158
173
  var App = function App() {
159
174
  var _useState = useState(data),
@@ -240,7 +240,8 @@ export type UpdateStateType = {
240
240
  };
241
241
  cellChangeHandler: (cells: CellData[], additions: CellData[] | undefined, type: ChangeType) => void;
242
242
  fixedInfo: SheetType.FixedInfo[];
243
- elementRef: any;
243
+ rowHeight: any;
244
+ cloWidthArray: any;
244
245
  } & SearchState;
245
246
  export type UpdateFocus = (start: CellPosition, end: CellPosition) => void;
246
247
  export type Options<T = any> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.1.89-beta.0",
3
+ "version": "0.1.89-beta.2",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",