28
React FullCalendar snippet
This was originally published on https://eichgi.hashnode.dev/react-fullcalendar-snippet
Hey folks, the following snippet is a basic example of what you can achieve with FullCalendar library for React. I hope you find my appointments calendar interesting, so let's dive into.
Here you have the react component full calendar docs:
https://fullcalendar.io/docs/react
https://fullcalendar.io/docs/react
Once you have installed the package let's focus on the component:
<FullCalendar
ref={calendar}
fixedWeekCount={false}
height={'auto'}
locale={esLocale}
plugins={[dayGridPlugin, interactionPlugin]}
initialView={setCalendarViewByWidth()}
headerToolbar={{
start: 'prev today next',
center: 'title',
end: 'newAppointment'
}}
footerToolbar={{
center: 'toggleMonth toggleWeek toggleDay',
}}
customButtons={{
newAppointment: {
text: 'Nueva cita',
click: () => {
dateClickHandler();
},
},
toggleDay: {
text: 'Hoy',
click: () => {
calendar.current.getApi().changeView('dayGridDay');
}
},
toggleWeek: {
text: 'Semana',
click: () => {
calendar.current.getApi().changeView('dayGridWeek');
}
},
toggleMonth: {
text: 'Mes',
click: () => {
calendar.current.getApi().changeView('dayGridMonth')
}
},
}}
dateClick={e => dateClickHandler(e)}
events={appointments}
datesSet={async (dateInfo) => {
await getEvents(dateInfo.startStr.split('T')[0], dateInfo.endStr.split('T')[0]);
}}
eventsSet={(events => {
console.log('Events set: ', events);
})}
eventClick={e => eventsHandler(e)}
/>
I will describe the props described in the snippet. These are the very basics functionalities you might need for a full dynamic calendar.
const calendar = useRef(null);
import esLocale from '@fullcalendar/core/locales/es';
import dayGridPlugin from '@fullcalendar/daygrid';
import interactionPlugin from '@fullcalendar/interaction';
headerToolbar={{
start: 'prev today next',
center: 'title',
end: 'newAppointment'
}}
footerToolbar={{
center: 'toggleMonth toggleWeek toggleDay',
}}
customButtons={{
newAppointment: {
text: 'Nueva cita',
click: () => {
dateClickHandler();
},
},
...
}}
dateClick={e => dateClickHandler(e)}
events={[
{ title: 'event 1', date: '2019-04-01' },
{ title: 'event 2', date: '2019-04-02' }
]}
datesSet={async (dateInfo) => {
await getEvents(dateInfo.startStr.split('T')[0], dateInfo.endStr.split('T')[0]);
}}
Every time you change the view you can request events from the backend like this. (Don't forget to create your own getEvents definition)
eventClick={e => eventsHandler(e)}
Here you have it, simple react fullcalendar snippet. There are plenty of options in the docs so you can customize your own calendar. CSS, Events, formats, etc... you will find them here: https://fullcalendar.io/docs#toc
28