Skip to main content
Version: 4.xx.xxSwizzle Ready

Delete

<DeleteButton> uses Material UI <LoadingButton> and <Dialog> components. When you try to delete something, a pop-up shows up and asks for confirmation. When confirmed it executes the useDelete method provided by your dataProvider.

Swizzle

You can swizzle this component to customize it with the refine CLI

Usage

http://localhost:3000
import {
useDataGrid,
List,
DeleteButton,
} from "@refinedev/mui";
import { DataGrid, GridColumns } from "@mui/x-data-grid";

const columns: GridColumns = [
{ field: "id", headerName: "ID", type: "number" },
{ field: "title", headerName: "Title", minWidth: 400, flex: 1 },
{
field: "actions",
headerName: "Actions",
renderCell: function render({ row }) {
return <DeleteButton size="small" recordItemId={row.id} />;
},
align: "center",
headerAlign: "center",
minWidth: 80,
},
];

const PostsList: React.FC = () => {
const { dataGridProps } = useDataGrid<IPost>();

return (
<List>
<DataGrid {...dataGridProps} columns={columns} autoHeight />
</List>
);
};

interface IPost {
id: number;
title: string;
}

Properties

recordItemId

recordItemId allows us to manage which record will be deleted.

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return <DeleteButton resource="posts" recordItemId="1" />;
};

Clicking the button will trigger the useDelete method and then the record whose resource is post and whose id is 1 gets deleted.

note

<DeleteButton> component reads the id information from the route by default.

resource

resource allows us to manage which resource's record is going to be deleted.

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return (
<DeleteButton resource="categories" recordItemId="2" />
);
};

onSuccess

onSuccess can be used if you want to do anything on the result returned after the delete request.

For example, let's console.log after deletion:

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return (
<DeleteButton
resource="posts"
recordItemId="1"
onSuccess={(value) => {
console.log(value);
}}
/>
);
};

mutationMode

Determines which mode mutation will have while executing <DeleteButton>.

Refer to the mutation mode docs for further information. &#8594

import { useTable } from "@refinedev/core";

import {
List,
DeleteButton,
} from "@refinedev/mui";
import {
Table,
TableHead,
TableRow,
TableCell,
TableBody,
} from "@mui/x-data-grid";

export const PostList: React.FC = () => {
const { tableQueryResult } = useTable<IPost>();

const { data } = tableQueryResult;

return (
<List>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>ID</TableCell>
<TableCell>Title</TableCell>
<TableCell align="center">Action</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data?.data.map((row) => (
<TableRow key={row.id}>
<TableCell>{row.id}</TableCell>
<TableCell component="th" scope="row">
{row.title}
</TableCell>
<TableCell align="center">
<DeleteButton
recordItemId={row.id}
mutationMode="undoable"
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
}

hideText

It is used to show and not show the text of the button. When true, only the button icon is visible.

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return (
<DeleteButton
hideText={true}
/>
);
};

accessControl

This prop can be used to skip access control check with its enabled property or to hide the button when the user does not have the permission to access the resource with hideIfUnauthorized property. This is relevant only when an accessControlProvider is provided to <Refine/>

import { DeleteButton } from "@refinedev/mui";

export const MyListComponent = () => {
return (
<DeleteButton
accessControl={{ enabled: true, hideIfUnauthorized: true }}
/>
);
};

resourceNameOrRouteName
deprecated

resourceNameOrRouteName prop is deprecated. Use resource prop instead.

resourceNameOrRouteName allows us to manage which resource's record is going to be deleted.

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return (
<DeleteButton resourceNameOrRouteName="categories" recordItemId="2" />
);
};

How to override confirm texts?

You can change the text that appears when you confirm a transaction with confirmTitle prop, as well as what ok and cancel buttons text look like with confirmOkText and confirmCancelText props.

http://localhost:3000
import { DeleteButton } from "@refinedev/mui";

const MyDeleteComponent = () => {
return (
<DeleteButton
confirmTitle="Title"
confirmOkText="Ok Text"
confirmCancelText="Delete Text"
/>
);
};

API Reference

Props

External Props

It also accepts all props of Material UI Button.