UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

69 lines (68 loc) 3.29 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = FieldBoundaryProvider; require("core-js/modules/web.dom-collections.iterator.js"); var _react = _interopRequireWildcard(require("react")); var _FieldBoundaryContext = _interopRequireDefault(require("./FieldBoundaryContext")); var _Context = _interopRequireDefault(require("../Context")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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 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 ? true : delete _errorsRef$current[path]; } forceUpdate(); (_onPathErrorRef$curre = onPathErrorRef.current) === null || _onPathErrorRef$curre === void 0 ? 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, setShowBoundaryErrors, setFieldError, revealError }; return _react.default.createElement(_FieldBoundaryContext.default.Provider, { value: context }, children); } //# sourceMappingURL=FieldBoundaryProvider.js.map