Skip to content

Commit ab7ffb2

Browse files
authored
fix expandRowByClick (#470)
* Add test case * fix expandRowByClick and expandIcon bug * add test case
1 parent 27a43d7 commit ab7ffb2

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

examples/expandIcon.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ function CustomExpandIcon(props) {
2424
return (
2525
<a
2626
className="expand-row-icon"
27-
onClick={e => props.onExpand(props.record, e)}
27+
onClick={e => {
28+
props.onExpand(props.record, e);
29+
}}
2830
// eslint-disable-next-line react/no-danger
2931
dangerouslySetInnerHTML={{ __html: text }}
3032
style={{ color: 'blue', cursor: 'pointer' }}
@@ -42,6 +44,7 @@ const Demo = () => (
4244
columns={columns}
4345
data={data}
4446
expandable={{
47+
expandRowByClick: true,
4548
expandedRowRender: record => <p>extra: {record.a}</p>,
4649
onExpand,
4750
expandIcon: CustomExpandIcon,

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
343343
expandedKeys: mergedExpandedKeys,
344344
getRowKey,
345345
// https://github.com/ant-design/ant-design/issues/23894
346-
onTriggerExpand: expandRowByClick && expandIcon ? () => {} : onTriggerExpand,
346+
onTriggerExpand,
347347
expandIcon: mergedExpandIcon,
348348
expandIconColumnIndex,
349349
direction,

src/hooks/useColumns.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function useColumns<RecordType>(
118118
rowExpandable,
119119
expandIconColumnIndex,
120120
direction,
121+
expandRowByClick,
121122
}: {
122123
prefixCls?: string;
123124
columns?: ColumnsType<RecordType>;
@@ -130,6 +131,7 @@ function useColumns<RecordType>(
130131
rowExpandable?: (record: RecordType) => boolean;
131132
expandIconColumnIndex?: number;
132133
direction?: 'ltr' | 'rtl';
134+
expandRowByClick?: boolean;
133135
},
134136
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
135137
): [ColumnsType<RecordType>, ColumnType<RecordType>[]] {
@@ -156,13 +158,25 @@ function useColumns<RecordType>(
156158
const expanded = expandedKeys.has(rowKey);
157159
const recordExpandable = rowExpandable ? rowExpandable(record) : true;
158160

159-
return expandIcon({
161+
const icon = expandIcon({
160162
prefixCls,
161163
expanded,
162164
expandable: recordExpandable,
163165
record,
164166
onExpand: onTriggerExpand,
165167
});
168+
return React.isValidElement(icon)
169+
? React.cloneElement(icon, {
170+
onClick: (e: MouseEvent) => {
171+
if (expandRowByClick) {
172+
e.stopPropagation();
173+
}
174+
if (icon && icon.props && icon.props.onClick) {
175+
icon.props.onClick(e);
176+
}
177+
},
178+
})
179+
: icon;
166180
},
167181
};
168182

tests/ExpandRow.spec.js

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,38 +371,51 @@ describe('Table.Expand', () => {
371371

372372
// https://github.com/ant-design/ant-design/issues/23894
373373
it('should be collapsible when use `expandIcon` & `expandRowByClick`', () => {
374-
const data = [{ key: 0, name: 'Lucy', age: 27, children: [{ key: 1, name: 'Jack', age: 28 }] }];
374+
const data = [{ key: 0, name: 'Lucy', age: 27 }];
375375
const onExpand = jest.fn();
376376
const wrapper = mount(
377377
createTable({
378378
expandable: {
379379
expandedRowRender,
380380
expandRowByClick: true,
381-
expandIcon: ({ onExpand: onIconExpand }) => (
382-
<span className="custom-expand-icon" onClick={onIconExpand} />
383-
),
384381
onExpand,
382+
expandIcon: ({ onExpand: onIconExpand, record }) => (
383+
<span className="custom-expand-icon" onClick={() => onIconExpand(record)} />
384+
),
385385
},
386386
data,
387387
}),
388388
);
389+
expect(wrapper.find('.rc-table-expanded-row').length).toBe(0);
389390
wrapper
390391
.find('.custom-expand-icon')
391392
.first()
392393
.simulate('click');
393394
expect(onExpand).toHaveBeenCalledWith(true, data[0]);
394395
expect(onExpand).toHaveBeenCalledTimes(1);
396+
expect(
397+
wrapper
398+
.find('.rc-table-expanded-row')
399+
.first()
400+
.getDOMNode().style.display,
401+
).toBe('');
395402
wrapper
396403
.find('.custom-expand-icon')
397404
.first()
398405
.simulate('click');
399406
expect(onExpand).toHaveBeenCalledWith(false, data[0]);
400407
expect(onExpand).toHaveBeenCalledTimes(2);
408+
expect(
409+
wrapper
410+
.find('.rc-table-expanded-row')
411+
.first()
412+
.getDOMNode().style.display,
413+
).toBe('none');
401414
});
402415

403416
// https://github.com/ant-design/ant-design/issues/23894
404417
it('should be collapsible when `expandRowByClick` without custom `expandIcon`', () => {
405-
const data = [{ key: 0, name: 'Lucy', age: 27, children: [{ key: 1, name: 'Jack', age: 28 }] }];
418+
const data = [{ key: 0, name: 'Lucy', age: 27 }];
406419
const onExpand = jest.fn();
407420
const wrapper = mount(
408421
createTable({
@@ -427,4 +440,57 @@ describe('Table.Expand', () => {
427440
expect(onExpand).toHaveBeenCalledWith(false, data[0]);
428441
expect(onExpand).toHaveBeenCalledTimes(2);
429442
});
443+
444+
it('should be collapsible when `expandRowByClick` with custom `expandIcon` and event.stopPropagation', () => {
445+
const data = [{ key: 0, name: 'Lucy', age: 27 }];
446+
const onExpand = jest.fn();
447+
const wrapper = mount(
448+
createTable({
449+
expandable: {
450+
expandedRowRender,
451+
expandRowByClick: true,
452+
onExpand,
453+
expandIcon: ({ onExpand: onIconExpand, record }) => (
454+
<span
455+
className="custom-expand-icon"
456+
onClick={e => {
457+
e.stopPropagation();
458+
onIconExpand(record);
459+
}}
460+
/>
461+
),
462+
},
463+
data,
464+
}),
465+
);
466+
wrapper
467+
.find('.custom-expand-icon')
468+
.first()
469+
.simulate('click');
470+
expect(onExpand).toHaveBeenCalledWith(true, data[0]);
471+
expect(onExpand).toHaveBeenCalledTimes(1);
472+
wrapper
473+
.find('.custom-expand-icon')
474+
.first()
475+
.simulate('click');
476+
expect(onExpand).toHaveBeenCalledWith(false, data[0]);
477+
expect(onExpand).toHaveBeenCalledTimes(2);
478+
});
479+
480+
it('support invalid expandIcon', () => {
481+
const data = [{ key: 0, name: 'Lucy', age: 27 }];
482+
const onExpand = jest.fn();
483+
const wrapper = mount(
484+
createTable({
485+
expandable: {
486+
expandedRowRender,
487+
expandRowByClick: true,
488+
onExpand,
489+
expandIcon: () => null,
490+
},
491+
data,
492+
}),
493+
);
494+
expect(wrapper.find('.rc-table-expanded-row').length).toBe(0);
495+
});
430496
});

0 commit comments

Comments
 (0)