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 clearing a nested filter re-renders the previous value when navigating back to the list #9491

Merged
merged 3 commits into from
Dec 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ export default {
title: 'ra-ui-materialui/list/filter/FilterButton',
argTypes: {
disableSaveQuery: {
control: {
type: 'select',
options: [false, true],
},
control: 'select',
options: [false, true],
},
size: {
control: {
type: 'select',
options: [undefined, 'small', 'medium'],
},
control: 'select',
options: [undefined, 'small', 'medium'],
},
},
};
Expand Down Expand Up @@ -198,13 +194,7 @@ export const Basic = (args: { disableSaveQuery?: boolean }) => {
source="title"
defaultValue="Accusantium qui nihil voluptatum quia voluptas maxime ab similique"
/>,
<TextInput
label="Nested"
source="nested"
defaultValue={{ foo: 'bar' }}
format={v => v?.foo || ''}
parse={v => ({ foo: v })}
/>,
<TextInput label="Nested" source="nested.foo" defaultValue="bar" />,
];
return (
<Admin dataProvider={fakerestDataProvider(data)}>
Expand Down Expand Up @@ -261,9 +251,30 @@ export const WithSearchInput = (args: {
);
};

export const WithAutoCompleteArrayInput = () => {
const Dashboard = () => <h1>Dashboard</h1>;

// necessary because fakerest doesn't support nested arrays as filter
const withNestedFiltersSupportDataProvider = () => {
const baseDataprovider = fakerestDataProvider(data);
return {
...baseDataprovider,
getList: (resource: string, params: any) => {
const newParams = { ...params, filter: { ...params.filter } };
if (newParams.filter?.nested?.foo) {
newParams.filter['nested.foo'] = newParams.filter.nested.foo;
delete newParams.filter.nested;
}
return baseDataprovider.getList(resource, newParams);
},
};
};

export const WithAutoCompleteArrayInput = (args: {
disableSaveQuery?: boolean;
size?: 'small' | 'medium';
}) => {
const postFilters: React.ReactElement[] = [
<SearchInput source="q" alwaysOn />,
<SearchInput source="q" alwaysOn size={args.size} />,
<AutocompleteArrayInput
label="Title"
source="id"
Expand All @@ -282,18 +293,29 @@ export const WithAutoCompleteArrayInput = () => {
{ id: 12, name: 'Qui tempore...' },
{ id: 13, name: 'Fusce...' },
]}
alwaysOn
multiple
size={args.size}
/>,
<AutocompleteArrayInput
label="Nested"
source="nested.foo"
choices={[
{ id: 'bar', name: 'bar' },
{ id: 'baz', name: 'baz' },
]}
size={args.size}
/>,
];
return (
<Admin dataProvider={fakerestDataProvider(data)}>
<Admin
dataProvider={withNestedFiltersSupportDataProvider()}
dashboard={Dashboard}
>
<Resource
name="posts"
list={
<PostList
postFilters={postFilters}
args={{ disableSaveQuery: false }}
args={{ disableSaveQuery: args.disableSaveQuery }}
/>
}
/>
Expand Down
48 changes: 46 additions & 2 deletions packages/ra-ui-materialui/src/list/filter/FilterForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as React from 'react';
import { AdminContext } from '../../AdminContext';
import { ReferenceInput, SelectInput, TextInput } from '../../input';
import { Filter } from './Filter';
import { Basic } from './FilterButton.stories';
import { Basic, WithAutoCompleteArrayInput } from './FilterButton.stories';
import {
FilterForm,
getFilterFormValues,
Expand Down Expand Up @@ -255,12 +255,56 @@ describe('<FilterForm />', () => {
getFilterFormValues(currentFormValues, newFilterValues)
).toEqual({
classicToClear: '',
nestedToClear: '',
nestedToClear: { nestedValue: '' },
classicUpdated: 'ghi2',
nestedUpdated: { nestedValue: 'jkl2' },
published_at: '2022-01-01T03:00:00.000Z',
clearedDateValue: '',
});
});
});

it('should not reapply previous filter form values when clearing nested AutocompleteArrayInput', async () => {
render(<WithAutoCompleteArrayInput />);

// Open Posts List
fireEvent.click(await screen.findByText('Posts'));

// Set nested filter value to 'bar'
fireEvent.click(await screen.findByLabelText('Add filter'));
fireEvent.click(
await screen.findByRole('menuitem', { name: 'Nested' })
);
fireEvent.click(await screen.findByText('bar'));
fireEvent.blur(await screen.findByLabelText('Nested'));
await screen.findByText('1-7 of 7');
expect(screen.queryByRole('button', { name: 'bar' })).not.toBeNull();

// Navigate to Dashboard
fireEvent.click(await screen.findByText('Dashboard'));
// Navigate back to Posts List
fireEvent.click(await screen.findByText('Posts'));
// Filter should still be applied
await screen.findByText('1-7 of 7');
expect(screen.queryByRole('button', { name: 'bar' })).not.toBeNull();

// Clear nested filter value
fireEvent.mouseDown(
await screen.findByLabelText('Nested', { selector: 'input' })
);
fireEvent.keyDown(
await screen.findByLabelText('Nested', { selector: 'input' }),
{
key: 'Backspace',
}
);
fireEvent.blur(
await screen.findByLabelText('Nested', { selector: 'input' })
);

// Wait until filter is cleared
await screen.findByText('1-10 of 13');
// Make sure the 'bar' value is not displayed anymore
expect(screen.queryByRole('button', { name: 'bar' })).toBeNull();
}, 10000);
});
3 changes: 0 additions & 3 deletions packages/ra-ui-materialui/src/list/filter/FilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,6 @@ const getInputValue = (
innerKey,
(filterValues || {})[key] ?? {}
);
if (nestedInputValue === '') {
return acc;
}
Comment on lines -288 to -290
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix

acc[innerKey] = nestedInputValue;
return acc;
},
Expand Down
Loading