-
Notifications
You must be signed in to change notification settings - Fork 600
Expand file tree
/
Copy pathexpanded.jsx
More file actions
324 lines (293 loc) · 11.2 KB
/
expanded.jsx
File metadata and controls
324 lines (293 loc) · 11.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import React, { Children } from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { polyfill } from 'react-lifecycles-compat';
import Icon from '../icon';
import { KEYCODE, dom, events } from '../util';
import RowComponent from './expanded/row';
import Col from './column';
import { statics } from './util';
const noop = () => {};
export default function expanded(BaseComponent, stickyLock) {
/** Table */
class ExpandedTable extends React.Component {
static ExpandedRow = RowComponent;
static propTypes = {
/**
* 额外渲染行的渲染函数
* @param {Object} record 该行所对应的数据
* @param {Number} index 该行所对应的序列
* @returns {Element}
*/
expandedRowRender: PropTypes.func,
/**
* 设置行是否可展开,设置 false 为不可展开
* @param {Object} record 该行所对应的数据
* @param {Number} index 该行所对应的序列
* @returns {Boolean} 是否可展开
* @version 1.21
*/
rowExpandable: PropTypes.func,
/**
* 额外渲染行的缩进
*/
expandedRowIndent: PropTypes.array,
/**
* 默认情况下展开的渲染行或者Tree, 传入此属性为受控状态
*/
openRowKeys: PropTypes.array,
/**
* 默认情况下展开的 Expand行 或者 Tree行,非受控模式
* @version 1.23.22
*/
defaultOpenRowKeys: PropTypes.array,
/**
* 是否显示点击展开额外渲染行的+号按钮
*/
hasExpandedRowCtrl: PropTypes.bool,
/**
* 设置额外渲染行的属性
*/
getExpandedColProps: PropTypes.func,
/**
* 在额外渲染行或者Tree展开或者收起的时候触发的事件
* @param {Array} openRowKeys 展开的渲染行的key
* @param {String} currentRowKey 当前点击的渲染行的key
* @param {Boolean} expanded 当前点击是展开还是收起
* @param {Object} currentRecord 当前点击额外渲染行的记录
*/
onRowOpen: PropTypes.func,
onExpandedRowClick: PropTypes.func,
locale: PropTypes.object,
...BaseComponent.propTypes,
};
static defaultProps = {
...BaseComponent.defaultProps,
getExpandedColProps: noop,
onRowOpen: noop,
hasExpandedRowCtrl: true,
components: {},
expandedRowIndent: stickyLock ? [0, 0] : [1, 0],
prefix: 'next-',
};
static childContextTypes = {
openRowKeys: PropTypes.array,
expandedRowRender: PropTypes.func,
expandedIndexSimulate: PropTypes.bool,
expandedRowWidthEquals2Table: PropTypes.bool,
expandedRowIndent: PropTypes.array,
getExpandedRowRef: PropTypes.func,
getTableInstanceForExpand: PropTypes.func,
};
state = {
openRowKeys: this.props.openRowKeys || this.props.defaultOpenRowKeys || [],
};
getChildContext() {
return {
openRowKeys: this.state.openRowKeys,
expandedRowRender: this.props.expandedRowRender,
expandedIndexSimulate: this.props.expandedIndexSimulate,
expandedRowWidthEquals2Table: stickyLock,
getExpandedRowRef: this.saveExpandedRowRef,
getTableInstanceForExpand: this.getTableInstance,
expandedRowIndent: stickyLock ? [0, 0] : this.props.expandedRowIndent,
};
}
static getDerivedStateFromProps(nextProps) {
if ('openRowKeys' in nextProps) {
return {
openRowKeys: nextProps.openRowKeys || [],
};
}
return null;
}
componentDidMount() {
this.setExpandedWidth();
events.on(window, 'resize', this.setExpandedWidth);
}
componentDidUpdate() {
this.setExpandedWidth();
}
componentWillUnmount() {
events.off(window, 'resize', this.setExpandedWidth);
}
saveExpandedRowRef = (key, rowRef) => {
if (!this.expandedRowRefs) {
this.expandedRowRefs = {};
}
this.expandedRowRefs[key] = rowRef;
};
setExpandedWidth = () => {
const { prefix } = this.props;
const tableEl = this.getTableNode();
const totalWidth = +(tableEl && tableEl.clientWidth) - 1 || '100%';
const bodyNode = tableEl && tableEl.querySelector(`.${prefix}table-body`);
Object.keys(this.expandedRowRefs || {}).forEach(key => {
dom.setStyle(this.expandedRowRefs[key], { width: (bodyNode && bodyNode.clientWidth) || totalWidth });
});
};
getTableInstance = instance => {
this.tableInc = instance;
};
getTableNode() {
const table = this.tableInc;
try {
// in case of finding an unmounted component due to cached data
// need to clear refs of table when dataSource Changed
// use try catch for temporary
return findDOMNode(table.tableEl);
} catch (error) {
return null;
}
}
expandedKeydown = (value, record, index, e) => {
e.preventDefault();
e.stopPropagation();
if (e.keyCode === KEYCODE.ENTER) {
this.onExpandedClick(value, record, index, e);
}
};
renderExpandedCell = (value, index, record) => {
const { getExpandedColProps, prefix, locale, rowExpandable } = this.props;
if (typeof rowExpandable === 'function' && !rowExpandable(record, index)) {
return '';
}
const { openRowKeys } = this.state,
{ primaryKey } = this.props,
hasExpanded = openRowKeys.indexOf(record[primaryKey]) > -1,
switchNode = hasExpanded ? (
<Icon type="minus" size="xs" className={`${prefix}table-expand-unfold`} />
) : (
<Icon type="add" size="xs" className={`${prefix}table-expand-fold`} />
),
attrs = getExpandedColProps(record, index) || {};
const cls = classnames({
[`${prefix}table-expanded-ctrl`]: true,
disabled: attrs.disabled,
[attrs.className]: attrs.className,
});
if (!attrs.disabled) {
attrs.onClick = this.onExpandedClick.bind(this, value, record, index);
}
return (
<span
{...attrs}
role="button"
tabIndex="0"
onKeyDown={this.expandedKeydown.bind(this, value, record, index)}
aria-label={hasExpanded ? locale.expanded : locale.folded}
aria-expanded={hasExpanded}
className={cls}
>
{switchNode}
</span>
);
};
onExpandedClick(value, record, i, e) {
const openRowKeys = [...this.state.openRowKeys],
{ primaryKey } = this.props,
id = record[primaryKey],
index = openRowKeys.indexOf(id);
if (index > -1) {
openRowKeys.splice(index, 1);
} else {
openRowKeys.push(id);
}
if (!('openRowKeys' in this.props)) {
this.setState({
openRowKeys: openRowKeys,
});
}
this.props.onRowOpen(openRowKeys, id, index === -1, record);
e.stopPropagation();
}
addExpandCtrl = columns => {
const { prefix, size } = this.props;
if (!columns.find(record => record.key === 'expanded')) {
columns.unshift({
key: 'expanded',
title: '',
cell: this.renderExpandedCell.bind(this),
width: size === 'small' ? 34 : 50,
className: `${prefix}table-expanded ${prefix}table-prerow`,
__normalized: true,
});
}
};
normalizeChildren(children) {
const { prefix, size } = this.props;
const toArrayChildren = Children.map(children, (child, index) =>
React.cloneElement(child, {
key: index,
})
);
toArrayChildren.unshift(
<Col
title=""
key="expanded"
cell={this.renderExpandedCell.bind(this)}
width={size === 'small' ? 34 : 50}
className={`${prefix}table-expanded ${prefix}table-prerow`}
__normalized
/>
);
return toArrayChildren;
}
normalizeDataSource(ds) {
const ret = [];
ds.forEach(item => {
const itemCopy = { ...item };
itemCopy.__expanded = true;
ret.push(item, itemCopy);
});
return ret;
}
render() {
/* eslint-disable no-unused-vars, prefer-const */
let {
components,
openRowKeys,
expandedRowRender,
rowExpandable,
hasExpandedRowCtrl,
children,
columns,
dataSource,
entireDataSource,
getExpandedColProps,
expandedRowIndent,
onRowOpen,
onExpandedRowClick,
...others
} = this.props;
if (expandedRowRender && !components.Row) {
components = { ...components };
components.Row = RowComponent;
dataSource = this.normalizeDataSource(dataSource);
entireDataSource = this.normalizeDataSource(entireDataSource);
}
if (expandedRowRender && hasExpandedRowCtrl) {
let useColumns = columns && !children;
if (useColumns) {
this.addExpandCtrl(columns);
} else {
children = this.normalizeChildren(children || []);
}
}
return (
<BaseComponent
{...others}
columns={columns}
dataSource={dataSource}
entireDataSource={entireDataSource}
components={components}
>
{children}
</BaseComponent>
);
}
}
statics(ExpandedTable, BaseComponent);
return polyfill(ExpandedTable);
}