-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuoteForm.js
More file actions
69 lines (64 loc) · 1.67 KB
/
QuoteForm.js
File metadata and controls
69 lines (64 loc) · 1.67 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
import React, { useReducer } from 'react'
const CHANGE_INPUT = 'CHANGE_INPUT'
const RESET_FORM = 'RESET_FORM'
const initialState = {
authorName: '',
quoteText: '',
}
const reducer = (state, action) => {
switch (action.type) {
case CHANGE_INPUT: {
const { name, value } = action.payload
return { ...state, [name]: value }
}
case RESET_FORM:
return { authorName: '', quoteText: '' }
default:
return state
}
}
export default function TodoForm(props) {
const { createQuote } = props
const [state, dispatch] = useReducer(reducer, initialState)
const onChange = ({ target: { name, value } }) => {
dispatch({ type: CHANGE_INPUT, payload: { name, value } })
}
const resetForm = () => {
dispatch({ type: RESET_FORM })
}
const onNewQuote = evt => {
evt.preventDefault()
const { authorName, quoteText } = state
createQuote({ authorName, quoteText })
resetForm()
}
return (
<form id="quoteForm" onSubmit={onNewQuote}>
<h3>New Quote Form</h3>
<label><span>Author:</span>
<input
type='text'
name='authorName'
placeholder='type author name'
onChange={onChange}
value={state.authorName}
/>
</label>
<label><span>Quote text:</span>
<textarea
type='text'
name='quoteText'
placeholder='type quote'
onChange={onChange}
value={state.quoteText}
/>
</label>
<label><span>Create quote:</span>
<button
role='submit'
disabled={!state.authorName.trim() || !state.quoteText.trim()}
>DO IT!</button>
</label>
</form>
)
}