0.0.7 • Published 3 years ago

formio-module v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

React Formio

A React library for rendering out forms based on the Form.io platform.

Example Application

To see an example application of how to implement all the components and modules in this library, see https://github.com/formio/react-app-starterkit

Install

npm

@formio/react can be used on the server, or bundled for the client using an npm-compatible packaging system such as Browserify or webpack.

npm install @formio/react --save
npm install formiojs --save // Install formiojs since it is a peerDependency

Components

Form

The form component is the primary component of the system. It is what takes the form definition (json) and renders the form into html. There are multiple ways to send the form to the Form component. The two main ways are to pass the src prop with a url to the form definition, usually a form.io server. The other is to pass the form prop with the json definition and optionally a url prop with the location of the form.

Props

NameTypeDefaultDescription
srcurlThe url of the form definition. This is commonly from a form.io server. When using src, the form will automatically submit the data to that url as well.
urlurlThe url of the form definition. The form will not be loaded from this url and the submission will not be saved here either. This is used for file upload, oauth and other components or actions that need to know where the server is. Use this in connection with form
formobjectInstead of loading a form from the src url, you can preload the form definition and pass it in with the form prop. You should also set url if you are using any advanced components like file upload or oauth.
submissionobjectSubmission data to fill the form. You can either load a previous submission or create a submission with some pre-filled data. If you do not provide a submissions the form will initialize an empty submission using default values from the form.
optionsobjectan options object that can pass options to the formio.js Form that is rendered. You can set options such as readOnly, noAlerts or hide. There are many options to be found in the formio.js library.

Event Props

You can respond to various events in the form. Simply pass in a prop with a function for one of these events.

NameParametersDescription
onSubmitsubmission: objectWhen the submit button is pressed and the submission has started. If src is not provided, this will be the final submit event.
onSubmitDonesubmission: objectWhen the submission has successfully been made to the server. This will only fire if src is set.
onChangesubmission: object, submission.changed: object of what changed, submission.isValid: boolean - if the submission passes validations.A value in the submission has changed.
onErrorerrors: array or string or booleanCalled when an error occurs during submission such as a validation issue.
onRenderTriggers when the form is finished rendering.
onCustomEvent{ type: string - event type, component: object - triggering component, data: object - data for component, event: string - raw event }Event that is triggered from a button configured with "Event" type.
onPrevPage{ page: integer - new page number, submission: object - submission data }Triggered for wizards when "Previous" button is pressed
onNextPage{ page: integer - new page number, submission: object - submission data }Triggered for wizards when "Next" button is pressed
formReadyformInstance: Webform/Wizard - form class instanceCalled when the form gets ready state

Example

Give Form a src property and render:

import React from 'react';
import ReactDOM from 'react-dom';
import {Form} from '@formio/react';
ReactDOM.render(
  <Form src="https://example.form.io/example" onSubmit={console.log} />
  , document.getElementById('example')
);

FormBuilder

The FormBuilder class can be used to embed a form builder directly in your react application. Please note that you'll need to include the CSS for the form builder from formio.js as well.

Please note that the FormBuilder component does not load and save from/to a url. You must handle the form definition loading and saving yourself or use the FormEdit component.

Props

NameTypeDefaultDescription
formobjectThis is the form definition object. It should at least have a display property set to form, wizard or pdf.
optionsobjectan options object that can pass options to the formio.js Form that is rendered. There are many options to be found in the formio.js library.

Event Props

NameParametersDescription
onChangeschema: objectTriggered any time the form definition changes
onEditComponentcomponent: objectTriggered when the component settings dialog is opened
onSaveComponentcomponent: objectTriggered when the component settings dialog is saved and closed
onCancelComponentcomponent: objectTriggered when the component settings dialog is cancelled
onDeleteComponentcomponent: objectTriggered when a component is removed from the form
onUpdateComponentcomponent: objectTriggered when a component is added or moved in the form

Example

import React from 'react';
import ReactDOM from 'react-dom';
import {FormBuilder} from '@formio/react';
ReactDOM.render(
  <FormBuilder form={{display: 'form'}} onChange={(schema) => console.log(schema)} />
  , document.getElementById('builder')
);

Errors

The Errors component can be used to print out errors that can be generated within an application. It can handle many types of errors that are generated by the form.io actions.

Props

NameTypeDefaultDescription
errorsanynullIf null is passed, the component is not rendered. Otherwise it will render the errors. There are various formats (including an array of errors) that can be passed in.
typestring'danger'The bootstrap alert type to render the container.

Event Props

None

FormEdit

The FormEdit component wraps the FormBuilder component and adds the title, display, name and path fields at the top along with a save button.

Props

NameTypeDefaultDescription
formobject{display: 'form' | 'wizard'}The form definition of the exiting form that is to be modified.
optionsobject{}The options to be passed to FormBuilder
saveTextstring''The string that will be displayed in the save-button

Event Props

NameParametersDescription
saveFormformCalled when the save button is pressed. Will pass the form definition to the callback.

FormGrid

The FormGrid component can be used to render a list of forms with buttons to edit, view, delete, etc on each row.

Props

