UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

175 lines (174 loc) 6.39 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("react"); var _clsx = require("clsx"); var _index = require("../../../../components/index.js"); var _IterateItemContext = _interopRequireDefault(require("../IterateItemContext.js")); var _ArrayItemAreaContext = _interopRequireDefault(require("./ArrayItemAreaContext.js")); var _FieldBoundaryContext = _interopRequireDefault(require("../../DataContext/FieldBoundary/FieldBoundaryContext.js")); var _useIsomorphicLayoutEffect = require("../../../../shared/helpers/useIsomorphicLayoutEffect.js"); var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function ArrayItemArea(props) { const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const { mode, open, ariaLabel, onAnimationEnd, className, children, openDelay = 100, variant = 'outline', toolbarVariant, ...restProps } = props; const localContextRef = (0, _react.useRef)(undefined); const { hasError, hasSubmitError } = (0, _react.useContext)(_FieldBoundaryContext.default) || {}; localContextRef.current = (0, _react.useContext)(_IterateItemContext.default) || {}; const nextFocusElementRef = (0, _react.useRef)(undefined); const { isNew } = localContextRef.current; const determineMode = (0, _react.useCallback)(() => { const { value, initialContainerMode } = localContextRef.current; if (initialContainerMode === 'auto') { if (hasSubmitError || hasError || !value || typeof value === 'object' && Object.keys(value).length === 0) { return 'edit'; } } return undefined; }, [hasError, hasSubmitError]); if (determineMode() === 'edit') { localContextRef.current.containerMode = 'edit'; if (!localContextRef.current.modeOptions) { localContextRef.current.modeOptions = {}; } localContextRef.current.modeOptions.omitFocusManagement = true; } if (localContextRef.current.containerMode === 'auto') { localContextRef.current.containerMode = 'view'; } (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { if (mode === 'edit') { const editMode = determineMode(); if (editMode) { const { switchContainerMode } = localContextRef.current; switchContainerMode?.(editMode, { omitFocusManagement: true }); } } }, [determineMode, mode]); const { handleRemove, index, previousContainerMode, containerMode } = localContextRef.current; const openRef = (0, _react.useRef)(open !== null && open !== void 0 ? open : containerMode === mode && !isNew); const isRemoving = (0, _react.useRef)(false); const openDelayRef = (0, _react.useRef)(undefined); const setOpenState = (0, _react.useCallback)(open => { openRef.current = open; forceUpdate(); }, []); (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { if (!isRemoving.current) { if (typeof open !== 'undefined') { setOpenState(open); } else { if (openRef.current !== (containerMode === mode)) { if (isNew) { openDelayRef.current = setTimeout(() => { setOpenState(containerMode === mode); }, openDelay); } else { setOpenState(containerMode === mode); } } } } return () => { clearTimeout(openDelayRef.current); }; }, [containerMode, isNew, mode, open, openDelay, setOpenState]); const setFocus = (0, _react.useCallback)(state => { if (localContextRef.current.modeOptions?.omitFocusManagement !== true && !hasSubmitError && containerMode === mode && containerMode !== previousContainerMode) { if (state === 'opened') { localContextRef.current.elementRef?.current?.focus?.(); } else if (state === 'closed') { nextFocusElementRef.current?.focus?.(); } } }, [containerMode, hasSubmitError, mode, previousContainerMode]); const handleAnimationEnd = (0, _react.useCallback)(state => { if (!openRef.current && isRemoving.current) { isRemoving.current = false; localContextRef.current.fulfillRemove?.(); const schedule = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : callback => setTimeout(callback); schedule(() => { setFocus(state); }); } else { setFocus(state); } onAnimationEnd?.(state); }, [onAnimationEnd, setFocus]); const handleRemoveItem = (0, _react.useCallback)(() => { try { const containerElement = localContextRef.current.containerRef.current; const elements = Array.from(containerElement.querySelectorAll('.dnb-forms-iterate__element')).filter(element => { return element.closest('.dnb-forms-iterate') === containerElement; }); nextFocusElementRef.current = elements.at(index - 1); } catch (e) {} isRemoving.current = true; setOpenState(false); handleRemove?.({ keepItems: true }); }, [handleRemove, index, setOpenState]); return (0, _jsxRuntime.jsx)(_ArrayItemAreaContext.default, { value: { handleRemoveItem, variant, toolbarVariant, divider: restProps.divider }, children: (0, _jsxRuntime.jsx)(_index.HeightAnimation, { className: (0, _clsx.clsx)('dnb-forms-section-block', variant && `dnb-forms-section-block--variant-${variant}`, isNew && 'dnb-forms-section-block--new', hasSubmitError && 'dnb-forms-section-block--error', className), open: openRef.current, onAnimationEnd: handleAnimationEnd, duration: 450, keepInDOM: true, children: (0, _jsxRuntime.jsx)(_index.Card, { stack: true, filled: variant === 'filled', innerSpace: variant === 'basic' ? false : 'small', className: "dnb-forms-section-block__inner", ...restProps, "aria-label": ariaLabel, children: children }) }) }); } (0, _withComponentMarkers.default)(ArrayItemArea, { _supportsSpacingProps: true }); var _default = exports.default = ArrayItemArea; //# sourceMappingURL=ArrayItemArea.js.map