Repository URL to install this package:
|
Version:
3.4.3 ▾
|
import { isFunction } from 'exotic'
import { observable, action, computed } from 'xmobx/mobx'
import { SerializedObj } from '../typings'
import { serializeListIntoMergedObj } from '../deps'
// @note @circular
import { InputState } from '../inputs/InputState'
import { FormGetPredicate } from './typings'
let identifierIndex = 0
export class FormState {
identifier: string = 'form-' + identifierIndex++
@observable.ref
inputsList: InputState[] = []
@observable.ref
inputPluginsList: any[] = []
@action
setInputsList(inputsList: InputState[]) {
inputsList.forEach(input => {
input.setFormState(this)
})
this.inputsList = inputsList
}
@action
setInputPluginsList(list: any[]) {
this.inputPluginsList = list
}
@action
setIdentifier(identifier: string) {
this.identifier = identifier
}
toJSON(): SerializedObj {
return serializeListIntoMergedObj(this.inputsList)
}
// ============ compat ============
/**
* @todo here, key is we need to finish FieldSet to recurse with FieldSet
*/
get(identifier: string | FormGetPredicate): InputState | undefined {
const isInputWeAreLookingFor = (inputState: InputState) => {
if (isFunction(identifier)) {
return identifier(inputState)
} else {
return inputState.identifier === identifier
}
}
return this.inputsList.find(isInputWeAreLookingFor)
}
@computed
get isValid() {
return this.inputsList.every(inputState => inputState.isValid)
}
}