@lemon-fe/components 1.4.17-alpha.0 → 1.4.17-alpha.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.
@@ -1,4 +1,9 @@
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
+ 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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
7
  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
8
  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
9
  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; }
@@ -21,6 +26,8 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
21
26
  focusColumn = props.focusColumn,
22
27
  _props$clearSelection = props.clearSelection,
23
28
  clearSelection = _props$clearSelection === void 0 ? true : _props$clearSelection;
29
+ // 单行搜索大致耗时
30
+ var duration = useRef(0);
24
31
  var defaultState = useMemo(function () {
25
32
  return {
26
33
  keywords: '',
@@ -39,18 +46,7 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
39
46
  _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 1),
40
47
  dataGridLocale = _useLocaleReceiver2[0];
41
48
  var searching = useRef(false);
42
- var handleSearch = useDebounce(function (str) {
43
- var keywords = str.trim();
44
- var finish = function finish(newState) {
45
- searching.current = false;
46
- setState(function (pre) {
47
- return _objectSpread(_objectSpread({}, pre), newState);
48
- });
49
- return newState;
50
- };
51
- if (keywords.length <= 0) {
52
- return finish(defaultState);
53
- }
49
+ var getCols = function getCols() {
54
50
  var cols = [];
55
51
  if (searchFields) {
56
52
  searchFields.forEach(function (col) {
@@ -64,46 +60,96 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
64
60
  } else {
65
61
  cols = grid.columnApi.getColumns() || [];
66
62
  }
63
+ return cols;
64
+ };
65
+ var checkNodes = function checkNodes(keywords, node, cols) {
66
+ for (var i = 0; i < cols.length; i++) {
67
+ var col = cols[i];
68
+ var value = grid.api.getValue(col, node);
69
+ if (value !== undefined && value !== null && typeof value === 'string' && value.toUpperCase().includes(keywords.toUpperCase())) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ };
75
+ var handleSearch = useDebounce(function (str, prevState) {
76
+ var keywords = str.trim();
77
+ var start = performance.now();
78
+ var finish = function finish(newState) {
79
+ searching.current = false;
80
+ setState(function (pre) {
81
+ return _objectSpread(_objectSpread({}, pre), newState);
82
+ });
83
+ return newState;
84
+ };
85
+ if (keywords.length <= 0) {
86
+ return finish(defaultState);
87
+ }
88
+ var cols = getCols();
67
89
  if (cols.length <= 0) {
68
90
  return;
69
91
  }
70
92
  var nodes = [];
93
+ var count = 0;
94
+ var index = -1;
95
+ var prevNodes = new Map();
96
+ if (prevState) {
97
+ for (var i = prevState.index; i >= 0; i--) {
98
+ prevNodes.set(prevState.nodes[i].id, undefined);
99
+ }
100
+ }
71
101
  grid.api.forEachNodeAfterFilterAndSort(function (node) {
72
- var idx = node.rowIndex;
73
- if (idx === null) {
102
+ var id = node.id;
103
+ count += 1;
104
+ if (id === undefined) {
74
105
  return;
75
106
  }
76
- for (var i = 0; i < cols.length; i++) {
77
- var col = cols[i];
78
- var value = grid.api.getValue(col, node);
79
- if (value !== undefined && value !== null && typeof value === 'string' && value.toUpperCase().includes(keywords.toUpperCase())) {
80
- nodes.push({
81
- index: idx,
82
- id: node.id
83
- });
84
- break;
107
+ if (checkNodes(keywords, node, cols)) {
108
+ nodes.push({
109
+ id: id
110
+ });
111
+ if (prevNodes.has(id)) {
112
+ prevNodes.set(id, nodes.length - 1);
85
113
  }
86
114
  }
87
115
  });
116
+ var _iterator = _createForOfIteratorHelper(prevNodes.entries()),
117
+ _step;
118
+ try {
119
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
120
+ var item = _step.value;
121
+ var _item = _slicedToArray(item, 2),
122
+ _i = _item[1];
123
+ if (_i !== undefined) {
124
+ index = _i;
125
+ break;
126
+ }
127
+ }
128
+ } catch (err) {
129
+ _iterator.e(err);
130
+ } finally {
131
+ _iterator.f();
132
+ }
133
+ duration.current = (performance.now() - start) / count;
88
134
  return finish({
89
135
  nodes: nodes,
90
- index: -1
136
+ index: index
91
137
  });
92
138
  }, 300);
93
- var focusCell = function focusCell(index) {
94
- var node = grid.api.getModel().getRow(index);
95
- if (node === undefined) {
139
+ var focusCell = function focusCell(id) {
140
+ var node = grid.api.getRowNode(id);
141
+ if (node === undefined || node.rowIndex === null) {
96
142
  return;
97
143
  }
98
- grid.api.ensureIndexVisible(index);
144
+ grid.api.ensureNodeVisible(node);
99
145
  var col = grid.columnApi.getAllDisplayedColumns()[0];
100
146
  if (focusColumn) {
101
147
  col = grid.columnApi.getColumn(focusColumn);
102
148
  if (col !== null) {
103
- grid.api.setFocusedCell(index, col);
149
+ grid.api.setFocusedCell(node.rowIndex, col);
104
150
  if (col.isCellEditable(node)) {
105
151
  grid.api.startEditingCell({
106
- rowIndex: index,
152
+ rowIndex: node.rowIndex,
107
153
  colKey: col
108
154
  });
109
155
  }
@@ -117,7 +163,7 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
117
163
  return;
118
164
  }
119
165
  var idx = (state.index - 1 + state.nodes.length) % state.nodes.length;
120
- focusCell(state.nodes[idx].index);
166
+ focusCell(state.nodes[idx].id);
121
167
  setState(function (prev) {
122
168
  return _objectSpread(_objectSpread({}, prev), {}, {
123
169
  index: idx
@@ -130,7 +176,7 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
130
176
  return;
131
177
  }
132
178
  var idx = (data.index + 1) % data.nodes.length;
133
- focusCell(data.nodes[idx].index);
179
+ focusCell(data.nodes[idx].id);
134
180
  setState(function (prev) {
135
181
  return _objectSpread(_objectSpread({}, prev), {}, {
136
182
  index: idx
@@ -141,6 +187,63 @@ export default /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Search(props,
141
187
  return {
142
188
  reset: function reset() {
143
189
  setState(defaultState);
190
+ },
191
+ updateNode: function updateNode(node) {
192
+ var id = node.id;
193
+ var rowIndex = node.rowIndex;
194
+ if (id === undefined || rowIndex === null) {
195
+ return;
196
+ }
197
+ var index = state.nodes.findIndex(function (item) {
198
+ return item.id === id;
199
+ });
200
+ var checked = checkNodes(state.keywords, node, getCols());
201
+ if (index >= 0) {
202
+ if (!checked) {
203
+ var newNodes = _toConsumableArray(state.nodes);
204
+ var newIndex = state.index;
205
+ newNodes.splice(index, 1);
206
+ if (index <= state.index) {
207
+ newIndex = state.index - 1;
208
+ }
209
+ setState(function (prev) {
210
+ return _objectSpread(_objectSpread({}, prev), {}, {
211
+ nodes: newNodes,
212
+ index: newIndex
213
+ });
214
+ });
215
+ }
216
+ } else {
217
+ if (checked) {
218
+ var _newNodes = _toConsumableArray(state.nodes);
219
+ var idx = 0;
220
+ for (var i = 0; i < _newNodes.length; i++) {
221
+ var currNode = grid.api.getRowNode(_newNodes[i].id);
222
+ if (currNode === undefined || currNode.rowIndex === null) {
223
+ continue;
224
+ }
225
+ if (rowIndex <= currNode.rowIndex) {
226
+ idx = i;
227
+ break;
228
+ }
229
+ }
230
+ _newNodes.splice(idx, 0, {
231
+ id: id
232
+ });
233
+ setState(function (prev) {
234
+ return _objectSpread(_objectSpread({}, prev), {}, {
235
+ nodes: _newNodes
236
+ });
237
+ });
238
+ }
239
+ }
240
+ },
241
+ update: function update(rows) {
242
+ // 如果预估搜索时间超过100ms,则不进行搜索
243
+ if (duration.current * rows > 100) {
244
+ return;
245
+ }
246
+ handleSearch(state.keywords, state);
144
247
  }
145
248
  };
146
249
  });
@@ -17,7 +17,7 @@ declare const Editors: {
17
17
  Text: React.ForwardRefExoticComponent<import("ag-grid-community").ICellEditorParams<any, any, any> & import("..").TextEditorParams<any> & React.RefAttributes<import("ag-grid-react").ICellEditorReactComp>>;
18
18
  Date: React.ForwardRefExoticComponent<import("ag-grid-community").ICellEditorParams<any, any, any> & import("..").DateEditorParams<any> & React.RefAttributes<import("ag-grid-react").ICellEditorReactComp>>;
19
19
  Number: React.ForwardRefExoticComponent<import("ag-grid-community").ICellEditorParams<any, any, any> & import("..").NumberEditorParams<any> & React.RefAttributes<import("ag-grid-react").ICellEditorReactComp>>;
20
- Select: React.ForwardRefExoticComponent<import("ag-grid-community").ICellEditorParams<any, any, any> & Pick<import("antd").SelectProps<any, import("antd/lib/select").BaseOptionType>, "disabled" | "mode" | "virtual" | "allowClear" | "showSearch" | "optionFilterProp" | "options" | "listHeight"> & {
20
+ Select: React.ForwardRefExoticComponent<import("ag-grid-community").ICellEditorParams<any, any, any> & Pick<import("antd").SelectProps<any, import("antd/lib/select").BaseOptionType>, "disabled" | "allowClear" | "mode" | "showSearch" | "virtual" | "optionFilterProp" | "options" | "listHeight"> & {
21
21
  fieldNames?: {
22
22
  label: string;
23
23
  value: string;
@@ -389,11 +389,12 @@ var InternalDataGrid = /*#__PURE__*/function (_Component) {
389
389
  * 这么做是为了解决可编辑表格直接修改了原始数据的问题
390
390
  */
391
391
  _this.dataSource = _toConsumableArray(_this.dataSource);
392
- (_this$searchRef$curre = _this.searchRef.current) === null || _this$searchRef$curre === void 0 || _this$searchRef$curre.reset();
393
392
  onDataSourceChange(_this.dataSource, evt);
393
+ (_this$searchRef$curre = _this.searchRef.current) === null || _this$searchRef$curre === void 0 || _this$searchRef$curre.updateNode(evt.node);
394
394
  }
395
395
  });
396
396
  _defineProperty(_assertThisInitialized(_this), "rowDataUpdated", function (evt) {
397
+ var _this$searchRef$curre2;
397
398
  if (!_this.isReady) {
398
399
  return;
399
400
  }
@@ -401,14 +402,23 @@ var InternalDataGrid = /*#__PURE__*/function (_Component) {
401
402
  onDataSourceChange = _this$props3.onDataSourceChange,
402
403
  onRowDataUpdated = _this$props3.onRowDataUpdated;
403
404
  var data = [];
405
+ var hasDataChanged = false;
404
406
  if (onRowDataUpdated !== undefined) {
405
407
  onRowDataUpdated(evt);
406
408
  }
407
409
  _this.api.forEachNode(function (rowNode) {
408
410
  if (rowNode.data !== undefined) {
409
411
  data.push(rowNode.data);
412
+ if (rowNode.data !== _this.dataSource[data.length - 1]) {
413
+ hasDataChanged = true;
414
+ }
410
415
  }
411
416
  });
417
+
418
+ // 检查是否有行被删除
419
+ if (data.length !== _this.dataSource.length) {
420
+ hasDataChanged = true;
421
+ }
412
422
  if (_this.isClientMode()) {
413
423
  var _this$state = _this.state,
414
424
  page = _this$state.page,
@@ -422,18 +432,13 @@ var InternalDataGrid = /*#__PURE__*/function (_Component) {
422
432
  total: data.length
423
433
  });
424
434
  }
425
-
426
- /**
427
- * 浅比较是为了防止通过设置props.dataSource触发onDataSourceChange
428
- */
429
- if (!shallowEqual(data, _this.dataSource)) {
435
+ if (hasDataChanged) {
430
436
  _this.dataSource = data;
431
437
  if (onDataSourceChange !== undefined) {
432
- var _this$searchRef$curre2;
433
- (_this$searchRef$curre2 = _this.searchRef.current) === null || _this$searchRef$curre2 === void 0 || _this$searchRef$curre2.reset();
434
438
  onDataSourceChange(data, evt);
435
439
  }
436
440
  }
441
+ (_this$searchRef$curre2 = _this.searchRef.current) === null || _this$searchRef$curre2 === void 0 || _this$searchRef$curre2.update(data.length);
437
442
  _this.updateSelection(data);
438
443
  });
439
444
  _defineProperty(_assertThisInitialized(_this), "cellEditingStop", function (evt) {
@@ -145,6 +145,8 @@ export interface DataGridSearch {
145
145
  }
146
146
  export interface DataGridSearchRef {
147
147
  reset: () => void;
148
+ updateNode: (node: IRowNode<any>) => void;
149
+ update: (rows: number) => void;
148
150
  }
149
151
  export interface CustomColumnData {
150
152
  fields: FieldCol[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lemon-fe/components",
3
- "version": "1.4.17-alpha.0",
3
+ "version": "1.4.17-alpha.1",
4
4
  "description": "> TODO: description",
5
5
  "homepage": "",
6
6
  "license": "MIT",
@@ -58,5 +58,5 @@
58
58
  "publishConfig": {
59
59
  "registry": "https://registry.npmjs.org"
60
60
  },
61
- "gitHead": "f6be5d166796ba535cd2759db280a58be898f27f"
61
+ "gitHead": "dde1bbf2ccb15cbde7734f528a685f968f91dcf9"
62
62
  }