NameTypeDefaultDescription
formsarray of forms[]A list of forms to be rendered in the grid.
permsobject{view: true, edit: truem data: true, delete: true}Whether or not to display buttons on the grid.
queryobject{}A query filter for passing to getForms when fetching forms.
getFormsfunction() => {}A function to trigger getting a new set of forms. Should accept the page number and filter query object as parameters.

Event Props

NameParametersDescription
onActionform: object, action: stringCalled when the user clicks on a button on a row of the form.

SubmissionGrid

The submisison grid will render a list of submissions and allow clicking on one row to select it.

Props

NameTypeDefaultDescription
submissionsarray of submissions[]A list of submissions to be rendered in the grid.
queryobject{}A query filter for passing to getForms when fetching submissions.
formobject{}The form definition for the submissions. This is used to render the submissions.
getSubmissionsfunction() => {}A function to trigger getting a new set of submissions. Should accept the page number and filter query object as parameters.

Event Props

NameParametersDescription
onActionsubmission: object, action: stringCalled when the user clicks on a button on a row of the submission.

Modules

Modules contain Redux actions, reducers, constants and selectors to simplify the API requests made for form.io forms. Reducers, actions and selectors all have names. This provides namespaces so the same actions and reducers can be re-used within the same redux state.

root

The root module is the container for things shared by other modules such as the selectRoot selector.

Selectors

NameParametersDescription
selectRootname: string, state: objectReturns the state for a namespace
selectErrorname: string, state: objectReturns any errors for a namespace
selectIsActivename: string, state: objectReturns isActive state for a namespace

auth

The auth module is designed to make it easier to login, register and authenticate users within react using the form.io login system.

Reducers

NameParametersDescription
authconfig: objectMounts the user and access information to the state tree. Config is not currently used but is a placeholder to make it consistent to the other reducers.

Actions

NameParametersDescription
initAuthThis is usually used at the start of an app code. It will check the localStorage for an existing user token and if found, log them in and fetch the needed information about the user.
setUseruser: objectWhen a user logs in, this will set the user and fetch the access information for that user. The user object is usually a submission from the login or register form.
logoutThis action will reset everything to the default state, including removing any localStorage information.

form

The form module is for interacting with a single form.

Reducers

NameParametersDescription
formconfig: objectMounts the form to the state tree. The config object should contain a name property defining a unique name for the redux state.

Actions

NameParametersDescription
getFormname: string, id: string, done: functionFetch a form from the server. If no id is provided, the name is used as the path. The done callback will be called when the action is complete. The first parameter is any errors and the second is the form definition.
saveFormname: string, form: object, done: functionSave a form to the server. It will use the _id property on the form to save it if it exists. Otherwise it will create a new form. The done callback will be called when the action is complete. The first parameter is any errors and the second is the form definition.
deleteFormname: string, id: string, done: functionDelete the form on the server with the id.
resetFormReset this reducer back to its initial state. This is automatically called after delete but can be called other times as well.

Selectors

NameParametersDescription
selectFormname: string, state: objectSelect the form definition from the state.

forms

The forms module handles multiple forms like a list of forms.

Reducers

NameParametersDescription
formsconfig: objectMounts the forms to the state tree. The config object should contain a name property defining a unique name for the redux state. The config object can also contain a query property which is added to all requests for forms. For example: {tags: 'common'} would limit the lists of forms to only forms tagged with 'common'.

Actions

NameParametersDescription
getFormsname: string, page: integer, params: objectFetch a list of forms from the server. params is a query object to filter the forms.
resetFormsReset this reducer back to its initial state. This is automatically called after delete but can be called other times as well.

Selectors

NameParametersDescription
selectFormsname: string, state: objectSelect the list of forms from the state.

submission

The submission module is for interacting with a single submission.

Reducers

NameParametersDescription
submissionconfig: objectMounts the submission to the state tree. The config object should contain a name property defining a unique name for the redux state.

Actions

NameParametersDescription
getSubmissionname: string, id: string, formId: string, done: functionFetch a submission from the server. The done callback will be called when the action is complete. The first parameter is any errors and the second is the submission.
saveSubmissionname: string, submission: object, formId: string, done: functionSave a submission to the server. It will use the _id property on the submission to save it if it exists. Otherwise it will create a new submission. The done callback will be called when the action is complete. The first parameter is any errors and the second is the submission.
deleteSubmissionname: string, id: string, formId: string, done: functionDelete the submission on the server with the id.
resetSubmissionReset this reducer back to its initial state. This is automatically called after delete but can be called other times as well.

Selectors

NameParametersDescription
selectSubmissionname: string, state: objectSelect the submission data from the state.

submissions

The submissions module handles multiple submissions within a form, like for a list of submissions.

Reducers

NameParametersDescription
submissionsconfig: objectMounts the submissions to the state tree. The config object should contain a name property defining a unique name for the redux state.

Actions

NameParametersDescription
getSubmissionsname: string, page: integer, params: object, formId: stringFetch a list of submissions from the server. params is a query object to filter the submissions.
resetSubmissionsReset this reducer back to its initial state. This is automatically called after delete but can be called other times as well.

Selectors

NameParametersDescription
selectSubmissionsname: string, state: objectSelect the list of submissions from the state.

License

Released under the MIT License.