markojs-form
v1.0.5
Published
Form Component Handler Library for MarkoJS
Downloads
14
Readme
@MarkoJS Form Handler
Form Component Handler Library for MarkoJS. This library simplify how to handle the flow within a MarkoJS form component by handling all input fields events, auto-fill, auto-saving, auto-completition, validation and the form submission by binding to the this
of the component. Enough talking, let get to the facts.
Installation
Install using npm
$ npm install markojs-form
Install using yarn
$ yarn add markojs-form
Usage
Create new instance of the FormHandler Object Class.
const FormHandler = require('markojs-form');
// or
import FormHandler from 'markojs-form';
static const options = {
key: 'form-id',
autosave: true,
crosscheck: true
}
static const fhandler = new FormHandler( options );
// or inside a class method
class {
onCreate(){
// ...
this.fhandler = new FormHandler( options )
}
}
Important: Avoid using form-handler instance globally.
Options
All options are optional
- key:
String
Same as the component tag key of the form being handled: Eg.<form key="form-id">...</form>
- autosave:
Boolean
Define whether to auto-save input values when the user is filling the form. Very useful especially in SPA case when the user manually or accidentally refresh the page, the inputs will stay filled with the saved values. Defaultfalse
- crosscheck:
Boolean
When submit form event is detected by FormHandler, it run a final validation cross-check through all inputs that require validation in the form before submitting it. Default:false
Methods
bind(this, [formObject])
Bind the form handler to the component. formObject
is a object passed to the handler as the initial form fields & values: It's optional
class {
onCreate(){
this.state = {
// ...
}
fhandler.bind(this)
}
}
When this is done, the handler add form
& formError
fields to the component state: Eg. this.state.form
. Those contain respectively the form data and the error status of the form validation.
Also, couple of methods are automatically bind to the component's class, to listen and handle events that occured when interacting with the form.
__onInput
Method handler for input event__onChange
Method handler for input change event__onSelect
Method handler for select input change event__onChecked
Method handler for checkbox and radio input events
All these methods assigned approprietly to the various form tags, empowers the FormHandler to collect and update the data and errors state of the form component.
Example
When filling the input form below, FormHandler automatically update this.state.form.name
at every input & change events, allowing the component state to be updated.
<input type="text"
name="name"
value=state.form.name
on-input("__onInput")
on-change("__onChange")>
Note: You can still assign your own method listener to handle inputs yourself. All is up to you.
unbind(this)
Unbind the form handler to the component.
set(name, value)
Manually set a value to a form input.
class {
// ...
onMount(){
fhandler.set('fullname', 'John Dupont')
}
}
fill(values)
Beside Set()
method which can only set one field at a time, fill()
method get an object of multiple fields & values to add manually to the form.
class {
// ...
onInput(){
fhandler.fill({ status: 'healty', age: 79, gender: 'female' })
}
}
error(name, [status])
Manually define the status of error of a given input. status
can be a simple boolean: true
meaning there's an error and false
to cancel error. status
can also be a string text message describing the error: Very usefull to display dynamic error messages to the user.
class {
// ...
onSubmit(){
fhandler.error('age', 'This field is required')
}
onDestry(){
// Cancel the error
fhandler.error('age', false)
}
}
<div>
<input type="number"
name="age"
value=state.form.age
on-change("__onChange")>
<!-- Dislay error message -->
<if( state.formError.age )>
<p>${state.formError.age || 'Your custom error'}</p>
</if>
</div>
reset()
Set the form back to its initial state. This method is automatilly call when the form get submited. It's also recommanded to call it when the form component onDestroy
event occurs: Proper clean up
class {
onCreate(){
const initialForm = { username: 'gremdol', email: '[email protected]' }
fhandler.bind(this, initialForm)
}
onDestroy(){
// Reset the form.
fhandler.reset()
}
}
alert(message, status)
Display general alert of the form component
class {
onSubmit(){
fhandler.alert('Unexpected Error Occured! Try again', 'warning')
}
}
<FormAlert alert=state.alert/>
Form Validation
FormHandler has couple of in-build validation rules that you can use.
required
: Specify that the input field is requiredurl
: Check whether the input value is a valid URLdomain
: Check whether the input value is a valid Domain/Hostnameemail
: Check whether the input value is a valid email addressphone
: Check whether the input value is a valid phone numberfullname
: Check whether the input value is a valid user fullnamepassword
: check and return password by strenghnumber
: Input value must be a valid numberboolean
: Input value must be True or Falsearray
: Check whether the input value is an Array: Usually set manuallyobject
: Check whether the input value is an Object: Usually set manuallylowercase
: Input value must be only a lowercase stringuppercase
: Input value must be onlye an uppercase stringlength
: Specify the required length of the input valueminLength
: Specify the minimun length of the input valuemaxLength
: Specify the maximum length of the input value
define(rule)
Define your own custom validation rules or overwrite in-build rules. There are 2 ways to define a validation rules:
statically using static or $
or inside the component's class methods or right into the template:
Examples
class {
onCreate(){
fhandler.define([{ name: 'custom1', regexp: '/\.+/' }, { name: 'custom2', regexp: '/\w+/' }])
}
}
<div>
<!-- Use inbuild rules -->
<input type="email"
name="email"
value=state.form.email
validate="email"
on-change("__onChange")>
<!-- Use an already defined custom rules -->
<input type="text"
name="name"
value=state.form.name
validate="custom1"
on-change("__onChange")>
<!-- Define and assign custom rules right way -->
<input type="text"
name="orderId"
value=state.form.orderId
validate=fhandler.define([{ name: 'orderId', regexp: '/PT-(\b+{4,})-00/' }])
on-change("__onChange")>
</div>
validate(rule, value)
Manually validate a form field.
class {
// ...
onCustomHandler( value ){
const isValid = fhandler.validate( 'email', value )
// ...
}
}
Events
FormHandler instanciate EventEmitter Object. Here a couple of events triggered by the form handler:
bind
: When FormHandler get bind to the componentunbind
: When FormHandler get unbind to the componentinput
: New form inputerror
: When an error occuredfill
: Whenfill()
method get calledautosave
: When new auto-save record occuredreset
: When form get reset
on(event, fn)
Register an event listener
off(event, fn)
Remove an event listener
removeListeners()
Remove all event listeners
Voilà!
Feedback & Contribution
You know the say: No one is whole alone! So, feedbacks and the smallest contributions you can think of are all welcome. Kindly report any encounted Issues here and I'll be glad to work on it right away. Thank you.
License
This software is free to use under the MIT license. See the LICENSE file for license text and copyright information.