-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generalise bulk delete button across UI pages [WD-19546] (#1123)
- Loading branch information
Showing
13 changed files
with
296 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { FC } from "react"; | ||
import { pluralize } from "util/instanceBulkActions"; | ||
import { | ||
ConfirmationButton, | ||
ConfirmationButtonProps, | ||
Icon, | ||
} from "@canonical/react-components"; | ||
import classnames from "classnames"; | ||
|
||
interface Props { | ||
entities: unknown[]; | ||
deletableEntities: unknown[]; | ||
entityType: string; | ||
onDelete: () => void; | ||
disabledReason?: string; | ||
bulkDeleteBreakdown?: string[]; | ||
confirmationButtonProps?: Partial<ConfirmationButtonProps>; | ||
buttonLabel?: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
const BulkDeleteButton: FC<Props> = ({ | ||
entities, | ||
deletableEntities, | ||
disabledReason, | ||
entityType, | ||
bulkDeleteBreakdown, | ||
confirmationButtonProps, | ||
onDelete, | ||
buttonLabel = "Delete", | ||
className, | ||
}) => { | ||
const totalCount = entities.length; | ||
const deleteCount = deletableEntities.length; | ||
|
||
const breakdown = | ||
bulkDeleteBreakdown?.map((action) => { | ||
return ( | ||
<li key={action} className="p-list__item"> | ||
- {action} | ||
</li> | ||
); | ||
}) || []; | ||
|
||
const modalContent = ( | ||
<> | ||
{breakdown.length > 0 && ( | ||
<> | ||
<p> | ||
<b>{totalCount}</b> {pluralize(entityType, entities.length)}{" "} | ||
selected: | ||
</p> | ||
<ul className="p-list">{breakdown}</ul> | ||
</> | ||
)} | ||
<p className="u-no-padding--top"> | ||
This will permanently delete <b>{deleteCount}</b>{" "} | ||
{pluralize(entityType, deleteCount)}.{"\n"}This action cannot be undone, | ||
and can result in data loss. | ||
</p> | ||
</> | ||
); | ||
|
||
return ( | ||
<ConfirmationButton | ||
className={classnames("has-icon", className)} | ||
onHoverText={ | ||
disabledReason ?? `Delete ${pluralize(entityType, entities.length)}` | ||
} | ||
disabled={deleteCount === 0 || !!disabledReason} | ||
shiftClickEnabled | ||
showShiftClickHint | ||
{...confirmationButtonProps} | ||
confirmationModalProps={{ | ||
title: "Confirm delete", | ||
children: modalContent, | ||
confirmButtonLabel: "Delete", | ||
onConfirm: onDelete, | ||
}} | ||
> | ||
<Icon name="delete" /> | ||
<span>{buttonLabel}</span> | ||
</ConfirmationButton> | ||
); | ||
}; | ||
|
||
export default BulkDeleteButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.