@hi-ui/table 4.2.0 → 4.3.1

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/lib/cjs/BaseTable.js +127 -71
  3. package/lib/cjs/ColGroupContent.js +56 -0
  4. package/lib/cjs/Table.js +14 -3
  5. package/lib/cjs/TableBody.js +22 -75
  6. package/lib/cjs/TableHeader.js +5 -71
  7. package/lib/cjs/TbodyContent.js +129 -0
  8. package/lib/cjs/TheadContent.js +120 -0
  9. package/lib/cjs/hooks/use-col-width.js +27 -22
  10. package/lib/cjs/node_modules/perfect-scrollbar/dist/perfect-scrollbar.esm.js +1286 -0
  11. package/lib/cjs/packages/hooks/use-merge-refs/lib/esm/index.js +76 -0
  12. package/lib/cjs/packages/ui/scrollbar/lib/esm/Scrollbar.js +185 -0
  13. package/lib/cjs/packages/ui/scrollbar/lib/esm/styles/index.scss.js +42 -0
  14. package/lib/cjs/packages/ui/scrollbar/lib/esm/utils/index.js +81 -0
  15. package/lib/cjs/styles/index.scss.js +1 -1
  16. package/lib/cjs/use-table.js +39 -23
  17. package/lib/esm/BaseTable.js +124 -71
  18. package/lib/esm/ColGroupContent.js +37 -0
  19. package/lib/esm/Table.js +14 -3
  20. package/lib/esm/TableBody.js +19 -75
  21. package/lib/esm/TableHeader.js +5 -69
  22. package/lib/esm/TbodyContent.js +105 -0
  23. package/lib/esm/TheadContent.js +96 -0
  24. package/lib/esm/hooks/use-col-width.js +27 -22
  25. package/lib/esm/node_modules/perfect-scrollbar/dist/perfect-scrollbar.esm.js +1281 -0
  26. package/lib/esm/packages/hooks/use-merge-refs/lib/esm/index.js +68 -0
  27. package/lib/esm/packages/ui/scrollbar/lib/esm/Scrollbar.js +162 -0
  28. package/lib/esm/packages/ui/scrollbar/lib/esm/styles/index.scss.js +25 -0
  29. package/lib/esm/packages/ui/scrollbar/lib/esm/utils/index.js +76 -0
  30. package/lib/esm/styles/index.scss.js +1 -1
  31. package/lib/esm/use-table.js +33 -17
  32. package/lib/types/BaseTable.d.ts +4 -0
  33. package/lib/types/ColGroupContent.d.ts +8 -0
  34. package/lib/types/TbodyContent.d.ts +18 -0
  35. package/lib/types/TheadContent.d.ts +9 -0
  36. package/lib/types/context.d.ts +14 -8
  37. package/lib/types/hooks/use-col-width.d.ts +1 -1
  38. package/lib/types/use-table.d.ts +18 -6
  39. package/package.json +6 -6
