react-build-form
v1.3.15
Published
Make dynamic forms. It uses antd ui library, formik, yup, dayjs
Downloads
118
Maintainers
Readme
What is this?
Build dynamic , complex forms more easy.
Requirements.
For components - ANTD ui library, Form - formik, Validation - Yup, Bootstrap for grid system (It will be removed next updates)
Installation
npm i react-build-form
add : @import "bootstrap/scss/bootstrap"
to your scss file
Usage
```
import {FormBuilder} from "react-build-form"
export default function YourComponent(){
function doSmth(values){
....
}
return <FormBuilder inputScheme={[
{
label:'Username',
field:'login',
type:"text",
col:"col-12",
initialValue:null,
validation:Yup.string().required(),
required:true,
props:{
placeholder:'Username or email',
size:'large',
prefix:<UsernameIcon/>,
style:{
padding:"12px"
}
}
}
]}
onSubmit={(values)=>doSmth(values)}
button={{
title:"Verify",
props: {
size:'large',
style:{
width:"100%",
backgroundColor:'#7F56D9',
color:'white',
padding:"14px",
height:"auto"
},
className:'mb-3'
}
}} />
}
```
Explanation
inputScheme
- label : label for Input
- field : write value's key (Above example values is {login:'any value you wrote'})
- type : there is 9 types:
- text : it renders simple Input component in ANTD
- text-area : it renders TextArea component in ANTD
- selection : it renders Select component in ANTD
- tree-select : it renders Tree-select component in ANTD
- switch : it renders switch component in ANTD
- date : it renders Date component in ANTD
- range : it renders Range component (Date component in range) in ANTD
- upload : it renders Upload component in ANTD and all uploaded files store in key
- custom : any custom component you want can render
- container type : responsible for grouping inputs in one component :
{ type:'container', component:(nodesComingFromSchema)=><YourContainerComponent>{nodesComingFromSchema}</YourContainerComponent>, schema:[ { type:'text', field:'test', initialValue:null, validation:Yup.string(), col:'col-12', } ] },
then test input will placed in YourContainerComponent
- col : it is bootstap column clases for (col-1 , col-md-1 , etc.)
- initialValue : it is initial value of form field
- validation : only accept yup validation schema
- required: this field is responsible red star, if required you can easily add red star
- props : it has all types of ANTD component props
- linear : default value is false | if true then input field items ordered horizontally
- showOn : receives array , first index of array is null or string[] (other fields name which depends on this fields) ,second index is callback that return boolen if true then shows othwerwise not shown
[{
field:"other",
initialValue:null,
validation:Yup.string().nullable(),
col: 'col-12 col-md-6 col-lg-4',
type:'text',
},
{
field:"parent",
initialValue:null,
validation:Yup.string().nullable(),
col: 'col-12 col-md-6 col-lg-4',
type:'text',
},
{
field:'children',
label:"Təsdiq edən şəxs",
type:"text",
required:true,
initialValue:null,
validation:Yup.number().nullable(),
col:'col-12 col-md-6 col-lg-4',
showOn:[['parent'],(values)=>{
return values[0] === condition
}],
},]
meaning : children input only depends on parent and values is array ([parent]) first index is parents value, if you want depends on both parent and other
{
field:'children',
label:"Təsdiq edən şəxs",
type:"text",
required:true,
initialValue:null,
validation:Yup.number().nullable(),
col:'col-12 col-md-6 col-lg-4',
showOn:[['parent','other'],([parentVal,otherVal])=>{
return ...your condition
}],
}
- calculateValue : it means calculate inputs value depends on other inputs value
[
{
field:"parent",
initialValue:null,
validation:Yup.string().nullable(),
col: 'col-12 col-md-6 col-lg-4',
type:'text',
},
{
field:'children',
label:"Təsdiq edən şəxs",
type:"text",
required:true,
initialValue:null,
validation:Yup.number().nullable(),
col:'col-12 col-md-6 col-lg-4',
calculateValue:[['parent'],([parentValue])=>{
return parentValue + ' world '
}],
},]
```
meaning if parents value is (hello) then children value is 'hello world'
- onSubmit : this is function called after submit button click and all field is valid
- button :
- title : button text
- props: similar to ANTD button props
- customButtons : you can add more buttons to form and receives array
values is : form's values customSubmit : validates form if valid return true otherwise falsecustomButtons={[ (values, customSubmit)=>{ return <Button>Second button</Button> } ]}