UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

49 lines (48 loc) 2.01 kB
"use client"; import { useContext, useMemo, useRef } from 'react'; import { createSharedState, createReferenceKey, useSharedState } from "../../../../shared/helpers/useSharedState.js"; import DataContext from "../../DataContext/Context.js"; import DataContextRefContext from "../../DataContext/DataContextRefContext.js"; import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js"; import FormElement from "../Element/index.js"; import { jsx as _jsx } from "react/jsx-runtime"; const invalidOutletErrorMessage = 'Form.Outlet needs a valid formHandlerId that points to a mounted Form.Handler'; function Outlet(props) { const { formHandlerId, children } = props; const outerContext = useContext(DataContext); const isInsideLinkedHandler = outerContext?.hasContext && outerContext?.id === formHandlerId; const { data: linkedAttachments } = useSharedState(createReferenceKey(formHandlerId, 'attachments')); const linkedValidationVersion = linkedAttachments?.validationVersion; const linkedShowAllErrors = linkedAttachments?.showAllErrors; const dataContextStore = useMemo(() => createSharedState(createReferenceKey(formHandlerId, 'data-context')), [formHandlerId]); const dataContextRef = useRef(undefined); const dataContext = useMemo(() => { void linkedValidationVersion; void linkedShowAllErrors; const context = dataContextStore.get(); if (!context?.hasContext) { throw new Error(invalidOutletErrorMessage); } return context; }, [dataContextStore, linkedValidationVersion, linkedShowAllErrors]); dataContextRef.current = dataContext; return _jsx(DataContext.Provider, { value: dataContext, children: _jsx(DataContextRefContext, { value: dataContextRef, children: isInsideLinkedHandler ? children : _jsx(FormElement, { children: children }) }) }); } withComponentMarkers(Outlet, { _supportsSpacingProps: 'children' }); export default Outlet; //# sourceMappingURL=Outlet.js.map