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" ;
210import TextField from "@mui/material/TextField" ;
3- import { ChangeEvent , useState , PropsWithChildren , FC } from "react" ;
411import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog" ;
512
613export interface DeleteDialogProps {
@@ -22,51 +29,23 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
2229 name,
2330 confirmLoading,
2431} ) => {
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 ( ) ;
3234
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 ) ;
4137
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" ;
7049
7150 return (
7251 < ConfirmDialog
@@ -76,19 +55,50 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
7655 title = { `Delete ${ entity } ` }
7756 onConfirm = { onConfirm }
7857 onClose = { onCancel }
79- description = { content }
8058 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+ }
82102 />
83103 ) ;
84104} ;
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