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: 4.0 table filters value may be previous #19873

Merged
merged 8 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ describe('Table.filter', () => {
<span onClick={() => clearFilters()} id="reset">
Reset
</span>
<span
onClick={() => {
setSelectedKeys([43]);
confirm();
}}
id="simulateOnSelect"
>
SimulateOnSelect
</span>
</div>
);

Expand Down Expand Up @@ -170,6 +179,11 @@ describe('Table.filter', () => {
.first()
.props().visible,
).toBeFalsy();

// Simulate onSelect, setSelectedKeys & confirm
wrapper.find('span.ant-dropdown-trigger').simulate('click', nativeEvent);
wrapper.find('#simulateOnSelect').simulate('click');
expect(getFilterMenu().props().filterState.filteredKeys).toEqual([43]);
});

it('can be controlled by filterDropdownVisible', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ exports[`Table.filter override custom filter correctly 1`] = `
>
Reset
</span>
<span
id="simulateOnSelect"
onClick={[Function]}
>
SimulateOnSelect
</span>
</div>
`;

Expand Down
15 changes: 9 additions & 6 deletions components/table/hooks/useFilter/FilterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,23 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
filterState && filterState.filteredKeys,
);
const [filteredKeys, setFilteredKeys] = React.useState(propFilteredKeys || []);
// Use it to fix 'setFilteredKeys' is async
const filteredKeysRef = React.useRef(filteredKeys);

const onSelectKeys = ({ selectedKeys }: { selectedKeys: Key[] }) => {
setFilteredKeys(selectedKeys);
filteredKeysRef.current = selectedKeys;
};

React.useEffect(() => {
// Sync internal filtered keys when props key changed
const newFilteredKeys = filterState && filterState.filteredKeys;
if (!shallowEqual(propFilteredKeys, newFilteredKeys)) {
setPropFilteredKeys(newFilteredKeys);
setFilteredKeys(newFilteredKeys || []);
onSelectKeys({selectedKeys: newFilteredKeys || []});
}
}, [filterState]);

const onSelectKeys = ({ selectedKeys }: { selectedKeys: Key[] }) => {
setFilteredKeys(selectedKeys);
};

// ====================== Open Keys ======================
const [openKeys, setOpenKeys] = React.useState<string[]>([]);
const openRef = React.useRef<number>();
Expand Down Expand Up @@ -140,7 +143,7 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
};

const onConfirm = () => {
internalTriggerFilter(filteredKeys);
internalTriggerFilter(filteredKeysRef.current);
};

const onReset = () => {
Expand Down