@@ -5,22 +5,42 @@ function Input(props: { value?: string; onChange?: (value: string) => void; styl
5
5
return < input value = { props . value } onChange = { ( ev ) => props . onChange ?.( ev . target . value ) } style = { props . style } /> ;
6
6
}
7
7
8
+ function validate ( _ : any ) {
9
+ return {
10
+ email : "yikes"
11
+ } ;
12
+ }
13
+
14
+ function Error ( props : { children : React . ReactNode } ) {
15
+ return (
16
+ < p >
17
+ not epic:
18
+ < strong > { props . children } </ strong >
19
+ </ p >
20
+ ) ;
21
+ }
22
+
8
23
export function FieldForm ( ) {
9
- const form = useForm ( { email : "" , firstName : "" , gender : "male" as "male" | "female" } ) ;
24
+ const form = useForm ( { email : "" , firstName : "" , gender : "male" as "male" | "female" } , validate ) ;
25
+
26
+ function submit ( ) {
27
+ console . log ( "this is epic" ) ;
28
+ }
10
29
11
30
return (
12
- < form >
31
+ < form onSubmit = { form . handleSubmit ( submit ) } >
13
32
< Field form = { form } name = "email" component = "textarea" />
14
33
< Field form = { form } name = "email" component = "input" />
15
- < Field form = { form } name = "email" component = { Input } style = { { margin : "1em " } } />
16
- < FieldError form = { form } name = "email" />
34
+ < Field form = { form } name = "email" component = { Input } style = { { margin : "2em " } } />
35
+ < FieldError form = { form } name = "email" component = { Error } />
17
36
< Field form = { form } name = "gender" component = "select" >
18
37
< option value = "male" > male</ option >
19
38
< option value = "female" > female</ option >
20
39
</ Field >
21
40
< pre >
22
41
< AnyListener form = { form } render = { ( ) => JSON . stringify ( form . values , null , 2 ) } />
23
42
</ pre >
43
+ < button type = "submit" > Submit</ button >
24
44
</ form >
25
45
) ;
26
46
}
0 commit comments