@@ -0,0 +1,105 @@
1
+ /** @LICENSE
2
+ * @hi-ui/table
3
+ * https://github.com/XiaoMi/hiui/tree/master/packages/ui/table#readme
4
+ *
5
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+ import React__default, { forwardRef } from 'react';
11
+ import { getPrefixCls } from '@hi-ui/classname';
12
+ import { __DEV__ } from '@hi-ui/env';
13
+ import { useLatestCallback } from '@hi-ui/use-latest';
14
+ import { isArrayNonEmpty } from '@hi-ui/type-assertion';
15
+ import { EmptyState } from '@hi-ui/empty-state';
16
+ import { TableRow } from './TableRow.js';
17
+ import { useTableContext } from './context.js';
18
+
19
+ var _prefix = getPrefixCls('table-body');
20
+
21
+ var TbodyContent = /*#__PURE__*/forwardRef(function (_ref, ref) {
22
+ var _ref$prefixCls = _ref.prefixCls,
23
+ prefixCls = _ref$prefixCls === void 0 ? _prefix : _ref$prefixCls,
24
+ emptyContent = _ref.emptyContent;
25
+
26
+ var _useTableContext = useTableContext(),
27
+ columns = _useTableContext.columns,
28
+ isExpandTreeRows = _useTableContext.isExpandTreeRows,
29
+ transitionData = _useTableContext.transitionData,
30
+ scrollBodyElementRef = _useTableContext.scrollBodyElementRef,
31
+ hasAvgColumn = _useTableContext.hasAvgColumn,
32
+ avgRow = _useTableContext.avgRow,
33
+ hasSumColumn = _useTableContext.hasSumColumn,
34
+ sumRow = _useTableContext.sumRow,
35
+ measureRowElementRef = _useTableContext.measureRowElementRef;
36
+
37
+ var getRequiredProps = useLatestCallback(function (id) {
38
+ return {
39
+ // @ts-ignore
40
+ expandedTree: isExpandTreeRows(id) // checked: isCheckedId(id),
41
+ // semiChecked: isSemiCheckedId(id),
42
+ // selected: selectedId === id,
43
+ // loading: isLoadingId(id),
44
+ // focused: focusedId === id,
45
+
46
+ };
47
+ }); // 外层增加 div 作为滚动容器
48
+
49
+ return /*#__PURE__*/React__default.createElement("tbody", null, isArrayNonEmpty(transitionData) ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, transitionData.map(function (row, index) {
50
+ return /*#__PURE__*/React__default.createElement(TableRow, Object.assign({
51
+ ref: index === 0 ? measureRowElementRef : null,
52
+ // key={depth + index}
53
+ key: row.id,
54
+ // @ts-ignore
55
+ rowIndex: index,
56
+ rowData: row
57
+ }, getRequiredProps(row.id)));
58
+ }), hasSumColumn ? /*#__PURE__*/React__default.createElement(TableRow, {
59
+ key: sumRow.id,
60
+ rowIndex: transitionData.length,
61
+ rowData: sumRow,
62
+ isSumRow: true
63
+ }) : null, hasAvgColumn ? /*#__PURE__*/React__default.createElement(TableRow, {
64
+ key: avgRow.id,
65
+ rowIndex: transitionData.length + 1,
66
+ rowData: avgRow,
67
+ isAvgRow: true
68
+ }) : null) : // 空状态,colSpan 占满表格整行
69
+ renderEmptyContent(Object.assign({
70
+ className: prefixCls + "-empty-content",
71
+ colSpan: columns.length,
72
+ emptyContent: emptyContent
73
+ }, scrollBodyElementRef.current ? {
74
+ scrollBodyWidth: window.getComputedStyle(scrollBodyElementRef.current).getPropertyValue('width')
75
+ } : {})));
76
+ });
77
+
78
+ if (__DEV__) {
79
+ TbodyContent.displayName = 'TbodyContent';
80
+ }
81
+ /**
82
+ * 负责空状态渲染
83
+ */
84
+
85
+
86
+ var renderEmptyContent = function renderEmptyContent(_ref2) {
87
+ var className = _ref2.className,
88
+ colSpan = _ref2.colSpan,
89
+ emptyContent = _ref2.emptyContent,
90
+ scrollBodyWidth = _ref2.scrollBodyWidth;
91
+ return /*#__PURE__*/React__default.createElement("tr", {
92
+ className: className
93
+ }, /*#__PURE__*/React__default.createElement("td", {
94
+ colSpan: colSpan
95
+ }, /*#__PURE__*/React__default.createElement("div", {
96
+ style: {
97
+ position: 'sticky',
98
+ left: 0,
99
+ width: scrollBodyWidth !== null && scrollBodyWidth !== void 0 ? scrollBodyWidth : '100%',
100
+ overflow: 'hidden'
101
+ }
102
+ }, emptyContent || /*#__PURE__*/React__default.createElement(EmptyState, null))));
103
+ };
104
+
105
+ export { TbodyContent, renderEmptyContent };
@@ -0,0 +1,96 @@
1
+ /** @LICENSE
2
+ * @hi-ui/table
3
+ * https://github.com/XiaoMi/hiui/tree/master/packages/ui/table#readme
4
+ *
5
+ * Copyright (c) HiUI <mi-hiui@xiaomi.com>.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+ import { __rest } from 'tslib';
11
+ import React__default, { forwardRef } from 'react';
12
+ import { Resizable } from 'react-resizable';
13
+ import { getPrefixCls, cx } from '@hi-ui/classname';
14
+ import { __DEV__ } from '@hi-ui/env';
15
+ import { isFunction } from '@hi-ui/type-assertion';
16
+ import { useCheckState } from '@hi-ui/use-check-state';
17
+ import { useTableContext } from './context.js';
18
+ import { renderFilter } from './TableAdvancedFilter.js';
19
+
20
+ var _prefix = getPrefixCls('table-header');
21
+
22
+ var TheadContent = /*#__PURE__*/forwardRef(function (_a, ref) {
23
+ var _a$prefixCls = _a.prefixCls,
24
+ prefixCls = _a$prefixCls === void 0 ? _prefix : _a$prefixCls,
25
+ rest = __rest(_a, ["prefixCls"]);
26
+
27
+ var _useTableContext = useTableContext(),
28
+ groupedColumns = _useTableContext.groupedColumns,
29
+ resizable = _useTableContext.resizable,
30
+ colWidths = _useTableContext.colWidths,
31
+ isHoveredHighlightCol = _useTableContext.isHoveredHighlightCol,
32
+ isHighlightedCol = _useTableContext.isHighlightedCol,
33
+ onColumnResizable = _useTableContext.onColumnResizable,
34
+ getStickyColProps = _useTableContext.getStickyColProps,
35
+ showColMenu = _useTableContext.showColMenu,
36
+ setHeaderTableElement = _useTableContext.setHeaderTableElement;
37
+
38
+ var activeColumnKeysAction = useCheckState();
39
+ return /*#__PURE__*/React__default.createElement("thead", Object.assign({}, rest), groupedColumns.map(function (cols, colsIndex) {
40
+ return /*#__PURE__*/React__default.createElement("tr", {
41
+ key: colsIndex,
42
+ ref: setHeaderTableElement
43
+ }, cols.map(function (col, colIndex) {
44
+ var _ref = col || {},
45
+ dataKey = _ref.dataKey,
46
+ title = _ref.title,
47
+ raw = _ref.raw;
48
+
49
+ var titleContent = isFunction(title) ? title(col) : title;
50
+ titleContent = resizable ? /*#__PURE__*/React__default.createElement("span", {
51
+ className: prefixCls + "-header__title"
52
+ }, titleContent) : titleContent;
53
+ var cell = /*#__PURE__*/React__default.createElement("th", Object.assign({
54
+ key: dataKey
55
+ }, getStickyColProps(col), {
56
+ className: cx(prefixCls + "-cell", raw.className, isHighlightedCol(dataKey) && prefixCls + "-cell__col--highlight", isHoveredHighlightCol(dataKey) && prefixCls + "-cell__col--hovered-highlight", activeColumnKeysAction.has(dataKey) && prefixCls + "-cell__col--active"),
57
+ // @ts-ignore
58
+ colSpan: col.colSpan,
59
+ // @ts-ignore
60
+ rowSpan: col.rowSpan
61
+ }), titleContent, renderFilter({
62
+ prefixCls: prefixCls + "-filter",
63
+ columnKey: dataKey,
64
+ showColMenu: showColMenu,
65
+ column: col,
66
+ onOpen: function onOpen() {
67
+ return activeColumnKeysAction.add(dataKey);
68
+ },
69
+ onClose: function onClose() {
70
+ return activeColumnKeysAction.remove(dataKey);
71
+ }
72
+ }));
73
+ return resizable && colIndex !== colWidths.length - 1 ? /*#__PURE__*/React__default.createElement(Resizable, {
74
+ key: colIndex,
75
+ className: prefixCls + "__resizable",
76
+ draggableOpts: {
77
+ enableUserSelectHack: false
78
+ },
79
+ handle: /*#__PURE__*/React__default.createElement("span", {
80
+ className: prefixCls + "__resizable-handle"
81
+ }),
82
+ height: 0,
83
+ width: colWidths[colIndex],
84
+ onResize: function onResize(evt, options) {
85
+ onColumnResizable(evt, options, colIndex);
86
+ }
87
+ }, cell) : cell;
88
+ }));
89
+ }));
90
+ });
91
+
92
+ if (__DEV__) {
93
+ TheadContent.displayName = 'TheadContent';
94
+ }
95
+
96
+ export { TheadContent };
@@ -12,7 +12,8 @@ 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
- var columns = _ref.columns,
15
+ var resizable = _ref.resizable,
16
+ columns = _ref.columns,
16
17
  virtual = _ref.virtual;
