import isUndefined from 'lodash/isUndefined' ; import { useState } from 'react' ; export default function useValidation < T extends HTMLInputElement | HTMLTextAreaElement > ( handleValidation ? ) { const [ isDirty , setIsDirty ] = useState ( false ) ; if ( isUndefined ( handleValidation ) || typeof handleValidation !== 'function' ) { return { onBlur : ( ) => { } , onChange : ( ) => { } , } ; } const onBlur = ( e ) => { setIsDirty ( true ) ; handleValidation ?. ( e . target . value ) ; } ; const onChange = ( e ) => { if ( isDirty ) { handleValidation ?. ( e . target . value ) ; } } ; return { onBlur , onChange , } ; }