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 <Filter> shows filter button even though there is no filter to add #7691

Merged
merged 2 commits into from
May 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion packages/ra-ui-materialui/src/list/filter/FilterButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import expect from 'expect';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, screen } from '@testing-library/react';
import { createTheme } from '@mui/material/styles';

import { AdminContext } from '../../AdminContext';
Expand Down Expand Up @@ -44,6 +44,49 @@ describe('<FilterButton />', () => {
expect(queryByText('Name')).toBeNull();
});

it('should not display the filter button if all filters are shown and there is no filter value', () => {
render(
<AdminContext theme={theme}>
<FilterButton
{...defaultProps}
filters={[
<TextInput source="title" label="Title" />,
<TextInput source="customer.name" label="Name" />,
]}
displayedFilters={{
title: true,
'customer.name': true,
}}
/>
</AdminContext>
);
expect(screen.queryByLabelText('ra.action.add_filter')).toBeNull();
});

it('should display the filter button if all filters are shown and there is a filter value', () => {
render(
<AdminContext theme={theme}>
<FilterButton
{...defaultProps}
filters={[
<TextInput source="title" label="Title" />,
<TextInput source="customer.name" label="Name" />,
]}
displayedFilters={{
title: true,
'customer.name': true,
}}
filterValues={{ title: 'foo' }}
/>
</AdminContext>
);
expect(
screen.queryByLabelText('ra.action.add_filter')
).not.toBeNull();
fireEvent.click(screen.getByLabelText('ra.action.add_filter'));
screen.getByText('ra.saved_queries.new_label');
});

it('should return disabled filter menu item when "disabled" passed to filter', () => {
const hiddenFilter = (
<TextInput source="Returned" label="Returned" disabled={true} />
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export const FilterButton = (props: FilterButtonProps): JSX.Element => {
setRemoveSavedQueryDialogOpen(true);
};

if (hiddenFilters.length === 0 && !hasFilterValues) {
return null;
}
return (
<Root className={className} {...sanitizeRestProps(rest)}>
<Button
Expand Down