FormBase
stapp-formbase module handles almost everything that you may need to build a form application.
Installation
npm install stapp-formbase stapp reselect
# OR using stapp-cli-tools
stapp install stapp-formbase
Peer dependencies
- stapp: >= 2.6
- reselect: >= 4
Definition
type formBase<Values> = (config: {
initialValues: Partial<Values>
}) => Module<{}, FormBaseState<Values>>
type FormBaseState<Values> = {
values: Values,
errors: { [K in keyof Values]: any },
touched: { [K in keyof Values]: boolean },
dirty: { [K in keyof Values]: boolean },
active: keyof Values | null,
ready: { [K: string]: boolean },
pristine: boolean,
submitting: boolean
}
Usage
import { createApp } from 'stapp'
import { formBase } from 'stapp-formbase'
const app = createApp({
name: 'My Form',
modules: [
formBase({
initialValues: {
name: 'Evan',
age: 16
}
})
]
})
State fields and events explanation
values:{ [fieldName]: fieldValue}. Value preferably should be of a string type. Related:setValueevent: sets values.resetFormevent: sets values back to initial.clearFieldsevent: removes values for fields listed in the payload.pickFieldsevent: removes values for fields NOT listed in the payload.
errors:{ [fieldName]: fieldError}. Value of any type can be an error. Related:setErrorevent: sets errors.resetFormevent: clears all errors.isValidSelectorselector creator: creates a selector that returnestrueif there is no errors,falseotherwise.clearFieldsevent: removes errors for fields listed in the payload.pickFieldsevent: removes errors for fields NOT listed in the payload.
touched:{ [fieldName]: boolean }. Indicates that a field was active at some time. Related:setTouchedevent: sets fields as touched. Should be called onblurevents.resetFormevent: clearstouchedstate.clearFieldsevent: removes values for fields listed in the payload.pickFieldsevent: removes values for fields NOT listed in the payload.
active:string | null. Indicates currently active (focused) field. Related:setActiveevent: sets a field as active.resetFormevent: setsactivetonull.clearFieldsevent: setsactivetonullif active field is listed in the payload.pickFieldsevent: setsactivetonullif active field is NOT listed in the payload.
dirty:{ [fieldName]: boolean}. Field is dirty if its value differs from initial value. Related:setValueevent: sets values.resetFormevent: clearsdirtystate.clearFieldsevent: removes values for fields listed in the payload.pickFieldsevent: removes values for fields NOT listed in the payload.isDirtySelectorselector creator: creates a selector that returnsfalseif form values state equals to initial state,trueotherwise.
pristine:boolean. Indicates that a user has ever interacted with a form. Related:setValueevent: setspristinevalue tofalse.setTouchedevent: setspristinevalue tofalse.resetFormevent: setspristinefalue totrueclearFieldsevent: removes values for fields listed in the payload.pickFieldsevent: removes values for fields NOT listed in the payload.isPrisineSelectorselector creator: creates a selector that returnspristinevalue.
ready:{ [K: string]: boolean }. Can be used to set readiness flags. Used for validation, async requests etc. Related:setReadyevent: sets readiness flags.resetFormevent: clearsreadystate.isReadySelectorselector creator: creates a selector that returnstrueif all readiness flags aretrue,falseotherwise.
submitting:boolean. Indicates that a form is being submitted. Related:setSubmittingevent: sets submitting totrueorfalse
Events
stapp-formbase exposes every event to the public application API.
Events are available in the formBase field of an api object.
type setValue = EventCreator<{ [K: string]: string }>
type setError = EventCreator<{ [K: string]: any }>
type setTouched = EventCreator<{ [K: string]: boolean }>
type setActive = EventCreator<string>
type setSubmitting = EventCreator<boolean>
type setReady = EventCreator<{ [K: string]: boolean }>
type clearFields = EventCreator<string[]>
type pickFields = EventCreator<string[]>
type setReady = EventCreator<{ [K: string]: boolean }>
type resetForm = EventCreator<void>
type submit = EventCreator<void>
Selectors
stapp-formbase is shipped with a bunch of memoized selectors.
type fieldSelector = <State, Extra>(name: string, extraSelector: (state: State) => Extra) =>
(state: State) => ({
value: string | undefined,
error: any,
dirty: boolean,
touched: boolean,
active: boolean,
extra?: Extra
})
type formSelector = () => <State>(state: State) => ({
submitting: boolean,
valid: boolean,
ready: boolean,
dirty: boolean,
pristine: boolean
})
type isValidSelector = () => <State>(state: State) => boolean
type isReadySelector = () => <State>(state: State) => boolean
type isDirtySelector = () => <State>(state: State) => boolean
type isPristineSelector = () => <State>(state: State) => boolean