import { useRouter } from 'next/router'; import { BaseSyntheticEvent, FormEvent, FormEventHandler, FormHTMLAttributes, forwardRef, useCallback, } from 'react'; import { HttpMethod } from '../http-methods'; import { useLatestRef } from '../lib/use-latest-ref'; import { getFormSubmitter, isButtonElement, isFormElement, isInputElement, isValidFormSubmitter, } from './dom'; import { FormStateWithHelpers } from './helpers'; import { useFormStoreSubmit } from './store'; export type FormProps = { /** * The method to use for form submissions. `get` appends the form-data to the * URL in name/value pairs, while others send the form-data as an HTTP post * transaction. Defaults to `post`. */ method?: HttpMethod | Uppercase; /** * A callback that's invoked on form submission. Call `event.preventDefault()` * if you don't want Form to handle your submission. For example to abort in * case of validation errors. * * @param formData the data that was entered in the form */ onSubmit?: FormEventHandler; /** * Called when the form is successfully submitted. * @param state The resulting form state */ onSuccess?: (state: FormStateWithHelpers) => void; /** * Called if an error occured during form submission. * @param state The resulting form state */ onError?: (state: FormStateWithHelpers) => void; } & Omit< FormHTMLAttributes, 'onSubmit' | 'onError' | 'method' >; function FormComponent( { method, action, encType, onSubmit, onSuccess, onError, ...props }: FormProps, ref: React.Ref, ) { const submit = useFormStoreSubmit(); const router = useRouter(); const onSuccessRef = useLatestRef(onSuccess); const onErrorRef = useLatestRef(onError); const handleSubmit: FormEventHandler = useCallback( async (event) => { onSubmit?.(event); if (event.defaultPrevented) return; event.preventDefault(); //