@nokecy/qc-ui 1.0.67 → 1.0.68

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.
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useRowDrag = exports.useKeyboardNavigation = exports.useCellFocus = void 0;
7
+ var _react = require("react");
8
+ 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; } } }; }
9
+ 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); }
10
+ 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; }
11
+ /**
12
+ * 单元格聚焦 Hook
13
+ * 处理 Editable 包装和普通输入框的聚焦逻辑
14
+ */
15
+ var useCellFocus = exports.useCellFocus = function useCellFocus() {
16
+ var focusInputInCell = (0, _react.useCallback)(function (cell) {
17
+ // 首先查找Editable包装的元素
18
+ var editableElement = cell.querySelector('.ant-formily-editable');
19
+ if (editableElement) {
20
+ // 检查Editable是否已经处于编辑状态
21
+ var isEditing = editableElement.getAttribute('data-editable-state') === 'editing';
22
+ if (isEditing) {
23
+ // 如果已经处于编辑状态,查找输入框并直接聚焦
24
+ var existingInput = editableElement.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
25
+ if (existingInput) {
26
+ existingInput.focus();
27
+ if ('setSelectionRange' in existingInput && existingInput.type !== 'number' && existingInput.type !== 'date') {
28
+ var _existingInput$value;
29
+ var length = ((_existingInput$value = existingInput.value) === null || _existingInput$value === void 0 ? void 0 : _existingInput$value.length) || 0;
30
+ setTimeout(function () {
31
+ existingInput.setSelectionRange(length, length);
32
+ }, 10);
33
+ }
34
+ }
35
+ } else {
36
+ // 如果还没有进入编辑状态,触发编辑状态
37
+ editableElement.click();
38
+ var focusEvent = new Event('focus', {
39
+ bubbles: true
40
+ });
41
+ editableElement.dispatchEvent(focusEvent);
42
+
43
+ // 延迟查找输入框并设置光标
44
+ setTimeout(function () {
45
+ var newInput = editableElement.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
46
+ if (newInput) {
47
+ newInput.focus();
48
+ if ('setSelectionRange' in newInput && newInput.type !== 'number' && newInput.type !== 'date') {
49
+ var _newInput$value;
50
+ var _length = ((_newInput$value = newInput.value) === null || _newInput$value === void 0 ? void 0 : _newInput$value.length) || 0;
51
+ setTimeout(function () {
52
+ newInput.setSelectionRange(_length, _length);
53
+ }, 10);
54
+ }
55
+ }
56
+ }, 150);
57
+ }
58
+ } else {
59
+ // 如果没有Editable包装,尝试多种选择器查找输入框
60
+ var inputElement = cell.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
61
+ if (!inputElement) {
62
+ // 尝试更宽泛的选择器
63
+ inputElement = cell.querySelector('input, textarea');
64
+ }
65
+ if (!inputElement) {
66
+ // 尝试查找Ant Design的输入框
67
+ inputElement = cell.querySelector('.ant-input');
68
+ }
69
+ if (!inputElement) {
70
+ // 查找任何可聚焦的元素
71
+ inputElement = cell.querySelector('[contenteditable="true"], [tabindex]');
72
+ }
73
+ if (inputElement) {
74
+ inputElement.focus();
75
+
76
+ // 将光标定位到末尾
77
+ if ('setSelectionRange' in inputElement && inputElement.type !== 'number' && inputElement.type !== 'date') {
78
+ var _inputElement$value;
79
+ var _length2 = ((_inputElement$value = inputElement.value) === null || _inputElement$value === void 0 ? void 0 : _inputElement$value.length) || 0;
80
+ setTimeout(function () {
81
+ inputElement.setSelectionRange(_length2, _length2);
82
+ }, 10);
83
+ }
84
+ } else {
85
+ // 尝试直接聚焦到单元格本身
86
+ cell.focus();
87
+ }
88
+ }
89
+ }, []);
90
+ return {
91
+ focusInputInCell: focusInputInCell
92
+ };
93
+ };
94
+
95
+ /**
96
+ * 键盘导航 Hook
97
+ * 处理方向键在表格单元格间的导航
98
+ */
99
+ var useKeyboardNavigation = exports.useKeyboardNavigation = function useKeyboardNavigation(ref, dataSource, focusInputInCell) {
100
+ var handleCellKeyDown = (0, _react.useCallback)(function (event) {
101
+ var keyEvent = event.event,
102
+ node = event.node,
103
+ colDef = event.colDef,
104
+ api = event.api;
105
+ if (!keyEvent) return;
106
+
107
+ // 处理上下左右键导航
108
+ if (keyEvent.key === 'ArrowDown' || keyEvent.key === 'ArrowUp' || keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight') {
109
+ var currentRowIndex = node.rowIndex;
110
+ var currentColId = colDef.field;
111
+ var targetRowIndex = currentRowIndex;
112
+ var targetColId = currentColId;
113
+
114
+ // 确定目标位置
115
+ if (keyEvent.key === 'ArrowDown') {
116
+ targetRowIndex = currentRowIndex + 1;
117
+ } else if (keyEvent.key === 'ArrowUp') {
118
+ targetRowIndex = currentRowIndex - 1;
119
+ } else if (keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight') {
120
+ // 对于左右键,使用AG-Grid的API获取列信息
121
+ var visibleColumns = api.getAllDisplayedColumns();
122
+ if (visibleColumns && visibleColumns.length > 0) {
123
+ // 找到当前列的索引
124
+ var currentColIndex = visibleColumns.findIndex(function (col) {
125
+ return col.getColId() === currentColId;
126
+ });
127
+ var targetColIndex;
128
+ if (keyEvent.key === 'ArrowLeft') {
129
+ targetColIndex = currentColIndex - 1;
130
+ } else {
131
+ targetColIndex = currentColIndex + 1;
132
+ }
133
+
134
+ // 检查目标列索引是否有效
135
+ if (targetColIndex >= 0 && targetColIndex < visibleColumns.length) {
136
+ targetColId = visibleColumns[targetColIndex].getColId();
137
+ } else {
138
+ return; // 无法移动到目标列
139
+ }
140
+ } else {
141
+ return; // 无列信息
142
+ }
143
+ }
144
+
145
+ // 检查目标位置是否有效
146
+ var isValidTarget = keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight' || targetRowIndex >= 0 && targetRowIndex < dataSource.length;
147
+ if (isValidTarget) {
148
+ // 延迟执行以确保当前编辑完成
149
+ setTimeout(function () {
150
+ api.setFocusedCell(targetRowIndex, targetColId);
151
+
152
+ // 不使用原生startEditingCell,而是手动触发编辑状态
153
+ // 使用DOM选择器查找目标单元格
154
+ setTimeout(function () {
155
+ var _ref$current;
156
+ // 通过ref获取网格容器
157
+ var gridContainer = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.querySelector('.ag-theme-alpine');
158
+ if (gridContainer) {
159
+ // 尝试通过行和列分步查找目标单元格
160
+ var rows = gridContainer.querySelectorAll('.ag-row');
161
+ if (rows[targetRowIndex]) {
162
+ var targetRow = rows[targetRowIndex];
163
+
164
+ // 在行内查找指定列的单元格
165
+ var targetCell = targetRow.querySelector(".ag-cell[col-id=\"".concat(targetColId, "\"]"));
166
+ if (!targetCell) {
167
+ // 如果通过col-id找不到,遍历所有单元格查找匹配的列
168
+ var cells = targetRow.querySelectorAll('.ag-cell');
169
+ var _iterator = _createForOfIteratorHelper(cells),
170
+ _step;
171
+ try {
172
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
173
+ var _cell = _step.value;
174
+ var colId = _cell.getAttribute('col-id') || _cell.getAttribute('data-col-id');
175
+ if (colId === targetColId) {
176
+ targetCell = _cell;
177
+ break;
178
+ }
179
+ }
180
+ } catch (err) {
181
+ _iterator.e(err);
182
+ } finally {
183
+ _iterator.f();
184
+ }
185
+ }
186
+ if (targetCell) {
187
+ focusInputInCell(targetCell);
188
+ }
189
+ }
190
+ }
191
+ }, 150);
192
+ }, 10);
193
+ }
194
+ keyEvent.preventDefault();
195
+ keyEvent.stopPropagation();
196
+ }
197
+ }, [dataSource, focusInputInCell, ref]);
198
+ return {
199
+ handleCellKeyDown: handleCellKeyDown
200
+ };
201
+ };
202
+
203
+ /**
204
+ * 行拖拽 Hook
205
+ * 处理行拖拽排序逻辑
206
+ */
207
+ var useRowDrag = exports.useRowDrag = function useRowDrag(field) {
208
+ var handleRowDragMove = (0, _react.useCallback)(function (event) {
209
+ var movingNode = event.node;
210
+ var overNode = event.overNode;
211
+ var rowNeedsToMove = movingNode !== overNode;
212
+ if (rowNeedsToMove) {
213
+ var movingData = movingNode.data;
214
+ var overData = overNode.data;
215
+ var dataSourceCopy = Array.isArray(field.value) ? field.value.slice() : [];
216
+ var fromIndex = dataSourceCopy.indexOf(movingData);
217
+ var toIndex = dataSourceCopy.indexOf(overData);
218
+ var newStore = dataSourceCopy.slice();
219
+
220
+ // 移动数组元素
221
+ var element = newStore[fromIndex];
222
+ newStore.splice(fromIndex, 1);
223
+ newStore.splice(toIndex, 0, element);
224
+ field.setValue(newStore);
225
+ }
226
+ }, [field]);
227
+ return {
228
+ handleRowDragMove: handleRowDragMove
229
+ };
230
+ };
@@ -10,12 +10,13 @@ var _agGridReact = require("ag-grid-react");
10
10
  var _react2 = require("@formily/react");
