@arim-aisdc/public-components 2.3.17 → 2.3.18

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.
@@ -10,24 +10,44 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
10
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
11
  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; } }
12
12
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
- import { useCallback, useEffect, useMemo, useState } from 'react';
13
+ import { useCallback, useEffect, useRef, useState } from 'react';
14
+ import { produce } from 'immer';
15
+ import { debounce } from 'lodash-es';
14
16
  import { getSizeInfo } from "../utils";
15
- import { debounce } from 'lodash';
16
- var CACHE_MAX_AGE = 1 * 60 * 60 * 1000; // 1小时
17
- var defaultMinSize = 120;
18
- var defaultMaxSize = 240;
17
+
18
+ // 可配置的常量
19
+ var DEFAULT_MIN_SIZE = 120;
20
+ var DEFAULT_MAX_SIZE = 240;
21
+ var DEFAULT_RESIZE_DEBOUNCE = 200;
22
+ var DEFAULT_TABLE_PADDING = 10;
23
+ /**
24
+ * 获取表格容器的有效宽度(减去边框)
25
+ */
26
+ var useTableWidth = function useTableWidth(tableContentRef) {
27
+ return useCallback(function () {
28
+ if (!tableContentRef.current) return 0;
29
+ var width = tableContentRef.current.getBoundingClientRect().width;
30
+ var style = window.getComputedStyle(tableContentRef.current);
31
+ var horizontalBorderWidth = parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
32
+ return Math.floor(width - horizontalBorderWidth);
33
+ }, [tableContentRef]);
34
+ };
35
+
36
+ /**
37
+ * 自定义Hook:表格列宽计算和管理
38
+ */
19
39
  export var useColumnWidth = function useColumnWidth(_ref) {
20
40
  var tableContentRef = _ref.tableContentRef,
21
41
  tableId = _ref.tableId,
22
42
  tableKeyPrefixCls = _ref.tableKeyPrefixCls,
23
43
  _ref$columnVisibleCon = _ref.columnVisibleConfig,
24
44
  columnVisibleConfig = _ref$columnVisibleCon === void 0 ? {} : _ref$columnVisibleCon,
25
- tableKey = _ref.tableKey;
45
+ tableKey = _ref.tableKey,
46
+ _ref$cacheMaxAge = _ref.cacheMaxAge,
47
+ cacheMaxAge = _ref$cacheMaxAge === void 0 ? 1 * 60 * 60 * 1000 : _ref$cacheMaxAge;
26
48
  var _useState = useState({
27
49
  columnSizing: {},
28
- //显示列的列宽
29
50
  allColumnSizing: {},
30
- //所有列的列宽
31
51
  allColumnCount: 0,
32
52
  tableContainerWidth: undefined,
33
53
  initColumnsSizeMap: {}
@@ -35,329 +55,244 @@ export var useColumnWidth = function useColumnWidth(_ref) {
35
55
  _useState2 = _slicedToArray(_useState, 2),
36
56
  state = _useState2[0],
37
57
  setState = _useState2[1];
38
- var getTableWidth = useCallback(function () {
39
- var _tableContentRef$curr;
40
- var width = (_tableContentRef$curr = tableContentRef.current) === null || _tableContentRef$curr === void 0 ? void 0 : _tableContentRef$curr.getBoundingClientRect().width;
41
- var style = window.getComputedStyle(tableContentRef.current);
42
- var horizontalBorderWidth = parseFloat(style.getPropertyValue('border-left-width')) + parseFloat(style.getPropertyValue('border-right-width'));
43
- return Math.floor(width - horizontalBorderWidth);
44
- }, [tableContentRef]);
58
+ var getTableWidth = useTableWidth(tableContentRef);
59
+ var debouncedHandleResizeRef = useRef(debounce(function (newWidth) {
60
+ return handleResize(newWidth);
61
+ }, DEFAULT_RESIZE_DEBOUNCE));
45
62
 
46
63
  /**
47
- * 处理容器宽度变化的逻辑
48
- * 1.容器变小:
49
- * 找到原始columns设置列宽度的第一列
50
- * 如果当前列的宽度大于初始宽度,则按照容器变化的总宽度,除选择列和展开列外,其他列平分容器减小的宽度
51
- * 如果当前列的宽度小于等于初始宽度,则列宽均不变
52
- * 2.容器变大:
53
- * 列宽之和大于容器宽度,不重新计算
54
- * 列宽之和小于容器宽度,除选择列和展开列外,其他列平分 列宽之和与容器宽度的差值
55
- */
56
- var handleResize = useCallback(function () {
57
- if (!tableContentRef.current) return;
58
- var newWidth = getTableWidth();
64
+ * 计算可调整的列(排除展开列和选择列)
65
+ */
66
+ var getAdjustableColumns = useCallback(function (columns) {
67
+ return Object.keys(columns).filter(function (key) {
68
+ return !['expander', 'selection-column'].includes(key);
69
+ });
70
+ }, []);
71
+
72
+ /**
73
+ * 计算可见列
74
+ */
75
+ var getVisibleColumns = useCallback(function (columns) {
76
+ return Object.keys(columns).filter(function (key) {
77
+ return columnVisibleConfig[key] !== false;
78
+ });
79
+ }, [columnVisibleConfig]);
80
+
81
+ /**
82
+ * 处理容器宽度变化的逻辑
83
+ */
84
+ var handleResize = useCallback(function (newWidth) {
59
85
  if (!newWidth) return;
60
86
  setState(function (prevState) {
61
- // 计算可见列,排除特定列
62
- var visibleColumns = Object.keys(prevState.columnSizing).filter(function (key) {
63
- return columnVisibleConfig[key] !== false && key !== 'expander' && key !== 'selection-column';
64
- });
65
- var totalVisibleWidth = Object.keys(prevState.columnSizing).filter(function (key) {
66
- return columnVisibleConfig[key] !== false;
67
- }).reduce(function (sum, key) {
68
- return sum + (prevState.columnSizing[key] || 120);
69
- }, 0);
70
-
71
- // 容器变大时的逻辑
72
- if (prevState.tableContainerWidth > 0 && newWidth > prevState.tableContainerWidth) {
73
- if (totalVisibleWidth < newWidth) {
74
- // 计算剩余宽度
75
- var remainWidth = newWidth - totalVisibleWidth - 10;
76
- var unselectedCount = visibleColumns.length;
87
+ return produce(prevState, function (draft) {
88
+ var visibleColumns = getVisibleColumns(draft.columnSizing);
89
+ var adjustableColumns = getAdjustableColumns(draft.columnSizing);
90
+ var totalVisibleWidth = visibleColumns.reduce(function (sum, key) {
91
+ return sum + (draft.columnSizing[key] || DEFAULT_MIN_SIZE);
92
+ }, 0);
77
93
 
78
- // 如果有未排除的列,均匀分配剩余宽度
79
- if (unselectedCount > 0) {
80
- var averageWidth = remainWidth / unselectedCount;
81
- var newVisibleColumnsSizing = _objectSpread({}, prevState.columnSizing);
82
- visibleColumns.forEach(function (key) {
83
- newVisibleColumnsSizing[key] = (newVisibleColumnsSizing[key] || 0) + averageWidth;
84
- });
85
- return _objectSpread(_objectSpread({}, prevState), {}, {
86
- columnSizing: newVisibleColumnsSizing,
87
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing || {}), newVisibleColumnsSizing),
88
- tableContainerWidth: newWidth
89
- });
94
+ // 容器变大时的逻辑
95
+ if (draft.tableContainerWidth && newWidth > draft.tableContainerWidth) {
96
+ if (totalVisibleWidth < newWidth - DEFAULT_TABLE_PADDING) {
97
+ var remainWidth = newWidth - totalVisibleWidth - DEFAULT_TABLE_PADDING;
98
+ if (adjustableColumns.length > 0) {
99
+ var averageWidth = remainWidth / adjustableColumns.length;
100
+ adjustableColumns.forEach(function (key) {
101
+ draft.columnSizing[key] = Math.min((draft.columnSizing[key] || 0) + averageWidth, DEFAULT_MAX_SIZE);
102
+ });
103
+ }
90
104
  }
91
105
  }
92
- } else if (prevState.tableContainerWidth > 0 && newWidth < prevState.tableContainerWidth && JSON.stringify(prevState.initColumnsSizeMap) !== '{}') {
93
- // 容器变小时,重新计算
94
- var reducedWidth = newWidth - totalVisibleWidth - 10;
95
- var _unselectedCount = visibleColumns.length;
96
- if (_unselectedCount > 0) {
97
- var _averageWidth = reducedWidth / _unselectedCount;
98
- var _newVisibleColumnsSizing = _objectSpread({}, prevState.columnSizing);
99
- visibleColumns.forEach(function (key) {
100
- _newVisibleColumnsSizing[key] = Math.max((_newVisibleColumnsSizing[key] || 0) + _averageWidth, Math.max(state.initColumnsSizeMap[visibleColumns[0]], defaultMinSize));
101
- });
102
- return _objectSpread(_objectSpread({}, prevState), {}, {
103
- columnSizing: _newVisibleColumnsSizing,
104
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing || {}), _newVisibleColumnsSizing),
105
- tableContainerWidth: newWidth
106
- });
106
+ // 容器变小时的逻辑
107
+ else if (draft.tableContainerWidth && newWidth < draft.tableContainerWidth) {
108
+ var reducedWidth = newWidth - totalVisibleWidth - DEFAULT_TABLE_PADDING;
109
+ if (adjustableColumns.length > 0) {
110
+ var _averageWidth = reducedWidth / adjustableColumns.length;
111
+ adjustableColumns.forEach(function (key) {
112
+ var _draft$initColumnsSiz;
113
+ draft.columnSizing[key] = Math.max((draft.columnSizing[key] || 0) + _averageWidth, Math.max(((_draft$initColumnsSiz = draft.initColumnsSizeMap) === null || _draft$initColumnsSiz === void 0 ? void 0 : _draft$initColumnsSiz[key]) || 0, DEFAULT_MIN_SIZE));
114
+ });
115
+ }
107
116
  }
108
- }
109
- return _objectSpread(_objectSpread({}, prevState), {}, {
110
- tableContainerWidth: newWidth
117
+ draft.tableContainerWidth = newWidth;
111
118
  });
112
119
  });
113
- }, [tableContentRef.current]);
114
-
115
- // 使用防抖处理容器宽度变化
116
- var debouncedHandleResize = useMemo(function () {
117
- return debounce(handleResize, 200);
118
- }, [handleResize]);
119
- useEffect(function () {
120
- if (!tableContentRef.current) return;
121
- var resizeObserver = new ResizeObserver(debouncedHandleResize);
122
- resizeObserver.observe(tableContentRef.current);
123
- return function () {
124
- resizeObserver.disconnect();
125
- debouncedHandleResize.cancel(); // 清理防抖
126
- };
127
- }, [tableContentRef.current, debouncedHandleResize]);
120
+ }, [getAdjustableColumns, getVisibleColumns]);
128
121
 
129
122
  /**
130
- * 列显示隐藏后调整列宽
131
- * @param key
132
- * @param value
133
- * 增加列,其余列宽度不变
134
- * 减少列,列宽之和大于容器宽度,其余列宽不变
135
- * 列宽之和小于容器宽度,除展开列和勾选列外,其余列平分列宽之和与容器宽度的差值
136
- */
137
-
138
- var handleVisibleConfigChange = function handleVisibleConfigChange(key, value) {
123
+ * 列显示隐藏后调整列宽
124
+ */
125
+ var handleVisibleConfigChange = useCallback(function (key, value) {
139
126
  setState(function (prevState) {
140
- //列宽增加
141
- if (value) {
142
- // console.log(prevState.allColumnSizing[key], prevState.allColumnSizing, 'prevState.allColumnSizing[key]')
143
- return _objectSpread(_objectSpread({}, prevState), {}, {
144
- columnSizing: _objectSpread(_objectSpread({}, prevState.columnSizing), {}, {
145
- key: prevState.allColumnSizing[key]
146
- }),
147
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing), {}, {
148
- key: prevState.allColumnSizing[key] || 120
149
- })
150
- });
151
- } else {
152
- //列宽减少
153
- var newVisibleColumnsSizing = prevState.columnSizing;
154
- delete newVisibleColumnsSizing[key];
155
- var containerWidth = getTableWidth() - 10 || 0;
156
- // 计算当前总宽度
157
- var totalWidth = Object.values(newVisibleColumnsSizing).reduce(function (sum, width) {
158
- return Number(sum || 0) + Number(width || 0);
159
- }, 0);
160
- if (containerWidth < totalWidth) {
161
- // 容器宽度小于增加列后的列总和,其他列宽度不变
162
- return _objectSpread(_objectSpread({}, prevState), {}, {
163
- columnSizing: newVisibleColumnsSizing
164
- });
127
+ return produce(prevState, function (draft) {
128
+ if (value) {
129
+ // 显示列
130
+ draft.columnSizing[key] = draft.allColumnSizing[key] || DEFAULT_MIN_SIZE;
165
131
  } else {
166
- // 容器宽度大于增加列后的列总和,其他列宽度平分差值
167
- var widthDifference = containerWidth - totalWidth; // 计算差值
168
- var joinCalculateColumns = Object.keys(prevState.columnSizing).filter(function (key) {
169
- return key !== 'expander' && key !== 'selection-column';
170
- });
171
- var averageWidthIncrease = widthDifference / (joinCalculateColumns === null || joinCalculateColumns === void 0 ? void 0 : joinCalculateColumns.length);
172
- joinCalculateColumns === null || joinCalculateColumns === void 0 || joinCalculateColumns.forEach(function (key) {
173
- newVisibleColumnsSizing[key] = (newVisibleColumnsSizing[key] || 0) + averageWidthIncrease;
174
- });
175
- return _objectSpread(_objectSpread({}, prevState), {}, {
176
- columnSizing: newVisibleColumnsSizing,
177
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing), newVisibleColumnsSizing)
178
- });
132
+ // 隐藏列
133
+ delete draft.columnSizing[key];
134
+ var containerWidth = getTableWidth() - DEFAULT_TABLE_PADDING || 0;
135
+ var totalWidth = Object.values(draft.columnSizing).reduce(function (sum, width) {
136
+ return sum + Number(width || 0);
137
+ }, 0);
138
+ if (containerWidth > totalWidth) {
139
+ var widthDifference = containerWidth - totalWidth;
140
+ var adjustableColumns = getAdjustableColumns(draft.columnSizing);
141
+ if (adjustableColumns.length > 0) {
142
+ var averageWidthIncrease = widthDifference / adjustableColumns.length;
143
+ adjustableColumns.forEach(function (colKey) {
144
+ draft.columnSizing[colKey] = Math.min((draft.columnSizing[colKey] || 0) + averageWidthIncrease, DEFAULT_MAX_SIZE);
145
+ });
146
+ }
147
+ }
179
148
  }
180
- }
149
+ });
181
150
  });
182
- };
151
+ }, [getAdjustableColumns, getTableWidth]);
183
152
 
184
153
  /**
185
- * 初始化时计算列宽
186
- * 未设置宽度的列,容器剩余宽度存在,平分剩余宽度,最大不超过240;容器剩余宽度不存在,默认120
187
- * 平分后还存在剩余宽度,除展开列和勾选列外的其他列平分剩余宽度
188
- * */
154
+ * 初始化列宽计算
155
+ */
189
156
  var calculateColumnSizing = useCallback(function (columnsSizeMap, allColumnCount, containerWidth) {
190
- var _Object$keys$filter;
191
- if (!tableContentRef.current || !columnsSizeMap) return {};
192
157
  var currentWidth = containerWidth || state.tableContainerWidth || getTableWidth();
193
- var hasColumnCount = ((_Object$keys$filter = Object.keys(columnsSizeMap).filter(function (item) {
194
- return !!columnsSizeMap[item];
195
- })) === null || _Object$keys$filter === void 0 ? void 0 : _Object$keys$filter.length) || 0;
196
- var remainColumnCount = allColumnCount - hasColumnCount;
197
- var allColumnWidth = Object.values(columnsSizeMap).reduce(function (sum, width) {
198
- return sum + Number(width || 0);
199
- }, 0);
200
- var remainTableContainerWidth = currentWidth - allColumnWidth - 10;
201
- var newColumnSizeMap = _objectSpread({}, columnsSizeMap);
158
+ if (!currentWidth) return columnsSizeMap;
159
+ var result = _objectSpread({}, columnsSizeMap);
160
+ var adjustableColumns = getAdjustableColumns(result);
202
161
 
203
- // 处理未设置宽度的列
204
- if (remainColumnCount > 0) {
205
- var averageSize = Math.max(Math.min(remainTableContainerWidth / remainColumnCount, defaultMaxSize), defaultMinSize);
206
- Object.keys(columnsSizeMap).forEach(function (key) {
207
- if (!newColumnSizeMap[key]) {
208
- newColumnSizeMap[key] = averageSize;
209
- }
210
- });
211
- allColumnWidth = Object.values(newColumnSizeMap).reduce(function (sum, width) {
212
- return sum + Number(width || 0);
162
+ // 计算已设置宽度的列
163
+ var hasWidthColumns = Object.keys(result).filter(function (key) {
164
+ return result[key];
165
+ });
166
+ var noWidthColumns = Object.keys(result).filter(function (key) {
167
+ return !result[key];
168
+ });
169
+
170
+ // 为未设置宽度的列分配初始宽度
171
+ if (noWidthColumns.length > 0) {
172
+ var availableWidth = currentWidth - DEFAULT_TABLE_PADDING - hasWidthColumns.reduce(function (sum, key) {
173
+ return sum + (result[key] || 0);
213
174
  }, 0);
214
- remainTableContainerWidth = currentWidth - allColumnWidth - 10;
175
+ var averageSize = Math.min(Math.max(availableWidth / noWidthColumns.length, DEFAULT_MIN_SIZE), DEFAULT_MAX_SIZE);
176
+ noWidthColumns.forEach(function (key) {
177
+ result[key] = averageSize;
178
+ });
215
179
  }
216
180
 
217
181
  // 分配剩余宽度
218
- if (remainTableContainerWidth > 0) {
219
- var excludeColumns = ['expander', 'selection-column'].filter(Boolean);
220
- var adjustableColumns = Object.keys(newColumnSizeMap).filter(function (key) {
221
- return !excludeColumns.includes(key);
182
+ var totalWidth = Object.values(result).reduce(function (sum, width) {
183
+ return sum + width;
184
+ }, 0);
185
+ var remainWidth = currentWidth - DEFAULT_TABLE_PADDING - totalWidth;
186
+ if (remainWidth > 0 && adjustableColumns.length > 0) {
187
+ var averageExtra = remainWidth / adjustableColumns.length;
188
+ adjustableColumns.forEach(function (key) {
189
+ result[key] = Math.min(result[key] + averageExtra, DEFAULT_MAX_SIZE);
222
190
  });
223
- if (adjustableColumns.length > 0) {
224
- var averageExtra = remainTableContainerWidth / adjustableColumns.length;
225
- adjustableColumns.forEach(function (key) {
226
- newColumnSizeMap[key] += averageExtra;
227
- });
228
- }
229
191
  }
230
- return newColumnSizeMap;
231
- }, [state.allColumnCount, state.tableContainerWidth]);
192
+ return result;
193
+ }, [getAdjustableColumns, getTableWidth, state.tableContainerWidth]);
232
194
 
233
- // 初始化列宽信息
195
+ /**
196
+ * 初始化列宽信息
197
+ */
234
198
  var initializeColumnSizing = useCallback(function (columns) {
235
199
  var _localStorage$getItem;
236
- if (!Array.isArray(columns) || columns.length === 0) {
237
- return;
238
- }
239
- var cache = JSON.parse((_localStorage$getItem = localStorage.getItem("".concat(tableKey))) !== null && _localStorage$getItem !== void 0 ? _localStorage$getItem : '{}');
200
+ if (!Array.isArray(columns) || columns.length === 0) return;
240
201
  var _getSizeInfo = getSizeInfo(columns),
241
202
  allCount = _getSizeInfo.allCount,
242
203
  columnSizeMap = _getSizeInfo.columnSizeMap;
243
- if (cache && cache !== null && cache !== void 0 && cache.editTime && +new Date() - cache.editTime > CACHE_MAX_AGE) {
244
- var _initColumnsSizeMap = _objectSpread({}, columnSizeMap);
245
- var _visibleColumnsSizeMap = {};
246
- var _notVisibleColumns = Object.keys(columnVisibleConfig).filter(function (key) {
247
- return !columnVisibleConfig[key];
248
- });
249
- var _newAllColumnCount = allCount - _notVisibleColumns.length;
250
- Object.keys(_initColumnsSizeMap).forEach(function (key) {
251
- if (!_notVisibleColumns.includes(key)) {
252
- _visibleColumnsSizeMap[key] = _initColumnsSizeMap[key];
253
- }
254
- });
255
- setState(function (prevState) {
256
- var columnSizing = calculateColumnSizing(_visibleColumnsSizeMap, _newAllColumnCount, prevState.tableContainerWidth);
257
- return {
258
- columnSizing: columnSizing,
259
- allColumnCount: _newAllColumnCount,
260
- allColumnSizing: _objectSpread(_objectSpread({}, _initColumnsSizeMap), columnSizing),
261
- tableContainerWidth: prevState.tableContainerWidth,
262
- initColumnsSizeMap: columnSizeMap
263
- };
264
- });
265
- return;
266
- }
267
- var cacheColumnSizing = cache.columnSizing,
268
- cacheColumnVisibility = cache.columnVisibility;
269
- var initColumnsSizeMap = _objectSpread(_objectSpread({}, columnSizeMap), cacheColumnSizing);
270
- var initColumnsVisibility = _objectSpread(_objectSpread({}, columnVisibleConfig), cacheColumnVisibility);
271
- var visibleColumnsSizeMap = {};
272
- var notVisibleColumns = Object.keys(initColumnsVisibility).filter(function (key) {
273
- return !initColumnsVisibility[key];
274
- });
275
- var newAllColumnCount = allCount - notVisibleColumns.length;
276
- Object.keys(initColumnsSizeMap).forEach(function (key) {
277
- if (!notVisibleColumns.includes(key)) {
278
- visibleColumnsSizeMap[key] = initColumnsSizeMap[key];
204
+ var cache = JSON.parse((_localStorage$getItem = localStorage.getItem(tableKey)) !== null && _localStorage$getItem !== void 0 ? _localStorage$getItem : '{}');
205
+ var now = Date.now();
206
+ setState(produce(function (prevState) {
207
+ // 初始化列宽映射
208
+ var initColumnsSizeMap = _objectSpread({}, columnSizeMap);
209
+ var visibleColumnsSizeMap = {};
210
+
211
+ // 处理缓存过期或无效的情况
212
+ if (!(cache !== null && cache !== void 0 && cache.editTime) || now - cache.editTime > cacheMaxAge) {
213
+ Object.keys(initColumnsSizeMap).forEach(function (key) {
214
+ if (columnVisibleConfig[key] !== false) {
215
+ visibleColumnsSizeMap[key] = initColumnsSizeMap[key];
216
+ }
217
+ });
218
+ } else {
219
+ // 使用缓存数据
220
+ Object.assign(initColumnsSizeMap, cache.columnSizing);
221
+ Object.assign(columnVisibleConfig, cache.columnVisibility);
222
+ Object.keys(initColumnsSizeMap).forEach(function (key) {
223
+ if (columnVisibleConfig[key] !== false) {
224
+ visibleColumnsSizeMap[key] = initColumnsSizeMap[key];
225
+ }
226
+ });
279
227
  }
280
- });
281
- setState(function (prevState) {
282
- var columnSizing = calculateColumnSizing(visibleColumnsSizeMap, newAllColumnCount, prevState.tableContainerWidth);
283
- return {
284
- columnSizing: columnSizing,
285
- allColumnSizing: _objectSpread(_objectSpread({}, initColumnsSizeMap), columnSizing),
286
- allColumnCount: newAllColumnCount,
287
- tableContainerWidth: prevState.tableContainerWidth,
288
- initColumnsSizeMap: columnSizeMap
289
- };
290
- });
291
- }, [columnVisibleConfig, tableId, tableKey, tableKeyPrefixCls, calculateColumnSizing]);
228
+
229
+ // 计算可见列数
230
+ var visibleCount = allCount - Object.keys(columnVisibleConfig).filter(function (key) {
231
+ return !columnVisibleConfig[key];
232
+ }).length;
233
+
234
+ // 计算最终列宽
235
+ prevState.columnSizing = calculateColumnSizing(visibleColumnsSizeMap, visibleCount, prevState.tableContainerWidth);
236
+ prevState.allColumnSizing = _objectSpread(_objectSpread({}, initColumnsSizeMap), prevState.columnSizing);
237
+ prevState.allColumnCount = visibleCount;
238
+ prevState.initColumnsSizeMap = columnSizeMap;
239
+ }));
240
+ }, [calculateColumnSizing, columnVisibleConfig, tableKey, cacheMaxAge]);
292
241
 
293
242
  /**
294
- * 列宽变化
295
- * 1。列宽增加,不重新计算其他列
296
- * 2.列宽减小:
297
- * 列宽之和大于容器宽度,不重新计算
298
- * 列宽之和小于容器宽度,当前列的下一列/上一列,增加列宽之与大于容器宽度差值
243
+ * 列宽变化处理
299
244
  */
300
- var handleColumnSizingChange = useCallback(function (data) {
301
- if (!data) return;
302
- var changedColumn = data();
245
+ var handleColumnSizingChange = useCallback(function (updater) {
246
+ var changedColumn = updater();
303
247
  if (!changedColumn) return;
304
- setState(function (prevState) {
305
- var changedColumnKey = Object.keys(changedColumn)[0];
306
- if (!changedColumnKey) return prevState;
307
- var oldWidth = prevState.columnSizing[changedColumnKey] || 0;
308
- var newWidth = changedColumn[changedColumnKey] || 0;
309
- var widthDiff = newWidth - oldWidth; // 计算宽度变化
248
+ setState(produce(function (prevState) {
249
+ var _ref2 = Object.entries(changedColumn)[0],
250
+ _ref3 = _slicedToArray(_ref2, 2),
251
+ changedKey = _ref3[0],
252
+ newWidth = _ref3[1];
253
+ if (!changedKey) return;
254
+ var oldWidth = prevState.columnSizing[changedKey] || 0;
255
+ var widthDiff = newWidth - oldWidth;
310
256
 
311
- // 创建新的列宽映射
312
- var newVisibleColumnsSizing = _objectSpread(_objectSpread({}, prevState.columnSizing), changedColumn);
313
-
314
- // 计算当前总宽度
315
- var totalWidth = Object.values(newVisibleColumnsSizing).reduce(function (sum, width) {
316
- return Number(sum || 0) + Number(width || 0);
317
- }, 0);
318
- var containerWidth = getTableWidth() - 10 || 0;
319
-
320
- // 处理列宽变大
321
- if (widthDiff > 0) {
322
- // 只更新变大的列的宽度
323
- return _objectSpread(_objectSpread({}, prevState), {}, {
324
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing || {}), newVisibleColumnsSizing),
325
- columnSizing: newVisibleColumnsSizing
326
- });
327
- }
257
+ // 更新列宽
258
+ prevState.columnSizing[changedKey] = newWidth;
259
+ prevState.allColumnSizing[changedKey] = newWidth;
328
260
 
329
- // 处理列宽缩小
261
+ // 只处理列宽缩小的情况
330
262
  if (widthDiff < 0) {
331
- // 如果缩小后总列宽小于容器宽度
263
+ var totalWidth = Object.values(prevState.columnSizing).reduce(function (sum, width) {
264
+ return sum + width;
265
+ }, 0);
266
+ var containerWidth = getTableWidth() - DEFAULT_TABLE_PADDING || 0;
332
267
  if (totalWidth < containerWidth) {
333
- var widthDifference = containerWidth - totalWidth; // 计算差值
334
-
335
- // 找到当前列的索引
336
268
  var columns = Object.keys(prevState.columnSizing);
337
- var currentIndex = columns.indexOf(changedColumnKey);
338
- var targetColumnKey;
339
-
340
- // 如果当前列是最后一列,处理前一列
341
- if (currentIndex === columns.length - 1) {
342
- targetColumnKey = columns[currentIndex - 1]; // 前一列
343
- } else {
344
- targetColumnKey = columns[currentIndex + 1]; // 下一列
345
- }
346
- // 增加目标列的宽度
347
- if (targetColumnKey) {
348
- newVisibleColumnsSizing[targetColumnKey] = (newVisibleColumnsSizing[targetColumnKey] || 0) + widthDifference;
269
+ var currentIndex = columns.indexOf(changedKey);
270
+ var targetKey = currentIndex < columns.length - 1 ? columns[currentIndex + 1] : columns[currentIndex - 1];
271
+ if (targetKey) {
272
+ var widthDifference = containerWidth - totalWidth;
273
+ prevState.columnSizing[targetKey] += widthDifference;
274
+ prevState.allColumnSizing[targetKey] += widthDifference;
349
275
  }
350
276
  }
277
+ }
278
+ }));
279
+ }, [getTableWidth]);
351
280
 
352
- // 更新当前列的宽度
353
- return _objectSpread(_objectSpread({}, prevState), {}, {
354
- allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing || {}), newVisibleColumnsSizing),
355
- columnSizing: newVisibleColumnsSizing
356
- });
281
+ // 监听容器大小变化
282
+ useEffect(function () {
283
+ if (!tableContentRef.current) return;
284
+ var observer = new ResizeObserver(function (entries) {
285
+ var _entries$;
286
+ var newWidth = (_entries$ = entries[0]) === null || _entries$ === void 0 ? void 0 : _entries$.contentRect.width;
287
+ if (newWidth) {
288
+ debouncedHandleResizeRef.current(newWidth);
357
289
  }
358
- return prevState; // 如果没有变化,返回原状态
359
290
  });
360
- }, [tableId, tableKeyPrefixCls]);
291
+ observer.observe(tableContentRef.current);
292
+ return function () {
293
+ return observer.disconnect();
294
+ };
295
+ }, [tableContentRef]);
361
296
  return {
362
297
  columnSizing: state.columnSizing,
363
298
  allColumnCount: state.allColumnCount,
@@ -312,7 +312,9 @@ export type TableMaxProps = {
312
312
  request?: (...params: any[]) => Promise<Blob>;
313
313
  /**编辑时修改组件值时触发的事件 */
314
314
  onEditValueChange?: (field: string, value: any, extra?: any) => void;
315
+ /**是否有总计行 */
315
316
  hasTotalRow?: boolean;
317
+ /**总计行数据 { '列id': '数据' }*/
316
318
  totalDatas?: any[];
317
319
  };
318
320
  export interface TableMaxColumnType {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arim-aisdc/public-components",
3
- "version": "2.3.17",
3
+ "version": "2.3.18",
4
4
  "description": "前端组件库",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -59,6 +59,7 @@
59
59
  "immer": "^10.0.3",
60
60
  "jsep": "^1.3.8",
61
61
  "lodash": "^4.17.21",
62
+ "lodash-es": "^4.17.21",
62
63
  "lottie-web": "^5.12.2",
63
64
  "path": "^0.12.7",
64
65
  "react-activation": "^0.12.4",
@@ -66,6 +67,8 @@
66
67
  "react-dnd": "^16.0.1",
67
68
  "react-dnd-html5-backend": "^16.0.1",
68
69
  "react-draggable": "^4.4.6",
70
+ "react-resizable": "^3.0.5",
71
+ "react-rnd": "^10.5.2",
69
72
  "react-router-dom": "^6.22.3",
70
73
  "react-split-pane": "^0.1.92"
71
74
  },