-
-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathBaseTable.js
More file actions
167 lines (149 loc) 路 4.54 KB
/
Copy pathBaseTable.js
File metadata and controls
167 lines (149 loc) 路 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'mini-store';
import ColGroup from './ColGroup';
import TableHeader from './TableHeader';
import TableRow from './TableRow';
import ExpandableRow from './ExpandableRow';
class BaseTable extends React.Component {
static propTypes = {
fixed: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
]),
columns: PropTypes.array.isRequired,
tableClassName: PropTypes.string.isRequired,
hasHead: PropTypes.bool.isRequired,
hasBody: PropTypes.bool.isRequired,
store: PropTypes.object.isRequired,
expander: PropTypes.object.isRequired,
getRowKey: PropTypes.func,
}
static contextTypes = {
table: PropTypes.any,
}
handleRowHover = (isHover, key) => {
this.props.store.setState({
currentHoverKey: isHover ? key : null,
});
}
renderRows = (renderData, indent, ancestorKeys = []) => {
const { table } = this.context;
const { columnManager, components } = table;
const {
prefixCls,
childrenColumnName,
rowClassName,
rowRef,
onRowClick,
onRowDoubleClick,
onRowContextMenu,
onRowMouseEnter,
onRowMouseLeave,
onRow,
} = table.props;
const { getRowKey, fixed, expander } = this.props;
const rows = [];
for (let i = 0; i < renderData.length; i++) {
const record = renderData[i];
const key = getRowKey(record, i);
const className = typeof rowClassName === 'string'
? rowClassName
: rowClassName(record, i, indent);
const onHoverProps = {};
if (columnManager.isAnyColumnsFixed()) {
onHoverProps.onHover = this.handleRowHover;
}
let leafColumns;
if (fixed === 'left') {
leafColumns = columnManager.leftLeafColumns();
} else if (fixed === 'right') {
leafColumns = columnManager.rightLeafColumns();
} else {
leafColumns = columnManager.leafColumns();
}
const rowPrefixCls = `${prefixCls}-row`;
const row = (
<ExpandableRow
{...expander.props}
fixed={fixed}
index={i}
prefixCls={rowPrefixCls}
record={record}
key={key}
rowKey={key}
onRowClick={onRowClick}
needIndentSpaced={expander.needIndentSpaced}
onExpandedChange={expander.handleExpandChange}
>
{(expandableRow) => ( // eslint-disable-line
<TableRow
fixed={fixed}
indent={indent}
className={className}
record={record}
index={i}
prefixCls={rowPrefixCls}
childrenColumnName={childrenColumnName}
columns={leafColumns}
onRow={onRow}
onRowDoubleClick={onRowDoubleClick}
onRowContextMenu={onRowContextMenu}
onRowMouseEnter={onRowMouseEnter}
onRowMouseLeave={onRowMouseLeave}
{...onHoverProps}
rowKey={key}
ancestorKeys={ancestorKeys}
ref={rowRef(record, i, indent)}
components={components}
{...expandableRow}
/>
)}
</ExpandableRow>
);
rows.push(row);
const expandedRows = expander.renderRows(
this.renderRows,
record,
i,
indent,
fixed,
key,
ancestorKeys
);
if (expandedRows) {
rows.push(...expandedRows);
}
}
return rows;
}
render() {
const { table } = this.context;
const { components } = table;
const { prefixCls, scroll, data, getBodyWrapper } = table.props;
const { expander, tableClassName, hasHead, hasBody, fixed, columns } = this.props;
const tableStyle = {};
if (!fixed && scroll.x) {
// not set width, then use content fixed width
if (scroll.x === true) {
tableStyle.tableLayout = 'fixed';
} else {
tableStyle.width = scroll.x;
}
}
const Table = hasBody ? components.table : 'table';
const BodyWrapper = components.body.wrapper;
return (
<Table className={tableClassName} style={tableStyle} key="table">
<ColGroup columns={columns} fixed={fixed} />
{hasHead && <TableHeader expander={expander} columns={columns} fixed={fixed} /> }
{hasBody && getBodyWrapper(
<BodyWrapper className={`${prefixCls}-tbody`}>
{this.renderRows(data, 0)}
</BodyWrapper>
)}
</Table>
);
}
}
export default connect()(BaseTable);