Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update <Confirm> to accept a React node as confirmTitle or confirmContent #9115

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Delete the current record after a confirm dialog has been accepted. To be used i
| `className` | Optional | `string` | - | Class name to customize the look and feel of the button element itself |
| `label` | Optional | `string` | 'ra.action.delete' | label or translation message to use |
| `icon` | Optional | `ReactElement` | `<DeleteIcon>` | iconElement, e.g. `<CommentIcon />` |
| `confirmTitle` | Optional | `string` | 'ra.message.delete_title' | Title of the confirm dialog |
| `confirmTitle` | Optional | `ReactNode` | 'ra.message.delete_title' | Title of the confirm dialog |
| `confirmContent` | Optional | `ReactNode` | 'ra.message.delete_content' | Message or React component to be used as the body of the confirm dialog |
| `redirect` | Optional | `string | false | Function` | 'list' | Custom redirection after success side effect |
| `translateOptions` | Optional | `{ id?: string, name?: string }` | {} | Custom id and name to be used in the confirm dialog's title |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export interface BulkDeleteWithConfirmButtonProps<
> extends BulkActionProps,
ButtonProps {
confirmContent?: React.ReactNode;
confirmTitle?: string;
confirmTitle?: React.ReactNode;
icon?: ReactElement;
mutationMode: MutationMode;
mutationOptions?: UseMutationOptions<
Expand Down Expand Up @@ -180,8 +180,8 @@ const StyledButton = styled(Button, {
const defaultIcon = <ActionDelete />;

BulkDeleteWithConfirmButton.propTypes = {
confirmTitle: PropTypes.string,
confirmContent: PropTypes.string,
confirmTitle: PropTypes.node,
confirmContent: PropTypes.node,
icon: PropTypes.element,
label: PropTypes.string,
mutationMode: PropTypes.oneOf(['pessimistic', 'optimistic', 'undoable']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export interface BulkUpdateWithConfirmButtonProps<
> extends BulkActionProps,
ButtonProps {
confirmContent?: React.ReactNode;
confirmTitle?: string;
confirmTitle?: React.ReactNode;
icon?: ReactElement;
data: any;
onSuccess?: () => void;
Expand All @@ -171,8 +171,8 @@ export interface BulkUpdateWithConfirmButtonProps<
}

BulkUpdateWithConfirmButton.propTypes = {
confirmTitle: PropTypes.string,
confirmContent: PropTypes.string,
confirmTitle: PropTypes.node,
confirmContent: PropTypes.node,
label: PropTypes.string,
resource: PropTypes.string,
selectedIds: PropTypes.arrayOf(PropTypes.any),
Expand Down
23 changes: 22 additions & 1 deletion packages/ra-ui-materialui/src/button/DeleteButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, createTheme } from '@mui/material';
import { colors, createTheme, Alert } from '@mui/material';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';
import frenchMessages from 'ra-language-french';
Expand Down Expand Up @@ -43,6 +43,27 @@ export const Pessimistic = () => (
</AdminContext>
);

export const PessimisticWithCustomDialogContent = () => (
<AdminContext i18nProvider={i18nProvider}>
<DeleteButton
mutationMode="pessimistic"
record={{ id: 1 }}
label="Delete"
resource="post"
confirmTitle={
<>
Delete <strong>Full Name</strong>
</>
}
confirmContent={
<Alert severity="warning">
Are you sure you want to delete this user?
</Alert>
}
/>
</AdminContext>
);

export const WithUserDefinedPalette = () => (
<AdminContext theme={theme}>
<DeleteButton label="Delete" record={{ id: 1 }} />
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/button/DeleteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export interface DeleteButtonProps<
MutationOptionsError = unknown
> extends ButtonProps,
SaveContextValue {
confirmTitle?: string;
confirmContent?: string;
confirmTitle?: React.ReactNode;
confirmContent?: React.ReactNode;
icon?: ReactElement;
mutationMode?: MutationMode;
mutationOptions?: UseMutationOptions<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import frenchMessages from 'ra-language-french';
import { Resource } from 'ra-core';
import fakeRestDataProvider from 'ra-data-fakerest';
import { createMemoryHistory } from 'history';
import { Alert } from '@mui/material';

import { DeleteWithConfirmButton } from './DeleteWithConfirmButton';
import { AdminContext } from '../AdminContext';
Expand Down Expand Up @@ -109,15 +110,15 @@ const dataProvider = fakeRestDataProvider({

const history = createMemoryHistory({ initialEntries: ['/books'] });

const BookList = () => {
const BookList = ({ children }) => {
return (
<List>
<Datagrid>
<TextField source="id" />
<TextField source="title" />
<TextField source="author" />
<TextField source="year" />
<DeleteWithConfirmButton />
{children}
</Datagrid>
</List>
);
Expand All @@ -130,7 +131,44 @@ export const Basic = () => (
history={history}
>
<AdminUI>
<Resource name="books" list={BookList} />
<Resource
name="books"
list={
<BookList>
<DeleteWithConfirmButton />
</BookList>
}
/>
</AdminUI>
</AdminContext>
);

export const WithCustomDialogContent = () => (
<AdminContext
dataProvider={dataProvider}
i18nProvider={i18nProvider}
history={history}
>
<AdminUI>
<Resource
name="books"
list={
<BookList>
<DeleteWithConfirmButton
confirmTitle={
<>
Delete <strong>Full Name</strong>
</>
}
confirmContent={
<Alert severity="warning">
Are you sure you want to delete this user?
</Alert>
}
/>
</BookList>
}
/>
</AdminUI>
</AdminContext>
);
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface DeleteWithConfirmButtonProps<
RecordType extends RaRecord = any,
MutationOptionsError = unknown
> extends ButtonProps {
confirmTitle?: string;
confirmTitle?: React.ReactNode;
confirmContent?: React.ReactNode;
icon?: ReactElement;
mutationMode?: MutationMode;
Expand All @@ -117,8 +117,8 @@ export interface DeleteWithConfirmButtonProps<

DeleteWithConfirmButton.propTypes = {
className: PropTypes.string,
confirmTitle: PropTypes.string,
confirmContent: PropTypes.string,
confirmTitle: PropTypes.node,
confirmContent: PropTypes.node,
label: PropTypes.string,
mutationMode: PropTypes.oneOf(['pessimistic', 'optimistic', 'undoable']),
record: PropTypes.any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface UpdateWithConfirmButtonProps<
> extends BulkActionProps,
ButtonProps {
confirmContent?: React.ReactNode;
confirmTitle?: string;
confirmTitle?: React.ReactNode;
icon?: ReactElement;
data: any;
mutationMode?: MutationMode;
Expand All @@ -161,8 +161,8 @@ export interface UpdateWithConfirmButtonProps<
}

UpdateWithConfirmButton.propTypes = {
confirmTitle: PropTypes.string,
confirmContent: PropTypes.string,
confirmTitle: PropTypes.node,
confirmContent: PropTypes.node,
label: PropTypes.string,
resource: PropTypes.string,
selectedIds: PropTypes.arrayOf(PropTypes.any),
Expand Down
11 changes: 7 additions & 4 deletions packages/ra-ui-materialui/src/layout/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export const Confirm = (props: ConfirmProps) => {
{...rest}
>
<DialogTitle id="alert-dialog-title">
{translate(title, { _: title, ...translateOptions })}
{typeof title === 'string'
? translate(title, { _: title, ...translateOptions })
: title}
</DialogTitle>
<DialogContent>
{typeof content === 'string' ? (
Expand Down Expand Up @@ -114,7 +116,8 @@ export const Confirm = (props: ConfirmProps) => {
);
};

export interface ConfirmProps extends Omit<DialogProps, 'open' | 'onClose'> {
export interface ConfirmProps
extends Omit<DialogProps, 'open' | 'onClose' | 'title'> {
cancel?: string;
className?: string;
confirm?: string;
Expand All @@ -126,7 +129,7 @@ export interface ConfirmProps extends Omit<DialogProps, 'open' | 'onClose'> {
loading?: boolean;
onClose: MouseEventHandler;
onConfirm: MouseEventHandler;
title: string;
title: React.ReactNode;
slax57 marked this conversation as resolved.
Show resolved Hide resolved
translateOptions?: object;
}

Expand All @@ -142,7 +145,7 @@ Confirm.propTypes = {
loading: PropTypes.bool,
onClose: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
title: PropTypes.node.isRequired,
sx: PropTypes.any,
};

Expand Down