vanilajs-form-lib
Version:
Reusable vanilla JS form library that works with React, Angular, Ionic, etc.
153 lines (111 loc) โข 3.53 kB
Markdown
# unvired-forms-lib
A lightweight, framework-agnostic JavaScript library for rendering [Form.io](https://form.io/) JSON schema-based forms inside an `<iframe>`. Works seamlessly in any modern web framework โ **React.js**, **Angular**, **Ionic**, or any **browser-based app**. Includes built-in support for geolocation, camera/file uploads, and barcode scanning, with easy integration via `postMessage` and custom DOM events.
## ๐ Features
- Renders Form.io JSON schema-based forms inside an `<iframe>`
- Automatically handles:
- ๐ Geolocation
- ๐ท Camera and file attachments
- ๐ฆ Barcode scanning
- Framework-agnostic: works in React, Angular, Ionic, Cordova, or any vanilla JS app
- Communication via `postMessage` and custom DOM events
- Simple integration and extensible event system
## ๐ฆ Installation
Clone the repository:
```bash
git clone <repo-url>
cd unvired-forms-lib
```
## ๐ Build the Library
If you make changes to the form rendering logic, rebuild the library:
```bash
npm install
npm run build
```
This will regenerate the `dist/unvired-formio-lib.js` file used in the iframe.
## ๐งฉ Usage
Embed the form using an `<iframe>` in your HTML/React/Angular/Ionic app:
```html
<iframe
id="form-frame"
src="/path/to/dist/unvired-formio-lib.js"
title="Form Iframe"
width="100%"
height="100%"
style="border: none;"
></iframe>
```
### Loading the Form Template
Send the form template to the iframe after it loads:
```js
const iframe = document.getElementById("form-frame");
const sendTemplate = () => {
iframe.contentWindow.postMessage(
{
type: "LOAD_FORM_TEMPLATE",
template: yourFormJsonTemplate,
semantic: yourSemanticData, // optional
},
"*"
);
};
iframe.addEventListener("load", sendTemplate);
```
### Handling Messages from the Form
Listen for submit, error, or navigation events:
```js
window.addEventListener("message", (event) => {
const data =
typeof event.data === "string" ? JSON.parse(event.data) : event.data;
switch (data?.type) {
case "FORM_BACK_NAVIGATION":
window.history.back();
break;
case "FORM_SUBMIT_RESULT":
console.log("โ
Submitted Data:", data.payload);
break;
case "FORM_SUBMIT":
console.log("๐ค Submit Triggered");
break;
case "FORM_SUBMIT_ERROR":
console.error("โ Submit Error:", data?.data);
break;
}
});
```
## ๐งฉ Event Constants
```js
const FORM_EVENTS = {
FORM_RENDER: "FORM_RENDER",
SAVE: "FORM_SAVE",
SUBMIT: "FORM_SUBMIT",
SUBMIT_ERROR: "FORM_SUBMIT_ERROR",
BACK_NAVIGATION: "FORM_BACK_NAVIGATION",
ONCHANGE: "FORM_ONCHANGE",
};
```
## ๐ Security Note
For production, replace `"*"` in `postMessage` calls with a specific target origin for enhanced security.
## ๐งช Compatibility
- โ
React.js (v17+)
- โ
Angular (v12+)
- โ
Ionic (v5+ with Capacitor)
- โ
Cordova WebView
- โ
Vanilla JS / HTML
## ๐ Folder Structure (Overview)
- `dist/` โ Compiled library output (use this in your app)
- `src/` โ Source code for the form renderer and supporting logic
- `example/` โ Example templates, assets, and demo HTML
- `scripts/` โ Build scripts and utilities
- `README.md` โ Project documentation
- `package.json` โ Project metadata and scripts
## ๐ License
[MIT](LICENSE)