Custom Component
Overview
All components are javascript classes that extend base classes or classes of other components from the formio.js library. This extending of base classes also needs to be done if you want to create a custom component. This example tutorial will show you how to
Create a custom component from scratch
Use the formio.js library with webpack
Test the component in the form builder
The custom component in this tutorial is a Rating component. The features that will be implemented are
Copy and paste SVGs to use them as icons
Change the width and height of the SVG
Change the unfilled and filled color
Change the number of icons/svgs
Prerequisites
Install Node.js
An IDE for developing code such as Visual Studio Code or Webstorm
Note
In this tutorial there will be + and - symbols next to some of the code / files. The + tells the code that something is being added. The - tells you that something is being removed
Initializing Project
Create a new directory to hold your project files (The directory in this tutorial is called Custom_Components)
Create a file called
template.html
In the template.html file add the following code
Create a new file called
index.js
Create a new file called
Rating.js
Your project structure should be as follows:
Initial Custom Component Code
The first step of any component is to extend from a base class, start by
Creating a new class in Rating.js called Rating that extends Field
Great! You now have a custom component, you can test that this is working by doing the following
In index.js add the code below
Add your
Rating.js
andindex.js
scripts to your template.html fileOpen your template.html file in your browser and view the DOM. You should see the following (notice the formio-component-rating):
The Formio library is now picking up our custom component and adding it to the DOM! In order to render content into the DOM add the following to Rating.js and refresh your browser
You should now see "I am a rating component" text in the browser
Notice how in the render function you are calling super.render(). This is because you are overriding the render function defined in Field component.
There are many functions that can be overridden from the base component that you extend from (Learn more here). Add the following function overrides to your Rating.js file
At this point you have
A basic template of a component (Rating.js)
The component being rendered by the Formio library (index.js)
The html page to view our custom component (template.html)
However, there are a couple of problems if you continue developing the custom component this way
You are using the Formio object in Rating.js and index.js, but it is not apparent that this object is available to us
You are not able to using npm packages
You are not able to use import / export statements
There is no syntax highlighting or intellisense
In order to solve these problems and make development of our custom component easier you need Webpack
Webpack
Webpack will compile our javascript modules and allow them to be used in the browser. You will also being using webpack to create a development server to develop the custom rating component
Start by
Opening your terminal
In your project directory type
npm init -y
Then type
npm install webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev
Create a webpack.config.cjs file in the root directory of your project
Your project structure should be as follows:
In webpack.config.cjs add the following code
This config file tells webpack how to bundle our modules and run the dev server
Now you can use module syntax in our JS files! Let's go back and make some changes to our code
In Rating.js make the following changes
In index.js make the following changes
In template.html make the following changes
In order for webpack to bundle the modules together it looks for a folder called
src
. You need to put the source code files in a directory calledsrc
Create a directory called src
Put index.js in src
/src/index.js
Create a directory called
rating
insrc
/src/rating/
Put Rating.js in rating directory
/src/rating/Rating.js
Your project structure should now look like
In order to use the
import {Formio} from formiojs
you need to install the formiojs library in our node_modules. Typenpm install formiojs
in your terminalNow that formiojs is installed, type
webpack serve --open
to start a development serverYou should now have a blank html page with the text
I am a rating component
. You now have your development environment setup
Creating the Rating Component
schema
The schema function is where you override properties from the component your class extends from and also create new properties
The Rating component will have some new properties that will be custom to the component itself. The properties will be
unfilledColor
filledColor
numOfIcons
iconHeight
iconWidth
icon
Add these properties to your schema in Rating.js
builderInfo
The builderInfo is how the component will show up within the form builder. The builder has 6 main properties
title - The text displayed on the component in the form builder
icon - The icon next to the title of the component. Formio has Font Awesome integrated in the library so any fa-* icons will work
group - The group the component will show up in the form builder
documentation - Gives the help button on the form builder a link to the documentation for this component
weight - Specifies the position of the component in the builder sidebar. Lower values place the component higher in the sidebar.
schema - The JSON schema used when the component is dragged onto the form builder
You do not need to add any new code
setIconProperties
setIconProperties is a custom function that when called adds iconHeight, iconWidth, and unfilledColor properties to the icon
Add the following code to Rating.js
init
Init function will be called immediately after the component has been initialized. This is useful for calling functions or set variables at the time the component is initialized in an application
You will be calling the setIconProperties function inside init function. Add the following code to Rating.js
render
This function returns the html that will render in the DOM
Add the following code to the render function
Notice
this.component.icon
andthis.component.numOfIcons
. You have access to the properties defined in schema by referencingthis.component
Notice
ref="rating"
. This will be important when you need to attach event listeners to the custom rating component
attach
The attach function is where you will attach logic to your component such as event listeners.
It is important to have a
ref
attribute within your component so that attach can reference your ref attribute within the functionAdd the following code to the attach function
this.loadRefs
looks for the DOM element that has the attributeref="property"
(in this the property israting
) and adds a reference for you to call on in the codethis.refs.rating
.Because this.refs.rating points to a div element (see render) and all of our icons are inside this div element, you can use
this.refs.rating.children
to loop over all the icons and attach event listeners to each of themNotice
this.updateValue(`${i+1}/${icons.length}`);
. Use the updateValue function to give the component data whenever it is submitted
updateValue
Use this function whenever you need to set the submission data of the component before it is submitted
Testing Custom Component
At this point, several lines of new code have been added to the Rating.js file. Check the component to make sure everything is working up to this point
In index.js change the following code
Notice the
sanitizeConfig
. In order to use SVGs with Formio you need to add this option to the createForm function to allow Formio to render SVGsClick here to learn more about form options
Save your changes in your IDE. Your webpage should automatically update to reflect the changes made due to webpacks hot module reloading
You should now see your rating component on the html page!
Getting the Rating Component in the Form Builder
Now that you are able to display the Rating component using Formio.createForm, it's time to test the Rating component in the form builder
To add the form builder within your dev environment make the following changes to the code
In template.html
In index.js
Save changes, and you should now see the formio builder in your dev environment along with the Rating component under Basic tab
Drag and drop the Rating component onto the form builder
You should now see the Rating component edit options... However, you may have noticed that you cannot see any options to change the icon, number of icons, the icon width and height and other custom options
In order to customize the Ratings edit menu, you need to set the editForm inside Rating.js
Add the following code to Rating.js
Now that you have set the edit form within Rating.js you need to create the Rating.form.js file
Inside the /rating directory add a
Rating.form.js
fileAdd the following code to Rating.form.js
For this component you won't be needing a data tab or a validation tab, so you are able to remove these tabs by defining a json schema with the key being the tab you want to remove and setting ignore to true. You are also able to define custom edit properties by creating a list of components. You will create this list of components in a folder called editForm
In the /rating directory create a new directory called
editForm
In the /editForm directory create a new file called
Rating.edit.display.js
Add the following code to Rating.edit.display.js
It is important when creating a custom component that the keys in your editForm javascript files match the properties in the custom component schema function. This allows the properties in the schema to be set dynamically from the input in the edit form
Your project structure should look like the following
Save your changes and drag and drop the Rating component onto the builder. You should now see your custom properties!
If you are building your own custom component then this is a good point to stop and develop your component. In the next steps you will be adding the rating component into the enterprise form builder. This will break the development setup that you currently have
Getting the Rating Component in the Enterprise Form Builder
In order to bundle the rating component into a file that only contains the code necessary for the enterprise form builder you need to make some changes to you code.
To start create a new webpack config file called
webpack.prod.cjs
in the root project directoryAdd the following code to webpack.prod.cjs
This webpack config will be used to create a production bundle of our rating component
Because the enterprise form builder already has the Formio object as a global variable you need to remove/comment out all imports of Formio so that it is not included in the bundle
In index.js comment out
import {Formio} from 'formiojs'
and comment out the builder and create form function calls. Commenting out the code allows you to uncomment it whenever you want to go back to developing the componentIn Rating.js and Rating.form.js comment out
import {Formio} from 'formiojs'
Now that the code only has the necessary lines to for the rating component to function you can create a production bundle using webpack.prod.cjs
Save your files and open your terminal and type
webpack --config ./webpack.prod.cjs
You should now see a
/dist
directory with anindex.bundle.js
file insideYou now need to take the code inside
index.bundle.js
and host it somewhere that allows a CDN to deliver the codeIf you know how to do host and create a link using a CDN then you can skip the following steps
Open up GitHub and create a new repository called
MyCustomComponent
Click
uploading an existing file
Upload the
index.bundle.js
fileClick
Commit changes
You should now have a repository called MyCustomComponent with index.bundle.js inside
jsDeliver is an easy way to create CDNs from GitHub repositories. If you followed the steps above your CDN should be
https://cdn.jsdelivr.net/gh/user/MyCustomComponent/index.bundle.js
Replace
user
with your GitHub username
Now that you have a CDN with your bundled code you can get the Rating component onto the enterprise form builder
Open up your developer portal and create a new project
In the navigation bar click
Settings
Click on
Custom JS & CSS
Under
Form Module
add the following codeJust like when you were in developing the Rating component, you also need to add sanitizeConfig to the enterprise form builder as well
Under
Custom Javascript
add your CDN to the textboxRemember to replace user with your GitHub username
Save Settings (Click OK if you're asked if you would like to load the javascript)
Create a new Web Form
You should now see Rating component under the basic tab in the form builder
You can now use the Rating component when creating Forms!
Note: Sometimes the sanitize configs won't take into effect unless you refresh the page. If you are not able to see the component when it is dragged onto the form builder try refreshing your browser
Last updated