UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

201 lines (200 loc) 9.54 kB
"use client"; var _BubbleValidation; import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import React, { useCallback, useContext, useMemo, useReducer, useRef, useState } from 'react'; import useMountEffect from "../../../../shared/helpers/useMountEffect.js"; import pointer from "../../utils/json-pointer/index.js"; import { isZodSchema } from "../../utils/zod.js"; import { extractZodSubSchema } from "./extractZodSubSchema.js"; import { extendDeep } from "../../../../shared/component-helper.js"; import { isAsync } from "../../../../shared/helpers/isAsync.js"; import useDataValue from "../../hooks/useDataValue.js"; import { Context as DataContext, Provider } from "../../DataContext/index.js"; import SectionContext from "../Section/SectionContext.js"; import useReportError from "./useReportError.js"; import IsolationCommitButton from "./IsolationCommitButton.js"; import IsolationResetButton from "./IsolationResetButton.js"; import { clearedData } from "../../DataContext/Provider/index.js"; import { createDataReference } from "./IsolationDataReference.js"; import IsolatedContainer, { isolationError } from "./IsolatedContainer.js"; import IsolationContext from "./IsolationContext.js"; import { structuredClone } from "../../../../shared/helpers/structuredClone.js"; function IsolationProvider(props) { var _outerContext$props, _outerContext$props2, _outerContext$props3, _IsolatedContainer; const [dataReferenceFallback] = useState(() => { if (!(props !== null && props !== void 0 && props.dataReference)) { return createDataReference(); } }); const { children, onPathChange, onCommit: onCommitProp, onClear: onClearProp, transformOnCommit: transformOnCommitProp, commitHandleRef, bubbleValidation, preventUncommittedChanges, data, defaultData, dataReference = dataReferenceFallback, resetDataAfterCommit } = 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 onUpdateDataValueHandler = useCallback(async (path, value, { preventUpdate = undefined } = {}) => { if (internalDataRef.current === clearedData) { internalDataRef.current = {}; } pointer.set(internalDataRef.current, path, value); if (!preventUpdate) { forceUpdate(); } }, []); 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 || _dataContextRef$curre.mountedFieldsRef.current.forEach((field, path) => { if (field.isMounted && pointer.has(data, path)) { pointer.set(mounterData, path, pointer.get(data, path)); } }); return mounterData; }, []); useMountEffect(() => { localDataRef.current = getMountedData(internalDataRef.current); }); 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 || structuredClone(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 setIsolatedData = useCallback(data => { localDataRef.current = data; internalDataRef.current = data; }, []); const onClear = useCallback(() => { setIsolatedData(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 (isZodSchema(outerContext.props.schema)) { providerProps.schema = extractZodSubSchema(outerContext.props.schema, props.path); } else { providerProps.schema = { $defs: { root: outerContext.props.schema }, $ref: `#/$defs/root${props.path.split('/').join('/properties/')}` }; } } return React.createElement(Provider, providerProps, React.createElement(IsolationContext.Provider, { value: { preventUncommittedChanges, dataReference, resetDataAfterCommit, outerContext, setIsolatedData } }, React.createElement(DataContext.Consumer, null, dataContext => { dataContextRef.current = dataContext; if (commitHandleRef) { commitHandleRef.current = dataContext === null || dataContext === void 0 ? void 0 : dataContext.handleSubmit; } return _IsolatedContainer || (_IsolatedContainer = React.createElement(IsolatedContainer, null, children, " ")); }), bubbleValidation && (_BubbleValidation || (_BubbleValidation = React.createElement(BubbleValidation, null))))); } function BubbleValidation() { var _addSetShowAllErrorsR; const innerContext = useContext(DataContext); const { outerContext } = useContext(IsolationContext); const { setShowAllErrors } = innerContext; const setShowAllErrorsNested = 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 || _pushInstanceProperty(_context = addSetShowAllErrorsRef.current).call(_context, setShowAllErrorsNested); } useReportError(innerContext.hasErrors() ? isolationError : undefined, outerContext, 'isolation'); return null; } IsolationProvider.CommitButton = IsolationCommitButton; IsolationProvider.ResetButton = IsolationResetButton; IsolationProvider.createDataReference = createDataReference; IsolationProvider._supportsSpacingProps = undefined; export default IsolationProvider; //# sourceMappingURL=Isolation.js.map