DeleteDialogTemplate.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. DialogActions,
  3. DialogContent,
  4. TextField,
  5. Theme,
  6. Typography,
  7. Checkbox,
  8. FormControlLabel,
  9. } from '@mui/material';
  10. import { ChangeEvent, FC, useContext, useState } from 'react';
  11. import { useTranslation } from 'react-i18next';
  12. import CustomButton from '@/components/customButton/CustomButton';
  13. import CustomDialogTitle from '@/components/customDialog/CustomDialogTitle';
  14. import { DeleteDialogContentType } from '@/components/customDialog/Types';
  15. import { rootContext } from '@/context';
  16. import { makeStyles } from '@mui/styles';
  17. const useStyles = makeStyles((theme: Theme) => ({
  18. root: {
  19. maxWidth: 540,
  20. backgroundColor: theme.palette.background.paper,
  21. },
  22. info: {
  23. marginBottom: theme.spacing(0.5),
  24. color: theme.palette.text.secondary,
  25. },
  26. mb: {
  27. marginBottom: theme.spacing(2.5),
  28. },
  29. btnWrapper: {
  30. display: 'flex',
  31. },
  32. label: {
  33. display: 'none',
  34. },
  35. btnLabel: {
  36. fontWeight: 'bold',
  37. },
  38. input: {
  39. padding: '10px 12px',
  40. },
  41. cancelBtn: {
  42. color: theme.palette.text.secondary,
  43. },
  44. checkBox: {},
  45. }));
  46. const DeleteTemplate: FC<DeleteDialogContentType> = props => {
  47. const {
  48. title,
  49. text,
  50. label,
  51. compare,
  52. handleDelete,
  53. handleCancel,
  54. forceDelLabel,
  55. } = props;
  56. const { handleCloseDialog } = useContext(rootContext);
  57. const classes = useStyles();
  58. const { t: dialogTrans } = useTranslation('dialog');
  59. const { t: btnTrans } = useTranslation('btn');
  60. const [value, setValue] = useState<string>('');
  61. const [force, setForce] = useState<boolean>(false);
  62. const [deleteReady, setDeleteReady] = useState<boolean>(false);
  63. const onCancelClick = () => {
  64. handleCloseDialog();
  65. handleCancel && handleCancel();
  66. };
  67. const onDeleteClick = (event: React.FormEvent<HTMLFormElement>) => {
  68. handleDelete(force);
  69. event.preventDefault();
  70. };
  71. const onChange = (event: ChangeEvent<HTMLInputElement>) => {
  72. const value = event.target.value;
  73. setValue(value);
  74. setDeleteReady(value.toLowerCase() === (compare || label).toLowerCase());
  75. };
  76. return (
  77. <div className={classes.root}>
  78. <form onSubmit={onDeleteClick}>
  79. <CustomDialogTitle
  80. classes={{ root: classes.mb }}
  81. onClose={onCancelClick}
  82. >
  83. {title}
  84. </CustomDialogTitle>
  85. <DialogContent>
  86. <Typography
  87. variant="body1"
  88. className={classes.info}
  89. dangerouslySetInnerHTML={{ __html: text }}
  90. ></Typography>
  91. <Typography variant="body1" className={classes.mb}>
  92. {dialogTrans('deleteTipAction')}
  93. <strong className={classes.btnLabel}>{` ${(
  94. compare || label
  95. ).toLowerCase()} `}</strong>
  96. {dialogTrans('deleteTipPurpose')}
  97. </Typography>
  98. <TextField
  99. value={value}
  100. onChange={onChange}
  101. InputLabelProps={{
  102. classes: {
  103. root: classes.label,
  104. },
  105. }}
  106. InputProps={{
  107. classes: {
  108. input: classes.input,
  109. },
  110. }}
  111. variant="filled"
  112. fullWidth={true}
  113. />
  114. {forceDelLabel ? (
  115. <FormControlLabel
  116. control={
  117. <Checkbox
  118. onChange={(
  119. e: React.ChangeEvent<HTMLInputElement>,
  120. checked: boolean
  121. ) => {
  122. setForce(checked);
  123. }}
  124. />
  125. }
  126. key={'force'}
  127. label={forceDelLabel}
  128. value={true}
  129. checked={force}
  130. className={classes.checkBox}
  131. />
  132. ) : null}
  133. </DialogContent>
  134. <DialogActions className={classes.btnWrapper}>
  135. <CustomButton
  136. name="cancel"
  137. onClick={onCancelClick}
  138. className={classes.cancelBtn}
  139. >
  140. {btnTrans('cancel')}
  141. </CustomButton>
  142. <CustomButton
  143. type="submit"
  144. variant="contained"
  145. color="secondary"
  146. disabled={!deleteReady}
  147. name="delete"
  148. >
  149. {label}
  150. </CustomButton>
  151. </DialogActions>
  152. </form>
  153. </div>
  154. );
  155. };
  156. export default DeleteTemplate;