|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "FilterLiveForm" |
| 4 | +--- |
| 5 | + |
| 6 | +# `<FilterLiveForm>` |
| 7 | + |
| 8 | +This component offers a convenient way to create a form that automatically updates the filters when the user changes its child input values. |
| 9 | + |
| 10 | +It fits nicely alongside a [`<FilterList>`](./FilterList.md) component, but you can also use it at other places to create your own filter UI. |
| 11 | + |
| 12 | +<video controls autoplay playsinline muted loop> |
| 13 | + <source src="./img/FilterLiveForm.mp4" type="video/mp4"/> |
| 14 | + Your browser does not support the video tag. |
| 15 | +</video> |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Use `<FilterLiveForm>` inside a component that provides a [`ListContext`](./useListContext.md), such as [`<List>`](./List.md). Use any React Admin [input component](./Inputs.md) as its children. |
| 20 | + |
| 21 | +Here is an example showing how you can use `<FilterLiveForm>` in a sidebar for the `<List>` view, alongside a [`<FilterList>`](./FilterList.md): |
| 22 | + |
| 23 | +{% raw %} |
| 24 | +```tsx |
| 25 | +import * as React from 'react'; |
| 26 | +import CategoryIcon from '@mui/icons-material/LocalOffer'; |
| 27 | +import Person2Icon from '@mui/icons-material/Person2'; |
| 28 | +import TitleIcon from '@mui/icons-material/Title'; |
| 29 | +import { Card, CardContent } from '@mui/material'; |
| 30 | +import { |
| 31 | + AutocompleteInput, |
| 32 | + FilterLiveForm, |
| 33 | + Datagrid, |
| 34 | + FilterList, |
| 35 | + FilterListItem, |
| 36 | + FilterListSection, |
| 37 | + List, |
| 38 | + ReferenceField, |
| 39 | + ReferenceInput, |
| 40 | + TextField, |
| 41 | + TextInput, |
| 42 | +} from 'react-admin'; |
| 43 | + |
| 44 | +const BookListAside = () => ( |
| 45 | + <Card sx={{ order: -1, mr: 2, mt: 6, width: 250, height: 'fit-content' }}> |
| 46 | + <CardContent> |
| 47 | + <FilterList label="Century" icon={<CategoryIcon />}> |
| 48 | + <FilterListItem |
| 49 | + label="21st" |
| 50 | + value={{ year_gte: 2000, year_lte: undefined }} |
| 51 | + /> |
| 52 | + <FilterListItem |
| 53 | + label="20th" |
| 54 | + value={{ year_gte: 1900, year_lte: 1999 }} |
| 55 | + /> |
| 56 | + <FilterListItem |
| 57 | + label="19th" |
| 58 | + value={{ year_gte: 1800, year_lte: 1899 }} |
| 59 | + /> |
| 60 | + </FilterList> |
| 61 | + <FilterListSection label="Title" icon={<TitleIcon />}> |
| 62 | + <FilterLiveForm> |
| 63 | + <TextInput source="title" resettable helperText={false} /> |
| 64 | + </FilterLiveForm> |
| 65 | + </FilterListSection> |
| 66 | + <FilterListSection label="Author" icon={<Person2Icon />}> |
| 67 | + <FilterLiveForm> |
| 68 | + <ReferenceInput source="authorId" reference="authors"> |
| 69 | + <AutocompleteInput helperText={false} /> |
| 70 | + </ReferenceInput> |
| 71 | + </FilterLiveForm> |
| 72 | + </FilterListSection> |
| 73 | + </CardContent> |
| 74 | + </Card> |
| 75 | +); |
| 76 | + |
| 77 | +export const BookList = () => ( |
| 78 | + <List aside={<BookListAside />}> |
| 79 | + <Datagrid> |
| 80 | + <TextField source="title" /> |
| 81 | + <ReferenceField source="authorId" reference="authors" /> |
| 82 | + <TextField source="year" /> |
| 83 | + </Datagrid> |
| 84 | + </List> |
| 85 | +); |
| 86 | +``` |
| 87 | +{% endraw %} |
| 88 | + |
| 89 | +**Tip:** This example leverages `<FilterListSection>`, the wrapper used internally by `<FilterList>`, in order to obtain a consistent look and feel for the filters. |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +**Tip:** `<FilterLiveForm>` accepts multiple children, but you can also use several `<FilterLiveForm>` components in the same filter UI, just like we did above. |
| 94 | + |
| 95 | +**Tip:** For simple cases where you only need a text input, you can use the [`<FilterLiveSearch>`](./FilterLiveSearch.md) component, which combines that logic in a single component. |
| 96 | + |
| 97 | +## Props |
| 98 | + |
| 99 | +Here are all the props you can set on the `<FilterLiveForm>` component: |
| 100 | + |
| 101 | +| Prop | Required | Type | Default | Description | |
| 102 | +| --------------- | -------- | ------------------- | -------------------- | ------------------------------------------------------------------------ | |
| 103 | +| `children` | Required | `ReactNode` | - | The children of the filter form (usually inputs) | |
| 104 | +| `formComponent` | Optional | React Component | Native HTML `<form>` | A React Component used to render the form | |
| 105 | +| `debounce` | Optional | `number` or `false` | 500 | The debounce delay to set the filters (pass `false` to disable debounce) | |
| 106 | +| `validate` | Optional | `function` | - | A function to validate the form values | |
| 107 | + |
| 108 | +Additional props are passed to `react-hook-form`'s [`useForm` hook](https://react-hook-form.com/docs/useform). |
| 109 | + |
| 110 | +## `children` |
| 111 | + |
| 112 | +`<FilterLiveForm>` accepts any children. It simply provides the required contexts for the inputs to work as filters. |
| 113 | + |
| 114 | +```tsx |
| 115 | +<FilterLiveForm> |
| 116 | + <TextInput source="title" resettable helperText={false} /> |
| 117 | + <TextInput source="author" resettable helperText={false} /> |
| 118 | +</FilterLiveForm> |
| 119 | +``` |
| 120 | + |
| 121 | +## `debounce` |
| 122 | + |
| 123 | +You can use the `debounce` prop to customize the delay before the filters are applied. The default value is `500` milliseconds. |
| 124 | + |
| 125 | +```tsx |
| 126 | +<FilterLiveForm debounce={1000}> |
| 127 | + <TextInput source="title" resettable helperText={false} /> |
| 128 | + <TextInput source="author" resettable helperText={false} /> |
| 129 | +</FilterLiveForm> |
| 130 | +``` |
| 131 | + |
| 132 | +You can also disable the debounce by setting the `debounce` prop to `false`. |
| 133 | + |
| 134 | +```tsx |
| 135 | +<FilterLiveForm debounce={false}> |
| 136 | + <TextInput source="title" resettable helperText={false} /> |
| 137 | + <TextInput source="author" resettable helperText={false} /> |
| 138 | +</FilterLiveForm> |
| 139 | +``` |
| 140 | + |
| 141 | +## `validate` |
| 142 | + |
| 143 | +Just like for [`<Form>`](./Form.md), you can provide a `validate` function to validate the form values. |
| 144 | + |
| 145 | +```tsx |
| 146 | +const validateFilters = values => { |
| 147 | + const errors: any = {}; |
| 148 | + if (!values.author) { |
| 149 | + errors.author = 'The author is required'; |
| 150 | + } |
| 151 | + return errors; |
| 152 | +}; |
| 153 | + |
| 154 | +const GlobalValidation = () => ( |
| 155 | + <FilterLiveForm validate={validateFilters}> |
| 156 | + <TextInput source="title" resettable helperText={false} /> |
| 157 | + <TextInput source="author" resettable helperText={false} /> |
| 158 | + </FilterLiveForm> |
| 159 | +); |
| 160 | +``` |
0 commit comments