Description
Form state manager written with mobx 💪 and typescript ❤️ which helps you to handle forms workflow.
-
Represents form state as a separate standalone store. Controlling the instance of the store you can manage the state from any point of your code.
-
Easy to test 🔧 as soon as it is a separate store.
-
Gives you a clear way 🚀 how to build and control forms with any complexity 🤓
-
Provides strict typescript checks for all fields including deep nested fields.
-
Provides clear and flexible client-side form validation flow. You don't need to use html attribute validation anymore. You are free to use any standalone validation library and test it separately. For the instance, consider this one 😎
-
Define a server-side validation error response with typescript. It forces you to unify all your API responses.
-
Allows you to add extra information to input fields (like hint or warning) and unify it within your forms.
-
Control the form's state. Like initial state or check if it's dirty.
Example
Check out the basic example here.
It uses fluent validation and gives you some the best practices to build forms.
How to use
In the example ☝️ we have code ideas how to build forms with the library. Let's review that example simplified it a bit.
Create form model
;
The form in the example relies on this model to check types:
this.form = new Form...;
And for the validation errors:
// Create validator based on Person model:// this._validationService.validate(model) method will return ValidationErrors<Person>.private _validator = this._validationService.create...;
You can see the example in BasicFormStore.ts
file 👇
Create form store
Form store is BasicFormStore.ts
. It links form store and validator.
It contains another helper methods like submit()
.
⭐️ Good to use along with AsyncOperationStore to handle submit operations.
-
Property
form
represents the form store instance -
Property
_validator
represents the instance ofBasicFormValidator
validator.
;;;;;;
Create validator
;;;;
Set up validator
Once validator is created you can set up a reaction to the form's value changes. Each time the value is changed validator will be applied.
reactionthis.form.value,,
Add form components
Form components is the third core idea. Form component is aware of specific form field store and know how to react on it.
In the giving example you can find TextField
form component.
It represents the basic text form field.
The form component should define
- How to render the error message from field store.
- How to render the form field's value.
- Process the change value event.
Let's see a simple example:
;;;;;;
The form consists of that fields
;;;;;
To submit the form just use store.submit()
method.
Installation
npm i ts-mobx-form-state
Code styles
Run npm run lint
.
Tests
Run npm run test
.
Roadmap
-
Get the stable release 1.0.0 ready.
-
Enhance typescript checking to get strict mode checking.