Getting Started with the KendoReact Form
This guide provides essential information about using the KendoReact Form package. You will learn how to install the package, add a Form component to your project, style the component, and activate your license.
After completing this guide, you will be able to reproduce the following example.
Setting Up Your React Project
Before you install the KendoReact Form, make sure that you have a running React project. The easiest way to set up a React project is to use the Create React App approach that is described in the Get Started with KendoReact article.
Installing the Form Package
All KendoReact packages are distributed through npm and offer a similar installation experience. To use the Form component, start with the installation of the Form npm package and its dependencies. Use Node.js v5.0.0 or later.
npm install --save @progress/kendo-react-form @progress/kendo-licensing
Importing the Component
In the src/App.js
file of your React project, import the Form, Field, and FormElement components from the Form package, and then the Error and Input components:
- The FormElement is a small wrapper for an HTML form element that automatically attaches the Form component's
onSubmit
event and provides the handyrender
prop as well as some useful CSS classes. - The Field component is needed to render inputs.
- The Error component from the Error package allows you to display error text messages.
- The Input component from the Inputs package allows users to submit values.
// ES2015 module syntax
import { Form, Field, FormElement } from '@progress/kendo-react-form';
import { Error } from '@progress/kendo-react-labels';
import { Input } from '@progress/kendo-react-inputs';
// CommonJS format
const { Form, Field, FormElement } = require('@progress/kendo-react-form');
const { Error } = require('@progress/kendo-react-labels');
const { Input } = require('@progress/kendo-react-inputs');
Using the Component
-
Create an
emailRegex
in order to validate the user's email address and anemailInput
component that will render the input field.const emailRegex = new RegExp(/\S+@\S+\.\S+/) const emailValidator = value => emailRegex.test(value) ? "" : "Please enter a valid email." const EmailInput = fieldRenderProps => { const { validationMessage, visited, ...others } = fieldRenderProps; return <div> <Input {...others} /> {visited && validationMessage && <Error>{validationMessage}</Error>} </div>; }; ];
-
Handle the
onSubmit
event by creating a handler function.const App = () => { const [messages, setMessages] = React.useState(initialMessages); const addNewMessage = (event) => { setMessages([...messages, event.message]); };
-
Add the component's markup to the
src/App.js
file in your project and set the Form's name, component, label, and validator. Optionally, set themaxWidth
.return <Form onSubmit={handleSubmit} render={formRenderProps => <FormElement style={{ maxWidth: 650 }}> <fieldset className={'k-form-fieldset'}> <legend className={'k-form-legend'}>Please fill in the fields:</legend> <div className="mb-3"> <Field name={'firstName'} component={Input} label={'First name'} /> </div> <div className="mb-3"> <Field name={'lastName'} component={Input} label={'Last name'} /> </div> <div className="mb-3"> <Field name={"email"} type={"email"} component={EmailInput} label={"Email"} validator={emailValidator} /> </div> </fieldset> <div className="k-form-buttons"> <button type={'submit'} className="k-button k-button-md k-rounded-md k-button-solid k-button-solid-base" disabled={!formRenderProps.allowSubmit}> Submit </button> </div> </FormElement>} />;
-
To style the Form, install and import the Default theme, which is one of the three beautiful themes for KendoReact.
4.1. Install the Default theme package.
npm install --save @progress/kendo-theme-default
4.2. Import the CSS file from the package in
src/App.js
. Add this import before your existingApp.css
import.import '@progress/kendo-theme-default/dist/all.css';
-
Build and run the application by typing the following command in the root folder of your project.
npm start
-
Navigate to http://localhost:3000 to see the KendoReact Form component on the page.
Activating Your License Key
Using any of the UI components in the KendoReact library requires either a commercial license key or an active trial license key.
Follow the instructions on the KendoReact My License page to activate your trial or commercial license. You can skip this step if your application already contains a KendoReact license file.
Dependencies
The Form package requires you to install the following peer dependencies in your application:
Package Name | Description |
---|---|
react 16.8.2* | Contains the functionality necessary to define React components. |
react-dom | Contains the React renderer for the web. |
@progress/kendo-licensing | Contains the internal infrastructure related to licensing. |