@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
56 lines (55 loc) • 1.99 kB
JavaScript
"use client";
import { useContext, useEffect } from 'react';
import useId from "../../../../shared/helpers/useId.js";
import DataContext from "../../DataContext/Context.js";
import WizardStepContext from "../../Wizard/Step/StepContext.js";
import WizardContext from "../../Wizard/Context/index.js";
import VisibilityContext from "../Visibility/VisibilityContext.js";
import { usePath } from "../../hooks/index.js";
export default function useReportError(error, customDataContext, name) {
const {
joinPath
} = usePath();
const dataContext = useContext(DataContext);
const wizardContext = useContext(WizardContext);
const wizardStepContext = useContext(WizardStepContext);
const visibilityContext = useContext(VisibilityContext);
const {
setFieldError: setFieldErrorDataContext,
setMountedFieldState,
prerenderFieldProps
} = customDataContext || dataContext;
const {
setFieldError: setFieldErrorWizard
} = wizardContext || {};
const {
index: wizardIndex
} = wizardStepContext || {};
const {
isVisible
} = visibilityContext || {};
const handleFieldAsVisible = isVisible;
const id = useId();
useEffect(() => {
if (prerenderFieldProps) {
return;
}
const path = joinPath(['internal', name, id]);
const currentError = handleFieldAsVisible !== false ? error : undefined;
if (currentError) {
setMountedFieldState?.(path, {
isMounted: true
});
}
setFieldErrorWizard?.(wizardIndex, path, currentError ? true : undefined);
setFieldErrorDataContext?.(path, currentError);
return () => {
setMountedFieldState?.(path, {
isMounted: false
});
setFieldErrorWizard?.(wizardIndex, path, undefined);
setFieldErrorDataContext?.(path, undefined);
};
}, [error, handleFieldAsVisible, id, joinPath, name, prerenderFieldProps, setFieldErrorDataContext, setFieldErrorWizard, setMountedFieldState, wizardIndex]);
}
//# sourceMappingURL=useReportError.js.map