Custom Components

Describes the process of building and creating a Custom Form Component.

Introduction

One of the more powerful features of the Form.io platform is the ability to create custom form components. Creating a custom component involves extending a Base class of the component that is "closest" to the implementation you desire, and then overriding methods or introducing new methods that will implement the custom logic. All out-of-the-box components within the Form.io platform also use this same pattern, presenting a multitude of examples by inspecting how the base components for the Form.io renderer are implemented. Here is the link to all of the existing components for the Form.io renderer.

As shown, the implementation of any component involves extending a base class and then creating your own implementation on top of those base classes.

In order to create quality components, it is important to understand the critical methods used to define a new component. They are as follows:

Extending Components

Every custom component will derive from a base class, whose behavior is closest to the behavior of the component you wish to create. It is possible to extend any other component within the Form.io renderer and a list of all of these components and their classes can be found @ https://github.com/formio/formio.js/tree/master/src/components

Because of this, the first task in building a custom component is to determine which component most closely resembles the behavior and data model of the component you are looking to achieve. For example, to build a multi-button select component, it may be best to start with a Radio component since it most closely resembles the behavior of the component you wish to create.

If you are unsure, then it is also fine to derive from the "core" components which serve as the base for all other components within the renderer. These core components are as follows:

Core Components

Class

Extends

Description

Component

Element

Base component class

Field

Component

Component that derives from Component class that implements a "field" render template

Multivalue

Field

A component that is able to implement the "multiple" configuration allowing for multiple inputs for this field type.

Input

Multivalue

A component type that implements an HTML value input.

Each of the components can be extended by first referencing them from the Components.components object, and then extending them as follows.

For the most generic components, it is fine to derive from "Component", but in most value components, you may wish to derive from the Input component.

Component Methods

Once you derive from a base component, the next step is to define methods that either override base behavior or introduce new behavior into the component class. It is recommended to look at the source code of your "base" component and the classes that it extends to understand what methods you have available to you, but the majority of all behavior can be achieved by implementing some of the following methods.

Component Modules

Custom components can be registered with the renderer using a module that is also able to include a large number of components. An example of a module that implements some custom components is the Contributed Components repo.

In this repo, you can see that each component is defined within the "components" folder as follows.

These components can then be included in the module by creating an export of these components like so.

Using Custom Components in applications

Once these components are created as a module, they can then easily be added to the renderer using the Formio.use method as follows.

Once this is done, any new components that have been added to the renderer / builder will show up and can be used in a web form.

Examples

Below are code examples of different Custom Components that you can use in your application or as a foundation for building your own custom components.

Follow the link for a full list of Custom Components found within the Contrib Repository

CheckMatrix

Below is a code example for a custom component - Check Matrix. This component allows you to set up a grid of checkboxes where each box can be independently checked, with the data saved as a multi-dimensional array.

This component can now be rendered in both the builder and renderer as the following JSFiddle illustrates.

Header Component

An example of custom component implementation would be to create a Custom Component called Header which will allow the configuration of one of the following <h1>, <h2>, <h3> etc

For this component, we will simply extend the HTML Element component which allows for dynamic HTML to be rendered within a form.

This example also shows how you can introduce changes to the Form Builder to introduce new configurations or make changes to other configurations.

Here is the code for a Header component

This component can actually be tested by opening up the Form.io hosted portal @ https://portal.form.io, pasting the code within the console of the browser, and then pressing Enter. Then when you navigate to the form builder, you will see the following.

Last updated

Was this helpful?