1.0.9 • Published 11 months ago

schema-form-component v1.0.9

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

schema-form-component

This component will render fields dynamically based on openApi schema JSON. Default field validation with ajv is also provided within this package.

Install

  npm install schema-form-component

Usage

Schema Example:

const userSchema ={
  "properties": {
     "id":{
       "type":"string",
       "readOnly": true
     },
    "name": {
      "type": "string", 
      "minLength": 3
    },
    "dob": {
      "type": "string",
      "format": "date"
    },
    "address": {
      "type": "string",
      "maxLength": 250
    }
  },
  "required":["name"]
} 
    import React from 'react';
    import { SchemaForm, IFieldConfig } from "schema-form-component";

    const config:{[keyName: string]: IFieldConfig} ={
        "id":{
            hidden:true
        },
        "dob":{
            title:"Date of Birth"
        }
    }
    
    interface IUserDetailType{}
    
    const Form=()=>{
        const [userDetail,setUserDetail]= useState();
        const [errorMessages, setErrorMessages] = React.useState<TSchemaFormErrors>();
        
        const onChangeHandler= React.useCallback(
            (value:IUserDetailType , key:string)=>{
                setUserDetail(value);
                if (errorMessages) {
                    // do somehting
                }
        },[errorMessages])
        return <Form>
            <SchemaForm
                formTitle={"User Details"}
                schema={userSchema}
                onInputChange={onChangeHandler}
                value={userDetail}
                errorMessages={errorMessages}
                config={config}
            />
        </Form>
    }
    

Component Props

Propdescription
schemaschemaObject to be rendered as a set of fields(example openapi schema).
valueObject, field value will depend on the value of the property of the object.
onInputChangechange handler and will be triggered when typing in the input field. (value: T, key: string) => void.
errorMessagesList of error messages to be shown specifically under the field.
configcustom UI config {keyName: string: IFieldConfig;}.
formTitleProvide a title for a form
formButtonReact Node type and it will render a buttons at the end of fields.
disableFieldsdisable fields conditionally. (boolean)

Config

you can import the type of config from the IFieldConfig.

   const config: { [keyName: string]: IFieldConfig } = {}