diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index 64b8a096a..b3537408c 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -76,7 +76,7 @@ export function getCellProps( {expandIcon({ prefixCls, expanded, - expandable: hasNestChildren, + expandable: hasNestChildren && expandable, record, onExpand: onTriggerExpand, })} diff --git a/src/hooks/useRowInfo.tsx b/src/hooks/useRowInfo.tsx index cdba4059e..034eeb5b0 100644 --- a/src/hooks/useRowInfo.tsx +++ b/src/hooks/useRowInfo.tsx @@ -74,12 +74,17 @@ export default function useRowInfo( // 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); diff --git a/tests/ExpandRow.spec.jsx b/tests/ExpandRow.spec.jsx index 50430c325..5f9a642a4 100644 --- a/tests/ExpandRow.spec.jsx +++ b/tests/ExpandRow.spec.jsx @@ -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 }, diff --git a/tests/Table.spec.jsx b/tests/Table.spec.jsx index a4d1a74c1..676fd90a2 100644 --- a/tests/Table.spec.jsx +++ b/tests/Table.spec.jsx @@ -1013,7 +1013,7 @@ describe('Table.Basic', () => { { key: 'parent', children: [ - { key: 'light', children: [] }, + { key: 'light', children: [{ key: 'light-child' }] }, { key: 'bamboo', children: [{ key: 'little' }] }, ], }, diff --git a/tests/__snapshots__/ExpandRow.spec.jsx.snap b/tests/__snapshots__/ExpandRow.spec.jsx.snap index 72baf12a6..f6825d7e4 100644 --- a/tests/__snapshots__/ExpandRow.spec.jsx.snap +++ b/tests/__snapshots__/ExpandRow.spec.jsx.snap @@ -910,7 +910,7 @@ exports[`Table.Expand > renders tree row correctly with different children 1`] = class="rc-table-row-indent indent-level-0" /> Jack