fix(Table): the onFilter methods of column adds ability to filter children (#47170)

This commit is contained in:
木木剑光 2024-01-27 22:11:54 +08:00 committed by GitHub
parent f613db0226
commit 25eda833fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 8 deletions

View File

@ -289,6 +289,7 @@ const InternalTable = <RecordType extends AnyObject = AnyObject>(
currentDataSource: getFilterData(
getSortData(rawData, changeInfo.sorterStates!, childrenColumnName),
changeInfo.filterStates!,
childrenColumnName,
),
action,
});
@ -355,7 +356,7 @@ const InternalTable = <RecordType extends AnyObject = AnyObject>(
getPopupContainer: getPopupContainer || getContextPopupContainer,
rootClassName: classNames(rootClassName, rootCls),
});
const mergedData = getFilterData(sortedData, filterStates);
const mergedData = getFilterData(sortedData, filterStates, childrenColumnName);
changeEventInfo.filters = filters;
changeEventInfo.filterStates = filterStates;

View File

@ -579,6 +579,47 @@ describe('Table.filter', () => {
expect(container.querySelectorAll('tbody tr').length).toBe(4);
});
it('can filter children by defaultFilteredValue', async () => {
const { container } = render(
createTable({
columns: [
{
...column,
defaultFilteredValue: ['Jim', 'Tom'],
onFilter: (value: string, record) => {
if (record.children && record.children.length) {
return true;
}
return record.name.includes(value);
},
},
],
dataSource: [
{
key: '0',
name: 'Jack',
children: [
{ key: '0-1', name: 'Jim' },
{ key: '0-2', name: 'Tony' },
],
},
{ key: '1', name: 'Lucy' },
{ key: '2', name: 'Tom' },
{ key: '3', name: 'Jerry' },
],
expandable: {
defaultExpandAllRows: true,
},
}),
);
expect([...container.querySelectorAll('tbody tr')].map((item) => item.textContent)).toEqual([
'Jack',
'Jim',
'Tom',
]);
});
// Warning: An update to Item ran an effect, but was not wrapped in act(...).
it('fires change event', () => {
const handleChange = jest.fn();

View File

@ -157,6 +157,7 @@ function generateFilterInfo<RecordType>(filterStates: FilterState<RecordType>[])
export function getFilterData<RecordType>(
data: RecordType[],
filterStates: FilterState<RecordType>[],
childrenColumnName: string,
) {
return filterStates.reduce((currentData, filterState) => {
const {
@ -164,13 +165,28 @@ export function getFilterData<RecordType>(
filteredKeys,
} = filterState;
if (onFilter && filteredKeys && filteredKeys.length) {
return currentData.filter((record) =>
filteredKeys.some((key) => {
const keys = flattenKeys(filters);
const keyIndex = keys.findIndex((k) => String(k) === String(key));
const realKey = keyIndex !== -1 ? keys[keyIndex] : key;
return onFilter(realKey, record);
}),
return (
currentData
// shallow copy
.map((record) => ({ ...record }))
.filter((record: any) =>
filteredKeys.some((key) => {
const keys = flattenKeys(filters);
const keyIndex = keys.findIndex((k) => String(k) === String(key));
const realKey = keyIndex !== -1 ? keys[keyIndex] : key;
// filter children
if (record[childrenColumnName]) {
record[childrenColumnName] = getFilterData(
record[childrenColumnName],
filterStates,
childrenColumnName,
);
}
return onFilter(realKey, record);
}),
)
);
}
return currentData;