1
1
'use client' ;
2
- import { Form , TextField } from '@obosbbl/grunnmuren-react' ;
3
- import { useFormState } from 'react-dom ' ;
2
+ import { Button , Form , TextField } from '@obosbbl/grunnmuren-react' ;
3
+ import { type FormEvent , startTransition , useActionState } from 'react' ;
4
4
5
5
import { submitForm } from './submit-action' ;
6
- import SubmitButton from './submit-button' ;
7
6
8
7
export default function ( ) {
9
- const [ { errors } , formAction ] = useFormState ( submitForm , { errors : { } } ) ;
8
+ const [ { errors } , formAction , isPending ] = useActionState ( submitForm , {
9
+ errors : { } ,
10
+ } ) ;
11
+
12
+ // cannot use action directly on the form submit, because it resets the form
13
+ // which means our validation messages aren't displayed...
14
+ // another workaround for this is here https://www.robinwieruch.de/react-server-action-reset-form/
15
+ const handleSubmit = ( event : FormEvent < HTMLFormElement > ) => {
16
+ event . preventDefault ( ) ;
17
+ startTransition ( ( ) => {
18
+ formAction ( new FormData ( event . currentTarget ) ) ;
19
+ } ) ;
20
+ } ;
10
21
11
22
return (
12
- < Form action = { formAction } validationErrors = { errors } className = "space-y-4" >
23
+ < Form
24
+ onSubmit = { handleSubmit }
25
+ validationErrors = { errors }
26
+ className = "space-y-4"
27
+ >
13
28
< p >
14
29
This is an uncontrolled form that uses zod to validate the form data on
15
30
the server side in a React server action.
@@ -26,7 +41,10 @@ export default function () {
26
41
</ >
27
42
}
28
43
/>
29
- < SubmitButton > Save</ SubmitButton >
44
+
45
+ < Button isPending = { isPending } type = "submit" >
46
+ Save
47
+ </ Button >
30
48
</ Form >
31
49
) ;
32
50
}
0 commit comments