1
- import { useRef } from 'react' ;
1
+ import { Fragment , useRef , useState } from 'react' ;
2
+ import { Prompt } from 'react-router-dom' ;
2
3
3
4
import Card from '../UI/Card' ;
4
5
import LoadingSpinner from '../UI/LoadingSpinner' ;
5
6
import classes from './QuoteForm.module.css' ;
6
7
7
8
const QuoteForm = ( props ) => {
9
+ const [ isEntering , setIsEntering ] = useState ( false ) ;
10
+
8
11
const authorInputRef = useRef ( ) ;
9
12
const textInputRef = useRef ( ) ;
10
13
@@ -19,28 +22,48 @@ const QuoteForm = (props) => {
19
22
props . onAddQuote ( { author : enteredAuthor , text : enteredText } ) ;
20
23
}
21
24
25
+ const finishEnteringHandler = ( ) => {
26
+ setIsEntering ( false ) ;
27
+ } ;
28
+
29
+ const formFocusedHandler = ( ) => {
30
+ setIsEntering ( true ) ;
31
+ } ;
32
+
22
33
return (
23
- < Card >
24
- < form className = { classes . form } onSubmit = { submitFormHandler } >
25
- { props . isLoading && (
26
- < div className = { classes . loading } >
27
- < LoadingSpinner />
34
+ < Fragment >
35
+ < Prompt
36
+ when = { isEntering }
37
+ message = { ( location ) =>
38
+ 'Are you sure you want to leave? All your entered data will be lost!'
39
+ }
40
+ />
41
+ < Card >
42
+ < form
43
+ onFocus = { formFocusedHandler }
44
+ className = { classes . form }
45
+ onSubmit = { submitFormHandler }
46
+ >
47
+ { props . isLoading && (
48
+ < div className = { classes . loading } >
49
+ < LoadingSpinner />
50
+ </ div >
51
+ ) }
52
+
53
+ < div className = { classes . control } >
54
+ < label htmlFor = 'author' > Author</ label >
55
+ < input type = 'text' id = 'author' ref = { authorInputRef } />
56
+ </ div >
57
+ < div className = { classes . control } >
58
+ < label htmlFor = 'text' > Text</ label >
59
+ < textarea id = 'text' rows = '5' ref = { textInputRef } > </ textarea >
60
+ </ div >
61
+ < div className = { classes . actions } >
62
+ < button onClick = { finishEnteringHandler } className = 'btn' > Add Quote</ button >
28
63
</ div >
29
- ) }
30
-
31
- < div className = { classes . control } >
32
- < label htmlFor = 'author' > Author</ label >
33
- < input type = 'text' id = 'author' ref = { authorInputRef } />
34
- </ div >
35
- < div className = { classes . control } >
36
- < label htmlFor = 'text' > Text</ label >
37
- < textarea id = 'text' rows = '5' ref = { textInputRef } > </ textarea >
38
- </ div >
39
- < div className = { classes . actions } >
40
- < button className = 'btn' > Add Quote</ button >
41
- </ div >
42
- </ form >
43
- </ Card >
64
+ </ form >
65
+ </ Card >
66
+ </ Fragment >
44
67
) ;
45
68
} ;
46
69
0 commit comments