Skip to content

Commit e7cc67f

Browse files
authored
Merge pull request #10365 from marmelab/doc/DateRangeInput/document_filter_usage
[Doc] Document `<DateRangeInput>` as a filter
2 parents b6b10e5 + 0ca75bd commit e7cc67f

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

docs/DateRangeInput.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const EventEdit = () => (
5656
| `sx` | - | `SxProps` | - | The style to apply to the component. |
5757
| `validate` | - | `function|Array` | - | Validation rules for the input. See the [Validation Documentation](./Validation.md#per-input-validation-built-in-field-validators) for details. |
5858

59-
`<DateRangeInput>` also accept the same props as [MUI X's `<DateRangePicker>`](https://mui.com/x/api/date-pickers/date-range-picker/), except for the `format` prop (renamed `mask`),
59+
`<DateRangeInput>` also accept the same props as [MUI X's `<DateRangePicker>`](https://mui.com/x/api/date-pickers/date-range-picker/), except for the `format` prop (renamed `mask`),
6060

6161
**Tip:** Since `<DateRangeInput>` stores its value as a date array, [react-admin's validators](./Validation.md#per-input-validation-built-in-field-validators) like `minValue` or `maxValue` won't work out of the box.
6262

@@ -142,6 +142,44 @@ const EventEdit = () => {
142142
};
143143
```
144144

145+
## Using `<DateRangeInput>` as a Filter
146+
147+
`<DateRangeInput>` can also be used to filter a `<List>`.
148+
149+
However, by default, `<DateRangeInput>` returns `Date` objects with their time set to 00:00:00, which makes the upper bound *exclusive*. Usually, users will expect the upper bound to be *inclusive*.
150+
151+
This can be achieved by providing a `parse` function that sets the time of the upper bound to 23:59:59.
152+
153+
Here is an example:
154+
155+
```tsx
156+
import { DateRangeInput } from '@react-admin/ra-form-layout/DateRangeInput';
157+
import { List, Datagrid, NumberField, TextField, DateField } from 'react-admin';
158+
import { endOfDay } from 'date-fns';
159+
160+
const dateRangeFilterParse = (dates: (Date | null)[]) => {
161+
return [dates[0], dates[1] ? endOfDay(dates[1]) : dates[1]];
162+
};
163+
164+
const eventsFilters = [
165+
<DateRangeInput
166+
source="date_between"
167+
key="date_filter"
168+
parse={dateRangeFilterParse}
169+
/>,
170+
];
171+
172+
export const EventsList = () => (
173+
<List filters={eventsFilters}>
174+
<Datagrid>
175+
<NumberField source="id" />
176+
<TextField source="name" />
177+
<DateField source="date" />
178+
</Datagrid>
179+
</List>
180+
);
181+
```
182+
145183
## Providing your own `LocalizationProvider`
146184

147185
MUI X Pickers need to be wrapped in a [LocalizationProvider](https://mui.com/components/pickers/#localization) to work properly. `<DateRangeInput>` already includes a default `<LocalizationProvider>` using the `date-fns` adapter and the `enUS` locale.
@@ -171,4 +209,4 @@ export const App = () => (
171209

172210
**Note:** React-admin only supports the `date-fns` adapter for now.
173211

174-
**Tip**: React-admin already depends on `date-fns` v3 but your package manager may require you to add it to your dependencies.
212+
**Tip**: React-admin already depends on `date-fns` v3 but your package manager may require you to add it to your dependencies.

0 commit comments

Comments
 (0)