import { lensProp , set } from "ramda" ; import { compose , withHandlers , withState } from "recompose" ; import { withFormSubmit , WithFormSubmit , WithFormSubmitOptions } from "enhancers" ; template TRequest export interface WithFormOptions < TRequest extends { } = any > extends WithFormSubmitOptions { initial : Readonly < TRequest > ; } template TRequest interface StateProps < TRequest extends { } > { request : Readonly < TRequest > ; setRequest : ( request : TRequest ) => Readonly < TRequest > ; } /**
 * Handler properties
 */ interface HandlerProps { handleChange ( ev ) : void ; handleSubmit ( ev ) : Promise < void > ; } template TRequest template TResponse type HandlerCallbackProps < TRequest extends { } , TResponse = void > = WithFormSubmit < TRequest , TResponse > & StateProps < TRequest > ; template TRequest template TResponse export type WithForm < TRequest extends { } = any , TResponse = void > = WithFormSubmit < TRequest , TResponse > & StateProps < TRequest > & HandlerProps ; template TRequest template TResponse param options return export const withForm = < TRequest extends { } , TResponse = void > ( { initial , ... options } ) => compose < WithForm < TRequest , TResponse > , { } > ( withFormSubmit < TRequest , TResponse > ( options ) , withState ( "request" , "setRequest" , initial ) , withHandlers < HandlerCallbackProps < TRequest , TResponse > , HandlerProps > ( { handleChange : ( { request , setRequest } ) => ev => { const { type , name , value , checked } = ev . currentTarget ; type === "checkbox" ? setRequest ( set ( lensProp ( name ) , checked , request ) ) : setRequest ( set ( lensProp ( name ) , value , request ) ) ; } , handleSubmit : ( { submit , request } ) => ev => { ev . preventDefault ( ) ; return submit ( request ) ; } } ) ) ;