1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 21:39:40 +02:00
portainer/app/react/components/buttons/DeleteButton.tsx
Steven Kang ea228c3d6d
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com>
Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io>
Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com>
Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
2024-10-01 14:15:51 +13:00

85 lines
1.8 KiB
TypeScript

import { Trash2 } from 'lucide-react';
import { ComponentProps, PropsWithChildren, ReactNode } from 'react';
import { AutomationTestingProps } from '@/types';
import { confirmDelete } from '@@/modals/confirm';
import { Button } from './Button';
import { LoadingButton } from './LoadingButton';
type ConfirmOrClick =
| {
confirmMessage: ReactNode;
onConfirmed(): Promise<void> | void;
onClick?: never;
}
| {
confirmMessage?: never;
onConfirmed?: never;
/** if onClick is set, will skip confirmation (confirmation should be done on the parent) */
onClick(): void;
};
export function DeleteButton({
disabled,
size,
children,
isLoading,
loadingText = 'Removing',
'data-cy': dataCy,
...props
}: PropsWithChildren<
AutomationTestingProps &
ConfirmOrClick & {
size?: ComponentProps<typeof Button>['size'];
disabled?: boolean;
isLoading?: boolean;
loadingText?: string;
}
>) {
if (isLoading === undefined) {
return (
<Button
size={size}
color="dangerlight"
disabled={disabled || isLoading}
onClick={() => handleClick()}
icon={Trash2}
className="!m-0"
data-cy={dataCy}
>
{children || 'Remove'}
</Button>
);
}
return (
<LoadingButton
size={size}
color="dangerlight"
disabled={disabled}
onClick={() => handleClick()}
icon={Trash2}
className="!m-0"
data-cy={dataCy}
isLoading={isLoading}
loadingText={loadingText}
>
{children || 'Remove'}
</LoadingButton>
);
async function handleClick() {
const { confirmMessage, onConfirmed, onClick } = props;
if (onClick) {
return onClick();
}
if (!(await confirmDelete(confirmMessage))) {
return undefined;
}
return onConfirmed();
}
}