react-smart-form
A form component that containes all the functionality you will need.
Installation
yarn add react-smart-form #or npm install react-smart-form --save
Examples
Basic
;;; { console; console; } { return <Form onSubmit=thisonSubmit> <Input name="username" label="Username" /> <Input name="password" label="Password" type="password" /> <Submit> Login </Submit> </Form> ; } ;
Validation
;;;; /* * the react-smart-form asks if the field has error * so you must return false if it doesn't * or true (or a string message) if it does*/const numberCustomValidator = { console; console; } { return <Form onSubmit=thisonSubmit> <Input name="username" label="Username" validators= /> <Input name="password" label="Password" type="password" // silent validation validators= /> <Submit> Login </Submit> </Form> ; }} ;
Handle custom errors and loading state
;;; const fakeRequest = { return { ; } }; { console; console; // Returning a promise react-smart-form can handle loading state of the form. return ; } { return <Form onSubmit=thisonSubmit> <Input name="username" label="Username" /> <Input name="password" label="Password" type="password" /> <Submit> Login </Submit> </Form> ; } ;
Custom input custom errors and loading state
;;; let { const smartForm // include loading(bool):the loading state of the form, disabled(bool):the disabled state of the form children ...formPros } = props; return <div> <form ...formPros> children </form> </div> }; CustomForm=CustomForm; let { const smartForm // include error(string || bool): the error of the field label ...inputProps }=props; return <div> <label>Custom field: label</label> <input ...inputProps /> <div>smartFormerror</div> </div> }; CustomField=CustomField; let { const smartForm // include loading(bool):the loading state of the form, disabled(bool):the disabled state of the form ...buttonProps } = props; return <div> <button ...buttonProps disabled=smartFormdisabled> smartFormloading ? 'loading...':children </button> </div> }; CustomButton=; { return <CustomForm onSubmit=thisonSubmit> <CustomField name="email" label="Email" validators=/> <CustomButton>Press</CustomButton> </CustomForm> ; } ;
withValidator only
;;;; let { const error label ...inputProps } return <div> <label>Custom field: label</label> <input ...inputProps /> <div>error</div> </div> }; CustomField=; { return <form> <CustomField name="email" label="Email" validators=/> </form> ; } ;
Reset form
;;; { return <div> <Form formRef={ thisform=form; }> <Input name="username"/> </Form> <button onClick={thisform}>Reset Username</button> <button onClick={thisform}>Reset All</button> </div> ; } ;
Match password
;;;; { console; console; }
Documentation
Form
propType | required | default | description |
---|---|---|---|
onValidate(func(hasError)) | no | - | A function that runs every time the overall validation status change |
onSubmit(func(FieldValues)) | no | - | the function that handles the form submission |
onChange(func(FieldValues)) | no | - | A function that runs every time a field changes |
disabled(bool) | no | - | set the form in a disabled state |
loading(bool) | no | - | set the form in a loading state |
formRef(func) | no | - | returns the Form instance. With form instance you can user reset, setErrors and setValue functions |
Input
propType | required | default | description |
---|---|---|---|
type(input type or 'textarea') | no | - | The type of the Input. Takes all the valid Input type except file and submit. |
name(string)) | yes | - | The name of the input |
label(string) | no | - | The value of the label |
focusedLabel(string) | no | - | the value of the label when it is focused |
icon(react Component) | no | - | sets and icon in the right side of the input |
validators([func(value)]) | no | - | An array of functions that validates the inputs value. Should return false if the validation pass and string or true if it dont |
onChange(func(value)) | no | - | A function that runs every time the field changes |
focused(bool) | no | - | initialize the input in focus state |
showPassword(bool) | true | - | if the type of the field is password appends a icon button that toggles the input display state |
defaultValue(string) | no | - | sets a default value for the field |
inputRef(func) | no | - | returns the input reference |
debounce(number) | no | 300 | set the input's debounce for validation |
disabled:readOnly (bool) | no | - | set the input in a readOnly state |
Submit
propType | required | default | description |
---|---|---|---|
size(number) | no | 18 | - |
color(string) | no | '#44A8FF' | set the button color |
loading(bool) | no | - | set the button in a loading state |
disabled(bool) | no | - | set the form in a disabled state |
children(string) | no | - | The button label |
Validators
name | description |
---|---|
email(errorMessage) | checks if the input value is valid email |
length(errorMessage,{min:number,max:number}) | validates the value length |
required(errorMessage) | checks if a has been set |
Form instance function
name | description |
---|---|
reset('fieldName') | reset the specified field. If no field name, all fields will be reset. |
getValues({inputName:value}) | Returns the requested value. If no input name an object with all values will be returned |
setValues({inputName:value}) | Sets values to the specified fields |
setErrors({inputName:errorMessage}) | Sets errors to the specified fields |