import PropTypes from 'prop-types'; import ActionDelete from '@mui/icons-material/Delete'; import clsx from 'clsx'; import inflection from 'inflection'; import { RaRecord, useDeleteWithConfirmController, useRecordContext, useResourceContext, useTranslate } from 'ra-core'; import { Button } from './Button'; import { Confirm, DeleteWithConfirmButtonProps as RaDeleteWithConfirmButtonProps } from 'ra-ui-materialui'; import { Box, SxProps, Typography, useTheme } from '@mui/material'; import { DeleteFilled } from '@ant-design/icons'; import { useCallback } from 'react'; type DeleteWithConfirmButtonProps = RaDeleteWithConfirmButtonProps & { dialogSx?: SxProps; /** * @deprecated confirmTitle prop is deprecated, please use confirmContent instead. */ confirmTitle?: React.ReactNode; /** * onConfirm {callback} is used to handle a custom delete action when user confirms the deletion. */ onConfirm?: () => void; }; function DeleteWithConfirmButton( props: DeleteWithConfirmButtonProps ): JSX.Element { const { className, confirmTitle, confirmContent, confirmColor = 'primary', color = 'error', dialogSx, icon = , label = 'ra.action.delete', mutationMode = 'pessimistic', mutationOptions, onClick, redirect = 'list', translateOptions = {}, onConfirm, ...rest } = props; const translate = useTranslate(); const record = useRecordContext(props); const resource = useResourceContext(props); const theme = useTheme(); const { open, isLoading, handleDialogOpen, handleDialogClose, handleDelete } = useDeleteWithConfirmController({ record, redirect, mutationMode, onClick, mutationOptions, resource }); const handleOnConfirm = useCallback( (event: any) => { if (onConfirm) { onConfirm(); } else { handleDelete(event); } }, [handleDelete, onConfirm] ); return ( <> ) } content={ {confirmContent || translate('ra.message.delete_content', { _: 'ra.message.delete_content', name: translate(`resources.${resource}.forcedCaseName`, { smart_count: 1, _: inflection.humanize( translate(`resources.${resource}.name`, { smart_count: 1, _: inflection.singularize(resource) }), true ) }), id: record?.id })} } confirmColor={confirmColor} translateOptions={{ name: translate(`resources.${resource}.forcedCaseName`, { smart_count: 1, _: inflection.humanize( translate(`resources.${resource}.name`, { smart_count: 1, _: inflection.singularize(resource) }), true ) }), id: record?.id, ...translateOptions }} onConfirm={handleOnConfirm} onClose={handleDialogClose} /> ); } DeleteWithConfirmButton.propTypes = { basePath: PropTypes.string, className: PropTypes.string, confirmColor: PropTypes.string, confirmContent: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), confirmTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), icon: PropTypes.element, label: PropTypes.string, mutationMode: PropTypes.oneOf(['pessimistic', 'optimistic', 'undoable']), onClick: PropTypes.func, record: PropTypes.any, redirect: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.func]), resource: PropTypes.string, translateOptions: PropTypes.object }; export { DeleteWithConfirmButton };