@hi-ui/table 4.1.0-beta.2 → 4.1.0-beta.3

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,11 @@
1
1
  # @hi-ui/table
2
2
 
3
+ ## 4.1.0-beta.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 优化大数据表格
8
+
3
9
  ## 4.1.0-beta.2
4
10
 
5
11
  ### Patch Changes
@@ -57,7 +57,7 @@ var TableBody = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
57
57
  var _useTableContext = context.useTableContext(),
58
58
  columns = _useTableContext.columns,
59
59
  leafColumns = _useTableContext.leafColumns,
60
- measureRowElementRef = _useTableContext.measureRowElementRef,
60
+ setMeasureRowElement = _useTableContext.setMeasureRowElement,
61
61
  isExpandTreeRows = _useTableContext.isExpandTreeRows,
62
62
  transitionData = _useTableContext.transitionData,
63
63
  getColgroupProps = _useTableContext.getColgroupProps,
@@ -122,7 +122,9 @@ var TableBody = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
122
122
  overflowX: canScroll ? 'scroll' : undefined
123
123
  }
124
124
  }, /*#__PURE__*/React__default["default"].createElement("div", {
125
- ref: measureRowElementRef,
125
+ ref: function ref(domElement) {
126
+ setMeasureRowElement(domElement);
127
+ },
126
128
  style: {
127
129
  height: 1,
128
130
  background: 'transparent'
@@ -187,7 +189,11 @@ var TableBody = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
187
189
  }, getColgroupProps(col, idx)));
