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
3 changes: 3 additions & 0 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Body = <RecordType,>(props: BodyProps<RecordType>) => {
getRowKey,
expandedKeys,
childrenColumnName,
rowExpandable,
emptyNode,
classNames,
styles,
Expand All @@ -44,6 +45,7 @@ const Body = <RecordType,>(props: BodyProps<RecordType>) => {
'getRowKey',
'expandedKeys',
'childrenColumnName',
'rowExpandable',
'emptyNode',
'classNames',
'styles',
Expand All @@ -59,6 +61,7 @@ const Body = <RecordType,>(props: BodyProps<RecordType>) => {
childrenColumnName,
expandedKeys,
getRowKey,
rowExpandable,
);

const rowKeys = React.useMemo(() => flattenData.map(item => item.rowKey), [flattenData]);
Expand Down
10 changes: 9 additions & 1 deletion src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
expandedKeys,
prefixCls,
childrenColumnName,
rowExpandable,
scrollX,
direction,
} = useContext(TableContext, [
Expand All @@ -48,6 +49,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
'prefixCls',
'expandedKeys',
'childrenColumnName',
'rowExpandable',
'scrollX',
'direction',
]);
Expand All @@ -64,7 +66,13 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
const listRef = React.useRef<ListRef>(null);

// =========================== Data ===========================
const flattenData = useFlattenRecords(data, childrenColumnName, expandedKeys, getRowKey);
const flattenData = useFlattenRecords(
data,
childrenColumnName,
expandedKeys,
getRowKey,
rowExpandable,
);

// ========================== Column ==========================
const columnsWidth = React.useMemo<[key: React.Key, width: number, total: number][]>(() => {
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/useExpand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ export default function useExpand<RecordType>(
return defaultExpandedRowKeys;
}
if (defaultExpandAllRows) {
return findAllChildrenKeys<RecordType>(mergedData, getRowKey, mergedChildrenColumnName);
return findAllChildrenKeys<RecordType>(
mergedData,
getRowKey,
mergedChildrenColumnName,
rowExpandable,
);
}
return [];
});
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/useFlattenRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function fillRecords<T>(
expandedKeys: Set<Key>,
getRowKey: GetRowKey<T>,
index: number,
rowExpandable?: (record: T) => boolean,
) {
const key = getRowKey(record, index);

Expand All @@ -22,7 +23,12 @@ function fillRecords<T>(

const expanded = expandedKeys?.has(key);

if (record && Array.isArray(record[childrenColumnName]) && expanded) {
if (
record &&
(!rowExpandable || rowExpandable(record)) &&
Array.isArray(record[childrenColumnName]) &&
expanded
) {
// expanded state, flat record
for (let i = 0; i < record[childrenColumnName].length; i += 1) {
fillRecords(
Expand All @@ -33,6 +39,7 @@ function fillRecords<T>(
expandedKeys,
getRowKey,
i,
rowExpandable,
);
}
}
Expand Down Expand Up @@ -61,6 +68,7 @@ export default function useFlattenRecords<T>(
childrenColumnName: string,
expandedKeys: Set<Key>,
getRowKey: GetRowKey<T>,
rowExpandable?: (record: T) => boolean,
): FlattenData<T>[] {
const arr = React.useMemo<FlattenData<T>[]>(() => {
if (expandedKeys?.size) {
Expand All @@ -71,7 +79,7 @@ export default function useFlattenRecords<T>(
const record = data[i];

// using array.push or spread operator may cause "Maximum call stack size exceeded" exception if array size is big enough.
fillRecords(list, record, 0, childrenColumnName, expandedKeys, getRowKey, i);
fillRecords(list, record, 0, childrenColumnName, expandedKeys, getRowKey, i, rowExpandable);
}

return list;
Expand All @@ -85,7 +93,7 @@ export default function useFlattenRecords<T>(
rowKey: getRowKey(item, index),
};
});
}, [data, childrenColumnName, expandedKeys, getRowKey]);
}, [data, childrenColumnName, expandedKeys, getRowKey, rowExpandable]);

return arr;
}
7 changes: 5 additions & 2 deletions src/hooks/useRowInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ export default function useRowInfo<RecordType>(
const nestExpandable = expandableType === 'nest';

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

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

const hasNestChildren = childrenColumnName && record && record[childrenColumnName];
const nestChildren = childrenColumnName && record && record[childrenColumnName];
const hasNestChildren =
rowSupportNestExpand && Array.isArray(nestChildren) && nestChildren.length > 0;
const mergedExpandable = rowSupportExpand || hasNestChildren;

const onInternalTriggerExpand = useEvent(onTriggerExpand);

Expand Down
5 changes: 5 additions & 0 deletions src/utils/expandUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ export function findAllChildrenKeys<RecordType>(
data: readonly RecordType[],
getRowKey: GetRowKey<RecordType>,
childrenColumnName: string,
rowExpandable?: (record: RecordType) => boolean,
): Key[] {
const keys: Key[] = [];

function dig(list: readonly RecordType[]) {
(list || []).forEach((item, index) => {
if (rowExpandable && !rowExpandable(item)) {
return;
}

keys.push(getRowKey(item, index));

dig((item as any)[childrenColumnName]);
Expand Down
75 changes: 75 additions & 0 deletions tests/ExpandRow.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,81 @@ describe('Table.Expand', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('honors rowExpandable for tree data', () => {
const onExpand = vi.fn();
const data = [
{
key: 'allowed',
name: 'Allowed parent',
children: [{ key: 'allowed-child', name: 'Allowed child' }],
},
{
key: 'blocked',
name: 'Blocked parent',
children: [{ key: 'blocked-child', name: 'Blocked child' }],
},
{ key: 'empty', name: 'Empty parent', children: [] },
];
const { container } = render(
createTable({
data,
expandable: {
expandedRowKeys: ['allowed', 'blocked', 'empty'],
expandRowByClick: true,
onExpand,
rowExpandable: record => record.key === 'allowed',
},
}),
);

expect(container.querySelector('[data-row-key="allowed-child"]')).toBeTruthy();
expect(container.querySelector('[data-row-key="blocked-child"]')).toBeFalsy();

const allowedIcon = container.querySelector(
'[data-row-key="allowed"] .rc-table-row-expand-icon',
);
const blockedIcon = container.querySelector(
'[data-row-key="blocked"] .rc-table-row-expand-icon',
);
const emptyIcon = container.querySelector('[data-row-key="empty"] .rc-table-row-expand-icon');
expect(allowedIcon).toHaveClass('rc-table-row-expanded');
expect(blockedIcon).toHaveClass('rc-table-row-spaced');
expect(emptyIcon).toHaveClass('rc-table-row-spaced');

fireEvent.click(container.querySelector('[data-row-key="blocked"]'));
expect(onExpand).not.toHaveBeenCalled();
});

it('honors rowExpandable when expanding all tree rows by default', () => {
const data = [
{
key: 'allowed',
name: 'Allowed parent',
children: [{ key: 'allowed-child', name: 'Allowed child' }],
},
{
key: 'blocked',
name: 'Blocked parent',
children: [{ key: 'blocked-child', name: 'Blocked child' }],
},
];
const { container } = render(
createTable({
data,
expandable: {
defaultExpandAllRows: true,
rowExpandable: record => record.key === 'allowed',
},
}),
);

expect(container.querySelector('[data-row-key="allowed-child"]')).toBeTruthy();
expect(container.querySelector('[data-row-key="blocked-child"]')).toBeFalsy();
expect(
container.querySelector('[data-row-key="blocked"] .rc-table-row-expand-icon'),
).toHaveClass('rc-table-row-spaced');
});

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: 'spark' }] },
{ key: 'bamboo', children: [{ key: 'little' }] },
],
},
Expand Down
25 changes: 25 additions & 0 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ describe('Table.Virtual', () => {
});

describe('expandable', () => {
it('honors rowExpandable for tree data', () => {
const { container } = getTable({
data: [
{
name: 'allowed',
children: [{ name: 'allowed-child' }],
},
{
name: 'blocked',
children: [{ name: 'blocked-child' }],
},
],
expandable: {
expandedRowKeys: ['allowed', 'blocked'],
rowExpandable: record => record.name === 'allowed',
},
});

expect(container.querySelector('[data-row-key="allowed-child"]')).toBeTruthy();
expect(container.querySelector('[data-row-key="blocked-child"]')).toBeFalsy();
expect(
container.querySelector('[data-row-key="blocked"] .rc-table-row-expand-icon'),
).toHaveClass('rc-table-row-spaced');
});

it('basic', () => {
(['bamboo', () => 'bamboo'] as const).forEach(cls => {
const { container } = getTable({
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 @@ -907,7 +907,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
Loading