Create and preview a Headless Form using a React app introduction
The starter kit helps you get started quickly using a React app. You are free to develop and use Headless Adaptive forms in an Angular, Vanilla JS, and other development environments of your choice.
Starting with Headless Adaptive forms is quite easy and quick. Clone the ready-made React project, install the dependencies, and run the project. You have a Headless Adaptive form integrated in a React app up and running. You can use the sample react project to build and test Headless Adaptive forms before deploying it in a production environment.
尝别迟鈥檚 start:
Before you start pre-requisites
To create and run a React app, you should have the following installed on your computer:
-
Install the . If you are new to Git, see .
-
Install .
Get Started
Once you fulfill the requirements, perform the following steps to get started:
1. Setup Headless Adaptive forms starter kit install
The starter kit is a React app with a sample Headless Adaptive form and corresponding libraries. Use the kit to develop and test your Headless Adaptive forms and corresponding React components. Run the following commands to set up the Headless Adaptive forms starter kit:
-
Open command prompt and run the following command:
code language-shell git clone https://github.com/adobe/react-starter-kit-aem-headless-forms
The command creates a directory called react-starter-kit-aem-headless-forms in your current location and clones the Headless Adaptive forms React starter app into it. Along with the configurations and list of dependencies required to render the form, the directory includes the following important content:
- Sample form: The starter kit includes a sample loan application form. To view the form (form definition) included with the app, open the
/react-starter-kit-aem-headless-forms/form-definations/form-model.json
file. - Sample React components: The starter kit includes sample react components for Rich text and Slider. This guide helps you create your own custom components using these Rich text and Slider components.
- Mappings.ts: The mappings.ts file helps you map custom components with form fields. For example, map a numeric stepper field with a ratings component.
- Environment configurations: Environment configurations let you choose to render a form included in the starter kit or fetch a form from an AEM Forms Server.
note note NOTE Examples in documents are based on VSCode. You are free to use any plain-text code editor. - Sample form: The starter kit includes a sample loan application form. To view the form (form definition) included with the app, open the
-
Navigate to the react-starter-kit-aem-headless-forms directory and run the following command to install the dependencies:
code language-shell npm install
The command downloads every package and library required to build and run the app鈥攊ncluding the Headless Adaptive forms libraries (@aemforms/af-react-renderer, @aemforms/af-react-components, @adobe/react-spectrum). It then runs validations and persists data for each form instance.
2. Preview the Headless Adaptive form preview
After setting up the starter kit, you can preview the sample Headless Adaptive form, replace it with your own custom form. You can also configure the starter kit to retrieve a form from an AEM Forms Server. To preview the form
-
Rename the
env_template
file to.env
file. Also ensure, the USE_LOCAL_JSON option is set to true. -
Use the following command to run the app:
code language-shell npm start
This command starts a local development server, and opens the sample Headless Adaptive form, included in the starter app, in your default web browser.
All set! You are ready to start developing a custom Headless Adaptive form.
3. Create and render your own Headless Adaptive form custom
A Headless Adaptive form represents the form and its components, such as fields and buttons, in JSON (JavaScript Object Notation) format. The advantage of using the JSON format is that it can be easily parsed and used by various programming languages, making it a convenient way to exchange form data between systems. To view the sample Headless Adaptive form included with the app, open the /react-starter-kit-aem-headless-forms/form-definations/form-model.json
file.
尝别迟鈥檚 create a Contact Us
form with four fields: 鈥淣ame,鈥 鈥淓mail,鈥 鈥淐ontact Number,鈥 and 鈥淢essage.鈥 The fields are defined as objects (items) within the JSON, with each object (item) having properties such as type, label, name, and required. The form also has a button of type 鈥渟ubmit.鈥 Here is the JSON for the form.
{
"afModelDefinition": {
"adaptiveform": "0.10.0",
"items": [
{
"fieldType": "text-input",
"label": {
"value": "Name"
},
"name": "name"
},
{
"fieldType": "text-input",
"format": "email",
"label": {
"value": "Email"
},
"name": "email"
},
{
"fieldType": "text-input",
"format": "phone",
"pattern": "[0-9]{10}",
"label": {
"value": "Contact Number"
},
"name": "Phone"
},
{
"fieldType": "multiline-input",
"label": {
"value":"Message"
},
"name": "message"
},
{
"fieldType": "button",
"label":{
"value": "Submit"
},
"name":"submit",
"events":{
"click": "submitForm()"
}
}
],
"action": "https://eozrmb1rwsmofct.m.pipedream.net",
"description": "Contact Us",
"title": "Contact Us",
"metadata": {
"grammar": "json-formula-1.0.0",
"version": "1.0.0"
}
}
}
- The 鈥渁fModelDefinition鈥 attribute is only needed for React applications and is not a part of the form definition.
- You can hand-craft the form JSON or use the AEM adaptive forms editor (adaptive forms WYSIWYG editor) to create and deliver the form JSON. In a production environment, you use AEM Forms to deliver the form JSON, more on it later.
- The tutorial uses the https://pipedream.com/ to test form submissions. You use your own or third-party endpoints approved by your organization to receive the data from a Headless Adaptive form.
To render the form, replace the sample Headless Adaptive form JSON /react-starter-kit-aem-headless-forms/form-definations/form-model.json
with the above JSON, save the file, wait for the starter-kit to compile and refresh the form.
You have successfully rendered the Headless Adaptive form.
Bonus
尝别迟鈥檚 set the title of the webpage hosting the form to Contact Us | WKND Adventures and Travel
. To change the title, open the react-starter-kit-aem-headless-forms/public/index.html file for editing and set the title.
Next step
By default, the starter kit uses components to render the form. You can create and use your own components or third-party components. For example, using Google Material UI or Chakra UI.
尝别迟鈥檚 use Google Material UI to render the Contact Us
form.