@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
69 lines (68 loc) • 2.23 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import React, { useCallback, useContext } from 'react';
import classnames from 'classnames';
import { Button, Dialog } from "../../../../components/index.js";
import IterateItemContext from "../IterateItemContext.js";
import { replaceItemNo } from "../ItemNo/index.js";
import { useTranslation } from "../../hooks/index.js";
import ArrayItemAreaContext from "../Array/ArrayItemAreaContext.js";
import { omitDataValueReadWriteProps } from "../../types.js";
import { trash } from "../../../../icons/index.js";
function RemoveButton(props) {
const iterateItemContext = useContext(IterateItemContext);
const {
handleRemove,
itemPath,
index
} = iterateItemContext || {};
if (!iterateItemContext) {
throw new Error('RemoveButton must be inside an Iterate.Array');
}
const {
text,
children,
className,
showConfirmDialog,
...restProps
} = props;
const buttonProps = omitDataValueReadWriteProps(restProps);
const translation = useTranslation().RemoveButton;
const textContent = text || children || translation.text;
const arrayItemAreaContext = useContext(ArrayItemAreaContext);
const {
handleRemoveItem
} = arrayItemAreaContext || {};
const handleClick = useCallback(({
close
}) => {
close?.();
if (handleRemoveItem && !itemPath) {
handleRemoveItem();
} else {
handleRemove?.();
}
}, [handleRemove, handleRemoveItem, itemPath]);
const triggerAttributes = {
className: classnames('dnb-forms-iterate-remove-element-button', className),
text: replaceItemNo(textContent, index),
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