UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

71 lines (70 loc) 3.2 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = FieldBoundaryProvider; var _react = _interopRequireWildcard(require("react")); var _FieldBoundaryContext = _interopRequireDefault(require("./FieldBoundaryContext.js")); var _Context = _interopRequireDefault(require("../Context.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function FieldBoundaryProvider(props) { const { showErrors = undefined, onPathError = null, children } = props; const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const { showAllErrors } = (0, _react.useContext)(_Context.default); const onPathErrorRef = (0, _react.useRef)(onPathError); onPathErrorRef.current = onPathError; const errorsRef = (0, _react.useRef)({}); const showBoundaryErrorsRef = (0, _react.useRef)(showErrors); const hasError = Object.keys(errorsRef.current).length > 0; const verifyFieldError = (0, _react.useCallback)(() => { return Object.keys(errorsRef.current).length > 0; }, []); const hasSubmitError = showAllErrors && hasError; const setFieldError = (0, _react.useCallback)((path, error) => { var _onPathErrorRef$curre; if (error) { errorsRef.current[path] = !!error; } else { var _errorsRef$current; (_errorsRef$current = errorsRef.current) === null || _errorsRef$current === void 0 || delete _errorsRef$current[path]; } forceUpdate(); (_onPathErrorRef$curre = onPathErrorRef.current) === null || _onPathErrorRef$curre === void 0 || _onPathErrorRef$curre.call(onPathErrorRef, path, error); }, []); const hasVisibleErrorRef = (0, _react.useRef)(new Map()); const revealError = (0, _react.useCallback)((path, hasError) => { if (hasError) { hasVisibleErrorRef.current.set(path, hasError); } else { hasVisibleErrorRef.current.delete(path); } forceUpdate(); }, []); const setShowBoundaryErrors = (0, _react.useCallback)(showBoundaryErrors => { showBoundaryErrorsRef.current = showBoundaryErrors ? Date.now() : false; forceUpdate(); }, []); const context = { hasError, hasSubmitError, hasVisibleError: hasVisibleErrorRef.current.size > 0, errorsRef, showBoundaryErrors: showBoundaryErrorsRef.current, verifyFieldError, setShowBoundaryErrors, setFieldError, revealError }; return _react.default.createElement(_FieldBoundaryContext.default.Provider, { value: context }, children); } //# sourceMappingURL=FieldBoundaryProvider.js.map