@hi-ui/table 5.0.0-canary.7 → 5.0.0-canary.9

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @hi-ui/table
2
2
 
3
+ ## 5.0.0-canary.9
4
+
5
+ ### Patch Changes
6
+
7
+ - fix(table): 修复 resizable 模式下列宽计算问题 (5.0)
8
+ - Updated dependencies [bf65028e6]
9
+ - @hi-ui/button@5.0.0-canary.7
10
+ - @hi-ui/popper@5.0.0-canary.4
11
+
12
+ ## 5.0.0-canary.8
13
+
14
+ ### Minor Changes
15
+
16
+ - ca7a945fe: feat(table): TableColumnItem 类型增加 minWidth 参数 (#3216)
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [756473045]
21
+ - Updated dependencies [b4cecde83]
22
+ - @hi-ui/scrollbar@5.0.0-canary.3
23
+ - @hi-ui/button@5.0.0-canary.6
24
+
3
25
  ## 5.0.0-canary.7
4
26
 
5
27
  ### Patch Changes
@@ -31,7 +31,7 @@ var useColWidth = function useColWidth(_ref) {
31
31
  // 是否重新设置过表格每列宽度
32
32
  var hasResetWidths = React__default["default"].useRef(false);
33
33
  var _React$useState = React__default["default"].useState(function () {
34
- return index.getGroupItemWidth(columns);
34
+ return index.getGroupItemWidth(columns).colWidths;
35
35
  }),
36
36
  colWidths = _React$useState[0],
37
37
  setColWidths = _React$useState[1];
@@ -39,12 +39,12 @@ var useColWidth = function useColWidth(_ref) {
39
39
  if (measureRowElement && measureRowElement.childNodes) {
40
40
  // 超出的宽度,即每列真实的宽度超出设置的宽度的总和
41
41
  var exceedWidth = 0;
42
- var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node, index) {
42
+ var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node, index$1) {
43
43
  var _a;
44
- var realWidth = node.getBoundingClientRect().width || 0;
45
- var _ref2 = (_a = columns[index]) !== null && _a !== void 0 ? _a : {},
46
- width = _ref2.width,
44
+ var realWidth = node.getBoundingClientRect().width || 60;
45
+ var _ref2 = (_a = columns[index$1]) !== null && _a !== void 0 ? _a : {},
47
46
  fixed = _ref2.fixed;
47
+ var width = index.getGroupItemWidth(columns).colWidths[index$1];
48
48
  // 如果该列设置了 fixed 并且真实宽度大于设置的 width 则设置为 width
49
49
  if (fixed && width && width < realWidth) {
50
50
  exceedWidth += realWidth - width;
@@ -71,12 +71,12 @@ var useColWidth = function useColWidth(_ref) {
71
71
  return _realColumnsWidth;
72
72
  }
73
73
  }
74
- return index.getGroupItemWidth(columns);
74
+ return index.getGroupItemWidth(columns).colWidths;
75
75
  }, [columns]);
76
76
  var getVirtualWidths = React.useCallback(function () {
77
77
  var measureRowElement = measureRowElementRef.current;
78
78
  if (!measureRowElement) {
79
- return index.getGroupItemWidth(columns);
79
+ return index.getGroupItemWidth(columns).colWidths;
80
80
  }
81
81
  /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
82
82
  var columnDefaultWidth = 200;
@@ -105,7 +105,7 @@ var useColWidth = function useColWidth(_ref) {
105
105
  }
106
106
  }, [getVirtualWidths, virtual]);
107
107
  useUpdateEffect.useUpdateEffect(function () {
108
- setColWidths(index.getGroupItemWidth(columns));
108
+ setColWidths(index.getGroupItemWidth(columns).colWidths);
109
109
  // setColWidths((prev) => {
110
110
  // // resizable 模式下通过 measureRowElementRef.current 去更新列宽,防止拖拽后宽度被重置
111
111
  // // 例如同时设置了 resizable 和 rowSelection,当拖拽某列宽度后再选中某行时,该列宽度会被重置
@@ -147,33 +147,35 @@ var useColWidth = function useColWidth(_ref) {
147
147
  setHeaderTableElement = _React$useState2[1];
148
148
  // 控制列最小可调整宽度
149
149
  var _React$useState3 = React__default["default"].useState([]),
150
- minColWidth = _React$useState3[0],
151
- setMinColWidth = _React$useState3[1];
152
- // 当列变化时同步更新 minColWidth
150
+ minColWidths = _React$useState3[0],
151
+ setMinColWidths = _React$useState3[1];
152
+ // 当列变化时同步更新 minColWidths
153
153
  React__default["default"].useEffect(function () {
154
154
  var resizeObserver;
155
155
  if (headerTableElement) {
156
156
  resizeObserver = new ResizeObserver(function () {
157
157
  var resizableHandlerWidth = 4;
158
- var _minColWidth = Array.from(headerTableElement.childNodes).map(function (th) {
158
+ var calcMinColWidths = Array.from(headerTableElement.childNodes).map(function (th, index$1) {
159
+ var minColWidth = index.getGroupItemWidth(columns).minColWidths[index$1];
159
160
  var thPaddingLeft = parseFloat(window.getComputedStyle(th).getPropertyValue('padding-left'));
160
161
  var childNodes = Array.from(th.childNodes);
161
- return childNodes.map(function (child) {
162
+ var childNodesWidth = childNodes.map(function (child) {
162
163
  return child.offsetWidth;
163
164
  }).reduce(function (prev, next) {
164
165
  return prev + next;
165
166
  }, 0) + thPaddingLeft * 2 + resizableHandlerWidth;
167
+ return childNodesWidth > minColWidth ? minColWidth : childNodesWidth;
166
168
  });
167
- setMinColWidth(_minColWidth);
169
+ setMinColWidths(calcMinColWidths);
168
170
  });
169
171
  resizeObserver.observe(headerTableElement);
170
172
  } else {
171
- setMinColWidth(Array(columns.length).fill(0));
173
+ setMinColWidths(Array(columns.length).fill(0));
172
174
  }
173
175
  return function () {
174
176
  resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
175
177
  };
176
- }, [columns.length, headerTableElement, resizable]);
178
+ }, [columns, columns.length, headerTableElement, resizable]);
177
179
  /**
178
180
  * 控制列最小可调整宽度
179
181
  */
@@ -198,8 +200,8 @@ var useColWidth = function useColWidth(_ref) {
198
200
  */
199
201
  var onColumnResizable = React__default["default"].useCallback(function (evt, _ref3, index) {
200
202
  var size = _ref3.size;
201
- var minWidth = minColWidth[index];
202
- var anotherMinWidth = minColWidth[index + 1];
203
+ var minWidth = minColWidths[index];
204
+ var anotherMinWidth = minColWidths[index + 1];
203
205
  var nextWidth = size.width > minWidth ? size.width : minWidth;
204
206
  setColWidths(function (prev) {
205
207
  var nextColWidths = [].concat(prev);
@@ -216,18 +218,18 @@ var useColWidth = function useColWidth(_ref) {
216
218
  nextColWidths[index + 1] = anotherWidth;
217
219
  return nextColWidths;
218
220
  });
219
- }, [minColWidth, tableWidthAdjustOnResize]);
221
+ }, [minColWidths, tableWidthAdjustOnResize]);
220
222
  var getColgroupProps = React__default["default"].useCallback(function (column, index) {
221
223
  var width = colWidths[index] || undefined;
224
+ var minWidth = minColWidths[index] || undefined;
222
225
  return {
223
226
  style: {
224
227
  width: width,
225
- // TODO(疑惑): minWidth 所要解决的问题
226
- minWidth: width
228
+ minWidth: minWidth || width
227
229
  }
228
230
  // 'data-hover-highlight': setAttrStatus(isHoveredCol(column.dataKey)),
229
231
  };
230
- }, [colWidths]);
232
+ }, [colWidths, minColWidths]);
231
233
  return {
232
234
  measureRowElementRef: measureRowElementRef,
233
235
  onColumnResizable: onColumnResizable,
@@ -66,22 +66,29 @@ var setColumnsDefaultWidth = function setColumnsDefaultWidth(columns, defaultWid
66
66
  * 如果是多级表头,将会递归 children 得到叶子结点层每项的宽度
67
67
  */
68
68
  var getGroupItemWidth = function getGroupItemWidth(columns) {
69
- var baseColWidths = [];
69
+ var colWidths = [];
70
+ var minColWidths = [];
70
71
  var dig = function dig(column) {
71
72
  column.forEach(function (_ref) {
72
73
  var children = _ref.children,
73
- width = _ref.width;
74
+ width = _ref.width,
75
+ minWidth = _ref.minWidth;
74
76
  if (Array.isArray(children)) {
75
77
  dig(children);
76
78
  return;
77
79
  }
78
80
  // 如果没有设置列宽度,css 宽度默认是 `auto`,这里对于非数字 width 均设置为 0
79
81
  var colWidth = typeAssertion.isNumeric(width) ? Number(width) : 0;
80
- baseColWidths.push(colWidth);
82
+ var minColWidth = typeAssertion.isNumeric(minWidth) ? Number(minWidth) : colWidth || 60;
83
+ colWidths.push(colWidth < minColWidth ? minColWidth : colWidth);
84
+ minColWidths.push(minColWidth);
81
85
  });
82
86
  };
83
87
  dig(columns);
84
- return baseColWidths;
88
+ return {
89
+ colWidths: colWidths,
90
+ minColWidths: minColWidths
91
+ };
85
92
  };
86
93
  var parseFixedColumns = function parseFixedColumns(item, index, columns, arr, key, parentStickyWidth) {
87
94
  if (parentStickyWidth === void 0) {
@@ -19,7 +19,7 @@ var useColWidth = function useColWidth(_ref) {
19
19
  // 是否重新设置过表格每列宽度
20
20
  var hasResetWidths = React__default.useRef(false);
21
21
  var _React$useState = React__default.useState(function () {
22
- return getGroupItemWidth(columns);
22
+ return getGroupItemWidth(columns).colWidths;
23
23
  }),
24
24
  colWidths = _React$useState[0],
25
25
  setColWidths = _React$useState[1];
@@ -29,10 +29,10 @@ var useColWidth = function useColWidth(_ref) {
29
29
  var exceedWidth = 0;
30
30
  var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node, index) {
31
31
  var _a;
32
- var realWidth = node.getBoundingClientRect().width || 0;
32
+ var realWidth = node.getBoundingClientRect().width || 60;
33
33
  var _ref2 = (_a = columns[index]) !== null && _a !== void 0 ? _a : {},
34
- width = _ref2.width,
35
34
  fixed = _ref2.fixed;
35
+ var width = getGroupItemWidth(columns).colWidths[index];
36
36
  // 如果该列设置了 fixed 并且真实宽度大于设置的 width 则设置为 width
37
37
  if (fixed && width && width < realWidth) {
38
38
  exceedWidth += realWidth - width;
@@ -59,12 +59,12 @@ var useColWidth = function useColWidth(_ref) {
59
59
  return _realColumnsWidth;
60
60
  }
61
61
  }
62
- return getGroupItemWidth(columns);
62
+ return getGroupItemWidth(columns).colWidths;
63
63
  }, [columns]);
64
64
  var getVirtualWidths = useCallback(function () {
65
65
  var measureRowElement = measureRowElementRef.current;
66
66
  if (!measureRowElement) {
67
- return getGroupItemWidth(columns);
67
+ return getGroupItemWidth(columns).colWidths;
68
68
  }
69
69
  /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
70
70
  var columnDefaultWidth = 200;
@@ -93,7 +93,7 @@ var useColWidth = function useColWidth(_ref) {
93
93
  }
94
94
  }, [getVirtualWidths, virtual]);
95
95
  useUpdateEffect(function () {
96
- setColWidths(getGroupItemWidth(columns));
96
+ setColWidths(getGroupItemWidth(columns).colWidths);
97
97
  // setColWidths((prev) => {
98
98
  // // resizable 模式下通过 measureRowElementRef.current 去更新列宽,防止拖拽后宽度被重置
99
99
  // // 例如同时设置了 resizable 和 rowSelection,当拖拽某列宽度后再选中某行时,该列宽度会被重置
@@ -135,33 +135,35 @@ var useColWidth = function useColWidth(_ref) {
135
135
  setHeaderTableElement = _React$useState2[1];
136
136
  // 控制列最小可调整宽度
137
137
  var _React$useState3 = React__default.useState([]),
138
- minColWidth = _React$useState3[0],
139
- setMinColWidth = _React$useState3[1];
140
- // 当列变化时同步更新 minColWidth
138
+ minColWidths = _React$useState3[0],
139
+ setMinColWidths = _React$useState3[1];
140
+ // 当列变化时同步更新 minColWidths
141
141
  React__default.useEffect(function () {
142
142
  var resizeObserver;
143
143
  if (headerTableElement) {
144
144
  resizeObserver = new ResizeObserver(function () {
145
145
  var resizableHandlerWidth = 4;
146
- var _minColWidth = Array.from(headerTableElement.childNodes).map(function (th) {
146
+ var calcMinColWidths = Array.from(headerTableElement.childNodes).map(function (th, index) {
147
+ var minColWidth = getGroupItemWidth(columns).minColWidths[index];
147
148
  var thPaddingLeft = parseFloat(window.getComputedStyle(th).getPropertyValue('padding-left'));
148
149
  var childNodes = Array.from(th.childNodes);
149
- return childNodes.map(function (child) {
150
+ var childNodesWidth = childNodes.map(function (child) {
150
151
  return child.offsetWidth;
151
152
  }).reduce(function (prev, next) {
152
153
  return prev + next;
153
154
  }, 0) + thPaddingLeft * 2 + resizableHandlerWidth;
155
+ return childNodesWidth > minColWidth ? minColWidth : childNodesWidth;
154
156
  });
155
- setMinColWidth(_minColWidth);
157
+ setMinColWidths(calcMinColWidths);
156
158
  });
157
159
  resizeObserver.observe(headerTableElement);
158
160
  } else {
159
- setMinColWidth(Array(columns.length).fill(0));
161
+ setMinColWidths(Array(columns.length).fill(0));
160
162
  }
161
163
  return function () {
162
164
  resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
163
165
  };
164
- }, [columns.length, headerTableElement, resizable]);
166
+ }, [columns, columns.length, headerTableElement, resizable]);
165
167
  /**
166
168
  * 控制列最小可调整宽度
167
169
  */
@@ -186,8 +188,8 @@ var useColWidth = function useColWidth(_ref) {
186
188
  */
187
189
  var onColumnResizable = React__default.useCallback(function (evt, _ref3, index) {
188
190
  var size = _ref3.size;
189
- var minWidth = minColWidth[index];
190
- var anotherMinWidth = minColWidth[index + 1];
191
+ var minWidth = minColWidths[index];
192
+ var anotherMinWidth = minColWidths[index + 1];
191
193
  var nextWidth = size.width > minWidth ? size.width : minWidth;
192
194
  setColWidths(function (prev) {
193
195
  var nextColWidths = [].concat(prev);
@@ -204,18 +206,18 @@ var useColWidth = function useColWidth(_ref) {
204
206
  nextColWidths[index + 1] = anotherWidth;
205
207
  return nextColWidths;
206
208
  });
207
- }, [minColWidth, tableWidthAdjustOnResize]);
209
+ }, [minColWidths, tableWidthAdjustOnResize]);
208
210
  var getColgroupProps = React__default.useCallback(function (column, index) {
209
211
  var width = colWidths[index] || undefined;
212
+ var minWidth = minColWidths[index] || undefined;
210
213
  return {
211
214
  style: {
212
215
  width: width,
213
- // TODO(疑惑): minWidth 所要解决的问题
214
- minWidth: width
216
+ minWidth: minWidth || width
215
217
  }
216
218
  // 'data-hover-highlight': setAttrStatus(isHoveredCol(column.dataKey)),
217
219
  };
218
- }, [colWidths]);
220
+ }, [colWidths, minColWidths]);
219
221
  return {
220
222
  measureRowElementRef: measureRowElementRef,
221
223
  onColumnResizable: onColumnResizable,
@@ -61,22 +61,29 @@ var setColumnsDefaultWidth = function setColumnsDefaultWidth(columns, defaultWid
61
61
  * 如果是多级表头,将会递归 children 得到叶子结点层每项的宽度
62
62
  */
63
63
  var getGroupItemWidth = function getGroupItemWidth(columns) {
64
- var baseColWidths = [];
64
+ var colWidths = [];
65
+ var minColWidths = [];
65
66
  var dig = function dig(column) {
66
67
  column.forEach(function (_ref) {
67
68
  var children = _ref.children,
68
- width = _ref.width;
69
+ width = _ref.width,
70
+ minWidth = _ref.minWidth;
69
71
  if (Array.isArray(children)) {
70
72
  dig(children);
71
73
  return;
72
74
  }
73
75
  // 如果没有设置列宽度,css 宽度默认是 `auto`,这里对于非数字 width 均设置为 0
74
76
  var colWidth = isNumeric(width) ? Number(width) : 0;
75
- baseColWidths.push(colWidth);
77
+ var minColWidth = isNumeric(minWidth) ? Number(minWidth) : colWidth || 60;
78
+ colWidths.push(colWidth < minColWidth ? minColWidth : colWidth);
79
+ minColWidths.push(minColWidth);
76
80
  });
77
81
  };
78
82
  dig(columns);
79
- return baseColWidths;
83
+ return {
84
+ colWidths: colWidths,
85
+ minColWidths: minColWidths
86
+ };
80
87
  };
81
88
  var parseFixedColumns = function parseFixedColumns(item, index, columns, arr, key, parentStickyWidth) {
82
89
  if (parentStickyWidth === void 0) {
@@ -95,7 +95,11 @@ export declare type TableColumnItem = {
95
95
  */
96
96
  width?: number;
97
97
  /**
98
- * 是否固定列宽,使用场景:设置 width 后,当所有列宽总和小于表格宽度时,多的宽度会分摊到每一列上,设置 fixed 后该列不会参与多余宽度均分。
98
+ * 该列最小宽度
99
+ */
100
+ minWidth?: number;
101
+ /**
102
+ * 是否固定列宽,使用场景:设置 width 后,当所有列宽总和小于表格宽度时,多的宽度会分摊到每一列上,设置 fixed 后该列不会参与多余宽度分摊。
99
103
  */
100
104
  fixed?: boolean;
101
105
  /**
@@ -6,7 +6,10 @@ export declare const setColumnsDefaultWidth: (columns: any, defaultWidth: any) =
6
6
  * 获取每个 Column 的真实列宽度(排除合并列头)
7
7
  * 如果是多级表头,将会递归 children 得到叶子结点层每项的宽度
8
8
  */
9
- export declare const getGroupItemWidth: (columns: TableColumnItem[]) => number[];
9
+ export declare const getGroupItemWidth: (columns: TableColumnItem[]) => {
10
+ colWidths: number[];
11
+ minColWidths: number[];
12
+ };
10
13
  export declare const getMaskItemsWIdth: (columns: TableColumnItem[]) => number;
11
14
  export declare const parseFixedColumns: (item: any, index: number, columns: any[], arr: any[], key: string, parentStickyWidth?: number) => any;
12
15
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/table",
3
- "version": "5.0.0-canary.7",
3
+ "version": "5.0.0-canary.9",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@hi-ui/array-utils": "^5.0.0-canary.2",
48
- "@hi-ui/button": "^5.0.0-canary.5",
48
+ "@hi-ui/button": "^5.0.0-canary.7",
49
49
  "@hi-ui/checkbox": "^5.0.0-canary.2",
50
50
  "@hi-ui/classname": "^5.0.0-canary.2",
51
51
  "@hi-ui/dom-utils": "^5.0.0-canary.2",
@@ -58,9 +58,9 @@
58
58
  "@hi-ui/loading": "^5.0.0-canary.2",
59
59
  "@hi-ui/object-utils": "^5.0.0-canary.2",
60
60
  "@hi-ui/pagination": "^5.0.0-canary.4",
61
- "@hi-ui/popper": "^5.0.0-canary.3",
61
+ "@hi-ui/popper": "^5.0.0-canary.4",
62
62
  "@hi-ui/react-utils": "^5.0.0-canary.2",
63
- "@hi-ui/scrollbar": "^5.0.0-canary.2",
63
+ "@hi-ui/scrollbar": "^5.0.0-canary.3",
64
64
  "@hi-ui/select": "^5.0.0-canary.7",
65
65
  "@hi-ui/spinner": "^5.0.0-canary.2",
66
66
  "@hi-ui/times": "^5.0.0-canary.2",