1
- import makeStyles from "@mui/styles/makeStyles" ;
1
+ import {
2
+ type FC ,
3
+ type FormEvent ,
4
+ type PropsWithChildren ,
5
+ useId ,
6
+ useState ,
7
+ } from "react" ;
8
+
9
+ import { useTheme } from "@emotion/react" ;
2
10
import TextField from "@mui/material/TextField" ;
3
- import { ChangeEvent , useState , PropsWithChildren , FC } from "react" ;
4
11
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog" ;
5
12
6
13
export interface DeleteDialogProps {
@@ -22,51 +29,23 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
22
29
name,
23
30
confirmLoading,
24
31
} ) => {
25
- const styles = useStyles ( ) ;
26
- const [ nameValue , setNameValue ] = useState ( "" ) ;
27
- const confirmed = name === nameValue ;
28
- const handleChange = ( event : ChangeEvent < HTMLInputElement > ) => {
29
- setNameValue ( event . target . value ) ;
30
- } ;
31
- const hasError = nameValue . length > 0 && ! confirmed ;
32
+ const hookId = useId ( ) ;
33
+ const theme = useTheme ( ) ;
32
34
33
- const content = (
34
- < >
35
- < p > Deleting this { entity } is irreversible!</ p >
36
- { Boolean ( info ) && < p className = { styles . warning } > { info } </ p > }
37
- < p > Are you sure you want to proceed?</ p >
38
- < p >
39
- Type “< strong > { name } </ strong > ” below to confirm.
40
- </ p >
35
+ const [ userConfirmationText , setUserConfirmationText ] = useState ( "" ) ;
36
+ const [ isFocused , setIsFocused ] = useState ( false ) ;
41
37
42
- < form
43
- onSubmit = { ( e ) => {
44
- e . preventDefault ( ) ;
45
- if ( confirmed ) {
46
- onConfirm ( ) ;
47
- }
48
- } }
49
- >
50
- < TextField
51
- fullWidth
52
- autoFocus
53
- className = { styles . textField }
54
- name = "confirmation"
55
- autoComplete = "off"
56
- id = "confirmation"
57
- placeholder = { name }
58
- value = { nameValue }
59
- onChange = { handleChange }
60
- label = { `Name of the ${ entity } to delete` }
61
- error = { hasError }
62
- helperText = {
63
- hasError && `${ nameValue } does not match the name of this ${ entity } `
64
- }
65
- inputProps = { { [ "data-testid" ] : "delete-dialog-name-confirmation" } }
66
- />
67
- </ form >
68
- </ >
69
- ) ;
38
+ const deletionConfirmed = name === userConfirmationText ;
39
+ const onSubmit = ( event : FormEvent ) => {
40
+ event . preventDefault ( ) ;
41
+ if ( deletionConfirmed ) {
42
+ onConfirm ( ) ;
43
+ }
44
+ } ;
45
+
46
+ const hasError = ! deletionConfirmed && userConfirmationText . length > 0 ;
47
+ const displayErrorMessage = hasError && ! isFocused ;
48
+ const inputColor = hasError ? "error" : "primary" ;
70
49
71
50
return (
72
51
< ConfirmDialog
@@ -76,19 +55,50 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
76
55
title = { `Delete ${ entity } ` }
77
56
onConfirm = { onConfirm }
78
57
onClose = { onCancel }
79
- description = { content }
80
58
confirmLoading = { confirmLoading }
81
- disabled = { ! confirmed }
59
+ disabled = { ! deletionConfirmed }
60
+ description = {
61
+ < >
62
+ < p > Deleting this { entity } is irreversible!</ p >
63
+
64
+ { Boolean ( info ) && (
65
+ < p css = { { color : theme . palette . warning . light } } > { info } </ p >
66
+ ) }
67
+
68
+ < p > Are you sure you want to proceed?</ p >
69
+
70
+ < p >
71
+ Type “< strong > { name } </ strong > ” below to confirm.
72
+ </ p >
73
+
74
+ < form onSubmit = { onSubmit } >
75
+ < TextField
76
+ fullWidth
77
+ autoFocus
78
+ sx = { { marginTop : theme . spacing ( 3 ) } }
79
+ name = "confirmation"
80
+ autoComplete = "off"
81
+ id = { `${ hookId } -confirm` }
82
+ placeholder = { name }
83
+ value = { userConfirmationText }
84
+ onChange = { ( event ) => setUserConfirmationText ( event . target . value ) }
85
+ onFocus = { ( ) => setIsFocused ( true ) }
86
+ onBlur = { ( ) => setIsFocused ( false ) }
87
+ label = { `Name of the ${ entity } to delete` }
88
+ color = { inputColor }
89
+ error = { displayErrorMessage }
90
+ helperText = {
91
+ displayErrorMessage &&
92
+ `${ userConfirmationText } does not match the name of this ${ entity } `
93
+ }
94
+ InputProps = { { color : inputColor } }
95
+ inputProps = { {
96
+ "data-testid" : "delete-dialog-name-confirmation" ,
97
+ } }
98
+ />
99
+ </ form >
100
+ </ >
101
+ }
82
102
/>
83
103
) ;
84
104
} ;
85
-
86
- const useStyles = makeStyles ( ( theme ) => ( {
87
- warning : {
88
- color : theme . palette . warning . light ,
89
- } ,
90
-
91
- textField : {
92
- marginTop : theme . spacing ( 3 ) ,
93
- } ,
94
- } ) ) ;
0 commit comments