Repository URL to install this package:
|
Version:
3.7.2 ▾
|
import { isObj, isArray } from 'exotic'
import { FormState } from '../forms'
import { OneFormState } from './OneFormState'
import { ComputedFormStateProps, CompatObserverFormProps } from './typings'
/**
* @todo renderProps
*/
export function fromObserverFormPropsToState(props: CompatObserverFormProps) {
const obj: ComputedFormStateProps = {
state: props.state as any,
inputsList: props.inputsList as any,
}
if (!isObj(props.state) && !isArray(props.inputsList)) {
throw new TypeError(
'cannot use @skava/form without .inputsList &| .state props!'
)
} else if (isObj(props.state) && !isArray(props.state.inputsList)) {
throw new TypeError('using @skava/form without inputsList')
} else if (props.state instanceof OneFormState) {
// @todo may need to make it into a `FormState` instead
// obj.state = props.state.formState || props.state
const { formState } = props.state
obj.state = formState
// could check inputs length is diff, then update
} else if (props.state instanceof FormState) {
// not really needing to check this since I moved the default above
obj.state = props.state
} else {
throw new TypeError('why using @skava/form without FormState?')
}
return obj
}