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

Function Form

exercises/03.lifting-state/03.problem.colocate/index.tsx:34–99  ·  view source on GitHub ↗
({
	query,
	setQuery,
}: {
	query: string
	setQuery: (query: string) => void
})

Source from the content-addressed store, hash-verified

32}
33
34function Form({
35 query,
36 setQuery,
37}: {
38 query: string
39 setQuery: (query: string) => void
40}) {
41 const words = query.split(' ').map((w) => w.trim())
42
43 const dogChecked = words.includes('dog')
44 const catChecked = words.includes('cat')
45 const caterpillarChecked = words.includes('caterpillar')
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>
86 <input
87 type="checkbox"
88 checked={caterpillarChecked}
89 onChange={(e) =>
90 handleCheck('caterpillar', e.currentTarget.checked)
91 }

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…