-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathDeleteButton.tsx
106 lines (100 loc) · 3.27 KB
/
DeleteButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as React from 'react';
import { FC, ReactElement, SyntheticEvent } from 'react';
import PropTypes from 'prop-types';
import { Record, RedirectionSideEffect, MutationMode } from 'ra-core';
import { ButtonProps } from './Button';
import DeleteWithUndoButton from './DeleteWithUndoButton';
import DeleteWithConfirmButton from './DeleteWithConfirmButton';
/**
* Button used to delete a single record. Added by default by the <Toolbar> of edit and show views.
*
* @typedef {Object} Props The props you can use (other props are injected if you used it in the <Toolbar>)
* @prop {boolean} undoable Confirm the deletion using an undo button in a notification or a confirmation dialog. Defaults to 'false'.
* @prop {Object} record The current resource record
* @prop {string} className
* @prop {string} label Button label. Defaults to 'ra.action.delete, translated.
* @prop {boolean} disabled Disable the button.
* @prop {string} variant Material-ui variant for the button. Defaults to 'contained'.
* @prop {ReactElement} icon Override the icon. Defaults to the Delete icon from material-ui.
*
* @param {Props} props
*
* @example Usage in the <TopToolbar> of an <Edit> form
*
* import * as React from 'react';
* import { Edit, DeleteButton, TopToolbar } from 'react-admin';
*
* const EditActions = props => {
* const { basePath, data, resource } = props;
* return (
* <TopToolbar>
* <DeleteButton
* basePath={basePath}
* record={data}
* resource={resource}
* undoable={false} // Renders the <DeleteWithConfirmButton>
* />
* </TopToolbar>
* );
* };
*
* const Edit = props => {
* return <Edit actions={<EditActions />} {...props} />;
* };
*/
const DeleteButton: FC<DeleteButtonProps> = ({
undoable,
mutationMode = 'undoable',
record,
...props
}) => {
if (!record || record.id == null) {
return null;
}
return undoable || mutationMode === 'undoable' ? (
<DeleteWithUndoButton record={record} {...props} />
) : (
<DeleteWithConfirmButton
mutationMode={mutationMode}
record={record}
{...props}
/>
);
};
interface Props {
basePath?: string;
classes?: object;
className?: string;
icon?: ReactElement;
label?: string;
mutationMode?: MutationMode;
onClick?: (e: MouseEvent) => void;
record?: Record;
redirect?: RedirectionSideEffect;
resource?: string;
// May be injected by Toolbar
handleSubmit?: (event?: SyntheticEvent<HTMLFormElement>) => Promise<Object>;
handleSubmitWithRedirect?: (redirect?: RedirectionSideEffect) => void;
invalid?: boolean;
pristine?: boolean;
saving?: boolean;
submitOnEnter?: boolean;
/** @deprecated use mutationMode: undoable instead */
undoable?: boolean;
}
export type DeleteButtonProps = Props & ButtonProps;
DeleteButton.propTypes = {
basePath: PropTypes.string,
label: PropTypes.string,
record: PropTypes.any,
// @ts-ignore
redirect: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.func,
]),
resource: PropTypes.string,
undoable: PropTypes.bool,
icon: PropTypes.element,
};
export default DeleteButton;