Skip to content

Commit 7a3f796

Browse files
authored
fix: cannot expand when using expandIcon and expandRowByClick (#468)
1 parent 435e79c commit 7a3f796

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

examples/expandIcon.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,29 @@ function CustomExpandIcon(props) {
2525
<a
2626
className="expand-row-icon"
2727
onClick={e => props.onExpand(props.record, e)}
28+
// eslint-disable-next-line react/no-danger
2829
dangerouslySetInnerHTML={{ __html: text }}
2930
style={{ color: 'blue', cursor: 'pointer' }}
3031
/>
3132
);
3233
}
3334

34-
class Demo extends React.Component {
35-
onExpand = (expanded, record) => {
36-
console.log('onExpand', expanded, record);
37-
};
35+
const onExpand = (expanded, record) => {
36+
// eslint-disable-next-line no-console
37+
console.log('onExpand', expanded, record);
38+
};
3839

39-
render() {
40-
return (
41-
<div>
42-
<Table
43-
columns={columns}
44-
expandedRowRender={record => <p>extra: {record.a}</p>}
45-
onExpand={this.onExpand}
46-
expandIcon={CustomExpandIcon}
47-
data={data}
48-
/>
49-
</div>
50-
);
51-
}
52-
}
40+
const Demo = () => (
41+
<Table
42+
columns={columns}
43+
data={data}
44+
expandable={{
45+
expandRowByClick: true,
46+
expandedRowRender: record => <p>extra: {record.a}</p>,
47+
onExpand,
48+
expandIcon: CustomExpandIcon,
49+
}}
50+
/>
51+
);
5352

5453
export default Demo;

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
342342
expandable: !!expandedRowRender,
343343
expandedKeys: mergedExpandedKeys,
344344
getRowKey,
345-
onTriggerExpand,
345+
onTriggerExpand: expandRowByClick ? () => {} : onTriggerExpand,
346346
expandIcon: mergedExpandIcon,
347347
expandIconColumnIndex,
348348
direction,

tests/ExpandRow.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,35 @@ describe('Table.Expand', () => {
368368
);
369369
expect(wrapper.find('tbody tr')).toHaveLength(3);
370370
});
371+
372+
// https://github.com/ant-design/ant-design/issues/23894
373+
it('should be collapsible when use `expandIcon` & `expandRowByClick`', () => {
374+
const data = [{ key: 0, name: 'Lucy', age: 27, children: [{ key: 1, name: 'Jack', age: 28 }] }];
375+
const onExpand = jest.fn();
376+
const wrapper = mount(
377+
createTable({
378+
expandable: {
379+
expandedRowRender,
380+
expandRowByClick: true,
381+
expandIcon: ({ onExpand: onIconExpand }) => (
382+
<span className="custom-expand-icon" onClick={onIconExpand} />
383+
),
384+
onExpand,
385+
},
386+
data,
387+
}),
388+
);
389+
wrapper
390+
.find('.custom-expand-icon')
391+
.first()
392+
.simulate('click');
393+
expect(onExpand).toHaveBeenCalledWith(true, data[0]);
394+
expect(onExpand).toHaveBeenCalledTimes(1);
395+
wrapper
396+
.find('.custom-expand-icon')
397+
.first()
398+
.simulate('click');
399+
expect(onExpand).toHaveBeenCalledWith(false, data[0]);
400+
expect(onExpand).toHaveBeenCalledTimes(2);
401+
});
371402
});

0 commit comments

Comments
 (0)