| 'use client' |
|
|
| import { type FormEvent, useContext, forwardRef } from 'react' |
| import { addBasePath } from './add-base-path' |
| import { RouterContext } from '../shared/lib/router-context.shared-runtime' |
| import type { NextRouter } from './router' |
| import { |
| checkFormActionUrl, |
| createFormSubmitDestinationUrl, |
| DISALLOWED_FORM_PROPS, |
| hasReactClientActionAttributes, |
| hasUnsupportedSubmitterAttributes, |
| type FormProps, |
| } from './form-shared' |
|
|
| export type { FormProps } |
|
|
| const Form = forwardRef<HTMLFormElement, FormProps>(function FormComponent( |
| { replace, scroll, prefetch: prefetchProp, ...props }, |
| ref |
| ) { |
| const router = useContext(RouterContext) |
|
|
| const actionProp = props.action |
| const isNavigatingForm = typeof actionProp === 'string' |
|
|
| |
| if (process.env.NODE_ENV === 'development') { |
| if (isNavigatingForm) { |
| checkFormActionUrl(actionProp, 'action') |
| } |
| } |
|
|
| |
| if (process.env.NODE_ENV === 'development') { |
| if (prefetchProp !== undefined) { |
| console.error( |
| 'Passing `prefetch` to a <Form> has no effect in the pages directory.' |
| ) |
| } |
| } |
|
|
| |
| if (process.env.NODE_ENV === 'development') { |
| if (!isNavigatingForm && (replace !== undefined || scroll !== undefined)) { |
| console.error( |
| 'Passing `replace` or `scroll` to a <Form> whose `action` is a function has no effect.\n' + |
| 'See the relevant docs to learn how to control this behavior for navigations triggered from actions:\n' + |
| ' `router.replace()` - https://nextjs.org/docs/pages/api-reference/functions/use-router#routerreplace\n' |
| ) |
| } |
| } |
|
|
| |
| for (const key of DISALLOWED_FORM_PROPS) { |
| if (key in props) { |
| if (process.env.NODE_ENV === 'development') { |
| console.error(`<Form> does not support changing \`${key}\`.`) |
| } |
| delete (props as Record<string, unknown>)[key] |
| } |
| } |
|
|
| if (!isNavigatingForm) { |
| return <form {...props} ref={ref} /> |
| } |
|
|
| const actionHref = addBasePath(actionProp) |
|
|
| return ( |
| <form |
| {...props} |
| ref={ref} |
| action={actionHref} |
| onSubmit={(event) => |
| onFormSubmit(event, { |
| router, |
| actionHref, |
| replace, |
| scroll, |
| onSubmit: props.onSubmit, |
| }) |
| } |
| /> |
| ) |
| }) |
|
|
| export default Form |
|
|
| function onFormSubmit( |
| event: FormEvent<HTMLFormElement>, |
| { |
| actionHref, |
| onSubmit, |
| replace, |
| scroll, |
| router, |
| }: { |
| actionHref: string |
| onSubmit: FormProps['onSubmit'] |
| replace: FormProps['replace'] |
| scroll: FormProps['scroll'] |
| router: NextRouter | null |
| } |
| ) { |
| if (typeof onSubmit === 'function') { |
| onSubmit(event) |
|
|
| |
| |
| if (event.defaultPrevented) { |
| return |
| } |
| } |
|
|
| if (!router) { |
| |
| |
| return |
| } |
|
|
| const formElement = event.currentTarget |
| const submitter = (event.nativeEvent as SubmitEvent).submitter |
|
|
| let action = actionHref |
|
|
| if (submitter) { |
| |
| |
| if (hasUnsupportedSubmitterAttributes(submitter)) { |
| return |
| } |
|
|
| |
| if (hasReactClientActionAttributes(submitter)) { |
| return |
| } |
|
|
| |
| |
| |
| |
| const submitterFormAction = submitter.getAttribute('formAction') |
| if (submitterFormAction !== null) { |
| if (process.env.NODE_ENV === 'development') { |
| checkFormActionUrl(submitterFormAction, 'formAction') |
| } |
| action = submitterFormAction |
| } |
| } |
|
|
| const targetUrl = createFormSubmitDestinationUrl(action, formElement) |
|
|
| |
| event.preventDefault() |
|
|
| const method = replace ? 'replace' : 'push' |
| const targetHref = targetUrl.href |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| router[method](targetHref, undefined, { scroll }) |
| } |
|
|