Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function getCellProps<RecordType>(
{expandIcon({
prefixCls,
expanded,
expandable: hasNestChildren,
expandable: hasNestChildren && expandable,
record,
onExpand: onTriggerExpand,
})}
Expand Down
13 changes: 9 additions & 4 deletions src/hooks/useRowInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ export default function useRowInfo<RecordType>(
// Only when row is not expandable and `children` exist in record
const nestExpandable = expandableType === 'nest';

const rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
const mergedExpandable = rowSupportExpand || nestExpandable;

const expanded = expandedKeys && expandedKeys.has(rowKey);

const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
const rowExpandablePass = !rowExpandable || rowExpandable(record);
const rowSupportExpand = expandableType === 'row' && rowExpandablePass;

const nestChildren = childrenColumnName ? record?.[childrenColumnName] : undefined;
// Empty children array should not be expandable
const hasNestChildren = Array.isArray(nestChildren) && nestChildren.length > 0;
// Keep an already expanded row collapsible even if `rowExpandable` now returns false
const mergedExpandable =
rowSupportExpand || (nestExpandable && hasNestChildren && (rowExpandablePass || expanded));

const onInternalTriggerExpand = useEvent(onTriggerExpand);

Expand Down
107 changes: 107 additions & 0 deletions tests/ExpandRow.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,113 @@ describe('Table.Expand', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('respects rowExpandable in tree mode', () => {
const data = [
{
key: 0,
name: 'Lucy',
age: 27,
children: [{ key: 2, name: 'Jim', age: 1 }],
},
{
key: 1,
name: 'Jack',
age: 28,
children: [{ key: 3, name: 'Tom', age: 2 }],
},
];
const { container } = render(
createTable({
data,
expandable: { rowExpandable: ({ key }) => key === 0 },
}),
);

const iconTypes = Array.from(
container.querySelectorAll('td .rc-table-row-expand-icon'),
).map(icon => (icon.classList.contains('rc-table-row-spaced') ? 'spaced' : 'expandable'));

expect(iconTypes).toEqual(['expandable', 'spaced']);
});

it('keeps expanded tree rows collapsible when rowExpandable returns false', () => {
const data = [
{
key: 0,
name: 'Lucy',
age: 27,
children: [{ key: 2, name: 'Jim', age: 1 }],
},
];
const { container } = render(
createTable({
data,
expandable: {
defaultExpandedRowKeys: [0],
rowExpandable: () => false,
},
}),
);

expect(container.querySelectorAll('tbody tr')).toHaveLength(2);

const icon = container.querySelector('td .rc-table-row-expand-icon');
expect(icon.classList.contains('rc-table-row-expanded')).toBeTruthy();

fireEvent.click(icon);

expect(container.querySelectorAll('tbody tr')).toHaveLength(1);
expect(
container
.querySelector('td .rc-table-row-expand-icon')
.classList.contains('rc-table-row-spaced'),
).toBeTruthy();
});

it('ignores row click for non-expandable tree rows', () => {
const onExpand = vi.fn();
const data = [
{
key: 0,
name: 'Lucy',
age: 27,
children: [{ key: 2, name: 'Jim', age: 1 }],
},
];
const { container } = render(
createTable({
data,
expandable: {
expandRowByClick: true,
rowExpandable: () => false,
onExpand,
},
}),
);

fireEvent.click(container.querySelector('tbody tr'));

expect(onExpand).not.toHaveBeenCalled();
expect(container.querySelectorAll('tbody tr')).toHaveLength(1);
});

it('does not treat empty children array as expandable', () => {
const onExpand = vi.fn();
const data = [{ key: 0, name: 'Lucy', age: 27, children: [] }];
const { container } = render(
createTable({
data,
expandable: { expandRowByClick: true, onExpand },
}),
);

const icon = container.querySelector('td .rc-table-row-expand-icon');
expect(icon.classList.contains('rc-table-row-spaced')).toBeTruthy();

fireEvent.click(container.querySelector('tbody tr'));
expect(onExpand).not.toHaveBeenCalled();
});

it('not use nest when children is invalidate', () => {
const data = [
{ key: 2, name: 'Jack', age: 28, children: null },
Expand Down
2 changes: 1 addition & 1 deletion tests/Table.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ describe('Table.Basic', () => {
{
key: 'parent',
children: [
{ key: 'light', children: [] },
{ key: 'light', children: [{ key: 'light-child' }] },
{ key: 'bamboo', children: [{ key: 'little' }] },
],
},
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/ExpandRow.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ exports[`Table.Expand > renders tree row correctly with different children 1`] =
class="rc-table-row-indent indent-level-0"
/>
<span
class="rc-table-row-expand-icon rc-table-row-collapsed"
class="rc-table-row-expand-icon rc-table-row-spaced"
/>
Jack
</td>
Expand Down