11
11
  var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));
12
12
  var _utils = require("./utils");
13
+ var _hooks = require("./hooks");
13
14
  var _arrayBase = _interopRequireDefault(require("./arrayBase"));
14
15
  var _arrayAddBtn = _interopRequireDefault(require("./arrayAddBtn"));
15
16
  var _preview = _interopRequireDefault(require("./preview"));
16
17
  var _antd = require("antd");
17
18
  var _setting = _interopRequireDefault(require("../ag-grid-plus/setting"));
18
- var _agGridPlus = require("../ag-grid-plus");
19
+ var _useGridPlusColumnState = require("../ag-grid-plus/hooks/useGridPlusColumnState");
19
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
21
22
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -24,7 +25,6 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
24
25
  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; }
25
26
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
26
27
  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); }
27
- 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; } } }; }
28
28
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
29
29
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
30
30
  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."); }
@@ -32,6 +32,47 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
32
32
  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; }
33
33
  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; } }
34
34
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
35
+ /**
36
+ * ArrayTable 内置的列状态管理 Hook
37
+ * 使用 localStorage 进行持久化存储
38
+ */
39
+ var useArrayTableColumnState = function useArrayTableColumnState(gridKey) {
40
+ var STORAGE_KEY = 'array-table-column-state';
41
+
42
+ // 从 localStorage 读取所有列状态
43
+ var getColumnStateFromStorage = (0, _react.useCallback)(function () {
44
+ try {
45
+ var stored = localStorage.getItem(STORAGE_KEY);
46
+ return stored ? JSON.parse(stored) : {};
47
+ } catch (error) {
48
+ console.error('Failed to read column state from localStorage:', error);
49
+ return {};
50
+ }
51
+ }, []);
52
+
53
+ // 保存列状态到 localStorage
54
+ var saveColumnStateToStorage = (0, _react.useCallback)(function (key, state) {
55
+ try {
56
+ var allStates = getColumnStateFromStorage();
57
+ allStates[key] = state;
58
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(allStates));
59
+ } catch (error) {
60
+ console.error('Failed to save column state to localStorage:', error);
61
+ }
62
+ }, [getColumnStateFromStorage]);
63
+
64
+ // 获取指定 gridKey 的列状态
65
+ var getColumnState = (0, _react.useCallback)(function (key) {
66
+ if (!key) return undefined;
67
+ var allStates = getColumnStateFromStorage();
68
+ return allStates[key];
69
+ }, [getColumnStateFromStorage]);
70
+ return {
71
+ getColumnState: getColumnState,
72
+ saveColumnState: saveColumnStateToStorage
73
+ };
74
+ };
75
+
35
76
  //@ts-ignore
