Skip to content

Commit e7880a8

Browse files
committed
feat(Table): add rowExpandable to remove + before row, close #1518
1 parent f00a2da commit e7880a8

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

docs/table/demo/expanded-lock.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- order: 6
44

5-
(IE下不支持) `Table.StickyLock` 模式下,展开行的可视区域宽度与 `Table` 占屏幕宽度保持一致, 本模式下 `expandedRowIndent``[0, 0]`,不可修改。
5+
`Table.StickyLock` 模式下(IE不支持,会fallback到旧有逻辑),展开行的可视区域宽度与 `Table` 占屏幕宽度保持一致, 本模式下 `expandedRowIndent``[0, 0]`,不可修改。
66

77
:::lang=en-us
88
# Expandable
@@ -23,7 +23,8 @@ const dataSource = () => {
2323
result.push({
2424
title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
2525
id: 100306660940 + i,
26-
time: 2000 + i
26+
time: 2000 + i,
27+
expandable: i !== 2
2728
});
2829
}
2930
return result;
@@ -63,6 +64,7 @@ class App extends React.Component {
6364
// expandedRowIndent 仅在IE下才会生效,非IE模式下为[0,0]且不可修改
6465
expandedRowIndent={[2, 0]}
6566
expandedRowRender={expandedRowRender}
67+
rowExpandable={record => record.expandable}
6668
onRowClick={() => console.log('rowClick')}
6769
>
6870
<Table.Column title="Id" dataIndex="id" lock width={100}/>

src/table/base.jsx

+7
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ class Table extends React.Component {
201201
* @returns {Element} 渲染内容
202202
*/
203203
expandedRowRender: PropTypes.func,
204+
/**
205+
* 设置行是否可展开,设置 false 为不可展开
206+
* @param {Object} record 该行所对应的数据
207+
* @param {Number} index 该行所对应的序列
208+
* @returns {Boolean} 是否可展开
209+
*/
210+
rowExpandable: PropTypes.func,
204211
/**
205212
* 额外渲染行的缩进
206213
*/

src/table/expanded.jsx

+19-26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export default function expanded(BaseComponent, stickyLock) {
2222
* @returns {Element}
2323
*/
2424
expandedRowRender: PropTypes.func,
25+
/**
26+
* 设置行是否可展开,设置 false 为不可展开
27+
* @param {Object} record 该行所对应的数据
28+
* @param {Number} index 该行所对应的序列
29+
* @returns {Boolean} 是否可展开
30+
*/
31+
rowExpandable: PropTypes.func,
2532
/**
2633
* 额外渲染行的缩进
2734
*/
@@ -64,6 +71,7 @@ export default function expanded(BaseComponent, stickyLock) {
6471
static childContextTypes = {
6572
openRowKeys: PropTypes.array,
6673
expandedRowRender: PropTypes.func,
74+
rowExpandable: PropTypes.func,
6775
expandedIndexSimulate: PropTypes.bool,
6876
expandedRowWidthEquals2Table: PropTypes.bool,
6977
expandedRowIndent: PropTypes.array,
@@ -79,9 +87,7 @@ export default function expanded(BaseComponent, stickyLock) {
7987
expandedRowRender: this.props.expandedRowRender,
8088
expandedIndexSimulate: this.props.expandedIndexSimulate,
8189
expandedRowWidthEquals2Table: stickyLock,
82-
expandedRowIndent: stickyLock
83-
? [0, 0]
84-
: this.props.expandedRowIndent,
90+
expandedRowIndent: stickyLock ? [0, 0] : this.props.expandedRowIndent,
8591
};
8692
}
8793

@@ -105,23 +111,19 @@ export default function expanded(BaseComponent, stickyLock) {
105111
};
106112

107113
renderExpandedCell = (value, index, record) => {
108-
const { getExpandedColProps, prefix, locale } = this.props;
114+
const { getExpandedColProps, prefix, locale, rowExpandable } = this.props;
115+
116+
if (typeof rowExpandable === 'function' && !rowExpandable(record, index)) {
117+
return '';
118+
}
109119

110120
const { openRowKeys } = this.state,
111121
{ primaryKey } = this.props,
112122
hasExpanded = openRowKeys.indexOf(record[primaryKey]) > -1,
113123
switchNode = hasExpanded ? (
114-
<Icon
115-
type="minus"
116-
size="xs"
117-
className={`${prefix}table-expand-unfold`}
118-
/>
124+
<Icon type="minus" size="xs" className={`${prefix}table-expand-unfold`} />
119125
) : (
120-
<Icon
121-
type="add"
122-
size="xs"
123-
className={`${prefix}table-expand-fold`}
124-
/>
126+
<Icon type="add" size="xs" className={`${prefix}table-expand-fold`} />
125127
),
126128
attrs = getExpandedColProps(record, index) || {};
127129
const cls = classnames({
@@ -131,24 +133,14 @@ export default function expanded(BaseComponent, stickyLock) {
131133
});
132134

133135
if (!attrs.disabled) {
134-
attrs.onClick = this.onExpandedClick.bind(
135-
this,
136-
value,
137-
record,
138-
index
139-
);
136+
attrs.onClick = this.onExpandedClick.bind(this, value, record, index);
140137
}
141138
return (
142139
<span
143140
{...attrs}
144141
role="button"
145142
tabIndex="0"
146-
onKeyDown={this.expandedKeydown.bind(
147-
this,
148-
value,
149-
record,
150-
index
151-
)}
143+
onKeyDown={this.expandedKeydown.bind(this, value, record, index)}
152144
aria-label={hasExpanded ? locale.expanded : locale.folded}
153145
aria-expanded={hasExpanded}
154146
className={cls}
@@ -228,6 +220,7 @@ export default function expanded(BaseComponent, stickyLock) {
228220
components,
229221
openRowKeys,
230222
expandedRowRender,
223+
rowExpandable,
231224
hasExpandedRowCtrl,
232225
children,
233226
columns,

test/table/index-spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,29 @@ describe('Table', () => {
520520
);
521521
});
522522

523+
it('should support rowExpandable', done => {
524+
timeout(
525+
{
526+
dataSource: [
527+
{ id: '1', name: 'test', expandable: false },
528+
{ id: '2', name: 'test2', expandable: true, },
529+
{ id: '3', name: 'test3', expandable: true },
530+
],
531+
expandedRowRender: record => record.name,
532+
rowExpandable: record => record.expandable
533+
},
534+
() => {
535+
let expandedTotal = wrapper
536+
.find('.next-table-row');
537+
let expandedIcon = wrapper
538+
.find('.next-table-prerow .next-table-cell-wrapper .next-icon');
539+
540+
assert(expandedTotal.length - expandedIcon.length === 1);
541+
done();
542+
}
543+
);
544+
});
545+
523546
it('should support multiple header', done => {
524547
timeout(
525548
{
@@ -594,6 +617,9 @@ describe('Table', () => {
594617
onFilter,
595618
children: [<Table.Column dataIndex="id" filters={filters} />],
596619
});
620+
621+
assert(wrapper.find('next-table-filter-active').length === 0);
622+
597623
wrapper.find('.next-icon-filter').simulate('click');
598624
wrapper
599625
.find('.next-btn')
@@ -612,6 +638,7 @@ describe('Table', () => {
612638
.simulate('click');
613639
assert.deepEqual(id, '3');
614640

641+
assert(wrapper.find('next-table-filter-active'));
615642
wrapper.setProps({
616643
filterParams: {
617644
id: {

0 commit comments

Comments
 (0)