UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

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