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 filters with complex object are not removed from the UI #9898

Merged
merged 3 commits into from
Jun 11, 2024
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
4 changes: 2 additions & 2 deletions cypress/e2e/mobile.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ describe('Mobile UI', () => {
});

describe('Infinite Scroll', () => {
it.only('should load more items when scrolling to the bottom of the page', () => {
it('should load more items when scrolling to the bottom of the page', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

😱

ListPagePosts.navigate();
cy.contains('Fusce massa lorem').should('exist');
cy.contains('Sed quo et et fugiat modi').should('not.exist');
cy.scrollTo('bottom');
cy.wait(500);
cy.scrollTo('bottom');
cy.contains('Sed quo et et fugiat modi');
Expand Down
29 changes: 29 additions & 0 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { Chip } from '@mui/material';
import { ListBase, memoryStore } from 'ra-core';
import {
Admin,
Expand All @@ -13,6 +14,7 @@ import {
TopToolbar,
SearchInput,
FilterButtonProps,
InputProps,
} from 'react-admin';
import fakerestDataProvider from 'ra-data-fakerest';
import {
Expand Down Expand Up @@ -383,6 +385,33 @@ export const WithAutoCompleteArrayInput = (args: {
);
};

const QuickFilter = ({ label }: InputProps) => <Chip label={label} />;

export const WithComplexValueFilter = (args: {
disableSaveQuery?: boolean;
}) => {
const postFilters: React.ReactElement[] = [
<QuickFilter
label="Complex"
source="nested"
defaultValue={{ foo: 'bar' }}
/>,
];
return (
<MemoryRouter>
<Admin
dataProvider={fakerestDataProvider(data)}
store={memoryStore()}
>
<Resource
name="posts"
list={<PostList postFilters={postFilters} args={args} />}
/>
</Admin>
</MemoryRouter>
);
};

export const Variant = () => {
const postFilters: React.ReactElement[] = [
<TextInput
Expand Down
25 changes: 23 additions & 2 deletions packages/ra-ui-materialui/src/list/filter/FilterForm.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { chipClasses } from '@mui/material/Chip';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import expect from 'expect';
import {
Expand All @@ -13,8 +14,9 @@ import { ReferenceInput, SelectInput, TextInput } from '../../input';
import { Filter } from './Filter';
import {
Basic,
WithAutoCompleteArrayInput,
WithArrayInput,
WithAutoCompleteArrayInput,
WithComplexValueFilter,
} from './FilterButton.stories';
import {
FilterForm,
Expand Down Expand Up @@ -209,7 +211,7 @@ describe('<FilterForm />', () => {
});
});

it('should allow to add and clear a filter with a complex object value', async () => {
it('should allow to add and clear a filter with a nested value', async () => {
render(<Basic />);

const addFilterButton = await screen.findByText('Add filter');
Expand All @@ -230,6 +232,25 @@ describe('<FilterForm />', () => {
expect(screen.queryByLabelText('Nested')).toBeNull();
});

it('should hide a removed filter with a complex object value', async () => {
render(<WithComplexValueFilter />);

const addFilterButton = await screen.findByText('Add filter');
fireEvent.click(addFilterButton);
fireEvent.click(await screen.findByText('Complex'));
await screen.findByText('1-7 of 7');
await screen.findByText('Complex', {
selector: `.${chipClasses.root} *`,
});
fireEvent.click(await screen.findByTitle('Remove this filter'));
await screen.findByText('1-10 of 13');
expect(
screen.queryByText('Complex', {
selector: `.${chipClasses.root} *`,
})
).toBeNull();
});

it('should provide a FormGroupContext', async () => {
render(<WithArrayInput />);

Expand Down
16 changes: 15 additions & 1 deletion packages/ra-ui-materialui/src/list/filter/FilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const FilterFormBase = (props: FilterFormBaseProps) => {
return (
filterElement.props.alwaysOn ||
displayedFilters[filterElement.props.source] ||
(filterValue !== '' && typeof filterValue !== 'undefined')
!isEmptyValue(filterValue)
);
});
};
Expand Down Expand Up @@ -301,3 +301,17 @@ const getInputValue = (
}
return get(filterValues, key, '');
};

const isEmptyValue = (filterValue: unknown) => {
if (filterValue === '' || typeof filterValue === 'undefined') return true;

// If one of the value leaf is not empty
// the value is considered not empty
if (typeof filterValue === 'object') {
return Object.keys(filterValue).every(key =>
isEmptyValue(filterValue[key])
);
}

return false;
};
Loading