useForm ⇒ Reactive Form
Reactive form management and input field validation hook
Provide it with a form model, flag input fields as required or add a value validation function with custom error messages. It validates the inputs as the user types, when there are no errors the form gets enabled for submission. On form submission, it triggers a callback function the user provides.
Requirements:
- ? Form model with optional validation function.
- ⚙️ Function to run after form validation and submission.
- ⚛️ React functional component with a form.
Install
- ? Tested using @testing-library/react-hooks.
- ?️ Built with rollup
and create-react-app.
Usage
Steps:
- create a form model:
- use as a hook in a functional react component:
Plain JSX example code
Material-UI example code
Options
useForm takes two params: formModel
and formSubmitCallback
, returns the rest.
Param | Type | Description |
---|---|---|
values | ValuesType |
returns form values state object |
errors | ErrorsType |
returns form errors state object |
handleOnChange | HandleOnChangeType |
binds to a HTMLInputElement: change event |
handleOnSubmit | HandleOnSubmitType |
binds to a HTMLFormElement: submit event |
isDisabled | boolean |
returns true / false when the form is valid / invalid |
isSubmitted | boolean |
returns true when the form was submitted without errors |
formModel | FormModelType |
initial form model with optional validation function |
formSubmitCallback | () => void |
function to run after form validation and submission |