UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

208 lines (207 loc) 11.2 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js")); var _react = _interopRequireWildcard(require("react")); var _useMountEffect = _interopRequireDefault(require("../../../../shared/helpers/useMountEffect.js")); var _index = _interopRequireDefault(require("../../utils/json-pointer/index.js")); var _zod = require("../../utils/zod.js"); var _extractZodSubSchema = require("./extractZodSubSchema.js"); var _componentHelper = require("../../../../shared/component-helper.js"); var _isAsync = require("../../../../shared/helpers/isAsync.js"); var _useDataValue = _interopRequireDefault(require("../../hooks/useDataValue.js")); var _index2 = require("../../DataContext/index.js"); var _SectionContext = _interopRequireDefault(require("../Section/SectionContext.js")); var _useReportError = _interopRequireDefault(require("./useReportError.js")); var _IsolationCommitButton = _interopRequireDefault(require("./IsolationCommitButton.js")); var _IsolationResetButton = _interopRequireDefault(require("./IsolationResetButton.js")); var _index3 = require("../../DataContext/Provider/index.js"); var _IsolationDataReference = require("./IsolationDataReference.js"); var _IsolatedContainer2 = _interopRequireWildcard(require("./IsolatedContainer.js")); var _IsolationContext = _interopRequireDefault(require("./IsolationContext.js")); var _structuredClone = require("../../../../shared/helpers/structuredClone.js"); var _BubbleValidation; 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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function IsolationProvider(props) { var _outerContext$props, _outerContext$props2, _outerContext$props3, _IsolatedContainer; const [dataReferenceFallback] = (0, _react.useState)(() => { if (!(props !== null && props !== void 0 && props.dataReference)) { return (0, _IsolationDataReference.createDataReference)(); } }); const { children, onPathChange, onCommit: onCommitProp, onClear: onClearProp, transformOnCommit: transformOnCommitProp, commitHandleRef, bubbleValidation, preventUncommittedChanges, data, defaultData, dataReference = dataReferenceFallback, resetDataAfterCommit } = props; const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const internalDataRef = (0, _react.useRef)(); const localDataRef = (0, _react.useRef)({}); const dataContextRef = (0, _react.useRef)(null); const outerContext = (0, _react.useContext)(_index2.Context); const { path: pathSection } = (0, _react.useContext)(_SectionContext.default) || {}; const { handlePathChange: handlePathChangeOuter, data: dataOuter } = outerContext || {}; const { moveValueToPath } = (0, _useDataValue.default)(); const onPathChangeHandler = (0, _react.useCallback)(async (path, value) => { if (localDataRef.current === _index3.clearedData) { localDataRef.current = {}; } _index.default.set(localDataRef.current, path, value); if (pathSection) { path = path.replace(pathSection, ''); } return await (onPathChange === null || onPathChange === void 0 ? void 0 : onPathChange(path, value)); }, [onPathChange, pathSection]); const onUpdateDataValueHandler = (0, _react.useCallback)(async (path, value, { preventUpdate = undefined } = {}) => { if (internalDataRef.current === _index3.clearedData) { internalDataRef.current = {}; } _index.default.set(internalDataRef.current, path, value); if (!preventUpdate) { forceUpdate(); } }, []); const removeSectionPath = (0, _react.useCallback)(data => { return pathSection && _index.default.has(data, pathSection) ? _index.default.get(data, pathSection) : data; }, [pathSection]); const getMountedData = (0, _react.useCallback)(data => { var _dataContextRef$curre; const mounterData = {}; (_dataContextRef$curre = dataContextRef.current) === null || _dataContextRef$curre === void 0 || _dataContextRef$curre.mountedFieldsRef.current.forEach((field, path) => { if (field.isMounted && _index.default.has(data, path)) { _index.default.set(mounterData, path, _index.default.get(data, path)); } }); return mounterData; }, []); (0, _useMountEffect.default)(() => { localDataRef.current = getMountedData(internalDataRef.current); }); (0, _react.useMemo)(() => { if (localDataRef.current === _index3.clearedData) { return; } let localData = data !== null && data !== void 0 ? data : defaultData; if (localData && pathSection && !_index.default.has(localDataRef.current, pathSection)) { localData = moveValueToPath(pathSection, localData); } internalDataRef.current = Object.assign({}, localData || (0, _structuredClone.structuredClone)(dataOuter) || {}, localDataRef.current); }, [data, defaultData, pathSection, dataOuter, moveValueToPath]); const onCommit = (0, _react.useCallback)(async (data, additionalArgs) => { var _props$path; const mountedData = getMountedData(data); const path = (_props$path = props.path) !== null && _props$path !== void 0 ? _props$path : '/'; const outerData = props.path && _index.default.has(dataOuter, path) ? _index.default.get(dataOuter, path) : dataOuter; localDataRef.current = mountedData; let isolatedData = (0, _structuredClone.structuredClone)(mountedData); if (typeof transformOnCommitProp === 'function') { isolatedData = transformOnCommitProp(isolatedData, outerData); } let stop = false; additionalArgs.preventCommit = () => stop = true; const commitData = removeSectionPath(isolatedData); const result = (0, _isAsync.isAsync)(onCommitProp) ? await (onCommitProp === null || onCommitProp === void 0 ? void 0 : onCommitProp(commitData, additionalArgs)) : onCommitProp === null || onCommitProp === void 0 ? void 0 : onCommitProp(commitData, additionalArgs); if (stop) { return; } await (handlePathChangeOuter === null || handlePathChangeOuter === void 0 ? void 0 : handlePathChangeOuter(path, Array.isArray(isolatedData) ? isolatedData : (0, _componentHelper.extendDeep)({}, outerData, isolatedData))); return result; }, [getMountedData, props.path, dataOuter, transformOnCommitProp, handlePathChangeOuter, onCommitProp, removeSectionPath]); const setIsolatedData = (0, _react.useCallback)(data => { localDataRef.current = data; internalDataRef.current = data; }, []); const onClear = (0, _react.useCallback)(() => { setIsolatedData(_index3.clearedData); forceUpdate(); onClearProp === null || onClearProp === void 0 || onClearProp(); }, [onClearProp, setIsolatedData]); const providerProps = { ...props, [defaultData ? 'defaultData' : 'data']: internalDataRef.current, onUpdateDataValue: onUpdateDataValueHandler, onPathChange: onPathChangeHandler, onCommit, onClear, isolate: true, schema: (props === null || props === void 0 ? void 0 : props.schema) || (outerContext === null || outerContext === void 0 || (_outerContext$props = outerContext.props) === null || _outerContext$props === void 0 ? void 0 : _outerContext$props.schema), ajvInstance: (props === null || props === void 0 ? void 0 : props.ajvInstance) || (outerContext === null || outerContext === void 0 || (_outerContext$props2 = outerContext.props) === null || _outerContext$props2 === void 0 ? void 0 : _outerContext$props2.ajvInstance) }; if (props !== null && props !== void 0 && props.path && (props === null || props === void 0 ? void 0 : props.path) !== '/' && !(props !== null && props !== void 0 && props.schema) && outerContext !== null && outerContext !== void 0 && (_outerContext$props3 = outerContext.props) !== null && _outerContext$props3 !== void 0 && _outerContext$props3.schema) { if ((0, _zod.isZodSchema)(outerContext.props.schema)) { providerProps.schema = (0, _extractZodSubSchema.extractZodSubSchema)(outerContext.props.schema, props.path); } else { providerProps.schema = { $defs: { root: outerContext.props.schema }, $ref: `#/$defs/root${props.path.split('/').join('/properties/')}` }; } } return _react.default.createElement(_index2.Provider, providerProps, _react.default.createElement(_IsolationContext.default.Provider, { value: { preventUncommittedChanges, dataReference, resetDataAfterCommit, outerContext, setIsolatedData } }, _react.default.createElement(_index2.Context.Consumer, null, dataContext => { dataContextRef.current = dataContext; if (commitHandleRef) { commitHandleRef.current = dataContext === null || dataContext === void 0 ? void 0 : dataContext.handleSubmit; } return _IsolatedContainer || (_IsolatedContainer = _react.default.createElement(_IsolatedContainer2.default, null, children, " ")); }), bubbleValidation && (_BubbleValidation || (_BubbleValidation = _react.default.createElement(BubbleValidation, null))))); } function BubbleValidation() { var _addSetShowAllErrorsR; const innerContext = (0, _react.useContext)(_index2.Context); const { outerContext } = (0, _react.useContext)(_IsolationContext.default); const { setShowAllErrors } = innerContext; const setShowAllErrorsNested = (0, _react.useCallback)(showAllErrors => { setShowAllErrors === null || setShowAllErrors === void 0 || setShowAllErrors(showAllErrors); }, [setShowAllErrors]); const { addSetShowAllErrorsRef } = outerContext || {}; if (!(addSetShowAllErrorsRef !== null && addSetShowAllErrorsRef !== void 0 && (_addSetShowAllErrorsR = addSetShowAllErrorsRef.current) !== null && _addSetShowAllErrorsR !== void 0 && _addSetShowAllErrorsR.includes(setShowAllErrorsNested))) { var _context; addSetShowAllErrorsRef === null || addSetShowAllErrorsRef === void 0 || (0, _push.default)(_context = addSetShowAllErrorsRef.current).call(_context, setShowAllErrorsNested); } (0, _useReportError.default)(innerContext.hasErrors() ? _IsolatedContainer2.isolationError : undefined, outerContext, 'isolation'); return null; } IsolationProvider.CommitButton = _IsolationCommitButton.default; IsolationProvider.ResetButton = _IsolationResetButton.default; IsolationProvider.createDataReference = _IsolationDataReference.createDataReference; IsolationProvider._supportsSpacingProps = undefined; var _default = exports.default = IsolationProvider; //# sourceMappingURL=Isolation.js.map