UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

161 lines (160 loc) 7.71 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); 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"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } 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)(); const { hasError, hasSubmitError } = (0, _react.useContext)(_FieldBoundaryContext.default) || {}; localContextRef.current = (0, _react.useContext)(_IterateItemContext.default) || {}; const nextFocusElementRef = (0, _react.useRef)(); 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'; } } }, [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 === null || switchContainerMode === void 0 || switchContainerMode(editMode, { omitFocusManagement: true, preventUpdate: 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 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) { setTimeout(() => { setOpenState(containerMode === mode); }, openDelay); } else { setOpenState(containerMode === mode); } } } } }, [containerMode, isNew, mode, open, openDelay, setOpenState]); const setFocus = (0, _react.useCallback)(state => { var _localContextRef$curr; if (((_localContextRef$curr = localContextRef.current.modeOptions) === null || _localContextRef$curr === void 0 ? void 0 : _localContextRef$curr.omitFocusManagement) !== true && !hasSubmitError && containerMode === mode && containerMode !== previousContainerMode) { if (state === 'opened') { var _localContextRef$curr2, _localContextRef$curr3; (_localContextRef$curr2 = localContextRef.current.elementRef) === null || _localContextRef$curr2 === void 0 || (_localContextRef$curr2 = _localContextRef$curr2.current) === null || _localContextRef$curr2 === void 0 || (_localContextRef$curr3 = _localContextRef$curr2.focus) === null || _localContextRef$curr3 === void 0 || _localContextRef$curr3.call(_localContextRef$curr2); } else if (state === 'closed') { var _nextFocusElementRef$, _nextFocusElementRef$2; (_nextFocusElementRef$ = nextFocusElementRef.current) === null || _nextFocusElementRef$ === void 0 || (_nextFocusElementRef$2 = _nextFocusElementRef$.focus) === null || _nextFocusElementRef$2 === void 0 || _nextFocusElementRef$2.call(_nextFocusElementRef$); } } }, [containerMode, hasSubmitError, mode, previousContainerMode]); const handleAnimationEnd = (0, _react.useCallback)(state => { if (!openRef.current && isRemoving.current) { var _localContextRef$curr4, _localContextRef$curr5; isRemoving.current = false; (_localContextRef$curr4 = (_localContextRef$curr5 = localContextRef.current).fulfillRemove) === null || _localContextRef$curr4 === void 0 || _localContextRef$curr4.call(_localContextRef$curr5); } setFocus(state); onAnimationEnd === null || onAnimationEnd === void 0 || onAnimationEnd(state); }, [onAnimationEnd, setFocus]); const handleRemoveItem = (0, _react.useCallback)(() => { try { nextFocusElementRef.current = Array.from(localContextRef.current.elementRef.current.parentElement.childNodes).at(index - 1); } catch (e) {} isRemoving.current = true; setOpenState(false); handleRemove === null || handleRemove === void 0 || handleRemove({ keepItems: true }); }, [handleRemove, index, setOpenState]); return _react.default.createElement(_ArrayItemAreaContext.default.Provider, { value: { handleRemoveItem, variant, toolbarVariant, divider: restProps.divider } }, _react.default.createElement(_index.HeightAnimation, { className: (0, _classnames.default)('dnb-forms-section-block', className, variant && `dnb-forms-section-block--variant-${variant}`, isNew && 'dnb-forms-section-block--new', hasSubmitError && 'dnb-forms-section-block--error'), open: openRef.current, onAnimationEnd: handleAnimationEnd, duration: 450, keepInDOM: true }, _react.default.createElement(_index.Card, _extends({ stack: true, filled: variant === 'filled', innerSpace: variant === 'basic' ? false : 'small', className: "dnb-forms-section-block__inner" }, restProps, { "aria-label": ariaLabel }), children))); } ArrayItemArea._supportsSpacingProps = true; var _default = exports.default = ArrayItemArea; //# sourceMappingURL=ArrayItemArea.js.map