Skip to content

Commit 6eaca36

Browse files
committed
Fix FilterForm
1 parent ff29fd2 commit 6eaca36

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

packages/ra-ui-materialui/src/list/filter/FilterForm.spec.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import expect from 'expect';
3-
import { fireEvent, render, screen } from '@testing-library/react';
3+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
44
import { minLength } from 'ra-core';
55

66
import { FilterForm, mergeInitialValuesWithDefaultValues } from './FilterForm';
@@ -40,7 +40,7 @@ describe('<FilterForm />', () => {
4040
expect(screen.queryAllByLabelText('Name')).toHaveLength(1);
4141
});
4242

43-
it('should change the filter when the user updates an input', () => {
43+
it('should change the filter when the user updates an input', async () => {
4444
const filters = [<TextInput source="title" label="Title" />];
4545
const displayedFilters = {
4646
title: true,
@@ -60,10 +60,12 @@ describe('<FilterForm />', () => {
6060
fireEvent.change(screen.queryByLabelText('Title'), {
6161
target: { value: 'foo' },
6262
});
63-
expect(setFilters).toHaveBeenCalledWith(
64-
{ title: 'foo' },
65-
{ title: true }
66-
);
63+
await waitFor(() => {
64+
expect(setFilters).toHaveBeenCalledWith(
65+
{ title: 'foo' },
66+
{ title: true }
67+
);
68+
});
6769
});
6870

6971
it('should not change the filter when the user updates an input with an invalid value', () => {

packages/ra-ui-materialui/src/list/filter/FilterForm.tsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
ReactNode,
99
} from 'react';
1010
import PropTypes from 'prop-types';
11-
import { useListContext, useResourceContext } from 'ra-core';
11+
import {
12+
ListFilterContextValue,
13+
useListContext,
14+
useResourceContext,
15+
} from 'ra-core';
1216
import { FormProvider, useForm } from 'react-hook-form';
1317
import classnames from 'classnames';
1418
import lodashSet from 'lodash/set';
@@ -89,20 +93,25 @@ FilterFormBase.propTypes = {
8993

9094
const sanitizeRestProps = ({
9195
displayedFilters,
96+
filterValues,
97+
hideFilter,
98+
setFilters,
9299
resource,
93100
...props
94101
}: Partial<FilterFormProps>) => props;
95102

96-
export interface FilterFormProps
97-
extends Omit<HtmlHTMLAttributes<HTMLFormElement>, 'children'> {
98-
className?: string;
99-
resource?: string;
100-
displayedFilters: any;
101-
filters: ReactNode[];
102-
initialValues?: any;
103-
margin?: 'none' | 'normal' | 'dense';
104-
variant?: 'standard' | 'outlined' | 'filled';
105-
}
103+
export type FilterFormProps = Omit<
104+
HtmlHTMLAttributes<HTMLFormElement>,
105+
'children'
106+
> &
107+
Partial<ListFilterContextValue> & {
108+
className?: string;
109+
resource?: string;
110+
filters: ReactNode[];
111+
initialValues?: any;
112+
margin?: 'none' | 'normal' | 'dense';
113+
variant?: 'standard' | 'outlined' | 'filled';
114+
};
106115

107116
export const mergeInitialValuesWithDefaultValues = (
108117
initialValues,
@@ -141,13 +150,18 @@ export const FilterForm = props => {
141150
defaultValues: mergedInitialValuesWithDefaultValues,
142151
});
143152

153+
const handleChange = values => {
154+
setFilters(values, displayedFilters);
155+
};
156+
144157
return (
145158
<FormProvider {...form}>
146159
<FilterFormBase
147-
onChange={() => setFilters(form.getValues(), displayedFilters)}
148-
onSubmit={form.handleSubmit(handleFormSubmit)}
160+
onChange={form.handleSubmit(handleChange)}
161+
onSubmit={handleFormSubmit}
149162
filters={filters}
150163
initialValues={mergedInitialValuesWithDefaultValues}
164+
{...props}
151165
/>
152166
</FormProvider>
153167
);

0 commit comments

Comments
 (0)