Creating a Header Toolbar with FullCalendar in React TypeScript

Introduction to FullCalendar and React

FullCalendar is a powerful JavaScript library designed to display and manage calendar events in a user-friendly manner. It provides a rich set of features for building interactive calendars. When combined with React, one of the most popular front-end libraries, developers can create sophisticated calendar interfaces that enhance user experiences. Today, we will explore how to implement a header toolbar with FullCalendar in a React application using TypeScript.

In this tutorial, we’ll walk through the process of setting up FullCalendar within a React component, customizing the header toolbar, and managing events effectively. By the end of this guide, you will have a fully functional calendar with a dynamic header, ready to be integrated into any project.

Focusing on TypeScript will also help us ensure our components are type-safe and maintainable. If you’re new to React or TypeScript, don’t worry — we will break down each step and provide clear code examples.

Setting Up Your React Project

The first step in our journey is to set up a new React project. You can easily create a new React application using Create React App. This tool provides a straightforward way to scaffold a modern React project with TypeScript support enabled. Run the following command in your terminal:

npx create-react-app my-calendar-app --template typescript

Once your project has been created, navigate into the project directory with cd my-calendar-app, and install FullCalendar along with its React integration by running:

npm install @fullcalendar/react @fullcalendar/daygrid

In addition to FullCalendar, you may want to install other views or plugins, such as the time grid or list view, depending on your needs. For instance, if you want to manage time-based events, you can add:

npm install @fullcalendar/timegrid

Creating the Calendar Component

Now that we have our project set up and FullCalendar installed, let’s create the main calendar component. Create a new file named Calendar.tsx in the src folder of your project.

In Calendar.tsx, we will import the necessary dependencies and initialize our calendar component. Our component will include the header toolbar, which allows users to navigate between different views of the calendar (like month, week, and day views).

import React, { useState } from 'react';
import FullCalendar from '@fullcalendar/react';
import dayGridPlugin from '@fullcalendar/daygrid';

const Calendar: React.FC = () => {
  const [currentDate, setCurrentDate] = useState(new Date());

  return (
    
  );
};

export default Calendar;

In the snippet above, we are importing FullCalendar and the day grid plugin. We then set an initial state for currentDate, which keeps track of the date currently displayed on the calendar. The headerToolbar property allows us to customize the header by specifying what navigation buttons and views are available.

Customizing the Header Toolbar

The header toolbar is a vital part of the FullCalendar component as it enhances usability. You can customize its appearance and functionality based on your project’s requirements. Beyond the default options, you can create custom buttons and modify what actions they perform.

To add custom buttons, you might consider organizing the toolbar into logical sections. For instance, let’s incorporate a button that jumps to the current date. You can do this by defining a function that sets currentDate to today.

const handleTodayClick = () => {
    setCurrentDate(new Date());
};

headerToolbar={{
  left: 'prev,next today',
  center: 'title',
  right: 'dayGridMonth,timeGridWeek,timeGridDay',
}}; 

Next, bind the today action button to our new function by modifying the headerToolbar property:

headerToolbar={{
  left: 'prev,next',
  center: 'title',
  right: 'today,dayGridMonth,timeGridWeek,timeGridDay',
}} 

This way, when a user clicks the ‘today’ button, it will reset the calendar to the current date. This interactivity significantly enhances the user experience, making it easier to navigate through different views.

Handling Events in FullCalendar

With our calendar component set up and the header toolbar customized, it’s time to handle events. FullCalendar provides a powerful API allowing you to create, manage, and display events in a user-friendly manner.

Start by defining an array of events within your component. These could represent meetings, tasks, or any other time-specific occurrences. The events can be structured in a simple format containing properties like title, date, and any additional details needed.

const initialEvents = [
  { title: 'Event 1', date: '2023-11-01' },
  { title: 'Event 2', date: '2023-11-02' },
];

Next, pass this array to the events prop of the FullCalendar component:

 

Now, you can see the events displayed on your calendar. To ensure the events’ data can be updated dynamically, consider managing the events through state by using the useState hook. This way, you can add more functionalities like adding new events from a form, removing events, or updating existing ones.

Integrating Event Creation and Deletion

To make your calendar functional, you will want to allow users to create and delete events. Start by providing a simple form where users can input the event title and date. Create a new component called AddEventForm and include it in your main Calendar component.

const AddEventForm: React.FC<{ onAddEvent: (event: any) => void }> = ({ onAddEvent }) => {
  const [title, setTitle] = useState('');
  const [date, setDate] = useState('');

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    onAddEvent({ title, date });
    setTitle('');
    setDate('');
  };

This component encapsulates the logic for handling user input. It accepts an onAddEvent prop, which will be the function to add the new event back in the Calendar component. You can then add this form to your Calendar component and handle adding events with a state update:

const [events, setEvents] = useState(initialEvents);

const addEvent = (newEvent: any) => {
  setEvents((prev) => [...prev, newEvent]);
};

To delete events, you can implement a simple mechanism to identify events by their title or date and remove them from the state.

Final Touches and Enhancements

Having implemented the core functionalities, enhance your calendar further by adding features like event date validation, user authentication for saving events, or even integration with external APIs like Google Calendar.

You may also consider using custom styles or themes to make your calendar visually appealing. FullCalendar allows you to customize its appearance using CSS. Add your styles in a separate CSS file or leverage a CSS-in-JS solution.

Testing is another critical aspect. Make sure to write unit tests for your components, especially when handling user inputs and events. You can use testing libraries like Jest or React Testing Library to ensure your application behaves as expected.

Conclusion

In this tutorial, we built a fully functioning calendar with a customized header toolbar using FullCalendar in a React TypeScript environment. We explored how to handle events and implemented functionalities for adding and deleting events, enhancing the overall user experience. With this foundational knowledge, you can now expand upon this application by integrating additional features or customizing it further to fit your project needs.

Whether you are building a simple app or embarking on a more extensive project, mastering tools like FullCalendar in React opens up avenues for creating engaging web applications. We hope this guide inspires you to explore the vast potential of web technologies and encourages you to keep learning and innovating!

Scroll to Top