UNPKG

envoc-form

Version:

Envoc form components

21 lines (20 loc) 1.3 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useContext } from 'react'; import { UNSAFE_NavigationContext as NavigationContext, } from 'react-router-dom'; import { useFormikContext } from 'formik'; import LegacyFormBasedPreventNavigation from './LegacyFormBasedPreventNavigation'; import NewFormBasedPreventNavigation from './NewFormBasedPreventNavigation'; /** Prevent the user from navigating away from a form if there are any changes. */ export default function FormBasedPreventNavigation(_a) { var ignoreLostChanges = _a.ignoreLostChanges, _b = _a.promptMessage, promptMessage = _b === void 0 ? 'Changes you made may not be saved.' : _b; var _c = useFormikContext(), dirty = _c.dirty, isSubmitting = _c.isSubmitting; var preventNavigate = !ignoreLostChanges && dirty && !isSubmitting; var navigator = useContext(NavigationContext).navigator; var isUsingDataRouter = navigator.location === undefined; if (isUsingDataRouter) { return (_jsx(NewFormBasedPreventNavigation, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator })); } else { return (_jsx(LegacyFormBasedPreventNavigation, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator })); } }