17
18
  var measureRowElementRef = React__default.useRef(null);
18
19
 
@@ -72,19 +73,17 @@ var useColWidth = function useColWidth(_ref) {
72
73
  var _resizeObserver = new ResizeObserver(function () {
73
74
  if (virtual) {
74
75
  setColWidths(getVirtualWidths());
75
- } else {
76
- if (measureRowElement.childNodes) {
77
- var _realColumnsWidth = Array.from(measureRowElement.childNodes).map(function (node) {
78
- return node.getBoundingClientRect().width || 0;
79
- });
80
-
81
- if (_realColumnsWidth.some(function (width) {
82
- return width && width > 0;
83
- })) {
84
- setColWidths(_realColumnsWidth);
85
- }
86
- }
87
- }
76
+ } // else {
77
+ // if (measureRowElement.childNodes) {
78
+ // const _realColumnsWidth = Array.from(measureRowElement.childNodes).map((node) => {
79
+ // return (node as HTMLElement).getBoundingClientRect().width || 0
80
+ // })
81
+ // if (_realColumnsWidth.some((width) => width && width > 0)) {
82
+ // setColWidths(_realColumnsWidth)
83
+ // }
84
+ // }
85
+ // }
86
+
88
87
  });
89
88
 
90
89
  _resizeObserver.observe(measureRowElement);
