import React, { PropsWithChildren, useEffect } from 'react'; import { UIRouter } from '@uirouter/angularjs'; import { FormState } from 'react-hook-form/dist/types/form'; import { FieldValues } from 'react-hook-form/dist/types/fields'; interface UnsavedFormAlertProps { uiRouter: UIRouter, formState: FormState, } /** * Alert the user about unsaved changes in the given form, before leaving the current page */ export const UnsavedFormAlert = ({ uiRouter, formState, children }: PropsWithChildren>) => { useEffect(() => { const { transitionService, globals: { current } } = uiRouter; transitionService.onBefore({ from: current.name }, () => { const { isDirty } = formState; console.log('transition start', isDirty); }); }, []); return (
{children}
); };