188
190
  })), /*#__PURE__*/React__default["default"].createElement("tbody", null, typeAssertion.isArrayNonEmpty(transitionData) ? /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, transitionData.map(function (row, index) {
189
191
  return /*#__PURE__*/React__default["default"].createElement(TableRow.TableRow, Object.assign({
190
- ref: index === 0 ? measureRowElementRef : null,
192
+ ref: function ref(dom) {
193
+ if (index === 0) {
194
+ setMeasureRowElement(dom);
195
+ }
196
+ },
191
197
  // key={depth + index}
192
198
  key: row.id,
193
199
  // @ts-ignore
@@ -32,52 +32,62 @@ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
32
32
  var useColWidth = function useColWidth(_ref) {
33
33
  var columns = _ref.columns,
34
34
  virtual = _ref.virtual;
35
- var measureRowElementRef = React.useRef(null);
36
35
 
37
- var _React$useState = React__default["default"].useState(function () {
36
+ var _React$useState = React__default["default"].useState(null),
37
+ measureRowElement = _React$useState[0],
38
+ setMeasureRowElement = _React$useState[1];
39
+
40
+ var _React$useState2 = React__default["default"].useState(function () {
38
41
  return index.getGroupItemWidth(columns);
39
42
  }),
40
- colWidths = _React$useState[0],
41
- setColWidths = _React$useState[1];
43
+ colWidths = _React$useState2[0],
44
+ setColWidths = _React$useState2[1];
42
45
 
43
- console.error({
44
- useColWidth: 1,
45
- colWidths: colWidths
46
- });
46
+ var getVirtualWidths = React.useCallback(function () {
47
+ if (!measureRowElement) {
48
+ return index.getGroupItemWidth(columns);
49
+ }
50
+ /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
51
+
52
+
53
+ var columnDefaultWidth = 200;
54
+ var containerWidth = measureRowElement.clientWidth;
55
+ var totalWidth = 0;
56
+ /** 虚拟滚动,需要根据collist的虚拟宽度来计算宽度 */
57
+
58
+ columns.forEach(function (columnItem) {
59
+ totalWidth += columnItem.width || columnDefaultWidth;
60
+ });
61
+
62
+ if (totalWidth < containerWidth) {
63
+ // 容器宽度大于设置的宽度总和时,col宽度等比分分配占满容器。
64
+ return columns.map(function (columnItem) {
65
+ return (columnItem.width || columnDefaultWidth) * containerWidth / totalWidth;
66
+ });
67
+ } else {
68
+ // 容器宽度小于设置的宽度总和时,col宽度等于设置/默认宽度。
69
+ return columns.map(function (columnItem) {
70
+ return columnItem.width || columnDefaultWidth;
71
+ });
72
+ }
73
+ }, [measureRowElement, columns]);
47
74
  useUpdateEffect.useUpdateEffect(function () {
48
- setColWidths(index.getGroupItemWidth(columns));
49
- }, [columns]);
75
+ if (virtual) {
76
+ // 虚拟滚动的计算需要根据容器来做分配,不能使用没有witdh默认设置为0的方式来做表格平均分配
77
+ setColWidths(getVirtualWidths());
78
+ } else {
79
+ setColWidths(index.getGroupItemWidth(columns));
80
+ }
81
+ }, [columns, getVirtualWidths, virtual]);
50
82
  /**
51
83
  * 根据实际内容区(table 的第一行)渲染,再次精确收集并设置每列宽度
52
84
  */
53
85
 
54
86
  React__default["default"].useEffect(function () {
55
- var measureRowElement = measureRowElementRef.current;
56
-
57
87
  if (measureRowElement) {
58
88
  var _resizeObserver = new ResizeObserver(function () {
59
89
  if (virtual) {
60
- /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
61
- var columnDefaultWidth = 200;
62
- var containerWidth = measureRowElement.clientWidth;
63
- var totalWidth = 0;
64
- /** 虚拟滚动,需要根据collist的虚拟宽度来计算宽度 */
65
-
66
- columns.forEach(function (columnItem) {
67
- totalWidth += columnItem.width || columnDefaultWidth;
68
- });
69
-
70
- if (totalWidth < containerWidth) {
71
- // 容器宽度大于设置的宽度总和时,col宽度等比分分配占满容器。
72
- setColWidths(columns.map(function (columnItem) {
73
- return (columnItem.width || columnDefaultWidth) * containerWidth / totalWidth;
74
- }));
75
- } else {
76
- // 容器宽度小于设置的宽度总和时,col宽度等于设置/默认宽度。
77
- setColWidths(columns.map(function (columnItem) {
78
- return columnItem.width || columnDefaultWidth;
79
- }));
80
- }
90
+ setColWidths(getVirtualWidths());
81
91
  } else {
82
92
  if (measureRowElement.childNodes) {
83
93
  var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node) {
@@ -96,12 +106,12 @@ var useColWidth = function useColWidth(_ref) {
96
106
  _resizeObserver.observe(measureRowElement);
97
107
  }
98
108
 
99
- return function () {};
100
- }, []);
109
+ return function () {}; // 测量元素在内容列为空时会是空,切换会使测量元素变化,导致后续的resize时间无法响应,此处测量元素变化时需要重新绑定
110
+ }, [measureRowElement, virtual]);
101
111
 
102
- var _React$useState2 = React__default["default"].useState(null),
103
- headerTableElement = _React$useState2[0],
104
- setHeaderTableElement = _React$useState2[1];
112
+ var _React$useState3 = React__default["default"].useState(null),
113
+ headerTableElement = _React$useState3[0],
114
+ setHeaderTableElement = _React$useState3[1];
105
115
  /**
106
116
  * 控制列最小可调整宽度
107
117
  */
@@ -155,7 +165,8 @@ var useColWidth = function useColWidth(_ref) {
155
165
  };
156
166
  }, [colWidths]);
157
167
  return {
158
- measureRowElementRef: measureRowElementRef,
168
+ measureRowElement: measureRowElement,
169
+ setMeasureRowElement: setMeasureRowElement,
159
170
  onColumnResizable: onColumnResizable,
160
171
  getColgroupProps: getColgroupProps,
161
172
  setHeaderTableElement: setHeaderTableElement,
@@ -215,7 +215,7 @@ var useTable = function useTable(_a) {
215
215
  resizable: resizable,
216
216
  virtual: virtual
217
217
  }),
218
- measureRowElementRef = _useColWidth.measureRowElementRef,
218
+ setMeasureRowElement = _useColWidth.setMeasureRowElement,
219
219
  getColgroupProps = _useColWidth.getColgroupProps,
220
220
  onColumnResizable = _useColWidth.onColumnResizable,
221
221
  colWidths = _useColWidth.colWidths; // ************************ 列冻结 ************************ //
@@ -565,7 +565,7 @@ var useTable = function useTable(_a) {
565
565
  // 行多选
566
566
  rowSelection: rowSelection,
567
567
  cacheData: cacheData,
568
- measureRowElementRef: measureRowElementRef,
568
+ setMeasureRowElement: setMeasureRowElement,
569
569
  leafColumns: leafColumns,
570
570
  // ui
571
571
  // 有表头分组那么也要 bordered
@@ -32,7 +32,7 @@ var TableBody = /*#__PURE__*/forwardRef(function (_ref, ref) {
32
32
  var _useTableContext = useTableContext(),
33
33
  columns = _useTableContext.columns,
34
34
  leafColumns = _useTableContext.leafColumns,
35
- measureRowElementRef = _useTableContext.measureRowElementRef,
35
+ setMeasureRowElement = _useTableContext.setMeasureRowElement,
36
36
  isExpandTreeRows = _useTableContext.isExpandTreeRows,
37
37
  transitionData = _useTableContext.transitionData,
38
38
  getColgroupProps = _useTableContext.getColgroupProps,
@@ -97,7 +97,9 @@ var TableBody = /*#__PURE__*/forwardRef(function (_ref, ref) {
97
97
  overflowX: canScroll ? 'scroll' : undefined
98
98
  }
99
99
  }, /*#__PURE__*/React__default.createElement("div", {
100
- ref: measureRowElementRef,
100
+ ref: function ref(domElement) {
101
+ setMeasureRowElement(domElement);
102
+ },
101
103
  style: {
102
104
  height: 1,
103
105
  background: 'transparent'
@@ -162,7 +164,11 @@ var TableBody = /*#__PURE__*/forwardRef(function (_ref, ref) {
162
164
  }, getColgroupProps(col, idx)));
163
165
  })), /*#__PURE__*/React__default.createElement("tbody", null, isArrayNonEmpty(transitionData) ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, transitionData.map(function (row, index) {
164
166
  return /*#__PURE__*/React__default.createElement(TableRow, Object.assign({
165
- ref: index === 0 ? measureRowElementRef : null,
167
+ ref: function ref(dom) {
168
+ if (index === 0) {
169
+ setMeasureRowElement(dom);
170
+ }
171
+ },
166
172
  // key={depth + index}
167
173
  key: row.id,
168
174
  // @ts-ignore
@@ -7,59 +7,69 @@
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
- import React__default, { useRef } from 'react';
10
+ import React__default, { useCallback } from 'react';
11
11
  import { getGroupItemWidth } from '../utils/index.js';
12
12
  import { useUpdateEffect } from '@hi-ui/use-update-effect';
13
13
 
14
14
  var useColWidth = function useColWidth(_ref) {
15
15
  var columns = _ref.columns,
16
16
  virtual = _ref.virtual;
17
- var measureRowElementRef = useRef(null);
18
17
 
19
- var _React$useState = React__default.useState(function () {
18
+ var _React$useState = React__default.useState(null),
19
+ measureRowElement = _React$useState[0],
20
+ setMeasureRowElement = _React$useState[1];
21
+
22
+ var _React$useState2 = React__default.useState(function () {
20
23
  return getGroupItemWidth(columns);
21
24
  }),
22
- colWidths = _React$useState[0],
23
- setColWidths = _React$useState[1];
25
+ colWidths = _React$useState2[0],
26
+ setColWidths = _React$useState2[1];
24
27
 
25
- console.error({
26
- useColWidth: 1,
27
- colWidths: colWidths
28
- });
28
+ var getVirtualWidths = useCallback(function () {
29
+ if (!measureRowElement) {
30
+ return getGroupItemWidth(columns);
31
+ }
32
+ /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
33
+
34
+
35
+ var columnDefaultWidth = 200;
36
+ var containerWidth = measureRowElement.clientWidth;
37
+ var totalWidth = 0;
38
+ /** 虚拟滚动,需要根据collist的虚拟宽度来计算宽度 */
39
+
40
+ columns.forEach(function (columnItem) {
41
+ totalWidth += columnItem.width || columnDefaultWidth;
42
+ });
43
+
44
+ if (totalWidth < containerWidth) {
45
+ // 容器宽度大于设置的宽度总和时,col宽度等比分分配占满容器。
46
+ return columns.map(function (columnItem) {
47
+ return (columnItem.width || columnDefaultWidth) * containerWidth / totalWidth;
48
+ });
49
+ } else {
50
+ // 容器宽度小于设置的宽度总和时,col宽度等于设置/默认宽度。
51
+ return columns.map(function (columnItem) {
52
+ return columnItem.width || columnDefaultWidth;
53
+ });
54
+ }
55
+ }, [measureRowElement, columns]);
29
56
  useUpdateEffect(function () {
30
- setColWidths(getGroupItemWidth(columns));
31
- }, [columns]);
57
+ if (virtual) {
58
+ // 虚拟滚动的计算需要根据容器来做分配,不能使用没有witdh默认设置为0的方式来做表格平均分配
59
+ setColWidths(getVirtualWidths());
60
+ } else {
61
+ setColWidths(getGroupItemWidth(columns));
62
+ }
63
+ }, [columns, getVirtualWidths, virtual]);
32
64
  /**
33
65
  * 根据实际内容区(table 的第一行)渲染,再次精确收集并设置每列宽度
34
66
  */
35
67
 
36
68
  React__default.useEffect(function () {
37
- var measureRowElement = measureRowElementRef.current;
38
-
39
69
  if (measureRowElement) {
40
70
  var _resizeObserver = new ResizeObserver(function () {
41
71
  if (virtual) {
42
- /** 虚拟滚动时,内容宽度不能用以前table自动渲染的方式获取,需要手动计算 */
43
- var columnDefaultWidth = 200;
44
- var containerWidth = measureRowElement.clientWidth;
45
- var totalWidth = 0;
46
- /** 虚拟滚动,需要根据collist的虚拟宽度来计算宽度 */
47
-
48
- columns.forEach(function (columnItem) {
49
- totalWidth += columnItem.width || columnDefaultWidth;
50
- });
51
-
52
- if (totalWidth < containerWidth) {
53
- // 容器宽度大于设置的宽度总和时,col宽度等比分分配占满容器。
54
- setColWidths(columns.map(function (columnItem) {
55
- return (columnItem.width || columnDefaultWidth) * containerWidth / totalWidth;
56
- }));
57
- } else {
58
- // 容器宽度小于设置的宽度总和时,col宽度等于设置/默认宽度。
59
- setColWidths(columns.map(function (columnItem) {
60
- return columnItem.width || columnDefaultWidth;
61
- }));
62
- }
72
+ setColWidths(getVirtualWidths());
63
73
  } else {
64
74
  if (measureRowElement.childNodes) {
65
75
  var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node) {
@@ -78,12 +88,12 @@ var useColWidth = function useColWidth(_ref) {
78
88
  _resizeObserver.observe(measureRowElement);
79
89
  }
80
90
 
81
- return function () {};
82
- }, []);
91
+ return function () {}; // 测量元素在内容列为空时会是空,切换会使测量元素变化,导致后续的resize时间无法响应,此处测量元素变化时需要重新绑定
92
+ }, [measureRowElement, virtual]);
83
93
 
84
- var _React$useState2 = React__default.useState(null),
85
- headerTableElement = _React$useState2[0],
86
- setHeaderTableElement = _React$useState2[1];
94
+ var _React$useState3 = React__default.useState(null),
95
+ headerTableElement = _React$useState3[0],
96
+ setHeaderTableElement = _React$useState3[1];
87
97
  /**
88
98
  * 控制列最小可调整宽度
89
99
  */
@@ -137,7 +147,8 @@ var useColWidth = function useColWidth(_ref) {
137
147
  };
138
148
  }, [colWidths]);
139
149
  return {
140
- measureRowElementRef: measureRowElementRef,
150
+ measureRowElement: measureRowElement,
151
+ setMeasureRowElement: setMeasureRowElement,
141
152
  onColumnResizable: onColumnResizable,
142
153
  getColgroupProps: getColgroupProps,
143
154
  setHeaderTableElement: setHeaderTableElement,
@@ -183,7 +183,7 @@ var useTable = function useTable(_a) {
183
183
  resizable: resizable,
184
184
  virtual: virtual
185
185
  }),
186
- measureRowElementRef = _useColWidth.measureRowElementRef,
186
+ setMeasureRowElement = _useColWidth.setMeasureRowElement,
187
187
  getColgroupProps = _useColWidth.getColgroupProps,
188
188
  onColumnResizable = _useColWidth.onColumnResizable,
189
189
  colWidths = _useColWidth.colWidths; // ************************ 列冻结 ************************ //
@@ -533,7 +533,7 @@ var useTable = function useTable(_a) {
533
533
  // 行多选
534
534
  rowSelection: rowSelection,
535
535
  cacheData: cacheData,
536
- measureRowElementRef: measureRowElementRef,
536
+ setMeasureRowElement: setMeasureRowElement,
537
537
  leafColumns: leafColumns,
538
538
  // ui
539
539
  // 有表头分组那么也要 bordered
@@ -69,7 +69,7 @@ export declare const TableProvider: import("react").Provider<(Omit<{
69
69
  flattedColumnsWithoutChildren: import("./types").FlattedTableColumnItemData[];
70
70
  rowSelection: import("./types").TableRowSelection | undefined;
71
71
  cacheData: object[];
72
- measureRowElementRef: import("react").RefObject<HTMLTableRowElement>;
72
+ setMeasureRowElement: import("react").Dispatch<import("react").SetStateAction<Element | null>>;
73
73
  leafColumns: import("packages/utils/tree-utils/lib/types/types").BaseFlattedTreeNodeData<any, any>[];
74
74
  bordered: boolean;
75
75
  scrollHeaderElementRef: import("react").RefObject<HTMLTableElement>;
@@ -174,7 +174,7 @@ export declare const useTableContext: () => Omit<{
174
174
  flattedColumnsWithoutChildren: import("./types").FlattedTableColumnItemData[];
175
175
  rowSelection: import("./types").TableRowSelection | undefined;
176
176
  cacheData: object[];
177
- measureRowElementRef: import("react").RefObject<HTMLTableRowElement>;
177
+ setMeasureRowElement: import("react").Dispatch<import("react").SetStateAction<Element | null>>;
178
178
  leafColumns: import("packages/utils/tree-utils/lib/types/types").BaseFlattedTreeNodeData<any, any>[];
179
179
  bordered: boolean;
180
180
  scrollHeaderElementRef: import("react").RefObject<HTMLTableElement>;
@@ -6,7 +6,8 @@ export declare const useColWidth: ({ resizable, data, columns, virtual, }: {
6
6
  columns: TableColumnItem[];
7
7
  virtual?: boolean | undefined;
8
8
  }) => {
9
- measureRowElementRef: React.RefObject<HTMLTableRowElement>;
9
+ measureRowElement: Element | null;
10
+ setMeasureRowElement: React.Dispatch<React.SetStateAction<Element | null>>;
10
11
  onColumnResizable: (_: any, { size }: any, index: number) => void;
11
12
  getColgroupProps: (column: FlattedTableColumnItemData, index: number) => {
12
13
  style: {
@@ -85,7 +85,7 @@ export declare const useTable: ({ data, columns: columnsProp, defaultFixedToColu
85
85
  flattedColumnsWithoutChildren: FlattedTableColumnItemData[];
86
86
  rowSelection: TableRowSelection | undefined;
87
87
  cacheData: object[];
88
- measureRowElementRef: React.RefObject<HTMLTableRowElement>;
88
+ setMeasureRowElement: React.Dispatch<React.SetStateAction<Element | null>>;
89
89
  leafColumns: import("packages/utils/tree-utils/lib/types/types").BaseFlattedTreeNodeData<any, any>[];
90
90
  bordered: boolean;
91
91
  scrollHeaderElementRef: React.RefObject<HTMLTableElement>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/table",
3
- "version": "4.1.0-beta.2",
3
+ "version": "4.1.0-beta.3",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",