Back to React Admin

The Confirm Component

docs/Confirm.md

5.14.66.3 KB
Original Source

<Confirm>

<Confirm> leverages Material UI's <Dialog> component to implement a confirmation popup.

Usage

To ask a confirmation to the user before performing an action, have the action button open a <Confirm>.

For instance, here is how to build a delete button that removes the record after asking for confirmation:

jsx
import { useState } from 'react';
import {
    Button,
    Confirm,
    useRecordContext,
    useDelete,
} from 'react-admin';

const BulkResetViewsButton = () => {
    const record = useRecordContext();
    const [open, setOpen] = useState(false);

    const [remove, { isPending }] = useDelete(
        'posts',
        { id: record && record.id }
    );

    const handleClick = () => setOpen(true);
    const handleDialogClose = () => setOpen(false);
    const handleConfirm = () => {
        remove();
        setOpen(false);
    };

    return (
        <>
            <Button label="Delete" onClick={handleClick} />
            <Confirm
                isOpen={open}
                loading={isPending}
                title={`Delete post #${record && record.id}`}
                content="Are you sure you want to delete this item?"
                onConfirm={handleConfirm}
                onClose={handleDialogClose}
            />
        </>
    );
};

Props

PropRequiredTypeDefaultDescription
titleRequiredstring-Title of the dialog
contentRequiredReactNode-Body of the dialog
onCloseRequiredMouse EventHandler-onClick event handler of the cancel button
onConfirmRequiredMouse EventHandler-onClick event handler of the confirm button
isOpenOptionalbooleanfalsetrue to show the dialog, false to hide it
loadingOptionalbooleanfalseBoolean to be applied to the disabled prop of the action buttons
cancelOptionalstring'ra.action. cancel'Label of the cancel button
confirmOptionalstring'ra.action. confirm'Label of the confirm button
confirmColorOptionalstring'primary'Color of the confirm button
ConfirmIconOptionalComponentCheckCircleIconIcon component of the confirm button
CancelIconOptionalComponentErrorOutlineIconIcon component of the cancel button
title Translate OptionsOptional{ id?: string, name?: string }{}Custom id and name to be used in the dialog title
content Translate OptionsOptional{ id?: string, name?: string }{}Custom id and name to be used in the dialog content
sxOptionalSxProps''Material UI shortcut for defining custom styles with access to the theme

Text props such as title, content, cancel and confirm are translatable. You can pass translation keys in these props. Note: content is only translatable when value is string, otherwise it renders the content as a ReactNode.

sx: CSS API

The <Confirm> component accepts the usual className prop. You can also override many styles of the inner components thanks to the sx property (see the sx documentation for syntax and examples). This property accepts the following subclasses:

Rule nameDescription
& .RaConfirm-confirmPrimaryApplied to the confirm button when confirmColor is primary
& .RaConfirm-confirmWarningApplied to the confirm button when confirmColor is warning

To override the style of all instances of <Confirm> using the application-wide style overrides, use the RaConfirm key.

Delete With Confirmation

React-admin's <DeleteButton> lets user delete the current record in an optimistic way: after clicking the button, users see a notification for the deletion with an "undo" link to cancel the deletion.

Alternately, you can force the user to confirm the deletion by using <DeleteButton mutationMode="pessimistic">. Under the hood, this leverages the <Confirm> component to ask for confirmation before deleting the record.

jsx
import { List, DataTable, DeleteButton } from 'react-admin';

const PostList = () => (
    <List>
        <DataTable>
            <DataTable.Col source="id" />
            <DataTable.Col source="title" />
            <DataTable.Col>
                <DeleteButton mutationMode="pessimistic" />
            </DataTable.Col>
        </DataTable>
    </List>
);

The same goes for deleting multiple records in a bulk action: use <BulkDeleteButton mutationMode="pessimistic"> to ask a confirmation before the deletion.

jsx
import { List, DataTable, BulkDeleteButton } from 'react-admin';

const PostList = () => (
    <List>
        <DataTable bulkActionButtons={<BulkDeleteButton mutationMode="pessimistic" />}>
            <DataTable.Col source="id" />
            <DataTable.Col source="title" />
        </DataTable>
    </List>
);