@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
74 lines (73 loc) • 2.3 kB
JavaScript
"use client";
import React, { useCallback, useContext } from 'react';
import clsx from 'clsx';
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";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
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: clsx('dnb-forms-iterate-remove-element-button', className),
text: replaceItemNo(textContent, index),
variant: textContent ? 'tertiary' : 'secondary',
icon: trash,
iconPosition: 'left',
...buttonProps
};
if (showConfirmDialog) {
return _jsx(Dialog, {
variant: "confirmation",
title: translation.confirmRemoveText,
triggerAttributes: triggerAttributes,
onConfirm: handleClick
});
}
return _jsx(Button, {
...triggerAttributes,
onClick: args => handleClick(args),
...buttonProps
});
}
withComponentMarkers(RemoveButton, {
_supportsSpacingProps: true
});
export default RemoveButton;
//# sourceMappingURL=RemoveButton.js.map