Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

contact-form.tsx

85 lines2.0 KB

1import { useRouter } from '@tanstack/react-router'
2import type { Contact } from './services/contacts/types'
3​
4export type ContactFormValues = Pick<
5 Contact,
6 'first' | 'last' | 'twitter' | 'avatar' | 'notes'
7>
8​
9export default function ContactForm({
10 contact,
11 onSubmit,
12}: {
13 contact?: Contact
14 onSubmit: (values: ContactFormValues) => void
15}) {
16 const router = useRouter()
17​
18 return (
19 <form
20 id="contact-form"
21 onSubmit={(event) => {
22 event.preventDefault()
23 const formData = new FormData(event.currentTarget)
24 onSubmit({
25 first: String(formData.get('first')),
26 last: String(formData.get('last')),
27 twitter: String(formData.get('twitter')),
28 avatar: String(formData.get('avatar')),
29 notes: String(formData.get('notes')),
30 })
31 }}
32 >
33 <p>
34 <span>Name</span>
35 <input
36 placeholder="First"
37 aria-label="First name"
38 type="text"
39 name="first"
40 defaultValue={contact?.first}
41 />
42 <input
43 placeholder="Last"
44 aria-label="Last name"
45 type="text"
46 name="last"
47 defaultValue={contact?.last}
48 />
49 </p>
50 <label>
51 <span>Twitter</span>
52 <input
53 type="text"
54 name="twitter"
55 placeholder="@jack"
56 defaultValue={contact?.twitter}
57 />
58 </label>
59 <label>
60 <span>Avatar URL</span>
61 <input
62 placeholder="https://example.com/avatar.jpg"
63 type="text"
64 name="avatar"
65 defaultValue={contact?.avatar}
66 />
67 </label>
68 <label>
69 <span>Notes</span>
70 <textarea name="notes" defaultValue={contact?.notes} rows={6} />
71 </label>
72 <p>
73 <button type="submit">Save</button>
74 <button
75 type="button"
76 onClick={() => {
77 router.history.back()
78 }}
79 >
80 Cancel
81 </button>
82 </p>
83 </form>
84 )