This library exports a form viewer for viewing and submitting forms. Use our editor to create and edit forms.
npm install @bpmn-io/form-js-viewer
import { Form } from '@bpmn-io/form-js-viewer';
const schema = {
type: 'default',
components: [
{
key: 'creditor',
label: 'Creditor',
type: 'textfield',
validate: {
required: true,
},
},
],
};
const data = {
creditor: 'John Doe Company',
};
const form = new Form({
container: document.querySelector('#form'),
});
await form.importSchema(schema, data);
// add event listeners
form.on('submit', (event) => {
console.log('Form <submit>', event);
});
// provide a priority to event listeners
form.on('changed', 500, (event) => {
console.log('Form <changed>', event);
});
Check out a full example.
For proper styling include the form-js.css
stylesheet and font used:
<link
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,600;1,400&display=swap"
rel="stylesheet" />
<link href="https://unpkg.com/@bpmn-io/form-js/dist/assets/form-js.css" rel="stylesheet" />
Create a new form with options { container?: HTMLElement }
.
import { Form } from '@bpmn-io/form-js-viewer';
const form = new Form({
container: document.querySelector('#form'),
});
Display a form represented via a form schema and the optional data.
try {
await form.importSchema(schema);
} catch (err) {
console.log('importing form failed', err);
}
Submit a form programatically.
const { data, errors } = form.submit();
if (Object.keys(errors).length) {
console.error('Form submitted with errors', errors);
}
Validate a form programatically.
const errors = form.validate();
if (Object.keys(errors).length) {
console.error('Form has errors', errors);
}
Reset a form programatically.
Set a form property such as readOnly
.
Attach the form to a parent node.
Detach the form from its parent node.
Subscribe to an event.
Remove form from the document.
Fired off every time there is a form state change.
Fired off on form submission.
Fired whenever a schema has finished importing, whether it succeeds or fails.
form.layoutCleared
form.layoutCalculated :: { rows }
detach
attach
form.init
form.clear
form.destroy
diagram.clear
diagram.destroy
formField.add :: { formField }
formField.remove :: { formField }
formField.focus :: { formField }
formField.blur :: { formField }
formField.search :: { formField, value }
Use under the terms of the bpmn.io license.