From 8dced1a7a240dc2edc6d5f10060418b34e3209cc Mon Sep 17 00:00:00 2001 From: "jianhu.wang" Date: Tue, 17 Nov 2026 10:54:16 +0800 Subject: [PATCH 1/3] fix: respect rowExpandable in tree mode --- src/Body/BodyRow.tsx | 3 ++- src/hooks/useRowInfo.tsx | 1 + tests/ExpandRow.spec.jsx | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index 64b8a096a..5857bfd7c 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -56,6 +56,7 @@ export function getCellProps( expandIcon, expanded, hasNestChildren, + rowExpandable, onTriggerExpand, expandable, expandedKeys, @@ -76,7 +77,7 @@ export function getCellProps( {expandIcon({ prefixCls, expanded, - expandable: hasNestChildren, + expandable: !!hasNestChildren && (!rowExpandable || rowExpandable(record)), record, onExpand: onTriggerExpand, })} diff --git a/src/hooks/useRowInfo.tsx b/src/hooks/useRowInfo.tsx index cdba4059e..909a530f5 100644 --- a/src/hooks/useRowInfo.tsx +++ b/src/hooks/useRowInfo.tsx @@ -27,6 +27,7 @@ export default function useRowInfo( | 'expandIconColumnIndex' | 'expandedKeys' | 'childrenColumnName' + | 'rowExpandable' | 'onRow' > & { columnsKey: React.Key[]; diff --git a/tests/ExpandRow.spec.jsx b/tests/ExpandRow.spec.jsx index 50430c325..928bff396 100644 --- a/tests/ExpandRow.spec.jsx +++ b/tests/ExpandRow.spec.jsx @@ -162,6 +162,35 @@ 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('not use nest when children is invalidate', () => { const data = [ { key: 2, name: 'Jack', age: 28, children: null }, From 0ba8cbee5c0d63c5b12e9094cf723c3b05aea0ae Mon Sep 17 00:00:00 2001 From: "jianhu.wang" Date: Tue, 17 Nov 2026 11:34:56 +0800 Subject: [PATCH 2/3] fix: align tree row expand interactions with rowExpandable --- src/Body/BodyRow.tsx | 3 +- src/hooks/useRowInfo.tsx | 9 +++--- tests/ExpandRow.spec.jsx | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index 5857bfd7c..aca53aed0 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -56,7 +56,6 @@ export function getCellProps( expandIcon, expanded, hasNestChildren, - rowExpandable, onTriggerExpand, expandable, expandedKeys, @@ -77,7 +76,7 @@ export function getCellProps( {expandIcon({ prefixCls, expanded, - expandable: !!hasNestChildren && (!rowExpandable || rowExpandable(record)), + expandable: !!hasNestChildren && expandable, record, onExpand: onTriggerExpand, })} diff --git a/src/hooks/useRowInfo.tsx b/src/hooks/useRowInfo.tsx index 909a530f5..175aebd94 100644 --- a/src/hooks/useRowInfo.tsx +++ b/src/hooks/useRowInfo.tsx @@ -27,7 +27,6 @@ export default function useRowInfo( | 'expandIconColumnIndex' | 'expandedKeys' | 'childrenColumnName' - | 'rowExpandable' | 'onRow' > & { columnsKey: React.Key[]; @@ -75,11 +74,13 @@ 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 rowExpandablePass = !rowExpandable || rowExpandable(record); + const rowSupportExpand = expandableType === 'row' && rowExpandablePass; + // Keep an already expanded row collapsible even if `rowExpandable` now returns false + const mergedExpandable = rowSupportExpand || (nestExpandable && (rowExpandablePass || expanded)); + const hasNestChildren = childrenColumnName && record && record[childrenColumnName]; const onInternalTriggerExpand = useEvent(onTriggerExpand); diff --git a/tests/ExpandRow.spec.jsx b/tests/ExpandRow.spec.jsx index 928bff396..8e4b0eeaa 100644 --- a/tests/ExpandRow.spec.jsx +++ b/tests/ExpandRow.spec.jsx @@ -191,6 +191,67 @@ describe('Table.Expand', () => { 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('not use nest when children is invalidate', () => { const data = [ { key: 2, name: 'Jack', age: 28, children: null }, From 851fb90f917882cb8003a87b9ec1a44532dfc4e0 Mon Sep 17 00:00:00 2001 From: "jianhu.wang" Date: Tue, 17 Nov 2026 11:52:09 +0800 Subject: [PATCH 3/3] fix: treat empty children array as non-expandable in tree mode --- src/Body/BodyRow.tsx | 2 +- src/hooks/useRowInfo.tsx | 9 ++++++--- tests/ExpandRow.spec.jsx | 17 +++++++++++++++++ tests/Table.spec.jsx | 2 +- tests/__snapshots__/ExpandRow.spec.jsx.snap | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index aca53aed0..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, + expandable: hasNestChildren && expandable, record, onExpand: onTriggerExpand, })} diff --git a/src/hooks/useRowInfo.tsx b/src/hooks/useRowInfo.tsx index 175aebd94..034eeb5b0 100644 --- a/src/hooks/useRowInfo.tsx +++ b/src/hooks/useRowInfo.tsx @@ -78,10 +78,13 @@ export default function useRowInfo( const rowExpandablePass = !rowExpandable || rowExpandable(record); const rowSupportExpand = expandableType === 'row' && rowExpandablePass; - // Keep an already expanded row collapsible even if `rowExpandable` now returns false - const mergedExpandable = rowSupportExpand || (nestExpandable && (rowExpandablePass || expanded)); - const hasNestChildren = childrenColumnName && record && record[childrenColumnName]; + 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 8e4b0eeaa..5f9a642a4 100644 --- a/tests/ExpandRow.spec.jsx +++ b/tests/ExpandRow.spec.jsx @@ -252,6 +252,23 @@ describe('Table.Expand', () => { 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