Formbit is a lightweight React state form library designed to simplify form management within your applications. With Formbit, you can easily handle form state, validate user input, and submit data efficiently.
- Features
- Abstract
- Install
- Getting started
- Local Development
- Type Aliases
- Check
- CheckFnOptions
- ClearIsDirty
- ErrorCallback
- ErrorCheckCallback
- ErrorFn
- Errors
- Form
- FormbitObject
- GenericCallback
- InitialValues
- Initialize
- IsDirty
- IsFormInvalid
- IsFormValid
- LiveValidation
- LiveValidationFn
- Object
- Remove
- RemoveAll
- ResetForm
- SetError
- SetSchema
- SubmitForm
- SuccessCallback
- SuccessCheckCallback
- SuccessSubmitCallback
- Validate
- ValidateAll
- ValidateFnOptions
- ValidateForm
- ValidateOptions
- ValidationError
- ValidationFormbitError
- ValidationSchema
- Write
- WriteAll
- WriteAllValue
- WriteFnOptions
- Writer
- License
- Intuitive and easy-to-use form state management.
- Out of the box support for validation with yup.
- Support for handling complex forms with dynamic and nested fields.
- Seamless and flexible integration with React.
- Full Typescript support.
The concept behind Formbit is to offer a lightweight library that assists you in managing all aspects of form state and error handling, while allowing you the flexibility to choose how to design the UI. It seamlessly integrates with various UI frameworks such as Antd, MaterialUI, or even plain HTML, as it provides solely a React hook that exposes methods to manage the form's React state. The responsibility of designing the UI remains entirely yours.
npm install --save formbit
yarn add formbit
import * as yup from 'yup';
import useFormbit from 'formbit';
const initialValues = { name: undefined, age: undefined };
const schema = yup.object().shape({
name: yup
.string()
.max(25, 'Name max length is 25 characters')
.required('Name is required'),
age: yup
.number()
.max(120, 'Age must be between 0 and 120')
.required('Age is required')
});
function Example() {
const { form, submitForm, write, error, isFormInvalid } = useFormbit({
initialValues,
yup: schema
});
const handleChangeName = ({ target: { value } }) => { write('name', value); }
const handleChangeAge = ({ target: { value } }) => { write('age', value); }
const handleSubmit = () => {
submitForm(
(writer) => {
console.log("Your validated form is: ", writer.form)
},
(writer) => {
console.error("The validation errors are: ", writer.errors)
});
}
return (
<div>
<label name="name">Name</label>
<input
name="name"
onChange={handleChangeName}
type="text"
value={form.name}
/>
<div>{error('name')}</div>
<label name="age">Age</label>
<input
name="age"
onChange={handleChangeAge}
type="number"
value={form.age}
/>
<div>{error('age')}</div>
<button disabled={isFormInvalid()} onClick={handleSubmit} type="button">
Submit
</button>
</div>
);
}
export default Example;
For local development we suggest using Yalc to test your local version of formbit in your projects.
Ƭ Check<Values
>: (json
: Form
, options?
: CheckFnOptions
<Values
>) => ValidationError
[] | undefined
Checks the given json against the form schema and returns and array of errors. It returns undefined if the json is valid.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (json
, options?
): ValidationError
[] | undefined
Name | Type |
---|---|
json |
Form |
options? |
CheckFnOptions <Values > |
ValidationError
[] | undefined
Ƭ CheckFnOptions<Values
>: Object
Options object to change the behavior of the check method
Name | Type |
---|---|
Values |
extends InitialValues |
Name | Type |
---|---|
errorCallback? |
ErrorCheckCallback <Values > |
options? |
ValidateOptions |
successCallback? |
SuccessCheckCallback <Values > |
Ƭ ClearIsDirty: () => void
Reset isDirty value to false
▸ (): void
void
Ƭ ErrorCallback<Values
>: (writer
: Writer
<Values
>, setError
: SetError
) => void
Invoked in case of errors raised by validation
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (writer
, setError
): void
Name | Type |
---|---|
writer |
Writer <Values > |
setError |
SetError |
void
Ƭ ErrorCheckCallback<Values
>: (json
: Form
, inner
: ValidationError
[], writer
: Writer
<Values
>, setError
: SetError
) => void
Invoked in case of errors raised by validation of check method
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (json
, inner
, writer
, setError
): void
Name | Type |
---|---|
json |
Form |
inner |
ValidationError [] |
writer |
Writer <Values > |
setError |
SetError |
void
Ƭ ErrorFn: (path
: string
) => string
| undefined
Returns the error message for the given path if any. It doesn't trigger any validation
▸ (path
): string
| undefined
Name | Type |
---|---|
path |
string |
string
| undefined
Ƭ Errors: Record
<string
, string
>
Object including all the registered errors messages since the last validation. Errors are stored using the same path of the corresponding form values.
Example
If the form object has this structure:
{
"age": 1
}
and age is a non valid field, errors object will look like this
{
"age": "Age must be greater then 18"
}
Ƭ Form: { __metadata?
: Object
} & Object
Object containing the updated form
Ƭ FormbitObject<Values
>: Object
Object returned by useFormbit() and useFormbitContextHook() It contains all the data and methods needed to handle the form.
Name | Type |
---|---|
Values |
extends InitialValues |
Name | Type | Description |
---|---|---|
check |
Check <Partial <Values >> |
Checks the given json against the form schema and returns and array of errors. It returns undefined if the json is valid. |
error |
ErrorFn |
Returns the error message for the given path if any. It doesn't trigger any validation |
errors |
Errors |
Object including all the registered errors messages since the last validation. Errors are stored using the same path of the corresponding form values. Example If the form object has this structure: json { "age": 1 } and age is a non valid field, errors object will look like this json { "age": "Age must be greater then 18" } |
form |
Partial <Values > |
Object containing the updated form |
initialize |
Initialize <Values > |
Initialize the form with new initial values |
isDirty |
boolean |
Returns true if the form is Dirty (user already interacted with the form), false otherwise. |
isFormInvalid |
IsFormInvalid |
Returns true if the form is NOT valid It doesn't perform any validation, it checks if any errors are present |
isFormValid |
IsFormValid |
Returns true id the form is valid It doesn't perform any validation, it checks if any errors are present |
liveValidation |
LiveValidationFn |
Returns true if live validation is active for the given path |
remove |
Remove <Values > |
This method updates the form state deleting value, setting isDirty to true. After writing, it validates all the paths contained into pathsToValidate (if any) and all the fields that have the live validation active. |
removeAll |
RemoveAll <Values > |
This method updates the form state deleting multiple values, setting isDirty to true. |
resetForm |
ResetForm |
Reset form to the initial state. Errors and liveValidation are set back to empty objects. isDirty is set back to false |
setError |
SetError |
Set a message(value) to the given error path. |
setSchema |
SetSchema <Values > |
Override the current schema with the given one. |
submitForm |
SubmitForm <Values > |
Perform a validation against the current form object, and execute the successCallback if the validation pass otherwise it executes the errorCallback |
validate |
Validate <Values > |
This method only validate the specified path. Do not check for fields that have the live validation active. |
validateAll |
ValidateAll <Values > |
This method only validate the specified paths. Do not check for fields that have the live validation active. |
validateForm |
ValidateForm <Partial <Values >> |
This method validates the entire form and set the corresponding errors if any. |
write |
Write <Values > |
This method update the form state writing $value into the $path, setting isDirty to true. After writing, it validates all the paths contained into $pathsToValidate (if any) and all the fields that have the live validation active. |
writeAll |
WriteAll <Values > |
This method takes an array of [path, value] and update the form state writing all those values into the specified paths. It set isDirty to true. After writing, it validate all the paths contained into $pathToValidate and all the fields that have the live validation active. |
Ƭ GenericCallback<Values
>: SuccessCallback
<Values
> | ErrorCallback
<Values
>
Name | Type |
---|---|
Values |
extends InitialValues |
Ƭ InitialValues: { __metadata?
: Object
} & Object
InitialValues used to setup formbit, used also to reset the form to the original version.
Ƭ Initialize<Values
>: (values
: Partial
<Values
>) => void
Initialize the form with new initial values
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (values
): void
Name | Type |
---|---|
values |
Partial <Values > |
void
Ƭ IsDirty: boolean
Returns true if the form is Dirty (user already interacted with the form), false otherwise.
Ƭ IsFormInvalid: () => boolean
Returns true if the form is NOT valid It doesn't perform any validation, it checks if any errors are present
▸ (): boolean
boolean
Ƭ IsFormValid: () => boolean
Returns true id the form is valid It doesn't perform any validation, it checks if any errors are present
▸ (): boolean
boolean
Ƭ LiveValidation: Record
<string
, true
>
Object including all the values that are being live validated. Usually fields that fail validation (using one of the method that triggers validation) will automatically set to be live-validated.
A value/path is live-validated when validated at every change of the form.
By default no field is live-validated
Example
If the form object has this structure:
{
"age": 1
}
and age is a field that is being live-validated, liveValidation object will look like this
{
"age": "Age must be greater then 18"
}
Ƭ LiveValidationFn: (path
: string
) => true
| undefined
Returns true if live validation is active for the given path
▸ (path
): true
| undefined
Name | Type |
---|---|
path |
string |
true
| undefined
Ƭ Object: Record
<string
, unknown
>
Generic object with string as keys
Ƭ Remove<Values
>: (path
: string
, options?
: WriteFnOptions
<Values
>) => void
This method updates the form state deleting value and set isDirty to true.
After writing, it validates all the paths contained into pathsToValidate (if any) and all the fields that have the live validation active.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (path
, options?
): void
Name | Type |
---|---|
path |
string |
options? |
WriteFnOptions <Values > |
void
Ƭ RemoveAll<Values
>: (arr
: string
[], options?
: WriteFnOptions
<Values
>) => void
This method updates the form state deleting multiple values, setting isDirty to true.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (arr
, options?
): void
Name | Type |
---|---|
arr |
string [] |
options? |
WriteFnOptions <Values > |
void
Ƭ ResetForm: () => void
Reset form to the initial state. Errors and liveValidation are set back to empty objects. isDirty is set back to false
▸ (): void
void
Ƭ SetError: (path
: string
, value
: string
) => void
Set a message(value) to the given error path.
▸ (path
, value
): void
Name | Type |
---|---|
path |
string |
value |
string |
void
Ƭ SetSchema<Values
>: (newSchema
: ValidationSchema
<Values
>) => void
Override the current schema with the given one.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (newSchema
): void
Name | Type |
---|---|
newSchema |
ValidationSchema <Values > |
void
Ƭ SubmitForm<Values
>: (successCallback
: SuccessSubmitCallback
<Values
>, errorCallback?
: ErrorCallback
<Partial
<Values
>>, options?
: ValidateOptions
) => void
Perform a validation against the current form object, and execute the successCallback if the validation pass otherwise it executes the errorCallback
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (successCallback
, errorCallback?
, options?
): void
Name | Type |
---|---|
successCallback |
SuccessSubmitCallback <Values > |
errorCallback? |
ErrorCallback <Partial <Values >> |
options? |
ValidateOptions |
void
Ƭ SuccessCallback<Values
>: (writer
: Writer
<Values
>, setError
: SetError
) => void
Success callback invoked by some formbit methods when the operation is successful.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (writer
, setError
): void
Name | Type |
---|---|
writer |
Writer <Values > |
setError |
SetError |
void
Ƭ SuccessCheckCallback<Values
>: (json
: Form
, writer
: Writer
<Values
>, setError
: SetError
) => void
Success callback invoked by the check method when the operation is successful.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (json
, writer
, setError
): void
Name | Type |
---|---|
json |
Form |
writer |
Writer <Values > |
setError |
SetError |
void
Ƭ SuccessSubmitCallback<Values
>: (writer
: Writer
<Values
| Omit
<Values
, "__metadata"
>>, setError
: SetError
, clearIsDirty
: ClearIsDirty
) => void
Success callback invoked by the submit method when the validation is successful. Is the right place to send your data to the backend.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (writer
, setError
, clearIsDirty
): void
Name | Type |
---|---|
writer |
Writer <Values | Omit <Values , "__metadata" >> |
setError |
SetError |
clearIsDirty |
ClearIsDirty |
void
Ƭ Validate<Values
>: (path
: string
, options?
: ValidateFnOptions
<Values
>) => void
This method only validate the specified path. Do not check for fields that have the live validation active.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (path
, options?
): void
Name | Type |
---|---|
path |
string |
options? |
ValidateFnOptions <Values > |
void
Ƭ ValidateAll<Values
>: (paths
: string
[], options?
: ValidateFnOptions
<Values
>) => void
This method only validate the specified paths. Do not check for fields that have the live validation active.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (paths
, options?
): void
Name | Type |
---|---|
paths |
string [] |
options? |
ValidateFnOptions <Values > |
void
Ƭ ValidateFnOptions<Values
>: Object
Options object to change the behavior of the validate methods
Name | Type |
---|---|
Values |
extends InitialValues |
Name | Type |
---|---|
errorCallback? |
ErrorCallback <Partial <Values >> |
options? |
ValidateOptions |
successCallback? |
SuccessCallback <Partial <Values >> |
Ƭ ValidateForm<Values
>: (successCallback?
: SuccessCallback
<Values
>, errorCallback?
: ErrorCallback
<Values
>, options?
: ValidateOptions
) => void
This method validates the entire form and set the corresponding errors if any.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (successCallback?
, errorCallback?
, options?
): void
Name | Type |
---|---|
successCallback? |
SuccessCallback <Values > |
errorCallback? |
ErrorCallback <Values > |
options? |
ValidateOptions |
void
Ƭ ValidateOptions: YupValidateOptions
Type imported from the yup library. It represents the object with all the options that can be passed to the internal yup validation method,
Link to the Yup documentation https://github.com/jquense/yup
Ƭ ValidationError: YupValidationError
Type imported from the yup library. It represents the error object returned when a validation fails
Link to the Yup documentation https://github.com/jquense/yup
Ƭ ValidationFormbitError: Pick
<ValidationError
, "message"
| "path"
>
Ƭ ValidationSchema<Values
>: ObjectSchema
<Values
>
Type imported from the yup library. It represents any validation schema created with the yup.object() method
Link to the Yup documentation https://github.com/jquense/yup
Name | Type |
---|---|
Values |
extends InitialValues |
Ƭ Write<Values
>: (path
: keyof Values
| string
, value
: unknown
, options?
: WriteFnOptions
<Values
>) => void
This method update the form state writing $value into the $path, setting isDirty to true.
After writing, it validates all the paths contained into $pathsToValidate (if any) and all the fields that have the live validation active.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (path
, value
, options?
): void
Name | Type |
---|---|
path |
keyof Values | string |
value |
unknown |
options? |
WriteFnOptions <Values > |
void
Ƭ WriteAll<Values
>: (arr
: WriteAllValue
<Values
>[], options?
: WriteFnOptions
<Values
>) => void
This method takes an array of [path, value] and update the form state writing all those values into the specified paths.
It set isDirty to true.
After writing, it validate all the paths contained into $pathToValidate and all the fields that have the live validation active.
Name | Type |
---|---|
Values |
extends InitialValues |
▸ (arr
, options?
): void
Name | Type |
---|---|
arr |
WriteAllValue <Values >[] |
options? |
WriteFnOptions <Values > |
void
Ƭ WriteAllValue<Values
>: [keyof Values
| string
, unknown
]
Tuple of [key, value] pair.
Name | Type |
---|---|
Values |
extends InitialValues |
Ƭ WriteFnOptions<Values
>: { noLiveValidation?
: boolean
; pathsToValidate?
: string
[] } & ValidateFnOptions
<Values
>
Options object to change the behavior of the write methods
Name | Type |
---|---|
Values |
extends InitialValues |
Ƭ Writer<Values
>: Object
Internal form state storing all the data of the form (except the validation schema)
Name | Type |
---|---|
Values |
extends InitialValues |
Name | Type |
---|---|
errors |
Errors |
form |
Values |
initialValues |
Values |
isDirty |
IsDirty |
liveValidation |
LiveValidation |
MIT © [Radicalbit (https://github.com/radicalbit)]