-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuoteForm.js
More file actions
72 lines (60 loc) · 1.88 KB
/
QuoteForm.js
File metadata and controls
72 lines (60 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Fragment, useRef, useState } from "react";
import Card from "../UI/Card";
import LoadingSpinner from "../UI/LoadingSpinner";
import classes from "./QuoteForm.module.css";
import { Prompt } from "react-router-dom";
const QuoteForm = (props) => {
const authorInputRef = useRef();
const textInputRef = useRef();
const [isEntering, setIsEntering] = useState(false);
function submitFormHandler(event) {
event.preventDefault();
const enteredAuthor = authorInputRef.current.value;
const enteredText = textInputRef.current.value;
// optional: Could validate here
props.onAddQuote({ author: enteredAuthor, text: enteredText });
}
const formFocusHandler = () => {
setIsEntering(true);
};
const finishEnteringHandler = () => {
setIsEntering(false);
};
return (
<Fragment>
<Prompt
when={isEntering}
message={(location) =>
"Are you sure that you want to leave this page ? All your entered data will be lost !"
}
/>
<Card>
<form
onFocus={formFocusHandler}
className={classes.form}
onSubmit={submitFormHandler}
>
{props.isLoading && (
<div className={classes.loading}>
<LoadingSpinner />
</div>
)}
<div className={classes.control}>
<label htmlFor="author">Author</label>
<input type="text" id="author" ref={authorInputRef} />
</div>
<div className={classes.control}>
<label htmlFor="text">Text</label>
<textarea id="text" rows="5" ref={textInputRef}></textarea>
</div>
<div className={classes.actions}>
<button onClick={finishEnteringHandler} className="btn">
Add Quote
</button>
</div>
</form>
</Card>
</Fragment>
);
};
export default QuoteForm;