1 | import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
2 | import React, { useContext, useMemo } from 'react';
|
3 | import { observer } from 'mobx-react-lite';
|
4 | import classNames from 'classnames';
|
5 | import measureScrollbar from '../../../es/_util/measureScrollbar';
|
6 | import { pxToRem } from '../../../es/_util/UnitConvertor';
|
7 | import TableContext from './TableContext';
|
8 | import { ColumnLock, DragColumnAlign } from './enum';
|
9 | import TableEditor from './TableEditor';
|
10 | import TableCol from './TableCol';
|
11 | import { getColumnKey, isStickySupport } from './utils';
|
12 | import { treeReduce } from '../_util/treeUtils';
|
13 | var TableWrapper = observer(function TableWrapper(props) {
|
14 | var children = props.children,
|
15 | hasBody = props.hasBody,
|
16 | lock = props.lock,
|
17 | hasHeader = props.hasHeader,
|
18 | hasFooter = props.hasFooter,
|
19 | columnGroups = props.columnGroups;
|
20 |
|
21 | var _useContext = useContext(TableContext),
|
22 | prefixCls = _useContext.prefixCls,
|
23 | summary = _useContext.summary,
|
24 | tableStore = _useContext.tableStore;
|
25 |
|
26 | var leafs = columnGroups.leafs,
|
27 | width = columnGroups.width;
|
28 | var overflowX = tableStore.overflowX,
|
29 | customizable = tableStore.customizable,
|
30 | rowDraggable = tableStore.rowDraggable,
|
31 | dragColumnAlign = tableStore.dragColumnAlign;
|
32 | var hasPlaceHolder = lock !== ColumnLock.left && (hasHeader || hasFooter) && tableStore.overflowY;
|
33 | var tableWidth = overflowX ? lock !== ColumnLock.left && !hasBody && tableStore.overflowY ? pxToRem(width + measureScrollbar()) : pxToRem(width) : '100%';
|
34 | var editors = useMemo(function () {
|
35 | return hasBody ? treeReduce(leafs.map(function (_ref) {
|
36 | var column = _ref.column;
|
37 | return column;
|
38 | }), function (nodes, column) {
|
39 | var editor = column.editor,
|
40 | name = column.name;
|
41 |
|
42 | if (editor && name && (lock || isStickySupport() || !column.lock || !overflowX)) {
|
43 | nodes.push(React.createElement(TableEditor, {
|
44 | key: getColumnKey(column),
|
45 | column: column
|
46 | }));
|
47 | }
|
48 |
|
49 | return nodes;
|
50 | }, []) : undefined;
|
51 | }, [leafs, overflowX, lock, hasBody]);
|
52 | var colGroup = useMemo(function () {
|
53 | var fixedColumnLength = 1;
|
54 |
|
55 | if (customizable) {
|
56 | fixedColumnLength += 1;
|
57 | }
|
58 |
|
59 | if (rowDraggable && dragColumnAlign === DragColumnAlign.right) {
|
60 | fixedColumnLength += 1;
|
61 | }
|
62 |
|
63 | var cols = leafs.map(function (_ref2, index, array) {
|
64 | var column = _ref2.column,
|
65 | key = _ref2.key;
|
66 | return React.createElement(TableCol, {
|
67 | key: key,
|
68 | column: column,
|
69 | last: !overflowX && index === array.length - fixedColumnLength
|
70 | });
|
71 | });
|
72 |
|
73 | if (hasPlaceHolder) {
|
74 | cols.push(React.createElement("col", {
|
75 | key: "fixed-column",
|
76 | style: {
|
77 | width: pxToRem(measureScrollbar())
|
78 | }
|
79 | }));
|
80 | }
|
81 |
|
82 | return React.createElement("colgroup", null, cols);
|
83 | }, [leafs, customizable, rowDraggable, dragColumnAlign, hasPlaceHolder, overflowX]);
|
84 | var style = useMemo(function () {
|
85 | return {
|
86 | width: tableWidth
|
87 | };
|
88 | }, [tableWidth]);
|
89 | var className = classNames(_defineProperty({}, "".concat(prefixCls, "-last-row-bordered"), hasBody && !tableStore.overflowY && (tableStore.height !== undefined || !tableStore.hasFooter && overflowX)));
|
90 | return React.createElement(React.Fragment, null, React.createElement("table", {
|
91 | key: "table",
|
92 | className: className,
|
93 | style: style,
|
94 | summary: hasBody ? summary : undefined
|
95 | }, colGroup, children), editors);
|
96 | });
|
97 | TableWrapper.displayName = 'TableWrapper';
|
98 | export default TableWrapper;
|
99 |
|