Skip to content

Commit 071d4c0

Browse files
committed
[Doc] Update <Calendar> and <CompleteCalendar>'s doc
1 parent fcb38f4 commit 071d4c0

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

docs/Calendar.md

Lines changed: 81 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,81 @@ 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+
```tsx
407+
import { DatesSetArg } from '@fullcalendar/core';
408+
import { add, set, sub } from 'date-fns';
409+
import {
410+
BooleanInput,
411+
DateTimeInput,
412+
SimpleForm,
413+
TextInput,
414+
} from 'react-admin';
415+
import { CompleteCalendar } from '@react-admin/ra-calendar';
416+
417+
const EventListEventFormat = () => {
418+
const converter = ({ start_date, end_date, ...rest }) => ({
419+
...rest,
420+
start: start_date,
421+
end: end_date,
422+
});
423+
const customGetFilterValues = (
424+
dateInfo?: DatesSetArg,
425+
filterValues: any = {}
426+
): any => {
427+
const now = set(new Date(), {
428+
hours: 0,
429+
minutes: 0,
430+
seconds: 0,
431+
milliseconds: 0,
432+
});
433+
const nowMinus1Month = sub(now, { months: 1 });
434+
const nowPlus2Months = add(now, { months: 2 });
435+
return !dateInfo ||
436+
(dateInfo.start > nowMinus1Month && dateInfo.end < nowPlus2Months)
437+
? {
438+
...filterValues,
439+
start_date_gte: nowMinus1Month.toISOString(),
440+
start_date_lte: nowPlus2Months.toISOString(),
441+
}
442+
: {
443+
...filterValues,
444+
start_date_gte: dateInfo.startStr,
445+
start_date_lte: dateInfo.endStr,
446+
};
447+
};
448+
return (
449+
<CompleteCalendar
450+
ListProps={{
451+
filterDefaultValues: customGetFilterValues(),
452+
}}
453+
CalendarProps={{
454+
getFilterValueFromInterval: customGetFilterValues,
455+
convertToEvent: converter,
456+
}}
457+
>
458+
<SimpleForm>
459+
<TextInput source="title" autoFocus isRequired />
460+
<DateTimeInput source="start_date" />
461+
<DateTimeInput source="end_date" />
462+
<BooleanInput source="allDay" fullWidth />
463+
</SimpleForm>
464+
</CompleteCalendar>
465+
);
466+
};
467+
```
468+
394469
## `<Calendar>`
395470

396471
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 +725,14 @@ For instance, let's say your `dataProvider` returns records like the following:
650725
}
651726
```
652727

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

655730
```json
656731
{
657732
"id": 8,
658-
"name": "Interview Helen",
659-
"begin": "2020-04-23 11:30:00",
660-
"finish": "2020-04-23 12:00:00",
733+
"title": "Interview Helen",
734+
"start": "2020-04-23 11:30:00",
735+
"end": "2020-04-23 12:00:00",
661736
"backgroundColor": "orange",
662737
"borderColor": "orange",
663738
"editable": false,

0 commit comments

Comments
 (0)