@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
61 lines (60 loc) • 1.75 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useHandleStatus;
var _react = require("react");
var _useHasContentChanged = _interopRequireDefault(require("./useHasContentChanged.js"));
var _useReportError = _interopRequireDefault(require("./useReportError.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useHandleStatus({
outerContext,
preventUncommittedChanges,
error,
name
}) {
const {
hasContentChanged
} = (0, _useHasContentChanged.default)({
enabled: preventUncommittedChanges
});
let errorToReport = undefined;
if (preventUncommittedChanges && hasContentChanged) {
errorToReport = error;
}
(0, _useReportError.default)(errorToReport, outerContext, name);
const showStatus = useShowStatus({
outerContext,
hasContentChanged,
preventUncommittedChanges
});
return {
hasContentChanged,
showStatus
};
}
function useShowStatus({
outerContext,
hasContentChanged,
preventUncommittedChanges
}) {
const showAllErrors = outerContext === null || outerContext === void 0 ? void 0 : outerContext.showAllErrors;
const [showStatus, setShowStatus] = (0, _react.useState)(showAllErrors);
const showRef = (0, _react.useRef)(showAllErrors);
(0, _react.useEffect)(() => {
if (!preventUncommittedChanges) {
return;
}
if (!hasContentChanged) {
setShowStatus(false);
} else {
if (showRef.current !== showAllErrors) {
setShowStatus(showAllErrors);
}
}
showRef.current = showAllErrors;
}, [hasContentChanged, preventUncommittedChanges, showAllErrors]);
return showStatus;
}
//# sourceMappingURL=useHandleStatus.js.map