Skip to content

Commit

Permalink
fix jsx filter not work (ant-design#22888)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Apr 3, 2020
1 parent 89abaca commit 75524df
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
prefixCls,
locale: tableLocale,
dropdownPrefixCls,
columns: columns || [],
columns,
children,
onFilterChange,
getPopupContainer,
});
Expand Down
21 changes: 21 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,4 +1113,25 @@ describe('Table.filter', () => {
expect(onFilter).toHaveBeenCalled();
expect(wrapper.find('tbody tr')).toHaveLength(1);
});

it('jsx work', () => {
const wrapper = mount(
<Table dataSource={data}>
<Table.Column
title="Name"
dataIndex="name"
filters={[
{ text: 'Jack', value: 'Jack' },
{ text: 'Lucy', value: 'Lucy' },
]}
onFilter={(value, record) => record.name.includes(value)}
defaultFilteredValue={['Jack']}
/>
</Table>,
);

expect(wrapper.find('tbody tr')).toHaveLength(1);
expect(wrapper.find('tbody tr td').text()).toEqual('Jack');

});
});
15 changes: 11 additions & 4 deletions components/table/hooks/useFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { convertChildrenToColumns } from 'rc-table/lib/hooks/useColumns';
import {
TransformColumns,
ColumnsType,
Expand Down Expand Up @@ -161,7 +162,8 @@ export function getFilterData<RecordType>(
interface FilterConfig<RecordType> {
prefixCls: string;
dropdownPrefixCls: string;
columns: ColumnsType<RecordType>;
columns?: ColumnsType<RecordType>;
children?: React.ReactNode;
locale: TableLocale;
onFilterChange: (
filters: Record<string, Key[] | null>,
Expand All @@ -174,6 +176,7 @@ function useFilter<RecordType>({
prefixCls,
dropdownPrefixCls,
columns,
children,
onFilterChange,
getPopupContainer,
locale: tableLocale,
Expand All @@ -182,20 +185,24 @@ function useFilter<RecordType>({
FilterState<RecordType>[],
() => Record<string, Key[] | null>,
] {
const mergedColumns = React.useMemo(() => {
return columns || convertChildrenToColumns(children);
}, [children, columns]);

const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(
collectFilterStates(columns, true),
collectFilterStates(mergedColumns, true),
);

const mergedFilterStates = React.useMemo(() => {
const collectedStates = collectFilterStates(columns, false);
const collectedStates = collectFilterStates(mergedColumns, false);

// Return if not controlled
if (collectedStates.every(({ filteredKeys }) => filteredKeys === undefined)) {
return filterStates;
}

return collectedStates;
}, [columns, filterStates]);
}, [mergedColumns, filterStates]);

const getFilters = React.useCallback(() => generateFilterInfo(mergedFilterStates), [
mergedFilterStates,
Expand Down

0 comments on commit 75524df

Please sign in to comment.