MCPcopy
hub / github.com/epicweb-dev/react-hooks / Form

Function Form

exercises/03.lifting-state/01.problem.lift/index.tsx:28–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26
27// 🐨 update the Form props to accept query and setQuery
28function Form() {
29 // 🐨 lift this up to the App
30 const [query, setQuery] = useState(getQueryParam)
31
32 const words = query.split(' ').map((w) => w.trim())
33
34 const dogChecked = words.includes('dog')
35 const catChecked = words.includes('cat')
36 const caterpillarChecked = words.includes('caterpillar')
37
38 // 🐨 move this up to the App as well
39 useEffect(() => {
40 const updateQuery = () => setQuery(getQueryParam())
41 window.addEventListener('popstate', updateQuery)
42 return () => {
43 window.removeEventListener('popstate', updateQuery)
44 }
45 }, [])
46
47 function handleCheck(tag: string, checked: boolean) {
48 const newWords = checked ? [...words, tag] : words.filter((w) => w !== tag)
49 setQuery(newWords.filter(Boolean).join(' ').trim())
50 }
51
52 return (
53 <form
54 action={() => {
55 setGlobalSearchParams({ query })
56 }}
57 >
58 <div>
59 <label htmlFor="searchInput">Search:</label>
60 <input
61 id="searchInput"
62 name="query"
63 type="search"
64 value={query}
65 onChange={(e) => setQuery(e.currentTarget.value)}
66 />
67 </div>
68 <div>
69 <label>
70 <input
71 type="checkbox"
72 checked={dogChecked}
73 onChange={(e) => handleCheck('dog', e.currentTarget.checked)}
74 />{' '}
75 🐶 dog
76 </label>
77 <label>
78 <input
79 type="checkbox"
80 checked={catChecked}
81 onChange={(e) => handleCheck('cat', e.currentTarget.checked)}
82 />{' '}
83 🐱 cat
84 </label>
85 <label>

Callers

nothing calls this directly

Calls 2

setGlobalSearchParamsFunction · 0.90
handleCheckFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…