mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-12 12:23:08 +08:00
fix: Table column.title missing filters
prop (#37629)
* fix: Table title missing filter data * test: add test case
This commit is contained in:
parent
82a971a433
commit
09b6032ec4
@ -269,7 +269,8 @@ function InternalTable<RecordType extends object = any>(
|
|||||||
/**
|
/**
|
||||||
* Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to read
|
* Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to read
|
||||||
* state out and then put it back to title render. Move these code into `hooks` but still too
|
* state out and then put it back to title render. Move these code into `hooks` but still too
|
||||||
* complex. We should provides Table props like `sorter` & `filter` to handle control in next big version.
|
* complex. We should provides Table props like `sorter` & `filter` to handle control in next big
|
||||||
|
* version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// ============================ Sorter =============================
|
// ============================ Sorter =============================
|
||||||
@ -317,7 +318,7 @@ function InternalTable<RecordType extends object = any>(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const [transformFilterColumns, filterStates, getFilters] = useFilter<RecordType>({
|
const [transformFilterColumns, filterStates, filters] = useFilter<RecordType>({
|
||||||
prefixCls,
|
prefixCls,
|
||||||
locale: tableLocale,
|
locale: tableLocale,
|
||||||
dropdownPrefixCls,
|
dropdownPrefixCls,
|
||||||
@ -327,16 +328,23 @@ function InternalTable<RecordType extends object = any>(
|
|||||||
});
|
});
|
||||||
const mergedData = getFilterData(sortedData, filterStates);
|
const mergedData = getFilterData(sortedData, filterStates);
|
||||||
|
|
||||||
changeEventInfo.filters = getFilters();
|
changeEventInfo.filters = filters;
|
||||||
changeEventInfo.filterStates = filterStates;
|
changeEventInfo.filterStates = filterStates;
|
||||||
|
|
||||||
// ============================ Column ============================
|
// ============================ Column ============================
|
||||||
const columnTitleProps = React.useMemo(
|
const columnTitleProps = React.useMemo(() => {
|
||||||
() => ({
|
const mergedFilters: Record<string, FilterValue> = {};
|
||||||
|
Object.keys(filters).forEach(filterKey => {
|
||||||
|
if (filters[filterKey] !== null) {
|
||||||
|
mergedFilters[filterKey] = filters[filterKey]!;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return {
|
||||||
...sorterTitleProps,
|
...sorterTitleProps,
|
||||||
}),
|
filters: mergedFilters,
|
||||||
[sorterTitleProps],
|
};
|
||||||
);
|
}, [sorterTitleProps, filters]);
|
||||||
|
|
||||||
const [transformTitleColumns] = useTitleColumns(columnTitleProps);
|
const [transformTitleColumns] = useTitleColumns(columnTitleProps);
|
||||||
|
|
||||||
// ========================== Pagination ==========================
|
// ========================== Pagination ==========================
|
||||||
|
@ -2475,4 +2475,26 @@ describe('Table.filter', () => {
|
|||||||
?.disabled,
|
?.disabled,
|
||||||
).toBeTruthy();
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('title render function support `filter`', () => {
|
||||||
|
const title = jest.fn(() => 'RenderTitle');
|
||||||
|
const { container } = render(
|
||||||
|
createTable({
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
...column,
|
||||||
|
title,
|
||||||
|
filteredValue: ['boy'],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(container.querySelector('.ant-table-column-title')?.textContent).toEqual('RenderTitle');
|
||||||
|
expect(title).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
filters: { name: ['boy'] },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -203,7 +203,7 @@ function useFilter<RecordType>({
|
|||||||
}: FilterConfig<RecordType>): [
|
}: FilterConfig<RecordType>): [
|
||||||
TransformColumns<RecordType>,
|
TransformColumns<RecordType>,
|
||||||
FilterState<RecordType>[],
|
FilterState<RecordType>[],
|
||||||
() => Record<string, FilterValue | null>,
|
Record<string, FilterValue | null>,
|
||||||
] {
|
] {
|
||||||
const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(
|
const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(
|
||||||
collectFilterStates(mergedColumns, true),
|
collectFilterStates(mergedColumns, true),
|
||||||
@ -235,10 +235,7 @@ function useFilter<RecordType>({
|
|||||||
return collectedStates;
|
return collectedStates;
|
||||||
}, [mergedColumns, filterStates]);
|
}, [mergedColumns, filterStates]);
|
||||||
|
|
||||||
const getFilters = React.useCallback(
|
const filters = React.useMemo(() => generateFilterInfo(mergedFilterStates), [mergedFilterStates]);
|
||||||
() => generateFilterInfo(mergedFilterStates),
|
|
||||||
[mergedFilterStates],
|
|
||||||
);
|
|
||||||
|
|
||||||
const triggerFilter = (filterState: FilterState<RecordType>) => {
|
const triggerFilter = (filterState: FilterState<RecordType>) => {
|
||||||
const newFilterStates = mergedFilterStates.filter(({ key }) => key !== filterState.key);
|
const newFilterStates = mergedFilterStates.filter(({ key }) => key !== filterState.key);
|
||||||
@ -258,7 +255,7 @@ function useFilter<RecordType>({
|
|||||||
tableLocale,
|
tableLocale,
|
||||||
);
|
);
|
||||||
|
|
||||||
return [transformColumns, mergedFilterStates, getFilters];
|
return [transformColumns, mergedFilterStates, filters];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useFilter;
|
export default useFilter;
|
||||||
|
@ -63,7 +63,7 @@ export interface ColumnTitleProps<RecordType> {
|
|||||||
sortColumn?: ColumnType<RecordType>;
|
sortColumn?: ColumnType<RecordType>;
|
||||||
sortColumns?: { column: ColumnType<RecordType>; order: SortOrder }[];
|
sortColumns?: { column: ColumnType<RecordType>; order: SortOrder }[];
|
||||||
|
|
||||||
filters?: Record<string, string[]>;
|
filters?: Record<string, FilterValue>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ColumnTitle<RecordType> =
|
export type ColumnTitle<RecordType> =
|
||||||
|
Loading…
Reference in New Issue
Block a user