Skip to content

Commit dbc5a3f

Browse files
committed
Customizable delete button.
1 parent 304e4bb commit dbc5a3f

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React, { Fragment, isValidElement, ReactEventHandler } from 'react';
22
import ActionDelete from '@mui/icons-material/Delete';
3+
import {
4+
ComponentsOverrides,
5+
styled,
6+
useThemeProps,
7+
} from '@mui/material/styles';
38
import clsx from 'clsx';
49

510
import { UseMutationOptions } from '@tanstack/react-query';
@@ -20,8 +25,13 @@ import { Button, ButtonProps } from './Button';
2025
import { humanize, singularize } from 'inflection';
2126

2227
export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
23-
props: DeleteWithConfirmButtonProps<RecordType>
28+
inProps: DeleteWithConfirmButtonProps<RecordType>
2429
) => {
30+
const props = useThemeProps({
31+
props: inProps,
32+
name: PREFIX,
33+
});
34+
2535
const {
2636
className,
2737
confirmTitle: confirmTitleProp,
@@ -80,7 +90,7 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
8090

8191
return (
8292
<Fragment>
83-
<Button
93+
<StyledButton
8494
onClick={handleDialogOpen}
8595
label={label}
8696
className={clsx('ra-delete-button', className)}
@@ -89,7 +99,7 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
8999
{...rest}
90100
>
91101
{icon}
92-
</Button>
102+
</StyledButton>
93103
<Confirm
94104
isOpen={open}
95105
loading={isPending}
@@ -158,3 +168,29 @@ export interface DeleteWithConfirmButtonProps<
158168
resource?: string;
159169
successMessage?: string;
160170
}
171+
172+
const PREFIX = 'RaDeleteWithConfirmButton';
173+
174+
const StyledButton = styled(Button, {
175+
name: PREFIX,
176+
overridesResolver: (props, styles) => styles.root,
177+
})({});
178+
179+
declare module '@mui/material/styles' {
180+
interface ComponentNameToClassKey {
181+
[PREFIX]: 'root';
182+
}
183+
184+
interface ComponentsPropsList {
185+
[PREFIX]: Partial<DeleteWithConfirmButtonProps>;
186+
}
187+
188+
interface Components {
189+
[PREFIX]?: {
190+
defaultProps?: ComponentsPropsList[typeof PREFIX];
191+
styleOverrides?: ComponentsOverrides<
192+
Omit<Theme, 'components'>
193+
>[typeof PREFIX];
194+
};
195+
}
196+
}

packages/ra-ui-materialui/src/button/DeleteWithUndoButton.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ import {
1313
} from 'ra-core';
1414

1515
import { Button, ButtonProps } from './Button';
16+
import {
17+
ComponentsOverrides,
18+
styled,
19+
useThemeProps,
20+
} from '@mui/material/styles';
1621

1722
export const DeleteWithUndoButton = <RecordType extends RaRecord = any>(
18-
props: DeleteWithUndoButtonProps<RecordType>
23+
inProps: DeleteWithUndoButtonProps<RecordType>
1924
) => {
25+
const props = useThemeProps({
26+
props: inProps,
27+
name: PREFIX,
28+
});
29+
2030
const {
2131
label = 'ra.action.delete',
2232
className,
@@ -41,7 +51,7 @@ export const DeleteWithUndoButton = <RecordType extends RaRecord = any>(
4151
});
4252

4353
return (
44-
<Button
54+
<StyledButton
4555
onClick={handleDelete}
4656
disabled={isPending}
4757
label={label}
@@ -51,7 +61,7 @@ export const DeleteWithUndoButton = <RecordType extends RaRecord = any>(
5161
{...rest}
5262
>
5363
{icon}
54-
</Button>
64+
</StyledButton>
5565
);
5666
};
5767

@@ -73,3 +83,29 @@ export interface DeleteWithUndoButtonProps<
7383
resource?: string;
7484
successMessage?: string;
7585
}
86+
87+
const PREFIX = 'RaDeleteWithUndoButton';
88+
89+
const StyledButton = styled(Button, {
90+
name: PREFIX,
91+
overridesResolver: (props, styles) => styles.root,
92+
})({});
93+
94+
declare module '@mui/material/styles' {
95+
interface ComponentNameToClassKey {
96+
[PREFIX]: 'root';
97+
}
98+
99+
interface ComponentsPropsList {
100+
[PREFIX]: Partial<DeleteWithUndoButtonProps>;
101+
}
102+
103+
interface Components {
104+
[PREFIX]?: {
105+
defaultProps?: ComponentsPropsList[typeof PREFIX];
106+
styleOverrides?: ComponentsOverrides<
107+
Omit<Theme, 'components'>
108+
>[typeof PREFIX];
109+
};
110+
}
111+
}

0 commit comments

Comments
 (0)