Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix filtered update not working #39883

Merged
merged 1 commit into from Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 51 additions & 19 deletions components/table/__tests__/Table.filter.test.tsx
Expand Up @@ -68,6 +68,16 @@ describe('Table.filter', () => {
return namesList;
}

// Seems raf not trigger when in useEffect for async update
// Need trigger multiple times
function refreshTimer() {
for (let i = 0; i < 3; i += 1) {
act(() => {
jest.runAllTimers();
});
}
}

beforeEach(() => {
jest.useFakeTimers();
});
Expand Down Expand Up @@ -657,16 +667,6 @@ describe('Table.filter', () => {
return container.querySelector('.ant-table-filter-dropdown');
}

// Seems raf not trigger when in useEffect for async update
// Need trigger multiple times
function refreshTimer() {
for (let i = 0; i < 3; i += 1) {
act(() => {
jest.runAllTimers();
});
}
}

// Open Level2
fireEvent.mouseEnter(
getFilterMenu()?.querySelectorAll('div.ant-dropdown-menu-submenu-title')[0]!,
Expand Down Expand Up @@ -1493,6 +1493,47 @@ describe('Table.filter', () => {
).toBeTruthy();
});

it('filtered should work after change', () => {
const App = () => {
const [filtered, setFiltered] = React.useState(true);
const columns = [
{
title: 'Name',
dataIndex: 'name',
filtered,
filters: [],
},
];

return (
<div className="App">
<Button
id="change-filtered-btn"
onClick={() => {
setFiltered(!filtered);
}}
>
Set
</Button>
<Table columns={columns} dataSource={data} />
</div>
);
};
const { container } = render(<App />);

expect(
container.querySelector('.ant-table-filter-trigger')?.className.includes('active'),
).toBeTruthy();

fireEvent.click(container.querySelector('#change-filtered-btn')!);

refreshTimer();

expect(
container.querySelector('.ant-table-filter-trigger')?.className.includes('active'),
).toBeFalsy();
});

it('filteredValue with empty array should not active the filtered icon', () => {
const { container } = render(
createTable({
Expand Down Expand Up @@ -1985,15 +2026,6 @@ describe('Table.filter', () => {
function getFilterMenu() {
return container.querySelector('.ant-table-filter-dropdown');
}
// Seems raf not trigger when in useEffect for async update
// Need trigger multiple times
function refreshTimer() {
for (let i = 0; i < 3; i += 1) {
act(() => {
jest.runAllTimers();
});
}
}

const items = getFilterMenu()?.querySelectorAll('li.ant-dropdown-menu-item');
fireEvent.click(items?.[0]!);
Expand Down
15 changes: 14 additions & 1 deletion components/table/hooks/useFilter/index.tsx
Expand Up @@ -230,7 +230,20 @@ function useFilter<RecordType>({
const keyList = (mergedColumns || []).map((column, index) =>
getColumnKey(column, getColumnPos(index)),
);
return filterStates.filter(({ key }) => keyList.includes(key));
return filterStates
.filter(({ key }) => keyList.includes(key))
.map((item) => {
const col = mergedColumns[keyList.findIndex((key) => key === item.key)];
const nweItem = {
...item,
column: {
...item.column,
...col,
},
forceFiltered: col.filtered,
};
return nweItem;
});
}

warning(
Expand Down