36
77
  var ArrayTable = (0, _react2.observer)(function (props) {
37
78
  var height = props.height;
@@ -47,7 +88,6 @@ var ArrayTable = (0, _react2.observer)(function (props) {
47
88
  gridApiState = _useState4[0],
48
89
  setGridApiState = _useState4[1];
49
90
  var dataSource = Array.isArray(field.value) ? field.value.slice() : [];
50
- var dataSourceCopy = dataSource.slice();
51
91
  var sources = (0, _utils.useArrayTableSources)();
52
92
  var actions = (0, _utils.useArrayTableActions)();
53
93
  var columns = (0, _utils.useArrayTableColumns)(dataSource, field, sources);
@@ -59,127 +99,85 @@ var ArrayTable = (0, _react2.observer)(function (props) {
59
99
  onMoveDown = props.onMoveDown,
60
100
  onMoveUp = props.onMoveUp;
61
101
  var addition = (0, _utils.useAddition)();
62
- var _useGridPlusColumnSta = (0, _agGridPlus.useGridPlusColumnState)(),
63
- onSaveColumnState = _useGridPlusColumnSta.onSaveColumnState,
64
- gridPlusColumnState = _useGridPlusColumnSta.columnState;
102
+
103
+ // 尝试从 Context 获取列状态管理(如果被 Provider 包裹)
104
+ var contextColumnState = (0, _useGridPlusColumnState.useGridPlusColumnState)();
105
+
106
+ // 使用内置的列状态管理作为后备方案
107
+ var _useArrayTableColumnS = useArrayTableColumnState(props.gridKey),
108
+ getColumnState = _useArrayTableColumnS.getColumnState,
109
+ saveColumnState = _useArrayTableColumnS.saveColumnState;
110
+
111
+ // 使用自定义 hooks
112
+ var _useCellFocus = (0, _hooks.useCellFocus)(),
113
+ focusInputInCell = _useCellFocus.focusInputInCell;
114
+ var _useKeyboardNavigatio = (0, _hooks.useKeyboardNavigation)(ref, dataSource, focusInputInCell),
115
+ handleCellKeyDown = _useKeyboardNavigatio.handleCellKeyDown;
116
+ var _useRowDrag = (0, _hooks.useRowDrag)(field),
117
+ handleRowDragMove = _useRowDrag.handleRowDragMove;
118
+
119
+ // 恢复列状态(优先使用 Context,否则使用内置的 localStorage)
65
120
  (0, _react.useEffect)(function () {
66
- if (props.gridKey && gridPlusColumnState && gridPlusColumnState[props.gridKey] && apiRef.current) {
121
+ var _contextColumnState$c;
122
+ if (!props.gridKey || !gridApiState) {
123
+ return;
124
+ }
125
+ var stateToApply = null;
126
+ var hasRealProvider = (contextColumnState === null || contextColumnState === void 0 ? void 0 : contextColumnState.columnState) !== undefined;
127
+
128
+ // 优先使用 Context 中的列状态(但只有在有真正的 Provider 时)
129
+ if (hasRealProvider && contextColumnState !== null && contextColumnState !== void 0 && (_contextColumnState$c = contextColumnState.columnState) !== null && _contextColumnState$c !== void 0 && _contextColumnState$c[props.gridKey]) {
130
+ stateToApply = contextColumnState.columnState[props.gridKey];
131
+ }
132
+ // 后备方案:使用内置的 localStorage 列状态
133
+ else {
134
+ stateToApply = getColumnState(props.gridKey);
135
+ }
136
+ if (stateToApply && Array.isArray(stateToApply)) {
137
+ // 转换为 AG-Grid 需要的格式
138
+ var formattedState = stateToApply.map(function (col) {
139
+ return {
140
+ colId: col.colId || col.field,
141
+ width: col.width,
142
+ hide: col.hide,
143
+ pinned: col.pinned === 'notPinned' ? null : col.pinned,
144
+ sort: col.sort,
145
+ sortIndex: col.sortIndex,
146
+ aggFunc: col.aggFunc,
147
+ flex: col.flex
148
+ };
149
+ });
67
150
  setTimeout(function () {
68
- apiRef.current.applyColumnState({
69
- state: gridPlusColumnState[props.gridKey],
151
+ gridApiState.applyColumnState({
152
+ state: formattedState,
70
153
  applyOrder: true
71
154
  });
72
- }, 10);
155
+ }, 100);
73
156
  }
74
- }, [gridPlusColumnState, props.gridKey]);
157
+ }, [props.gridKey, gridApiState, contextColumnState === null || contextColumnState === void 0 ? void 0 : contextColumnState.columnState, getColumnState]);
75
158
 
76
159
  //@ts-ignore
77
160
  if (columns.length > 0) {
78
161
  //@ts-ignore
79
162
  columns[0].rowDrag = props.onRowDrag ? true : false;
80
163
  }
81
- var onRowDragMove = (0, _react.useCallback)(function (event) {
82
- var movingNode = event.node;
83
- var overNode = event.overNode;
84
- var rowNeedsToMove = movingNode !== overNode;
85
- if (rowNeedsToMove) {
86
- var movingData = movingNode.data;
87
- var overData = overNode.data;
88
- var fromIndex = dataSourceCopy.indexOf(movingData);
89
- var toIndex = dataSourceCopy.indexOf(overData);
90
- var newStore = dataSourceCopy.slice();
91
- moveInArray(newStore, fromIndex, toIndex);
92
- dataSourceCopy = newStore;
93
- field.setValue(newStore);
94
- apiRef.current.clearFocusedCell();
95
- }
96
- function moveInArray(arr, fromIndex, toIndex) {
97
- var element = arr[fromIndex];
98
- arr.splice(fromIndex, 1);
99
- arr.splice(toIndex, 0, element);
100
- }
101
- }, [field]);
102
- var onColumnStateChanged = function onColumnStateChanged(columnState) {
103
- if (props.gridKey) {
104
- onSaveColumnState && onSaveColumnState(props.gridKey, columnState);
105
- }
106
- };
107
164
 
108
- // 通用的聚焦函数
109
- var focusInputInCell = (0, _react.useCallback)(function (cell) {
110
- // 首先查找Editable包装的元素
111
- var editableElement = cell.querySelector('.ant-formily-editable');
112
- if (editableElement) {
113
- // 检查Editable是否已经处于编辑状态
114
- var isEditing = editableElement.getAttribute('data-editable-state') === 'editing';
115
- if (isEditing) {
116
- // 如果已经处于编辑状态,查找输入框并直接聚焦
117
- var existingInput = editableElement.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
118
- if (existingInput) {
119
- existingInput.focus();
120
- if ('setSelectionRange' in existingInput && existingInput.type !== 'number' && existingInput.type !== 'date') {
121
- var _existingInput$value;
122
- var length = ((_existingInput$value = existingInput.value) === null || _existingInput$value === void 0 ? void 0 : _existingInput$value.length) || 0;
123
- setTimeout(function () {
124
- existingInput.setSelectionRange(length, length);
125
- }, 10);
126
- }
127
- }
128
- } else {
129
- // 如果还没有进入编辑状态,触发编辑状态
130
- editableElement.click();
131
- var focusEvent = new Event('focus', {
132
- bubbles: true
133
- });
134
- editableElement.dispatchEvent(focusEvent);
165
+ // 保存列状态(优先使用 Context,否则使用内置的 localStorage)
166
+ var onColumnStateChanged = function onColumnStateChanged(columnState) {
167
+ if (!props.gridKey) return;
135
168
 
136
- // 延迟查找输入框并设置光标
137
- setTimeout(function () {
138
- var newInput = editableElement.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
139
- if (newInput) {
140
- newInput.focus();
141
- if ('setSelectionRange' in newInput && newInput.type !== 'number' && newInput.type !== 'date') {
142
- var _newInput$value;
143
- var _length = ((_newInput$value = newInput.value) === null || _newInput$value === void 0 ? void 0 : _newInput$value.length) || 0;
144
- setTimeout(function () {
145
- newInput.setSelectionRange(_length, _length);
146
- }, 10);
147
- }
148
- }
149
- }, 150);
150
- }
151
- } else {
152
- // 如果没有Editable包装,尝试多种选择器查找输入框
153
- var inputElement = cell.querySelector('input:not([readonly]):not([disabled]), textarea:not([readonly]):not([disabled])');
154
- if (!inputElement) {
155
- // 尝试更宽泛的选择器
156
- inputElement = cell.querySelector('input, textarea');
157
- }
158
- if (!inputElement) {
159
- // 尝试查找Ant Design的输入框
160
- inputElement = cell.querySelector('.ant-input');
161
- }
162
- if (!inputElement) {
163
- // 查找任何可聚焦的元素
164
- inputElement = cell.querySelector('[contenteditable="true"], [tabindex]');
165
- }
166
- if (inputElement) {
167
- inputElement.focus();
169
+ // 判断是否有真正的 Provider:检查 columnState 是否不是 undefined(默认值)
170
+ var hasRealProvider = (contextColumnState === null || contextColumnState === void 0 ? void 0 : contextColumnState.columnState) !== undefined;
168
171
 
169
- // 将光标定位到末尾
170
- if ('setSelectionRange' in inputElement && inputElement.type !== 'number' && inputElement.type !== 'date') {
171
- var _inputElement$value;
172
- var _length2 = ((_inputElement$value = inputElement.value) === null || _inputElement$value === void 0 ? void 0 : _inputElement$value.length) || 0;
173
- setTimeout(function () {
174
- inputElement.setSelectionRange(_length2, _length2);
175
- }, 10);
176
- }
177
- } else {
178
- // 尝试直接聚焦到单元格本身
179
- cell.focus();
180
- }
172
+ // 优先使用 Context 的保存方法(但只有在有真正的 Provider 时)
173
+ if (hasRealProvider && contextColumnState !== null && contextColumnState !== void 0 && contextColumnState.onSaveColumnState) {
174
+ contextColumnState.onSaveColumnState(props.gridKey, columnState);
181
175
  }
182
- }, []);
176
+ // 后备方案:使用内置的 localStorage 保存
177
+ else {
178
+ saveColumnState(props.gridKey, columnState);
179
+ }
180
+ };
183
181
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
184
182
  ref: ref,
185
183
  style: {
@@ -238,6 +236,38 @@ var ArrayTable = (0, _react2.observer)(function (props) {
238
236
  params.api.hidePopupMenu();
239
237
  apiRef.current = params.api;
240
238
  setGridApiState(params.api);
239
+
240
+ // 立即尝试恢复列状态
241
+ if (props.gridKey) {
242
+ var _contextColumnState$c2;
243
+ var hasRealProvider = (contextColumnState === null || contextColumnState === void 0 ? void 0 : contextColumnState.columnState) !== undefined;
244
+ var stateToApply = null;
245
+ if (hasRealProvider && contextColumnState !== null && contextColumnState !== void 0 && (_contextColumnState$c2 = contextColumnState.columnState) !== null && _contextColumnState$c2 !== void 0 && _contextColumnState$c2[props.gridKey]) {
246
+ stateToApply = contextColumnState.columnState[props.gridKey];
247
+ } else {
248
+ stateToApply = getColumnState(props.gridKey);
249
+ }
250
+ if (stateToApply && Array.isArray(stateToApply)) {
251
+ var formattedState = stateToApply.map(function (col) {
252
+ return {
253
+ colId: col.colId || col.field,
254
+ width: col.width,
255
+ hide: col.hide,
256
+ pinned: col.pinned === 'notPinned' ? null : col.pinned,
257
+ sort: col.sort,
258
+ sortIndex: col.sortIndex,
259
+ aggFunc: col.aggFunc,
260
+ flex: col.flex
261
+ };
262
+ });
263
+ setTimeout(function () {
264
+ params.api.applyColumnState({
265
+ state: formattedState,
266
+ applyOrder: true
267
+ });
268
+ }, 50);
269
+ }
270
+ }
241
271
  },
242
272
  suppressColumnVirtualisation: true,
243
273
  gridOptions: {
@@ -248,104 +278,7 @@ var ArrayTable = (0, _react2.observer)(function (props) {
248
278
  enterNavigatesVertically: true,
249
279
  enterNavigatesVerticallyAfterEdit: true
250
280
  },
251
- onCellKeyDown: function onCellKeyDown(event) {
252
- var keyEvent = event.event,
253
- node = event.node,
254
- colDef = event.colDef,
255
- api = event.api;
256
- if (!keyEvent) return;
257
-
258
- // 处理上下左右键导航
259
- if (keyEvent.key === 'ArrowDown' || keyEvent.key === 'ArrowUp' || keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight') {
260
- var currentRowIndex = node.rowIndex;
261
- var currentColId = colDef.field;
262
- var targetRowIndex = currentRowIndex;
263
- var targetColId = currentColId;
264
-
265
- // 确定目标位置
266
- if (keyEvent.key === 'ArrowDown') {
267
- targetRowIndex = currentRowIndex + 1;
268
- } else if (keyEvent.key === 'ArrowUp') {
269
- targetRowIndex = currentRowIndex - 1;
270
- } else if (keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight') {
271
- // 对于左右键,使用AG-Grid的API获取列信息
272
- var visibleColumns = api.getAllDisplayedColumns();
273
- if (visibleColumns && visibleColumns.length > 0) {
274
- // 找到当前列的索引
275
- var currentColIndex = visibleColumns.findIndex(function (col) {
276
- return col.getColId() === currentColId;
277
- });
278
- var targetColIndex;
279
- if (keyEvent.key === 'ArrowLeft') {
280
- targetColIndex = currentColIndex - 1;
281
- } else {
282
- targetColIndex = currentColIndex + 1;
283
- }
284
-
285
- // 检查目标列索引是否有效
286
- if (targetColIndex >= 0 && targetColIndex < visibleColumns.length) {
287
- targetColId = visibleColumns[targetColIndex].getColId();
288
- } else {
289
- return; // 无法移动到目标列
290
- }
291
- } else {
292
- return; // 无列信息
293
- }
294
- }
295
-
296
- // 检查目标位置是否有效
297
- var isValidTarget = keyEvent.key === 'ArrowLeft' || keyEvent.key === 'ArrowRight' || targetRowIndex >= 0 && targetRowIndex < dataSource.length;
298
- if (isValidTarget) {
299
- // 延迟执行以确保当前编辑完成
300
- setTimeout(function () {
301
- api.setFocusedCell(targetRowIndex, targetColId);
302
-
303
- // 不使用原生startEditingCell,而是手动触发编辑状态
304
- // 使用DOM选择器查找目标单元格
305
- setTimeout(function () {
306
- var _ref$current;
307
- // 通过ref获取网格容器
308
- var gridContainer = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.querySelector('.ag-theme-alpine');
309
- if (gridContainer) {
310
- // 尝试通过行和列分步查找目标单元格
311
- var rows = gridContainer.querySelectorAll('.ag-row');
312
- if (rows[targetRowIndex]) {
313
- var targetRow = rows[targetRowIndex];
314
-
315
- // 在行内查找指定列的单元格
316
- var targetCell = targetRow.querySelector(".ag-cell[col-id=\"".concat(targetColId, "\"]"));
317
- if (!targetCell) {
318
- // 如果通过col-id找不到,遍历所有单元格查找匹配的列
319
- var cells = targetRow.querySelectorAll('.ag-cell');
320
- var _iterator = _createForOfIteratorHelper(cells),
321
- _step;
322
- try {
323
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
324
- var cell = _step.value;
325
- var colId = cell.getAttribute('col-id') || cell.getAttribute('data-col-id');
326
- if (colId === targetColId) {
327
- targetCell = cell;
328
- break;
329
- }
330
- }
331
- } catch (err) {
332
- _iterator.e(err);
333
- } finally {
334
- _iterator.f();
335
- }
336
- }
337
- if (targetCell) {
338
- focusInputInCell(targetCell);
339
- }
340
- }
341
- }
342
- }, 150);
343
- }, 10);
344
- }
345
- keyEvent.preventDefault();
346
- keyEvent.stopPropagation();
347
- }
348
- },
281
+ onCellKeyDown: handleCellKeyDown,
349
282
  onCellClicked: function onCellClicked(event) {
350
283
  var _event$event;
351
284
  // 点击单元格时触发编辑状态
@@ -357,7 +290,7 @@ var ArrayTable = (0, _react2.observer)(function (props) {
357
290
  }
358
291
  }, 10);
359
292
  },
360
- onRowDragMove: onRowDragMove,
293
+ onRowDragMove: handleRowDragMove,
361
294
  onRowDataUpdated: function onRowDataUpdated(e) {
362
295
  if (isAdd) {
363
296
  var _apiRef$current;