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

contact-form.tsx

82 lines2.0 KB

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