ra-calendar

react-admin ≥ 3.14.1

Display and manipulate events, drag and resize appointments, and browse a calendar in react-admin apps.

This module integrates Full Calendar with react-admin, and supports:

  • month, week, day views
  • list view
  • drag and resize events
  • whole-day events
  • creating an event by clicking in the calendar
  • edition of event title, and metadata
  • events spanning on multiple days
  • recurring events
  • background events
  • theming
  • locales and timezones
  • resource time grid (e.g. rooms) (requires additional licence from Full Calendar)

Installation

npm install --save @react-admin/ra-calendar
# or
yarn add @react-admin/ra-calendar

Tip: ra-calendar is part of the React-Admin Enterprise Edition, and hosted in a private npm registry. You need to subscribe to one of the Enterprise Edition plans to access this package.

Events Format

The calendar can display a list of Events. Events must be resources with at least a string id, a title, and a start date. Here is a typical event:

{
    id: '432646',
    title: 'Package delivery',
    start: '2020-10-20T08:00:00.000Z',
    end: '2020-10-20T09:30:00.000Z',
},

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.

Events can have many more fields, e.g. for recurrent events, groups, colors, etc. Check the Event format on the Full Calendar documentation.

In addition, the calendar queries a list of events in a time interval. Your dataProvider must support getList() queries with an interval filter. By default, the interval filter looks like the following:

{
    // lower time boundary (gte stands for 'greater than or equal')
    start_gte: '2020-10-01T00:00:00.000Z',
    // upper time boundary (lte stands for 'less than or equal')
    start_lte: '2020-12-31T23:59:59.000Z'
}

The ra-calendar provides a function to transform the display interval into a dataProvider filter.

<CompleteCalendar>

This all-in one component renders a calendar, as well as a form to edit or create new events that opens in a dialog. It is deeply integrated with react-admin, and benefits from the same speed optimizations.

Use it as the list prop of a <Resource>. No need to specify an edit or create prop for this resource, but you'll have to pass a form component (like <SimpleForm>) as child of <CompleteCalendar> to define the event edition form.

Here is an example:

import React, { FC } from 'react';
import {
    Admin,
    Resource,
    List,
    ListProps,
    SimpleForm,
    TextInput,
    DateTimeInput,
} from 'react-admin';
import { CompleteCalendar } from '@react-admin/ra-calendar';

import dataProvider from './dataProvider';

const EventList: FC<ListProps> = props => (
    <CompleteCalendar {...props}>
        <SimpleForm>
            <TextInput source="title" autoFocus />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </CompleteCalendar>
);

export const Basic: FC = () => (
    <Admin dataProvider={dataProvider}>
        <Resource name="events" list={EventList} />
    </Admin>
);

ListProps

<CompleteCalendar> renders a react-admin <List> element, that you can customize by specifying a ListProps prop.

For instance, to limit the number of events fetched from the server to 100 (instead of the default 1000), you can override the <List perPage> prop as follows:

const EventList: FC<ListProps> = props => (
    <CompleteCalendar
        {...props}
        ListProps={{
            perPage: 100
        }}
    >
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </CompleteCalendar>
);

Check the possible values for ListProps in the <List> component documentation.

CalendarProps

Under the hood, <CompleteCalendar> renders a <Calendar> element, which is a react-admin wrapper for Full Calendar. You can customize all the <Calendar> and FullCalendar props by passing a CalendarProps prop.

For instance, to set a French locale, map non-standard event records to the expected Event type, and define boundaries to the possible events:

import frLocale from '@fullcalendar/core/locales/fr';

const EventList: FC<ListProps> = props => (
    <CompleteCalendar
        {...props}
        CalendarProps={{
            locale: frLocale,
            convertToEvent: (event: any): EventInput => ({
                id: String(event.id),
                title: event.name,
                start: event.begin,
                end: event.finish,
                backgroundColor: colorForCategory(event.category)
                borderColor: colorForCategory(event.category)
                editable: event.can_edit,
                url: `https://meet.jit.si/${event.jitsi_meet_id}`
            }),
            validRange: {
                start: '2017-05-01',
                end: '2017-06-01'
            }
        }}
    >
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </CompleteCalendar>
);

Check the possible values for CalendarProps in the <Calendar> and [<FullCalendar>]((https://fullcalendar.io/docs#toc) documentation.

EditDialogProps

For content edition, <CompleteCalendar> relies on ra-form-layout's <EditDialog> to display its child form in a dialog.

You can customize <EditDialog> props like title, redirect, onSuccess and onFailure by passing a custom EditDialogProps prop.

For instance, to customize the title of the Edit dialog:

const EventList: FC<ListProps> = props => (
    <CompleteCalendar
        {...props}
        EditDialogProps={{
            title: ({ record }) => record ? <span>Edit {record.title}</span> : null
        }}
    >
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </CompleteCalendar>
);

Check the possible values for EditDialogProps in the <EditDialog> component documentation.

CreateDialogProps

For content addition, <CompleteCalendar> relies on ra-form-layout's <CreateDialog> to display its child form in a dialog.

You can customize <CreateDialog> props like title, redirect, onSuccess and onFailure by passing a custom CreateDialogProps prop.

For instance, to customize the title of the Creation dialog:

const EventList: FC<ListProps> = props => (
    <CompleteCalendar
        {...props}
        CreateDialogProps={{
            title: () => <span>Create new appointment</span>
        }}
    >
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </CompleteCalendar>
);

Check the possible values for CreateDialogProps in the <CreateDialog> component documentation.

<Calendar>

A wrapper around full-calendar's <FullCalendar> component, using the react-admin Redux store as content provider, and linking to the edit and create views of the current resource. Must be used inside a <ListContext>.

Use this component as a child of <List>, as follows:

import { Calendar, getFilterValuesFromInterval } from '@react-admin/ra-calendar';
import { Admin, Resource, List, Edit, SimpleForm, TextInput, DateTimeInput } from 'react-admin';

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={getFilterValuesFromInterval()}
        perPage={1000}
        pagination={false}
    >
        <Calendar />
    </List>
);

const EventEdit: FC<ComponentProps<typeof Edit>> = props => (
    <Edit {...props}>
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </Edit>
);

const EventCreate: FC<ComponentProps<typeof Create>> = props => (
    <Create {...props}>
        <SimpleForm>
            <TextInput source="title" />
            <DateTimeInput source="start" />
            <DateTimeInput source="end" />
        </SimpleForm>
    </Create>
);

export const App: FC = () => (
    <Admin dataProvider={dataProvider}>
        <Resource
            name="events"
            list={EventList}
            edit={EventEdit}
            create={EventCreate}
        />
    </Admin>
);

In this example, the <List> is initialized to load at most 1000 events, in a 3-months interval around the current date.

Props passed to the <Calendar> element are passed down to an underlying <FullCalendar> element, so you can set every Full Calendar options via the <Calendar> props. For instance, to customize the content rendered for each event, use the eventContent prop:

    <Calendar
        eventContent={(eventContent: EventContentArg): JSX.Element => (
            <>
                <b>{eventContent.timeText}</b>{' '}
                <i>{eventContent.event.title}</i>
            </>
        )}
    />

In addition to Full Calendar props, ra-calendar's <Calendar> supports a few more additional props, documented below.

locale

The locale and locales options allow you to localize certain aspects of the calendar:

  • the text in buttons, as defined by headerToolbar
  • text that contains month or day-of-week strings
  • date formatting, such as eventTimeFormat
  • weekNumberCalculation
  • firstDay

If your admin uses only one locale, import the localization from @fullcalendar/core/locales, and pass it as locale:

import { List } from 'react-admin';
import { Calendar, getFilterValuesFromInterval } from '@react-admin/ra-calendar';
import frLocale from '@fullcalendar/core/locales/fr';

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={getFilterValuesFromInterval()}
        perPage={1000}
        pagination={false}
    >
        <Calendar locale={frLocale} />
    </List>
);

If your admin can have a finite number of locales, load them all in the locales prop, and set the initial locale via the locale prop:

import { List } from 'react-admin';
import { Calendar, getFilterValuesFromInterval } from '@react-admin/ra-calendar';
import esLocale from '@fullcalendar/core/locales/es';
import frLocale from '@fullcalendar/core/locales/fr';

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={getFilterValuesFromInterval()}
        perPage={1000}
        pagination={false}
    >
        <Calendar 
            locales={[esLocale, frLocale]}
            locale="fr"
        />
    </List>
);

And if you want to support all locales and initialize the calendar based on the user navigator preferences, use ra-calendar's getNavigatorLanguage() helper:

import { List } from 'react-admin';
import { Calendar, getFilterValuesFromInterval, getNavigatorLanguage } from '@react-admin/ra-calendar';
import allLocales from '@fullcalendar/core/locales-all';

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={getFilterValuesFromInterval()}
        perPage={1000}
        pagination={false}
    >
        <Calendar 
            locales={allLocales}
            locale={getNavigatorLanguage()}
        />
    </List>
);

Note that using allLocales make the JS bundle significantly larger - avoid it if you only need to support a few locales.

getFilterValuesFromInterval

The getFilterValuesFromInterval() function returns filter values based on the interval displayed on the screen (e.g. the current mont, the current week, etc). ra-calendar does its best to minimize queries to the dataProvider by requesting a 3 months interval by default (1 month before the current day, and 2 months after). You can change that behavior, and transform the filter object sent to the dataProvider.getList() method, by passing your own getFilterValueFromInterval prop:

import { DatesSetArg } from '@fullcalendar/react';
import { add, sub, set } from 'date-fns';

/**
 * By default, return an interval of 3 months around now (1 month before, 2 months after)
 * unless the user requires a larger interval.
 *
 * This minimizes queries while navigating.
 */
