UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

72 lines (71 loc) 3.23 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["text", "children", "className", "showConfirmDialog"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback, useContext } from 'react'; import classnames from 'classnames'; import { Button, Dialog } from '../../../../components'; import IterateItemContext from '../IterateItemContext'; import { useTranslation } from '../../hooks'; import ArrayItemAreaContext from '../Array/ArrayItemAreaContext'; import { omitDataValueReadWriteProps } from '../../types'; import { trash } from '../../../../icons'; function RemoveButton(props) { const iterateItemContext = useContext(IterateItemContext); const { handleRemove, itemPath } = iterateItemContext || {}; if (!iterateItemContext) { throw new Error('RemoveButton must be inside an Iterate.Array'); } const { text, children, className, showConfirmDialog } = props, restProps = _objectWithoutProperties(props, _excluded); const buttonProps = omitDataValueReadWriteProps(restProps); const translation = useTranslation().RemoveButton; const textContent = text || children || translation.text; const arrayItemAreaContext = useContext(ArrayItemAreaContext); const { handleRemoveItem } = arrayItemAreaContext || {}; const handleClick = useCallback(_ref => { let { close } = _ref; close === null || close === void 0 ? void 0 : close(); if (handleRemoveItem && !itemPath) { handleRemoveItem(); } else { handleRemove === null || handleRemove === void 0 ? void 0 : handleRemove(); } }, [handleRemove, handleRemoveItem, itemPath]); const triggerAttributes = _objectSpread({ className: classnames('dnb-forms-iterate-remove-element-button', className), text: textContent, variant: textContent ? 'tertiary' : 'secondary', icon: trash, icon_position: 'left' }, buttonProps); if (showConfirmDialog) { return React.createElement(Dialog, { variant: "confirmation", title: translation.confirmRemoveText, triggerAttributes: triggerAttributes, onConfirm: handleClick }); } return React.createElement(Button, _extends({}, triggerAttributes, { on_click: handleClick }, buttonProps)); } RemoveButton._supportsSpacingProps = true; export default RemoveButton; //# sourceMappingURL=RemoveButton.js.map