A full-featured CRM built with React, shadcn/ui, and Supabase.
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Button } from "@/components/ui/button";
import { Trash } from "lucide-react";
import type { RaRecord, UseBulkDeleteControllerParams } from "ra-core";
import {
useBulkDeleteController,
useGetResourceLabel,
useResourceContext,
useResourceTranslation,
} from "ra-core";
import { cn } from "@/lib/utils";
import type { ReactNode } from "react";
/**
* A button that deletes multiple selected records at once.
*
* Allows to delete selected records in a DataTable. Use within
* the bulkActionsButtons prop of DataTable or inside BulkActionsToolbar.
*
* @see {@link https://marmelab.com/shadcn-admin-kit/docs/bulkdeletebutton/ BulkDeleteButton documentation}
*
* @example
* import { BulkDeleteButton, BulkExportButton, DataTable, List } from '@/components/admin';
*
* export const PostList = () => (
* <List>
* <DataTable
* bulkActionsButtons={
* <>
* <BulkExportButton />
* <BulkDeleteButton />
* </>
* }
* >
* ...
* </DataTable>
* </List>
* );
*/
export const BulkDeleteButton = <
RecordType extends RaRecord = any,
MutationOptionsError = unknown,
>({
icon = defaultIcon,
label: labelProp,
className,
...props
}: BulkDeleteButtonProps<RecordType, MutationOptionsError>) => {
const { handleDelete, isPending } = useBulkDeleteController(props);
const resource = useResourceContext(props);
const getResourceLabel = useGetResourceLabel();
const label = useResourceTranslation({
resourceI18nKey: resource
? `resources.${resource}.action.delete`
: undefined,
baseI18nKey: "ra.action.delete",
options: {
name: resource ? getResourceLabel(resource, 1) : undefined,
},
userText: labelProp,
});
return (
<Button
variant="destructive"
type="button"
onClick={handleDelete}
disabled={isPending}
aria-label={typeof label === "string" ? label : undefined}
className={cn("h-9", className)}
>
{icon}
{label}
</Button>
);
};
export type BulkDeleteButtonProps<
RecordType extends RaRecord = any,
MutationOptionsError = unknown,
> = {
label?: string;
icon?: ReactNode;
} & React.ComponentPropsWithoutRef<"button"> &
UseBulkDeleteControllerParams<RecordType, MutationOptionsError>;
const defaultIcon = <Trash />;