const customGetFilterValues = (
    dateInfo?: DatesSetArg,
    filterValues: any = {}
): any => {
    const now = set(new Date(), {
        hours: 0,
        minutes: 0,
        seconds: 0,
        milliseconds: 0,
    });
    const nowMinus1Month = sub(now, { months: 1 });
    const nowPlus2Months = add(now, { months: 2 });
    return !dateInfo ||
        (dateInfo.start > nowMinus1Month && dateInfo.end < nowPlus2Months)
        ? {
              ...filterValues,
              start_gte: nowMinus1Month.toISOString(),
              start_lte: nowPlus2Months.toISOString(),
          }
        : {
              ...filterValues,
              start_gte: dateInfo.startStr,
              start_lte: dateInfo.endStr,
          };
};

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={customGetFilterValues()}
        perPage={1000}
        pagination={false}
    >
        <Calendar getFilterValuesFromInterval={customGetFilterValues} />
    </List>
);
import { DatesSetArg } from '@fullcalendar/react';
import { add, sub, set } from 'date-fns';

/**
 * By default, return an interval of 3 months around now (1 month before, 2 months after)
 * unless the user requires a larger interval.
 *
 * This minimizes queries while navigating.
 */
const customGetFilterValues = (
    dateInfo?: DatesSetArg,
    filterValues: any = {}
): any => {
    const now = set(new Date(), {
        hours: 0,
        minutes: 0,
        seconds: 0,
        milliseconds: 0,
    });
    const nowMinus1Month = sub(now, { months: 1 });
    const nowPlus2Months = add(now, { months: 2 });
    return !dateInfo ||
        (dateInfo.start > nowMinus1Month && dateInfo.end < nowPlus2Months)
        ? {
              ...filterValues,
              start_gte: nowMinus1Month.toISOString(),
              start_lte: nowPlus2Months.toISOString(),
          }
        : {
              ...filterValues,
              start_gte: dateInfo.startStr,
              start_lte: dateInfo.endStr,
          };
};

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={customGetFilterValues()}
        perPage={1000}
        pagination={false}
    >
        <Calendar getFilterValuesFromInterval={customGetFilterValues} />
    </List>
);

convertToEvent

Full Calendar accepts many properties for events, like allDay, backgroundColor, url, editable, etc. If you build an admin for an existing API, your events structure will probably not match that shape. In that case, use the convertToEvent prop to convert records of arbitrary shape to the Full Calendar event format.

For instance, let's say your dataProvider returns records like the following:

{
    id: 8,
    name: 'Interview Helen',
    begin: '2020-04-23 11:30:00',
    finish: '2020-04-23 12:00:00',
    category: 'job_interview',
    can_edit: false,
    jitsi_meet_id: 'CynicalOxygensContainOut'
}

Full Calendar won't work unless you convert these rcords to events looking like the following:

{
    id: '8',
    name: 'Interview Helen',
    begin: '2020-04-23 11:30:00',
    finish: '2020-04-23 12:00:00',
    backgroundColor: 'orange',
    borderColor: 'orange',
    editable: false,
    url: 'https://meet.jit.si/CynicalOxygensContainOut'
}

Pass a convertion function as the convertToEvent prop of the <Calendar> element:

import React, { ComponentProps } from 'react';
import { List } from 'react-admin';
import { Calendar } from '@react-admin/ra-calendar';
import { EventInput, DatesSetArg } from '@full-calendar/react';

const converter = (event: any): EventInput => ({
    id: String(event.id),
    title: event.name,
    start: event.begin,
    end: event.finish,
    backgroundColor: colorForCategory(event.category)
    borderColor: colorForCategory(event.category)
    editable: event.can_edit,
    url: `https://meet.jit.si/${event.jitsi_meet_id}`
});

const customGetFilterValues = (dateInfo?: DatesSetArg): any =>
    dateInfo
        ? {
              begin_gte: dateInfo.startStr,
              begin_lte: dateInfo.endStr,
          }
        : {
              begin_gte: new Date('2020-10-01'),
              begin_lte: new Date('2020-10-31'),
          };

const EventList: FC<ComponentProps<typeof List>> = props => (
    <List
        {...props}
        filterDefaultValues={customGetFilterValues()}
        perPage={1000}
        pagination={false}
    >
        <Calendar convertToEvent={converter} getFilterValuesFromInterval={customGetFilterValues} />
    </List>
);

CHANGELOG

v1.0.2

2021-04-22

  • Fix calendar popup styles in dark mode

v1.0.1

2020-10-27

  • Fix calendar does not resize when opening/closing the sidebar

v1.0.0

2020-10-22

  • First release 🥂
  • (feat) Write a <CompleteCalendar> component requiring zero configuration for a quick start
  • (feat) Do not load all locales by default to minimize the build size
  • (feat) Adapt the default FullCalendar header to a material-ui look and feel
  • (fix) Fix error when deleting an event
  • Improve the documentation

v0.0.1

2020-10-21