UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

97 lines (96 loc) 5.55 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 _SectionContainerContext = _interopRequireDefault(require("./SectionContainerContext")); var _FieldBoundaryContext = _interopRequireDefault(require("../../../DataContext/FieldBoundary/FieldBoundaryContext")); const _excluded = ["mode", "open", "ariaLabel", "onAnimationEnd", "className", "children", "variant", "omitFocusManagementRef"]; 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; } function SectionContainer(props) { const { mode, open, ariaLabel, onAnimationEnd, className, children, variant = 'outline', omitFocusManagementRef = { current: undefined } } = props, restProps = _objectWithoutProperties(props, _excluded); const [, forceUpdate] = (0, _react.useReducer)(() => ({}), {}); const containerRef = (0, _react.useRef)(); const contextRef = (0, _react.useRef)(); contextRef.current = (0, _react.useContext)(_SectionContainerContext.default) || {}; const { hasError, hasSubmitError } = (0, _react.useContext)(_FieldBoundaryContext.default) || {}; contextRef.current.hasError = hasError; contextRef.current.hasSubmitError = hasSubmitError; if (hasSubmitError) { contextRef.current.containerMode = 'edit'; } const { containerMode } = contextRef.current; const openRef = (0, _react.useRef)(open !== null && open !== void 0 ? open : containerMode === mode); const setOpenState = (0, _react.useCallback)(open => { openRef.current = open; forceUpdate(); }, []); (0, _react.useEffect)(() => { if (typeof open !== 'undefined') { setOpenState(open); } else { if (openRef.current !== (containerMode === mode)) { setOpenState(containerMode === mode); } } }, [containerMode, mode, open, setOpenState]); const setFocus = (0, _react.useCallback)(state => { if (state === 'opened') { if (!omitFocusManagementRef.current && !contextRef.current.hasSubmitError) { var _containerRef$current, _containerRef$current2; containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : (_containerRef$current2 = _containerRef$current.focus) === null || _containerRef$current2 === void 0 ? void 0 : _containerRef$current2.call(_containerRef$current); } omitFocusManagementRef.current = false; } }, [omitFocusManagementRef]); const handleAnimationEnd = (0, _react.useCallback)(state => { setFocus(state); onAnimationEnd === null || onAnimationEnd === void 0 ? void 0 : onAnimationEnd(state); }, [onAnimationEnd, setFocus]); return _react.default.createElement(_components.HeightAnimation, { className: (0, _classnames.default)('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 }, _react.default.createElement(_components.Card, _extends({ stack: true, innerSpace: variant === 'basic' ? false : 'small', filled: variant === 'filled', className: "dnb-forms-section-block__inner" }, restProps, { "aria-label": ariaLabel, innerRef: containerRef, tabIndex: -1 }), children)); } SectionContainer._supportsSpacingProps = true; var _default = exports.default = SectionContainer; //# sourceMappingURL=SectionContainer.js.map