-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathSearchForm.js
127 lines (119 loc) · 3.35 KB
/
SearchForm.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { Fragment, useState, useEffect, useRef } from 'react'
import PropTypes from 'prop-types'
const RATINGS = [
{ value: '', label: 'All' },
{ value: 'g', label: 'G' },
{ value: 'pg', label: 'PG' },
{ value: 'pg-13', label: 'PG-13' },
{ value: 'r', label: 'R' },
]
const LIMITS = [6, 12, 18, 24, 30]
const SearchForm = ({
initialLimit,
initialRating,
initialSearchQuery,
initialShowInstant,
onChange,
}) => {
const [inputValue, setInputValue] = useState(initialSearchQuery)
const [searchQuery, setSearchQuery] = useState(initialSearchQuery)
const [showInstant, setShowInstant] = useState(initialShowInstant)
const [searchRating, setSearchRating] = useState(initialRating)
const [searchLimit, setSearchLimit] = useState(initialLimit)
const realSearchQuery = showInstant ? inputValue : searchQuery
const queryFieldEl = useRef(null)
useEffect(() => {
onChange({
searchQuery: realSearchQuery,
rating: searchRating,
limit: searchLimit,
})
}, [onChange, realSearchQuery, searchRating, searchLimit])
const handleSubmit = (e) => {
e.preventDefault()
setSearchQuery(inputValue)
// focus the query field after submitting
// to make easier to quickly search again
queryFieldEl.current.focus()
}
return (
<form onSubmit={handleSubmit}>
<section className="input-group">
<input
type="search"
placeholder="Search Giphy"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value)
}}
className="input-group-field"
ref={queryFieldEl} // ok?
/>
<aside className="input-group-button">
<button type="submit" className="button">
Search
</button>
</aside>
</section>
<section>
<input
type="checkbox"
id="instant-results"
name="instant-results"
checked={showInstant}
onChange={(e) => {
setShowInstant(e.target.checked)
}}
/>
<label htmlFor="instant-results">Show instant results?</label>
</section>
<hr />
<fieldset>
<legend>Choose a rating</legend>
{RATINGS.map(({ value, label }) => (
<Fragment key={value}>
<input
type="radio"
name="rating"
value={value}
id={`rating-${value}`}
checked={value === searchRating}
onChange={() => {
setSearchRating(value)
}}
/>
<label htmlFor={`rating-${value}`}>{label}</label>
</Fragment>
))}
</fieldset>
<hr />
<label>
# of Results
<select
onChange={(e) => setSearchLimit(e.target.value)}
value={searchLimit}
>
{LIMITS.map((limit) => (
<option key={limit} value={limit}>
{limit}
</option>
))}
</select>
</label>
</form>
)
}
SearchForm.propTypes = {
initialLimit: PropTypes.number,
initialRating: PropTypes.string,
initialSearchQuery: PropTypes.string,
initialShowInstant: PropTypes.bool,
onChange: PropTypes.func.isRequired,
}
SearchForm.defaultProps = {
initialLimit: 12,
initialRating: '',
initialSearchQuery: '',
initialShowInstant: false,
}
export default SearchForm