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 regression on filters using dot separator in source #4545

Merged
merged 6 commits into from
Mar 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions packages/ra-core/src/controller/useListController.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import expect from 'expect';
import { fireEvent, cleanup } from '@testing-library/react';
import { fireEvent, wait, cleanup } from '@testing-library/react';
import lolex from 'lolex';
import TextField from '@material-ui/core/TextField/TextField';

Expand Down Expand Up @@ -35,6 +35,7 @@ describe('useListController', () => {
sort: 'id',
order: SORT_ASC,
filter: {},
displayedFilters: {},
},
resource: 'posts',
debounce: 200,
Expand Down Expand Up @@ -119,6 +120,7 @@ describe('useListController', () => {
list: {
params: {
filter: { q: 'hello' },
displayedFilters: { q: true },
},
cachedRequests: {},
},
Expand All @@ -141,6 +143,9 @@ describe('useListController', () => {

const state = reduxStore.getState();
expect(state.admin.resources.posts.list.params.filter).toEqual({});
expect(
state.admin.resources.posts.list.params.displayedFilters
).toEqual({ q: true });
});

it('should update data if permanent filters change', () => {
Expand Down Expand Up @@ -203,17 +208,21 @@ describe('useListController', () => {
});
});
describe('showFilter', () => {
it('Does not remove previously shown filter when adding a new one', () => {
it('Does not remove previously shown filter when adding a new one', async () => {
let currentDisplayedFilters;

let fakeComponent = ({ showFilter, displayedFilters }) => {
let fakeComponent = ({
showFilter,
displayedFilters,
filterValues,
}) => {
currentDisplayedFilters = displayedFilters;
return (
<>
<button
aria-label="Show filter 1"
onClick={() => {
showFilter('filter1', '');
showFilter('filter1.subdata', 'bob');
}}
/>
<button
Expand All @@ -238,7 +247,9 @@ describe('useListController', () => {
resources: {
posts: {
list: {
params: { filter: { q: 'hello' } },
params: {
filter: { q: 'hello' },
},
cachedRequests: {},
},
},
Expand All @@ -248,11 +259,17 @@ describe('useListController', () => {
);

fireEvent.click(getByLabelText('Show filter 1'));
expect(currentDisplayedFilters).toEqual({ filter1: true });
await wait(() => {
expect(currentDisplayedFilters).toEqual({
'filter1.subdata': true,
});
});
fireEvent.click(getByLabelText('Show filter 2'));
expect(currentDisplayedFilters).toEqual({
filter1: true,
filter2: true,
await wait(() => {
expect(currentDisplayedFilters).toEqual({
'filter1.subdata': true,
filter2: true,
});
});
});
});
Expand Down
19 changes: 11 additions & 8 deletions packages/ra-core/src/controller/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,21 @@ const useListParams = ({

const hideFilter = useCallback((filterName: string) => {
const newFilters = removeKey(filterValues, filterName);
const newDisplayedFilters = removeKey(
displayedFilterValues,
filterName
);
const newDisplayedFilters = {
...displayedFilterValues,
[filterName]: undefined,
};

setFilters(newFilters, newDisplayedFilters);
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps

const showFilter = useCallback((filterName: string, defaultValue: any) => {
setFilters(
set(filterValues, filterName, defaultValue),
set(displayedFilterValues, filterName, true)
);
const newFilters = set(filterValues, filterName, defaultValue);
const newDisplayedFilters = {
...displayedFilterValues,
[filterName]: true,
};
setFilters(newFilters, newDisplayedFilters);
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps

return [
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-core/src/util/removeKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const deepRemoveKey = (target, path) => {
}

const deepKey = paths[0];
if (target[deepKey] === undefined) {
return target;
}
const deep = deepRemoveKey(target[deepKey], paths.slice(1).join('.'));

if (Object.keys(deep).length === 0) {
Expand Down