import { useCallback , useMemo } from 'react' ; import { BITS , useFormContext } from './Contexts' ; import useErrors from './useErrors' ; export interface UseFormSubmitOptions { triggers ? : string [ ] ; } param options param { string [ ] } options . trigger export default function useFormSubmit ( { triggers } = { } ) { const { actions , submits } = useFormContext ( BITS . actions | BITS . submits ) ; const errors = useErrors ( triggers ) ; const handleSubmit = useCallback ( ( ... args ) => { if ( ! actions ) { if ( process . env . NODE_ENV !== 'production' ) return console . error ( 'A Form submit event ' + 'was triggered from a component outside the context of a Form. ' + 'The Button should be wrapped in a Form component' ) ; } if ( triggers && triggers . length ) { actions . onValidate ( triggers , 'submit' , args ) ; } else actions . onSubmit ( ) ; } , [ actions , triggers && triggers . join ( ',' ) ] ) ; return [ handleSubmit , useMemo ( ( ) => ( { errors , ... submits } ) , [ errors , submits ] ) , ] as const ; }