@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.
@@ -0,0 +1,369 @@
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); }
7
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
+ 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."); }
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
+ 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
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
+ import { useCallback, useEffect, useMemo, useState } from 'react';
14
+ 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;
19
+ export var useColumnWidth = function useColumnWidth(_ref) {
20
+ var tableContentRef = _ref.tableContentRef,
21
+ tableId = _ref.tableId,
22
+ tableKeyPrefixCls = _ref.tableKeyPrefixCls,
23
+ _ref$columnVisibleCon = _ref.columnVisibleConfig,
24
+ columnVisibleConfig = _ref$columnVisibleCon === void 0 ? {} : _ref$columnVisibleCon,
25
+ tableKey = _ref.tableKey;
26
+ var _useState = useState({
27
+ columnSizing: {},
28
+ //显示列的列宽
29
+ allColumnSizing: {},
30
+ //所有列的列宽
31
+ allColumnCount: 0,
32
+ tableContainerWidth: undefined,
33
+ initColumnsSizeMap: {}
34
+ }),
35
+ _useState2 = _slicedToArray(_useState, 2),
36
+ state = _useState2[0],
37
+ 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]);
45
+
46
+ /**
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();
59
+ if (!newWidth) return;
60
+ 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;
77
+
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
+ });
90
+ }
91
+ }
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
+ });
107
+ }
108
+ }
109
+ return _objectSpread(_objectSpread({}, prevState), {}, {
110
+ tableContainerWidth: newWidth
111
+ });
112
+ });
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]);
128
+
129
+ /**
130
+ * 列显示隐藏后调整列宽
131
+ * @param key
132
+ * @param value
133
+ * 增加列,其余列宽度不变
134
+ * 减少列,列宽之和大于容器宽度,其余列宽不变
135
+ * 列宽之和小于容器宽度,除展开列和勾选列外,其余列平分列宽之和与容器宽度的差值
136
+ */
137
+
138
+ var handleVisibleConfigChange = function handleVisibleConfigChange(key, value) {
139
+ 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
+ });
165
+ } 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
+ });
179
+ }
180
+ }
181
+ });
182
+ };
183
+
184
+ /**
185
+ * 初始化时计算列宽
186
+ * 未设置宽度的列,容器剩余宽度存在,平分剩余宽度,最大不超过240;容器剩余宽度不存在,默认120
187
+ * 平分后还存在剩余宽度,除展开列和勾选列外的其他列平分剩余宽度
188
+ * */
189
+ var calculateColumnSizing = useCallback(function (columnsSizeMap, allColumnCount, containerWidth) {
190
+ var _Object$keys$filter;
191
+ if (!tableContentRef.current || !columnsSizeMap) return {};
192
+ 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);
202
+
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);
213
+ }, 0);
214
+ remainTableContainerWidth = currentWidth - allColumnWidth - 10;
215
+ }
216
+
217
+ // 分配剩余宽度
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);
222
+ });
223
+ if (adjustableColumns.length > 0) {
224
+ var averageExtra = remainTableContainerWidth / adjustableColumns.length;
225
+ adjustableColumns.forEach(function (key) {
226
+ newColumnSizeMap[key] += averageExtra;
227
+ });
228
+ }
229
+ }
230
+ return newColumnSizeMap;
231
+ }, [state.allColumnCount, state.tableContainerWidth]);
232
+
233
+ // 初始化列宽信息
234
+ var initializeColumnSizing = useCallback(function (columns) {
235
+ 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 : '{}');
240
+ var _getSizeInfo = getSizeInfo(columns),
241
+ allCount = _getSizeInfo.allCount,
242
+ 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];
279
+ }
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]);
292
+
293
+ /**
294
+ * 列宽变化
295
+ * 1。列宽增加,不重新计算其他列
296
+ * 2.列宽减小:
297
+ * 列宽之和大于容器宽度,不重新计算
298
+ * 列宽之和小于容器宽度,当前列的下一列/上一列,增加列宽之与大于容器宽度差值
299
+ */
300
+ var handleColumnSizingChange = useCallback(function (data) {
301
+ if (!data) return;
302
+ var changedColumn = data();
303
+ 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; // 计算宽度变化
310
+
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
+ }
328
+
329
+ // 处理列宽缩小
330
+ if (widthDiff < 0) {
331
+ // 如果缩小后总列宽小于容器宽度
332
+ if (totalWidth < containerWidth) {
333
+ var widthDifference = containerWidth - totalWidth; // 计算差值
334
+
335
+ // 找到当前列的索引
336
+ 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;
349
+ }
350
+ }
351
+
352
+ // 更新当前列的宽度
353
+ return _objectSpread(_objectSpread({}, prevState), {}, {
354
+ allColumnSizing: _objectSpread(_objectSpread({}, prevState.allColumnSizing || {}), newVisibleColumnsSizing),
355
+ columnSizing: newVisibleColumnsSizing
356
+ });
357
+ }
358
+ return prevState; // 如果没有变化,返回原状态
359
+ });
360
+ }, [tableId, tableKeyPrefixCls]);
361
+ return {
362
+ columnSizing: state.columnSizing,
363
+ allColumnCount: state.allColumnCount,
364
+ onColumnSizingChange: handleColumnSizingChange,
365
+ tableContainerWidth: state.tableContainerWidth,
366
+ initializeColumnSizing: initializeColumnSizing,
367
+ handleVisibleConfigChange: handleVisibleConfigChange
368
+ };
369
+ };
@@ -6,13 +6,17 @@ interface UseColumnWidthProps {
6
6
  tableKeyPrefixCls: string;
7
7
  columnVisibleConfig?: Record<string, boolean>;
8
8
  tableKey: string;
9
+ cacheMaxAge?: number;
9
10
  }
10
- export declare const useColumnWidth: ({ tableContentRef, tableId, tableKeyPrefixCls, columnVisibleConfig, tableKey, }: UseColumnWidthProps) => {
11
+ /**
12
+ * 自定义Hook:表格列宽计算和管理
13
+ */
14
+ export declare const useColumnWidth: ({ tableContentRef, tableId, tableKeyPrefixCls, columnVisibleConfig, tableKey, cacheMaxAge, }: UseColumnWidthProps) => {
11
15
  columnSizing: ColumnSizing;
12
16
  allColumnCount: number;
13
- onColumnSizingChange: (data: any) => void;
17
+ onColumnSizingChange: (updater: any) => void;
14
18
  tableContainerWidth: number;
15
19
  initializeColumnSizing: (columns: any) => void;
16
- handleVisibleConfigChange: (key: any, value: any) => void;
20
+ handleVisibleConfigChange: (key: string, value: boolean) => void;
17
21
  };
18
22
  export {};