Hands-on: Integrating React Hook Form with React data grid

Akash Mittal Hands-on / August 17, 2023

Hands-on: Integrating React Hook Form with React data grid

Welcome to our Hands-on series! Here, we prioritize practicality, with a strong focus on Handsontable demos and useful code snippets. Why? To help you save time and seamlessly begin your projects using our data grid. Let’s dive into some hands-on action! 💪

Introduction

This article will guide you through the process of integrating React Hook Form with Handsontable, enabling you to design forms for adding and editing records within a data grid. React Hook Form is a library for handling form validation and state management in React apps, making form handling easy with its simple API. 

What are we building?

In this project, we’ll create an HTML form using React Hook Form and feed the data into the Handsontable data grid. The driving factor behind this endeavor is a specific business requirement: the need to transform user-submitted form data into a structured tabular layout. For example, think of a scenario where the form captures various details about employees.

Demo

Resources used in the project

First of all, let’s see what resources we are going to use in this project:

  • Create-React-App: For handling everything related to the initial setup of a React project.
  • React Hook Form: A handy library for creating forms in React which is lightweight, performant, and provides inbuilt validations.
  • Handsontable: Handsontable is used to create data grids that give the look and feel of a spreadsheet.
  • TailwindCSS: We’ll use TailwindCSS to improve the style of our forms. You may use any CSS framework of your choice. 

Additionally, we’ll also use form-builder to create forms without the need for manual coding. It’s a utility created by the React Hook Form team to generate form code instead of starting from the ground up. While entirely optional, it comes highly recommended due to its time-saving benefits.

Setting up the project

Installations

Set up the project using create-react-app and install the required libraries. Run the below commands:

We assume that you have Node.js >= 14 installed with an npm version (5.2+) supporting npx

In the next step, we’ll need to add template file paths in tailwind.config.js:

The last step includes adding tailwind directives to the ./src/index.css file:

Creating the form

As mentioned, we’ll use the create-hook-form library to handle HTML forms. But before that, we need to create those forms. Although we can write the code for form creation, it’s better to utilize the form-builder utility to generate code.

When you open the builder, you’ll get the screen like this:

Form builder in React Hook Form - Hands-on: Integrating React Hook Form with React data grid

For this project, we’ve created a form with the following fields:

  • First name – Text field, maxLength 80
  • Last name – Text field, maxLength 100
  • Email – Text field, pattern `^\S+@\S+$`
  • Mobile number – Number, minLength 6, maxLength 12
  • Skills – Select a field with options (Frontend, Backend, Full Stack)
  • Developer – Radio field with options (yes, no)

Check the Code section and see if the generated code represents your form.

Now open the App.js file and paste the generated code there:

Time to test if things are working till this point. So, run the app using the below command:

The browser will open and display the form like this:

Form UI without using TailwindCSS - Hands-on: Integrating React Hook Form with React data grid

The form is currently visible; however, its visual presentation leaves room for improvement. To enhance its appearance, we’ll apply styling using TailwindCSS:

You may notice that we have introduced some constants to hold the values of strings. It’s a good practice to separate strings from the code.

Now run the project and check the output:

Form UI with TailwindCSS - Hands-on: Integrating React Hook Form with React data grid

The form looks much better now.

Integrating Handsontable with React Hook Form

The final step in this project is to create a data grid table using Handsontable and integrate it with React Hook Form. To accomplish this, we need to follow a few steps:

  • Initialize the table on the mount of the component so that an empty data grid can be displayed with headers.
  • Add an extra column in the data grid table for the row editing action button.
  • Attach a click hook to the Edit button.
  • Populate the data grid with the form’s values.
  • Use the Handsontable CSS file to style the data grid.
  • Reset the form after submitting it.

Here is the code:

App.js

App.css

The output will look like this:

Hands-on: Integrating React Hook Form with React data grid

You can edit the record by clicking the Edit button in the Action column.

Conclusion

The aim of this article was to provide you with ready-made integration of Handsontable data grid and React Hook Form, enabling easy implementation in your own project. In future articles, we’ll introduce more useful functionalities using a hands-on approach.

You might also be interested in our article demonstrating how to quickly build a public ledger to track your company’s expenses with React and Handsontable. Take a look!