file description import * as fs from 'formstate' ; import * as fsx from '..' ; import { observable } from 'mobx' ; export type Xify < T > = fsx . ComposibleValidatable < ValueOf < T > > & { origin : T ; } ; /** Convert formstate field / form state into formstate-x state */ export function xify < T extends fs . ComposibleValidatable < any > > ( state ) { const stateX = { origin : state , get $ ( ) { return getValue ( state , true ) ; } , get value ( ) { return getValue ( state , false ) ; } , get hasError ( ) { return ! ! this . error ; } , get error ( ) { return getError ( state ) ; } , get validating ( ) { return this . _validateStatus === fsx . ValidateStatus . Validating ; } , get validated ( ) { return this . _validateStatus === fsx . ValidateStatus . Validated ; } , validationDisabled : false , async validate ( ) { await state . validate ( ) ; if ( this . hasError ) return { hasError : true , error : this . error } ; return { hasError : false , value : this . value } ; } , reset ( ) { state . reset ( ) ; } , dispose ( ) { } , get dirty ( ) { return getDirty ( state ) ; } , get _activated ( ) { return getActivated ( state ) ; } , get _validateStatus ( ) { return getValidateStatus ( state ) ; } } ; return observable ( stateX ) ; } /** Value of `FieldState`. */ export type ValueOfFieldState < State > = ( State extends fs . FieldState < infer FieldType > ? FieldType : never ) ; /** Value Array of given Field. */ export interface ValueArrayOf < Field > extends Array < ValueOf < Field > > { } /** Value of object-fields. */ export type ValueOfObjectFields < Fields > = { [ FieldKey in keyof Fields ] : ValueOf < Fields [ FieldKey ] > ; } ; /** Value of array-fields. */ export type ValueOfArrayFields < Fields > = ( Fields extends Array < infer Field > ? ValueArrayOf < Field > : never ) ; /** Value of fields. */ export type ValueOfFields < Fields > = ( Fields extends { [ key ] : fs . ComposibleValidatable < any > ; } ? ValueOfObjectFields < Fields > : ValueOfArrayFields < Fields > ) ; /** Value of state (`FormState` or `FieldState`) */ export type ValueOf < State > = ( State extends fs . FormState < infer Fields > ? ValueOfFields < Fields > : ValueOfFieldState < State > ) ; function getValueOfForm < T extends fs . FormState < any > > ( state , safe ) { const mode = state [ 'mode' ] ; if ( mode === 'array' ) { return state . $ . map ( ( field ) => getValue ( field , safe ) ) ; } if ( mode === 'object' ) { const fields = state . $ ; return Object . keys ( fields ) . reduce ( ( value , key ) => ( { ... value , [ key ] : getValue ( fields [ key ] , safe ) } ) , { } ) as any ; } throw new Error ( `Unsupported mode: ${ mode }` ) ; } export function getValue < T extends fs . ComposibleValidatable < any > > ( state , safe ) { if ( state instanceof fs . FieldState ) return safe ? state . $ : state . value ; if ( state instanceof fs . FormState ) return getValueOfForm ( state , safe ) ; throw new Error ( `Expecting ComposibleValidatable value, while got ${ typeof state }` ) ; } function getValidateStatusOfField ( state ) { if ( state . hasBeenValidated ) return fsx . ValidateStatus . Validated ; if ( state . validating ) return fsx . ValidateStatus . Validating ; return fsx . ValidateStatus . NotValidated ; } function getValidateStatusOfForm ( state ) { const fields = state [ 'getValues' ] ( ) ; if ( fields . every ( field => getValidateStatus ( field ) === fsx . ValidateStatus . NotValidated ) ) { return fsx . ValidateStatus . NotValidated ; } if ( fields . every ( field => getValidateStatus ( field ) === fsx . ValidateStatus . Validated ) ) { return fsx . ValidateStatus . Validated ; } return fsx . ValidateStatus . Validating ; } export function getValidateStatus ( state ) { if ( state instanceof fs . FieldState ) return getValidateStatusOfField ( state ) ; if ( state instanceof fs . FormState ) return getValidateStatusOfForm ( state ) ; throw new Error ( `Expecting ComposibleValidatable value, while got ${ typeof state }` ) ; } function getDirtyOfField ( state ) { return ! ! state . dirty ; } function getDirtyOfForm ( state ) { return state [ 'getValues' ] ( ) . some ( field => getDirty ( field ) ) ; } export function getDirty ( state ) { if ( state instanceof fs . FieldState ) return getDirtyOfField ( state ) ; if ( state instanceof fs . FormState ) return getDirtyOfForm ( state ) ; throw new Error ( `Expecting ComposibleValidatable value, while got ${ typeof state }` ) ; } function getActivatedOfField ( state ) { return ! ! ( state [ '_autoValidationEnabled' ] && state . dirty ) ; } function getActivatedOfForm ( state ) { return state [ 'getValues' ] ( ) . some ( field => getActivated ( field ) ) ; } export function getActivated ( state ) { if ( state instanceof fs . FieldState ) return getActivatedOfField ( state ) ; if ( state instanceof fs . FormState ) return getActivatedOfForm ( state ) ; throw new Error ( `Expecting ComposibleValidatable value, while got ${ typeof state }` ) ; } function getError ( state ) { return state . hasError ? ( state . error as string ) : undefined ; }