-
Notifications
You must be signed in to change notification settings - Fork 8
/
[index].tsx
executable file
·70 lines (61 loc) · 1.72 KB
/
[index].tsx
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
import { object, string } from '@recoiljs/refine'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { SubmitHandler } from 'react-hook-form/dist/types/form'
import { syncEffect } from 'recoil-sync'
import { initializableAtom } from 'recoil-sync-next'
import { useFormSync } from '../../src/hooks/useFormSync'
import styles from '../../styles/form.module.css'
import type { NextPage } from 'next'
type FormState = {
name: string
comment: string
}
const formState = initializableAtom<FormState>({
key: 'formState',
effects: [
syncEffect({
refine: object({
name: string(),
comment: string(),
}),
}),
],
})
const Form: NextPage = () => {
// check render
console.log('Form: re render')
const { register, onChangeForm, handleSubmit } = useFormSync(
formState({ name: 'a', comment: 'b' })
)
const router = useRouter()
const onSubmit: SubmitHandler<FormState> = async (data) => {
console.log('submit data', data)
await router.push('/form/success')
}
return (
<div className={styles.container}>
<Head>
<title>Form</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>Form</h1>
<form onSubmit={handleSubmit(onSubmit)} onChange={onChangeForm}>
<dl className={styles.formList}>
<dt>name</dt>
<dd>
<input type="text" {...register('name')} />
</dd>
<dt>comment</dt>
<dd>
<input type="text" {...register('comment')} />
</dd>
</dl>
<button>submit</button>
</form>
</main>
</div>
)
}
export default Form