@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
50 lines (49 loc) • 1.96 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["className", "text", "children"];
import "core-js/modules/es.string.replace.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useCallback, useContext, useMemo } from 'react';
import classnames from 'classnames';
import Button from '../../../../components/Button';
import { add } from '../../../../icons';
import IterateItemContext from '../IterateItemContext';
import PushContainerContext from './PushContainerContext';
import { convertJsxToString } from '../../../../shared/component-helper';
function OpenButton(props) {
const {
className,
text,
children
} = props,
restProps = _objectWithoutProperties(props, _excluded);
const {
switchContainerMode
} = useContext(IterateItemContext) || {};
const {
entries
} = useContext(PushContainerContext) || {};
const handleClick = useCallback(() => {
switchContainerMode === null || switchContainerMode === void 0 ? void 0 : switchContainerMode('edit');
}, [switchContainerMode]);
const content = useMemo(() => {
if (children || text) {
const str = convertJsxToString(children || text);
if (str.includes('{nextItemNo}')) {
const nextItemNo = ((entries === null || entries === void 0 ? void 0 : entries.length) || 0) + 1;
return str.replace('{nextItemNo}', String(nextItemNo));
}
}
return children || text;
}, [entries === null || entries === void 0 ? void 0 : entries.length, children, text]);
return React.createElement(Button, _extends({
className: classnames('dnb-forms-iterate__open-button', className),
variant: "secondary",
icon: add,
icon_position: "left",
on_click: handleClick
}, restProps), content);
}
export default OpenButton;
//# sourceMappingURL=OpenButton.js.map