UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

79 lines (78 loc) 3.1 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _Fragment, _Toolbar; const _excluded = ["toolbar", "toolbarVariant", "children"], _excluded2 = ["children", "className", "title", "titleWhenNew", "toolbar", "toolbarVariant"]; import React, { useContext, useMemo } from 'react'; import classnames from 'classnames'; import { convertJsxToString } from '../../../../shared/component-helper'; import { Lead } from '../../../../elements'; import IterateItemContext from '../IterateItemContext'; import ArrayItemArea from '../Array/ArrayItemArea'; import Toolbar from '../Toolbar'; import { useSwitchContainerMode } from '../hooks'; import DoneButton from './DoneButton'; import CancelButton, { useWasNew } from './CancelButton'; import { replaceItemNo } from '../ItemNo'; export default function EditContainer(props) { const { toolbar, toolbarVariant, children } = props, rest = _objectWithoutProperties(props, _excluded); const { arrayValue } = useContext(IterateItemContext); let toolbarElement = toolbar; if (toolbarVariant === 'minimumOneItem' && arrayValue.length <= 1) { toolbarElement = _Fragment || (_Fragment = React.createElement(React.Fragment, null)); } const hasToolbar = !toolbarElement && React.Children.toArray(children).some(child => { return (child === null || child === void 0 ? void 0 : child['type']) === Toolbar; }); return React.createElement(EditContainerWithoutToolbar, _extends({ toolbar: hasToolbar ? null : toolbarElement !== null && toolbarElement !== void 0 ? toolbarElement : toolbarVariant !== 'custom' && (_Toolbar || (_Toolbar = React.createElement(Toolbar, null, React.createElement(DoneButton, null), React.createElement(CancelButton, null)))), toolbarVariant: toolbarVariant }, rest), children); } export function EditContainerWithoutToolbar(props) { const { containerMode, isNew, index, path } = useContext(IterateItemContext); const _ref = props || {}, { children, className, title, titleWhenNew, toolbar, toolbarVariant } = _ref, restProps = _objectWithoutProperties(_ref, _excluded2); const wasNew = useWasNew({ isNew, containerMode }); const itemTitle = useMemo(() => { return replaceItemNo(wasNew && titleWhenNew ? titleWhenNew : title, index); }, [index, title, titleWhenNew, wasNew]); useSwitchContainerMode(path); return React.createElement(ArrayItemArea, _extends({ mode: "edit", className: classnames('dnb-forms-section-edit-block', className), ariaLabel: convertJsxToString(itemTitle), toolbarVariant: toolbarVariant }, restProps), itemTitle && React.createElement(Lead, { size: "basis" }, itemTitle), children, toolbar); } EditContainer.DoneButton = DoneButton; EditContainer.CancelButton = CancelButton; EditContainer._supportsSpacingProps = true; EditContainerWithoutToolbar._supportsSpacingProps = true; //# sourceMappingURL=EditContainer.js.map