@ascentcore/react-schema-form
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

@ascentcore/react-schema-form

React Forms based on JSON Schemas

NPM JavaScript Style Guide

Install

npm install --save @ascentcore/react-schema-form

Usage

Having a schema defined (basic-schema.json):

{
    "$id": "https://example.com/person.schema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "title": "Person",
    "description": "Person Information",
    "required": ["firstName", "lastName", "type", "phoneNumbers"],
    "properties": {
        "firstName": {
            "type": "string",
            "title": "First Name",
            "description": "The person's first name."
        },
        "lastName": {
            "type": "string",
            "title": "Last Name",
            "description": "The person's last name."
        },
        "age": {
            "title": "Age",
            "description": "Age in years which must be equal to or greater than zero.",
            "type": "integer",
            "minimum": 10
        },
        "type": {
            "title": "Type",
            "type": "string",
            "enum": ["NW", "NE", "SW", "SE"]
        },
        "agree": {
            "title": "Agree with TOC",
            "type": "boolean",
            "default": false
        },
        "phoneNumbers": {
            "title": "Phone numbers",
            "type": "array",
            "items": {
                "title": "Phone number",
                "type": "string",
                "pattern": "^[0-9]+$"
            }
        }
    }
}

Example of form generation usage based on schema:

import React from 'react'
import schema from './basic-schema.json'
import { SchemaForm } from '@ascentcore/react-schema-form'

export default function BasicSchemaExample() {

    function onSubmit(data) {
        console.log(data)
    }

    const data = {
        firstName: 'Defined Value'
    }

    return (<SchemaForm schema={schema} onSubmit={onSubmit} data={data} />)

}

Development

Library dev building:

npm install
npm run start

Examples dev building:

cd example
npm install
npm run start

License

MIT

Dependents (0)

Package Sidebar

Install

npm i @ascentcore/react-schema-form

Weekly Downloads

2

Version

0.2.1

License

MIT

Unpacked Size

267 kB

Total Files

37

Last publish

Collaborators

  • cstefanache
  • adamcorina