UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

164 lines (163 loc) 8.32 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/es.string.replace.js"; import "core-js/modules/web.dom-collections.iterator.js"; import React, { useCallback, useContext, useEffect, useMemo, useReducer, useRef } from 'react'; import pointer from '../../utils/json-pointer'; import { extendDeep } from '../../../../shared/component-helper'; import { isAsync } from '../../../../shared/helpers/isAsync'; import useId from '../../../../shared/helpers/useId'; import useDataValue from '../../hooks/useDataValue'; import { Context as DataContext, Provider } from '../../DataContext'; import SectionContext from '../Section/SectionContext'; import IsolationCommitButton from './IsolationCommitButton'; import { clearedData } from '../../DataContext/Provider'; import structuredClone from '@ungap/structured-clone'; function IsolationProvider(props) { var _outerContext$addSetS, _outerContext$addSetS2; const { children, onPathChange, onCommit: onCommitProp, onClear: onClearProp, transformOnCommit: transformOnCommitProp, commitHandleRef, bubbleValidation, data, defaultData } = props; const [, forceUpdate] = useReducer(() => ({}), {}); const internalDataRef = useRef(); const localDataRef = useRef({}); const dataContextRef = useRef(null); const outerContext = useContext(DataContext); const { path: pathSection } = useContext(SectionContext) || {}; const { handlePathChange: handlePathChangeOuter, data: dataOuter } = outerContext || {}; const { moveValueToPath } = useDataValue(); const onPathChangeHandler = useCallback(async (path, value) => { if (localDataRef.current === clearedData) { localDataRef.current = {}; } pointer.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 removeSectionPath = useCallback(data => { return pathSection && pointer.has(data, pathSection) ? pointer.get(data, pathSection) : data; }, [pathSection]); const getMountedData = useCallback(data => { var _dataContextRef$curre; const mounterData = {}; (_dataContextRef$curre = dataContextRef.current) === null || _dataContextRef$curre === void 0 ? void 0 : _dataContextRef$curre.mountedFieldsRef.current.forEach((field, path) => { if (field.isMounted && pointer.has(data, path)) { pointer.set(mounterData, path, pointer.get(data, path)); } }); return mounterData; }, []); useEffect(() => { localDataRef.current = getMountedData(internalDataRef.current); }, [getMountedData]); useMemo(() => { if (localDataRef.current === clearedData) { return; } let localData = data !== null && data !== void 0 ? data : defaultData; if (localData && pathSection && !pointer.has(localDataRef.current, pathSection)) { localData = moveValueToPath(pathSection, localData); } internalDataRef.current = Object.assign({}, localData || dataOuter || {}, localDataRef.current); }, [data, defaultData, pathSection, dataOuter, moveValueToPath]); const onCommit = 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 && pointer.has(dataOuter, path) ? pointer.get(dataOuter, path) : dataOuter; localDataRef.current = mountedData; let isolatedData = structuredClone(mountedData); if (typeof transformOnCommitProp === 'function') { isolatedData = transformOnCommitProp(isolatedData, outerData); } let stop = false; additionalArgs.preventCommit = () => stop = true; const commitData = removeSectionPath(isolatedData); const result = 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 : extendDeep({}, outerData, isolatedData))); return result; }, [getMountedData, props.path, dataOuter, transformOnCommitProp, handlePathChangeOuter, onCommitProp, removeSectionPath]); const onClear = useCallback(() => { localDataRef.current = clearedData; internalDataRef.current = clearedData; forceUpdate(); onClearProp === null || onClearProp === void 0 ? void 0 : onClearProp(); }, [onClearProp]); const setShowAllErrorsNested = useCallback(showAllErrors => { var _dataContextRef$curre2, _dataContextRef$curre3; (_dataContextRef$curre2 = dataContextRef.current) === null || _dataContextRef$curre2 === void 0 ? void 0 : (_dataContextRef$curre3 = _dataContextRef$curre2.setShowAllErrors) === null || _dataContextRef$curre3 === void 0 ? void 0 : _dataContextRef$curre3.call(_dataContextRef$curre2, showAllErrors); }, []); if (bubbleValidation && !(outerContext !== null && outerContext !== void 0 && (_outerContext$addSetS = outerContext.addSetShowAllErrorsRef) !== null && _outerContext$addSetS !== void 0 && (_outerContext$addSetS2 = _outerContext$addSetS.current) !== null && _outerContext$addSetS2 !== void 0 && _outerContext$addSetS2.includes(setShowAllErrorsNested))) { var _outerContext$addSetS3; (_outerContext$addSetS3 = outerContext.addSetShowAllErrorsRef) === null || _outerContext$addSetS3 === void 0 ? void 0 : _outerContext$addSetS3.current.push(setShowAllErrorsNested); } const providerProps = _objectSpread(_objectSpread({}, props), {}, { [defaultData ? 'defaultData' : 'data']: internalDataRef.current, onPathChange: onPathChangeHandler, onCommit, onClear, isolate: true }); return React.createElement(Provider, providerProps, React.createElement(DataContext.Consumer, null, dataContext => { dataContextRef.current = dataContext; if (commitHandleRef) { commitHandleRef.current = dataContext === null || dataContext === void 0 ? void 0 : dataContext.handleSubmit; } return children; }), bubbleValidation && React.createElement(BubbleValidation, { outerContext: outerContext })); } function BubbleValidation(_ref) { let { outerContext } = _ref; const { setMountedFieldState, setFieldError } = outerContext || {}; const errors = useContext(DataContext).hasErrors(); const id = useId(); useEffect(() => { const path = `/${id}`; if (errors) { setMountedFieldState === null || setMountedFieldState === void 0 ? void 0 : setMountedFieldState(path, { isMounted: true }); } setFieldError === null || setFieldError === void 0 ? void 0 : setFieldError(path, errors ? new Error('Form.Isolation') : undefined); return () => { setFieldError === null || setFieldError === void 0 ? void 0 : setFieldError(path, undefined); setMountedFieldState === null || setMountedFieldState === void 0 ? void 0 : setMountedFieldState(path, { isMounted: false }); }; }, [errors, id, setFieldError, setMountedFieldState]); return null; } IsolationProvider.CommitButton = IsolationCommitButton; IsolationProvider._supportsSpacingProps = undefined; export default IsolationProvider; //# sourceMappingURL=Isolation.js.map