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 <FilterLiveSearch> should react to filter values change #9996

Merged
merged 1 commit into from
Jul 12, 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';

import { Basic, HiddenLabel } from './FilterLiveSearch.stories';
import {
Basic,
HiddenLabel,
WithFilterButton,
} from './FilterLiveSearch.stories';

describe('FilterLiveSearch', () => {
it('renders an empty text input', () => {
Expand Down Expand Up @@ -30,6 +34,32 @@ describe('FilterLiveSearch', () => {
fireEvent.click(screen.getByLabelText('ra.action.clear_input_value'));
expect(screen.queryAllByRole('listitem')).toHaveLength(27);
});
it('clears the filter when user click on the Remove all filters button', async () => {
render(<WithFilterButton />);
const filterLiveSearchInput = screen.getByLabelText('ra.action.search');
fireEvent.change(filterLiveSearchInput, {
target: { value: 'st' },
});
expect(filterLiveSearchInput.getAttribute('value')).toBe('st');
expect(screen.queryAllByRole('listitem')).toHaveLength(2);
fireEvent.click(screen.getByLabelText('ra.action.add_filter'));
fireEvent.click(await screen.findByText('Remove all filters'));
expect(screen.queryAllByRole('listitem')).toHaveLength(27);
expect(filterLiveSearchInput.getAttribute('value')).toBe('');
});
it('updates its value when filter values change', async () => {
render(<WithFilterButton />);
const filterLiveSearchInput = screen.getByLabelText('ra.action.search');
const textInput = screen.getByLabelText('Q');
fireEvent.change(textInput, {
target: { value: 'st' },
});
expect(textInput.getAttribute('value')).toBe('st');
await waitFor(() => {
expect(filterLiveSearchInput.getAttribute('value')).toBe('st');
});
expect(screen.queryAllByRole('listitem')).toHaveLength(2);
});
describe('hiddenLabel', () => {
it('turns the label into a placeholder', () => {
render(<HiddenLabel />);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import * as React from 'react';
import { useList, ListContextProvider, useListContext } from 'ra-core';
import {
Box,
createTheme,
List,
ListItem,
ListItemText,
ThemeProvider,
createTheme,
} from '@mui/material';
import {
ListContextProvider,
ResourceContextProvider,
TestMemoryRouter,
useList,
useListContext,
} from 'ra-core';
import * as React from 'react';

import { FilterLiveSearch } from './FilterLiveSearch';
import { TextInput } from '../../input';
import { defaultTheme } from '../../theme/defaultTheme';
import { FilterButton } from './FilterButton';
import { FilterForm } from './FilterForm';
import { FilterLiveSearch } from './FilterLiveSearch';

export default {
title: 'ra-ui-materialui/list/filter/FilterLiveSearch',
Expand Down Expand Up @@ -108,3 +117,17 @@ export const Sx = () => (
<CountryList />
</Wrapper>
);

const countryFilters = [<TextInput source="q" alwaysOn />];
export const WithFilterButton = () => (
<TestMemoryRouter>
<ResourceContextProvider value="countries">
<Wrapper>
<FilterLiveSearch source="q" />
<FilterForm filters={countryFilters} />
<FilterButton filters={countryFilters} />
<CountryList />
</Wrapper>
</ResourceContextProvider>
</TestMemoryRouter>
);
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const FilterLiveSearch = memo((props: FilterLiveSearchProps) => {
[filterValues, source]
);

const form = useForm({ defaultValues: initialValues });
const form = useForm({ values: initialValues });

const onSubmit = e => {
e.preventDefault();
Expand Down
Loading