@@ -102,26 +101,32 @@ var useColWidth = function useColWidth(_ref) {
102
101
 
103
102
 
104
103
  var minColWidth = React__default.useMemo(function () {
105
- if (headerTableElement && headerTableElement.childNodes && headerTableElement.childNodes[1].childNodes[0]) {
106
- var _minColWidth = Array.from(headerTableElement.childNodes[1].childNodes[0].childNodes).map(function (th) {
107
- // @ts-ignore
108
- return th.childNodes[0].className === 'hi-table__header__title' ? // @ts-ignore
109
- th.childNodes[0].offsetWidth : 0;
104
+ if (resizable && headerTableElement) {
105
+ var resizableHandlerWidth = 4;
106
+
107
+ var _minColWidth = Array.from(headerTableElement.childNodes).map(function (th) {
108
+ var thPaddingLeft = parseFloat(window.getComputedStyle(th).getPropertyValue('padding-left'));
109
+ var childNodes = Array.from(th.childNodes);
110
+ return childNodes.map(function (child) {
111
+ return child.offsetWidth;
112
+ }).reduce(function (prev, next) {
113
+ return prev + next;
114
+ }) + thPaddingLeft * 2 + resizableHandlerWidth;
110
115
  });
111
116
 
112
117
  return _minColWidth;
113
118
  }
114
119
 
115
120
  return Array(columns.length).fill(0);
116
- }, [columns, headerTableElement]);
121
+ }, [columns.length, headerTableElement, resizable]);
117
122
  /**
118
123
  * 列宽拖拽 resize,只处理拖拽线两边的列宽度
119
124
  */
120
125
 
121
126
  var onColumnResizable = React__default.useCallback(function (_, _ref2, index) {
122
127
  var size = _ref2.size;
123
- var minWidth = minColWidth[index] + 31;
124
- var anotherMinWidth = minColWidth[index + 1] + 31;
128
+ var minWidth = minColWidth[index];
129
+ var anotherMinWidth = minColWidth[index + 1];
125
130
  var nextWidth = size.width > minWidth ? size.width : minWidth;
126
131
  setColWidths(function (prev) {
127
132
  var nextColWidths = [].concat(prev);