Skip to content

Instantly share code, notes, and snippets.

@adylevy
Last active May 22, 2021 11:46
Show Gist options
  • Save adylevy/759900e7c50f9ccebe9f037bc5e32506 to your computer and use it in GitHub Desktop.
Save adylevy/759900e7c50f9ccebe9f037bc5e32506 to your computer and use it in GitHub Desktop.
react-admin delete button with confirmation
/*
Copyright (c) 2018 Ady Levy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import { fade } from '@material-ui/core/styles/colorManipulator';
import ActionDelete from '@material-ui/icons/Delete';
import classnames from 'classnames';
import { translate, crudDelete, startUndoable } from 'ra-core';
import IconCancel from '@material-ui/icons/Cancel';
import Dialog from '@material-ui/core/Dialog';
import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContent from '@material-ui/core/DialogContent';
import DialogActions from '@material-ui/core/DialogActions';
import { SimpleForm, Button, DeleteButton, TextInput, LongTextInput, required } from 'react-admin';
const styles = (theme) => ({
deleteButton: {
color: theme.palette.error.main,
'&:hover': {
backgroundColor: fade(theme.palette.error.main, 0.12),
// Reset on mouse devices
'@media (hover: none)': {
backgroundColor: 'transparent'
}
}
}
});
class DeleteButtonWithConfirmation extends Component {
state = {
showDialog: false
};
handleClick = () => {
this.setState({ showDialog: true });
};
handleCloseClick = () => {
this.setState({ showDialog: false });
};
handleDelete = (event) => {
event.preventDefault();
this.setState({ showDialog: false });
const { dispatchCrudDelete, startUndoable, resource, record, basePath, redirect, undoable } = this.props;
if (undoable) {
startUndoable(crudDelete(resource, record.id, record, basePath, redirect));
} else {
dispatchCrudDelete(resource, record.id, record, basePath, redirect);
}
};
render() {
const { showDialog } = this.state;
const { label = 'ra.action.delete', classes = {}, className } = this.props;
return (
<Fragment>
<Button onClick={this.handleClick} label={label} className={classnames('ra-delete-button', classes.deleteButton, className)} key="button">
<ActionDelete />
</Button>
<Dialog fullWidth open={showDialog} onClose={this.handleCloseClick} aria-label="Are you sure?">
<DialogTitle>Are you sure you want to delete this entity?</DialogTitle>
<DialogContent>
<div>
Your actions will be logged.
</div>
</DialogContent>
<DialogActions>
<Button onClick={this.handleDelete} label={label} className={classnames('ra-delete-button', classes.deleteButton, className)} key="button">
<ActionDelete />
</Button>
<Button label="ra.action.cancel" onClick={this.handleCloseClick}>
<IconCancel />
</Button>
</DialogActions>
</Dialog>
</Fragment>
);
}
}
DeleteButtonWithConfirmation.propTypes = {
basePath: PropTypes.string,
classes: PropTypes.object,
className: PropTypes.string,
dispatchCrudDelete: PropTypes.func.isRequired,
label: PropTypes.string,
record: PropTypes.object,
redirect: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.func]),
resource: PropTypes.string.isRequired,
startUndoable: PropTypes.func,
translate: PropTypes.func,
undoable: PropTypes.bool
};
DeleteButtonWithConfirmation.defaultProps = {
redirect: 'list',
undoable: true
};
export default compose(
connect(
null,
{ startUndoable, dispatchCrudDelete: crudDelete }
),
translate,
withStyles(styles)
)(DeleteButtonWithConfirmation);
@te-online
Copy link

Awesome work! 🎉
I adjusted it to be more like the admin-on-rest dialog, also stating the name of the entry that gets deleted.
https://gist.github.com/te-online/9b7cd7d81ecd47f56f2191121031cc9f

@qpointsystems
Copy link

Great work. Just what I wanted :)

@dorklord23
Copy link

Hi,
Where does prop record come from? I checked the component tree but have yet to find such prop.

@ludob78
Copy link

ludob78 commented Sep 19, 2019

Nice work! Completely match with my need!

@traeper
Copy link

traeper commented Oct 24, 2019

Good work!!
Thank you for nice codes.

@pradnyabwebonise
Copy link

how to call this from List component in react-admin

@nadeeraka
Copy link

Thank you

@RbyteSoftware
Copy link

I`m really appreciate this feature. Thank you.

@ouabel
Copy link

ouabel commented Oct 16, 2020

Thanks for the useful component.
I think a DialogContentText should be used inside the DialogContent to properly show the text.

@abhishekthomas17
Copy link

Great Work !!
I've written a very small post on how to add custom Toast Notification to this instead of it just saying "Element Deleted"
https://abhishekthoms.medium.com/custom-delete-button-in-react-admin-with-custom-notification-message-3392526cb43c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment