UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

97 lines (96 loc) 3.69 kB
"use client"; import React, { useCallback, useContext, useEffect, useReducer, useRef } from 'react'; import clsx from 'clsx'; import { Card, HeightAnimation } from "../../../../../components/index.js"; import SectionContainerContext from "./SectionContainerContext.js"; import FieldBoundaryContext from "../../../DataContext/FieldBoundary/FieldBoundaryContext.js"; import withComponentMarkers from "../../../../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx } from "react/jsx-runtime"; function SectionContainer(props) { const { mode, open, ariaLabel, onAnimationEnd, className, children, variant = 'outline', omitFocusManagementRef = { current: undefined }, ...restProps } = props; const [, forceUpdate] = useReducer(() => ({}), {}); const containerRef = useRef(undefined); const contextRef = useRef(undefined); contextRef.current = useContext(SectionContainerContext) || {}; const { hasError, hasSubmitError } = useContext(FieldBoundaryContext) || {}; contextRef.current.hasError = hasError; contextRef.current.hasSubmitError = hasSubmitError; const { containerMode, switchContainerMode, disableEditing } = contextRef.current; const openRef = useRef(open !== null && open !== void 0 ? open : containerMode === mode); const setOpenState = useCallback(open => { openRef.current = open; forceUpdate(); }, []); useEffect(() => { if (typeof open !== 'undefined') { setOpenState(open); } else { if (openRef.current !== (containerMode === mode)) { setOpenState(containerMode === mode); } } }, [containerMode, mode, open, setOpenState]); useEffect(() => { if (disableEditing) { return; } if (hasSubmitError && containerMode !== 'edit') { switchContainerMode === null || switchContainerMode === void 0 || switchContainerMode('edit'); } }, [disableEditing, hasSubmitError, containerMode, switchContainerMode]); const setFocus = useCallback(state => { if (state === 'opened') { if (!omitFocusManagementRef.current && !contextRef.current.hasSubmitError) { var _containerRef$current, _containerRef$current2; containerRef === null || containerRef === void 0 || (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 || (_containerRef$current2 = _containerRef$current.focus) === null || _containerRef$current2 === void 0 || _containerRef$current2.call(_containerRef$current); } omitFocusManagementRef.current = false; } }, [omitFocusManagementRef]); const handleAnimationEnd = useCallback(state => { setFocus(state); onAnimationEnd === null || onAnimationEnd === void 0 || onAnimationEnd(state); }, [onAnimationEnd, setFocus]); return _jsx(HeightAnimation, { className: clsx('dnb-forms-section-block', className, variant && `dnb-forms-section-block--variant-${variant}`, omitFocusManagementRef.current && 'dnb-forms-section-block--no-animation', contextRef.current.hasSubmitError && 'dnb-forms-section-block--error'), open: openRef.current, onAnimationEnd: handleAnimationEnd, duration: 450, keepInDOM: true, children: _jsx(Card, { stack: true, innerSpace: variant === 'basic' ? false : 'small', filled: variant === 'filled', className: "dnb-forms-section-block__inner", ...restProps, "aria-label": ariaLabel, ref: containerRef, tabIndex: -1, children: children }) }); } withComponentMarkers(SectionContainer, { _supportsSpacingProps: true }); export default SectionContainer; //# sourceMappingURL=SectionContainer.js.map