Skip to content

Commit 64e1186

Browse files
authored
Merge pull request marmelab#10214 from marmelab/doc/calendar
[Doc] Update `<Calendar>` and `<CompleteCalendar>`'s doc
2 parents c888d32 + d875b86 commit 64e1186

File tree

1 file changed

+85
-6
lines changed

1 file changed

+85
-6
lines changed

docs/Calendar.md

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ The calendar can display a list of _Events_. Events must be resources with at le
128128
},
129129
```
130130

131-
That means that in order to be able to use `ra-calendar`, your `dataProvider` must return event-like objects for at least one resource. In case your event records don't exactly match this format, ra-calendar allows to specify [a function to convert records to Events](#converttoevent).
131+
That means that in order to be able to use `ra-calendar`, your `dataProvider` must return event-like objects for at least one resource. In case your event records don't exactly match this format, ra-calendar allows to specify [a function to convert records to Events](#converttoevent). See [Using A Custom Event Format](#using-a-custom-event-format) for an example.
132132

133133
Events can have many more fields, e.g. for recurrent events, groups, colors, etc. Check the [Event format on the Full Calendar documentation](https://fullcalendar.io/docs/event-parsing).
134134

@@ -143,7 +143,7 @@ In addition, the calendar queries a list of events in a time interval. Your data
143143
}
144144
```
145145

146-
The `ra-calendar` provides [a function to transform the display interval into a dataProvider filter](#getfiltervaluesfrominterval).
146+
The `ra-calendar` provides [a function to transform the display interval into a dataProvider filter](#getfiltervaluesfrominterval). Again, see [Using A Custom Event Format](#using-a-custom-event-format) for an example.
147147

148148
## `<CompleteCalendar>`
149149

@@ -391,6 +391,85 @@ const EventList = () => {
391391
};
392392
```
393393

394+
### Using A Custom Event Format
395+
396+
If your events don't match the Full Calendar event format, you can still use `<CompleteCalendar>`.
397+
398+
You will need to:
399+
400+
- Use the `convertToEvent` prop to tell `<CompleteCalendar>` how to convert the events
401+
- Use a custom function to compute the filter values from the current date interval (called `customGetFilterValues` in our example)
402+
- Use your own field names in the form (e.g. use `start_date` and `end_date` instead of `start` and `end`)
403+
404+
Here is an example:
405+
406+
{% raw %}
407+
408+
```tsx
409+
import { DatesSetArg } from '@fullcalendar/core';
410+
import { add, set, sub } from 'date-fns';
411+
import {
412+
BooleanInput,
413+
DateTimeInput,
414+
SimpleForm,
415+
TextInput,
416+
} from 'react-admin';
417+
import { CompleteCalendar } from '@react-admin/ra-calendar';
418+
419+
const EventListEventFormat = () => {
420+
const converter = ({ start_date, end_date, ...rest }) => ({
421+
...rest,
422+
start: start_date,
423+
end: end_date,
424+
});
425+
const customGetFilterValues = (
426+
dateInfo?: DatesSetArg,
427+
filterValues: any = {}
428+
): any => {
429+
const now = set(new Date(), {
430+
hours: 0,
431+
minutes: 0,
432+
seconds: 0,
433+
milliseconds: 0,
434+
});
435+
const nowMinus1Month = sub(now, { months: 1 });
436+
const nowPlus2Months = add(now, { months: 2 });
437+
return !dateInfo ||
438+
(dateInfo.start > nowMinus1Month && dateInfo.end < nowPlus2Months)
439+
? {
440+
...filterValues,
441+
start_date_gte: nowMinus1Month.toISOString(),
442+
start_date_lte: nowPlus2Months.toISOString(),
443+
}
444+
: {
445+
...filterValues,
446+
start_date_gte: dateInfo.startStr,
447+
start_date_lte: dateInfo.endStr,
448+
};
449+
};
450+
return (
451+
<CompleteCalendar
452+
ListProps={{
453+
filterDefaultValues: customGetFilterValues(),
454+
}}
455+
CalendarProps={{
456+
getFilterValueFromInterval: customGetFilterValues,
457+
convertToEvent: converter,
458+
}}
459+
>
460+
<SimpleForm>
461+
<TextInput source="title" autoFocus isRequired />
462+
<DateTimeInput source="start_date" />
463+
<DateTimeInput source="end_date" />
464+
<BooleanInput source="allDay" fullWidth />
465+
</SimpleForm>
466+
</CompleteCalendar>
467+
);
468+
};
469+
```
470+
471+
{% endraw %}
472+
394473
## `<Calendar>`
395474

396475
A wrapper around full-calendar's `<FullCalendar>` component, using react-admin's `useListContext` hook to read data, and linking to the edit and create views of the current resource. Must be used inside a `<ListContext>`.
@@ -650,14 +729,14 @@ For instance, let's say your `dataProvider` returns records like the following:
650729
}
651730
```
652731

653-
Full Calendar won't work unless you convert these rcords to events looking like the following:
732+
Full Calendar won't work unless you convert these records to events looking like the following:
654733

655734
```json
656735
{
657736
"id": 8,
658-
"name": "Interview Helen",
659-
"begin": "2020-04-23 11:30:00",
660-
"finish": "2020-04-23 12:00:00",
737+
"title": "Interview Helen",
738+
"start": "2020-04-23 11:30:00",
739+
"end": "2020-04-23 12:00:00",
661740
"backgroundColor": "orange",
662741
"borderColor": "orange",
663742
"editable": false,

0 commit comments

Comments
 (0)