UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

76 lines (75 loc) 4.37 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useValidation; var _react = require("react"); var _useSharedState = require("../../../../shared/helpers/useSharedState.js"); var _Context = _interopRequireDefault(require("../../DataContext/Context.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function useValidation(id = undefined) { const { data: sharedDataContext } = (0, _useSharedState.useSharedState)(id ? (0, _useSharedState.createReferenceKey)(id, 'data-context') : undefined); const { data } = (0, _useSharedState.useSharedState)(id ? (0, _useSharedState.createReferenceKey)(id, 'attachments') : undefined); const fallback = (0, _react.useCallback)(() => false, []); const context = (0, _react.useContext)(_Context.default); const sharedContext = id ? sharedDataContext : undefined; const hasErrors = (sharedContext === null || sharedContext === void 0 ? void 0 : sharedContext.hasErrors) || (data === null || data === void 0 ? void 0 : data.hasErrors) || !id && (context === null || context === void 0 ? void 0 : context.hasErrors) || fallback; const hasFieldError = (sharedContext === null || sharedContext === void 0 ? void 0 : sharedContext.hasFieldError) || (data === null || data === void 0 ? void 0 : data.hasFieldError) || !id && (context === null || context === void 0 ? void 0 : context.hasFieldError) || fallback; const setSubmitState = (sharedContext === null || sharedContext === void 0 ? void 0 : sharedContext.setSubmitState) || (data === null || data === void 0 ? void 0 : data.setSubmitState) || !id && (context === null || context === void 0 ? void 0 : context.setSubmitState) || fallback; const setFormError = (0, _react.useCallback)(error => { setSubmitState === null || setSubmitState === void 0 || setSubmitState({ error }); }, [setSubmitState]); const { getFieldConnections, setFieldStatusCache } = useConnections(id); const setFieldStatus = (0, _react.useCallback)((path, status) => { var _connections$path, _connections$path$set; setFieldStatusCache(path, status); const connections = getFieldConnections(); connections === null || connections === void 0 || (_connections$path = connections[path]) === null || _connections$path === void 0 || (_connections$path$set = _connections$path.setEventResult) === null || _connections$path$set === void 0 || _connections$path$set.call(_connections$path, status); }, [getFieldConnections, setFieldStatusCache]); return (0, _react.useMemo)(() => ({ hasErrors, hasFieldError, setFormError, setFieldStatus }), [hasErrors, hasFieldError, setFormError, setFieldStatus]); } function useConnections(id = undefined) { const { data: sharedDataContext } = (0, _useSharedState.useSharedState)(id ? (0, _useSharedState.createReferenceKey)(id, 'data-context') : undefined); const { get } = (0, _useSharedState.useSharedState)(id ? (0, _useSharedState.createReferenceKey)(id, 'attachments') : undefined); const dataContext = (0, _react.useContext)(_Context.default); const { fieldConnectionsRef } = dataContext || {}; const sharedFieldConnectionsRef = sharedDataContext === null || sharedDataContext === void 0 ? void 0 : sharedDataContext.fieldConnectionsRef; const getFieldConnections = (0, _react.useCallback)(() => { const attachments = get(); const connections = sharedFieldConnectionsRef || (attachments === null || attachments === void 0 ? void 0 : attachments.fieldConnectionsRef) || !id && fieldConnectionsRef; return connections === null || connections === void 0 ? void 0 : connections.current; }, [fieldConnectionsRef, get, id, sharedFieldConnectionsRef]); const setFieldStatusCache = (0, _react.useCallback)((path, status) => { var _attachments$fieldSta; const attachments = get(); if (attachments !== null && attachments !== void 0 && (_attachments$fieldSta = attachments.fieldStatusRef) !== null && _attachments$fieldSta !== void 0 && _attachments$fieldSta.current) { attachments.fieldStatusRef.current[path] = status; } }, [get]); return (0, _react.useMemo)(() => ({ getFieldConnections, setFieldStatusCache }), [getFieldConnections, setFieldStatusCache]); } //# sourceMappingURL=useValidation.js.map