{"version":3,"sources":["../../src/client/form.tsx"],"sourcesContent":["'use client'\n\nimport { type FormEvent, useContext, forwardRef } from 'react'\nimport { addBasePath } from './add-base-path'\nimport { RouterContext } from '../shared/lib/router-context.shared-runtime'\nimport type { NextRouter } from './router'\nimport {\n checkFormActionUrl,\n createFormSubmitDestinationUrl,\n DISALLOWED_FORM_PROPS,\n hasReactClientActionAttributes,\n hasUnsupportedSubmitterAttributes,\n type FormProps,\n} from './form-shared'\n\nexport type { FormProps }\n\nconst Form = forwardRef(function FormComponent(\n { replace, scroll, prefetch: prefetchProp, ...props },\n ref\n) {\n const router = useContext(RouterContext)\n\n const actionProp = props.action\n const isNavigatingForm = typeof actionProp === 'string'\n\n // Validate `action`\n if (process.env.NODE_ENV === 'development') {\n if (isNavigatingForm) {\n checkFormActionUrl(actionProp, 'action')\n }\n }\n\n // Validate `prefetch`\n if (process.env.NODE_ENV === 'development') {\n if (prefetchProp !== undefined) {\n console.error(\n 'Passing `prefetch` to a
has no effect in the pages directory.'\n )\n }\n }\n\n // Validate `scroll` and `replace`\n if (process.env.NODE_ENV === 'development') {\n if (!isNavigatingForm && (replace !== undefined || scroll !== undefined)) {\n console.error(\n 'Passing `replace` or `scroll` to a whose `action` is a function has no effect.\\n' +\n 'See the relevant docs to learn how to control this behavior for navigations triggered from actions:\\n' +\n ' `router.replace()` - https://nextjs.org/docs/pages/api-reference/functions/use-router#routerreplace\\n'\n )\n }\n }\n\n // Clean up any unsupported form props (and warn if present)\n for (const key of DISALLOWED_FORM_PROPS) {\n if (key in props) {\n if (process.env.NODE_ENV === 'development') {\n console.error(` does not support changing \\`${key}\\`.`)\n }\n delete (props as Record)[key]\n }\n }\n\n if (!isNavigatingForm) {\n return \n }\n\n const actionHref = addBasePath(actionProp)\n\n return (\n \n onFormSubmit(event, {\n router,\n actionHref,\n replace,\n scroll,\n onSubmit: props.onSubmit,\n })\n }\n />\n )\n})\n\nexport default Form\n\nfunction onFormSubmit(\n event: FormEvent,\n {\n actionHref,\n onSubmit,\n replace,\n scroll,\n router,\n }: {\n actionHref: string\n onSubmit: FormProps['onSubmit']\n replace: FormProps['replace']\n scroll: FormProps['scroll']\n router: NextRouter | null\n }\n) {\n if (typeof onSubmit === 'function') {\n onSubmit(event)\n\n // if the user called event.preventDefault(), do nothing.\n // (this matches what Link does for `onClick`)\n if (event.defaultPrevented) {\n return\n }\n }\n\n if (!router) {\n // Form was somehow used outside of the router (but not in app/, the implementation is forked!).\n // We can't perform a soft navigation, so let the native submit handling do its thing.\n return\n }\n\n const formElement = event.currentTarget\n const submitter = (event.nativeEvent as SubmitEvent).submitter\n\n let action = actionHref\n\n if (submitter) {\n // this is page-router-only, so we don't need to worry about false positives\n // from the attributes that react adds for server actions.\n if (hasUnsupportedSubmitterAttributes(submitter)) {\n return\n }\n\n // client actions have `formAction=\"javascript:...\"`. We obviously can't prefetch/navigate to that.\n if (hasReactClientActionAttributes(submitter)) {\n return\n }\n\n // If the submitter specified an alternate formAction,\n // use that URL instead -- this is what a native form would do.\n // NOTE: `submitter.formAction` is unreliable, because it will give us `location.href` if it *wasn't* set\n // NOTE: this should not have `basePath` added, because we can't add it before hydration\n const submitterFormAction = submitter.getAttribute('formAction')\n if (submitterFormAction !== null) {\n if (process.env.NODE_ENV === 'development') {\n checkFormActionUrl(submitterFormAction, 'formAction')\n }\n action = submitterFormAction\n }\n }\n\n const targetUrl = createFormSubmitDestinationUrl(action, formElement)\n\n // Finally, no more reasons for bailing out.\n event.preventDefault()\n\n const method = replace ? 'replace' : 'push'\n const targetHref = targetUrl.href // TODO: will pages router be happy about an absolute URL here?\n\n // TODO(form): Make this use a transition so that pending states work\n //\n // Unlike the app router, pages router doesn't use startTransition,\n // and can't easily be wrapped in one because of implementation details\n // (e.g. it doesn't use any react state)\n // But it's important to have this wrapped in a transition because\n // pending states from e.g. `useFormStatus` rely on that.\n // So this needs some follow up work.\n router[method](targetHref, undefined, { scroll })\n}\n"],"names":["useContext","forwardRef","addBasePath","RouterContext","checkFormActionUrl","createFormSubmitDestinationUrl","DISALLOWED_FORM_PROPS","hasReactClientActionAttributes","hasUnsupportedSubmitterAttributes","Form","FormComponent","replace","scroll","prefetch","prefetchProp","props","ref","router","actionProp","action","isNavigatingForm","process","env","NODE_ENV","undefined","console","error","key","form","actionHref","onSubmit","event","onFormSubmit","defaultPrevented","formElement","currentTarget","submitter","nativeEvent","submitterFormAction","getAttribute","targetUrl","preventDefault","method","targetHref","href"],"mappings":"AAAA;;AAEA,SAAyBA,UAAU,EAAEC,UAAU,QAAQ,QAAO;AAC9D,SAASC,WAAW,QAAQ,kBAAiB;AAC7C,SAASC,aAAa,QAAQ,8CAA6C;AAE3E,SACEC,kBAAkB,EAClBC,8BAA8B,EAC9BC,qBAAqB,EACrBC,8BAA8B,EAC9BC,iCAAiC,QAE5B,gBAAe;AAItB,MAAMC,qBAAOR,WAAuC,SAASS,cAC3D,EAAEC,OAAO,EAAEC,MAAM,EAAEC,UAAUC,YAAY,EAAE,GAAGC,OAAO,EACrDC,GAAG;IAEH,MAAMC,SAASjB,WAAWG;IAE1B,MAAMe,aAAaH,MAAMI,MAAM;IAC/B,MAAMC,mBAAmB,OAAOF,eAAe;IAE/C,oBAAoB;IACpB,IAAIG,QAAQC,GAAG,CAACC,QAAQ,KAAK,eAAe;QAC1C,IAAIH,kBAAkB;YACpBhB,mBAAmBc,YAAY;QACjC;IACF;IAEA,sBAAsB;IACtB,IAAIG,QAAQC,GAAG,CAACC,QAAQ,KAAK,eAAe;QAC1C,IAAIT,iBAAiBU,WAAW;YAC9BC,QAAQC,KAAK,CACX;QAEJ;IACF;IAEA,kCAAkC;IAClC,IAAIL,QAAQC,GAAG,CAACC,QAAQ,KAAK,eAAe;QAC1C,IAAI,CAACH,oBAAqBT,CAAAA,YAAYa,aAAaZ,WAAWY,SAAQ,GAAI;YACxEC,QAAQC,KAAK,CACX,4FACE,0GACA;QAEN;IACF;IAEA,4DAA4D;IAC5D,KAAK,MAAMC,OAAOrB,sBAAuB;QACvC,IAA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