11import { useForm } from '@inertiajs/react' ;
22import { useRef } from 'react' ;
3- import { useFormStatus } from 'react-dom' ;
43
5- export function NewPuppyForm ( ) {
6- const { post, setData, data, errors, reset } = useForm ( {
4+ export function NewPuppyForm ( { mainRef } : { mainRef ?: React . RefObject < HTMLElement | null > } ) {
5+ const { post, setData, data, errors, reset, processing } = useForm ( {
76 name : '' ,
87 trait : '' ,
98 image : null as File | null ,
@@ -22,8 +21,8 @@ export function NewPuppyForm() {
2221 if ( fileInputRef . current ) {
2322 fileInputRef . current . value = '' ;
2423 }
25- if ( typeof window !== 'undefined' ) {
26- window . scrollTo ( { top : 0 , behavior : 'smooth ' } ) ;
24+ if ( mainRef ?. current ) {
25+ mainRef . current . scrollIntoView ( { behavior : 'smooth' , block : 'start ' } ) ;
2726 }
2827 } ,
2928 } ) ;
@@ -68,22 +67,15 @@ export function NewPuppyForm() {
6867 { errors . image && < p className = "mt-1 text-xs text-red-500" > { errors . image } </ p > }
6968 </ fieldset >
7069 </ div >
71- < SubmitButton />
70+ < button
71+ className = "mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200"
72+ type = "submit"
73+ disabled = { processing }
74+ >
75+ { processing ? `Adding ${ data . name } ...` : 'Add puppy' }
76+ </ button >
7277 </ form >
7378 </ div >
7479 </ >
7580 ) ;
7681}
77-
78- function SubmitButton ( ) {
79- const status = useFormStatus ( ) ;
80- return (
81- < button
82- className = "mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200"
83- type = "submit"
84- disabled = { status . pending }
85- >
86- { status . pending ? `Adding ${ status ?. data ?. get ( 'name' ) || 'puppy' } ...` : 'Add puppy' }
87- </ button >
88- ) ;
89- }
0 commit comments