make a react form faster by Object
In my opinion, It takes a lot of time on dealing with make pairs with <label>
& <input>
when creating a form setting the id & htmlFor.
Moreover, usually, the pairs would share the same className or style.
so, why not use a tool to deal with them.
And you still can set className, style and so on for any pairs.
npm i react-form-maker
import React from 'react'
import { ReactFormMaker, getReactFormData } from 'react-form-maker'
const formClassName = {
formGroupClassName: 'form-group',
labelClassName: '',
inputClassName: 'form-control'
}
const formItems = [
{ id: 'name', label: 'Name:', type: 'text', defaultValue: 'test' },
{ id: 'account' },
{ id: 'phone', type: 'number' },
{ id: 'birthday', type: 'date' },
{
type: 'radio',
name: 'framework',
radioClassName: 'form-check-input',
radioGroupClassName: 'form-check',
radioLabelClassName: 'form-check-label',
options: [
{ value: 'React', defaultChecked: true },
'Angular',
{ value: 'Vue', text: 'Vue.js' }
]
},
{
id: 'gender',
type: 'select',
formGroupClassName: 'form-group mb-5',
options: [{ value: 'male', text: 'Male' }, 'female']
},
'test1',
'test2',
{ type: 'div', className: 'm-5', child: <hr /> },
{
value: 'Submit',
type: 'submit',
className: 'btn btn-primary'
}
]
const App = () => {
return (
<ReactFormMaker
className='container'
header={<h1>react-form-maker demo</h1>}
formItems={formItems}
formClassName={formClassName}
onSubmit={(event) => {
event.preventDefault()
const data = getReactFormData(formItems)
console.log(data)
}}
/>
)
}
export default App
Bootstrap is used for demo, however, this module don't dependency on Bootstrap. It's NOT necessary to use Bootstrap.
for most of the <label>
& <input>
pair of formItem would be like:
<div>
<label></label>
<input />
</div>
the div tag would be called "formGroup"
if you use button or submit, there would be no div of formGroup.
formGroupClassName: string (className of the formGroup) labelClassName:string (className of the label) inputClassName:string (className of the input)
formGroupStyle: Object (style of the formGroup) labelStyle: Object (style of the label) inputStyle: Object (style of the input)
Ps.The way to write the ClassName and style is the way you write in JSX
For more detail, please read the next section.
Add any content before the formItems by JSX.
Add any content after the formItems by JSX.
Those Would be the attributes of <form>
For item in array of formItems, use type of object or string. If it's a string, it would be equal to {id:'string'}
This is the id of the <input>
and the htmlFor of the <label>
The text in the <label></label>
The default is the defined "id"
Ps.The way to write the ClassName and style is the way you write in JSX. "className" or "style" in formItems have priority than in the formClassName or formStyle.
Those Would be the attributes of <input>
Use (options : array) to create list of <option>
.
In options use Object or string to create each <option>
, which contain key value & text.
If type of string is used, that would be equal to {value:'string'}
Key of 'text' would be the text in <option></option>
, the default is the defined "value"
Use (options : array) to create list of following components.
If type of string is used, that would be equal to {value:'string'}
.
<div>
<input type='radio' />
<label>{text}</label>
</div>
Like inputGroup, the outer <div>
would be radioGroup.
Use property "radioGroupClassName" or "radioGroupStyle" to change the radioGroup style.
Use property "radioClassName" or "radioStyle" to change the radio style.
Use property "radioLabelClassName" or "radioLabelStyle" to change the label style.
"text" property is the text of the label, the default is the defined "value". Like inputGroup, "id" would be used to the id of input and the htmlFor of label, the default is also the defined "value" . Other properties would be the attributes of input, such as "defaultChecked:true"
If you use button or submit, there would be no div of formGroup. Simply use "className" or "style" to change the style.
Create <div></div>
,and anything could be insert into it by property "child".
Any other property would be the attributes of the div.
This function could get the date of <ReactFormMaker />
form.
The "formItems" should be the formItems used by <ReactFormMaker />
.
The return is Object with key: value pair. If there is a name in formItem, the key would be the "name" property. Otherwise, the key would be the "id" property.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
MIT © ronny1020