UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

166 lines (165 loc) 8.69 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/web.dom-collections.iterator.js"); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _components = require("../../../../components"); var _IterateItemContext = _interopRequireDefault(require("../IterateItemContext")); var _ArrayItemAreaContext = _interopRequireDefault(require("./ArrayItemAreaContext")); var _FieldBoundaryContext = _interopRequireDefault(require("../../DataContext/FieldBoundary/FieldBoundaryContext")); const _excluded = ["mode", "open", "ariaLabel", "onAnimationEnd", "className", "children", "openDelay", "variant", "toolbarVariant"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } const useLayoutEffect = typeof window === 'undefined' ? _react.default.useEffect : _react.default.useLayoutEffect; function ArrayItemArea(props) { const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const { mode, open, ariaLabel, onAnimationEnd, className, children, openDelay = 100, variant = 'outline', toolbarVariant } = props, restProps = _objectWithoutProperties(props, _excluded); 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'; } useLayoutEffect(() => { if (mode === 'edit') { const editMode = determineMode(); if (editMode) { const { switchContainerMode } = localContextRef.current; switchContainerMode === null || switchContainerMode === void 0 ? 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(); }, []); useLayoutEffect(() => { 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$curr4; (_localContextRef$curr2 = localContextRef.current.elementRef) === null || _localContextRef$curr2 === void 0 ? void 0 : (_localContextRef$curr3 = _localContextRef$curr2.current) === null || _localContextRef$curr3 === void 0 ? void 0 : (_localContextRef$curr4 = _localContextRef$curr3.focus) === null || _localContextRef$curr4 === void 0 ? void 0 : _localContextRef$curr4.call(_localContextRef$curr3); } else if (state === 'closed') { var _nextFocusElementRef$, _nextFocusElementRef$2; (_nextFocusElementRef$ = nextFocusElementRef.current) === null || _nextFocusElementRef$ === void 0 ? void 0 : (_nextFocusElementRef$2 = _nextFocusElementRef$.focus) === null || _nextFocusElementRef$2 === void 0 ? void 0 : _nextFocusElementRef$2.call(_nextFocusElementRef$); } } }, [containerMode, hasSubmitError, mode, previousContainerMode]); const handleAnimationEnd = (0, _react.useCallback)(state => { if (!openRef.current && isRemoving.current) { var _localContextRef$curr5, _localContextRef$curr6; isRemoving.current = false; (_localContextRef$curr5 = (_localContextRef$curr6 = localContextRef.current).fulfillRemove) === null || _localContextRef$curr5 === void 0 ? void 0 : _localContextRef$curr5.call(_localContextRef$curr6); } setFocus(state); onAnimationEnd === null || onAnimationEnd === void 0 ? 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 ? 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(_components.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(_components.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