MCPcopy
hub / github.com/vercel/next.js / onFormSubmit

Function onFormSubmit

packages/next/src/client/app-dir/form.tsx:147–224  ·  view source on GitHub ↗
(
  event: SubmitEvent<HTMLFormElement>,
  {
    actionHref,
    onSubmit,
    replace,
    scroll,
    router,
  }: {
    actionHref: string
    onSubmit: FormProps['onSubmit']
    replace: FormProps['replace']
    scroll: FormProps['scroll']
    router: AppRouterInstance | null
  }
)

Source from the content-addressed store, hash-verified

145}
146
147function onFormSubmit(
148 event: SubmitEvent<HTMLFormElement>,
149 {
150 actionHref,
151 onSubmit,
152 replace,
153 scroll,
154 router,
155 }: {
156 actionHref: string
157 onSubmit: FormProps['onSubmit']
158 replace: FormProps['replace']
159 scroll: FormProps['scroll']
160 router: AppRouterInstance | null
161 }
162) {
163 if (typeof onSubmit === 'function') {
164 onSubmit(event)
165
166 // if the user called event.preventDefault(), do nothing.
167 // (this matches what Link does for `onClick`)
168 if (event.defaultPrevented) {
169 return
170 }
171 }
172
173 if (!router) {
174 // Form was somehow used outside of the router (but not in pages, the implementation is forked!).
175 // We can't perform a soft navigation, so let the native submit handling do its thing.
176 return
177 }
178
179 const formElement = event.currentTarget
180 const submitter = event.nativeEvent.submitter
181
182 let action = actionHref
183
184 if (submitter) {
185 if (process.env.NODE_ENV === 'development') {
186 // the way server actions are encoded (e.g. `formMethod="post")
187 // causes some unnecessary dev-mode warnings from `hasUnsupportedSubmitterAttributes`.
188 // we'd bail out anyway, but we just do it silently.
189 if (hasReactServerActionAttributes(submitter)) {
190 return
191 }
192 }
193
194 if (hasUnsupportedSubmitterAttributes(submitter)) {
195 return
196 }
197
198 // client actions have `formAction="javascript:..."`. We obviously can't prefetch/navigate to that.
199 if (hasReactClientActionAttributes(submitter)) {
200 return
201 }
202
203 // If the submitter specified an alternate formAction,
204 // use that URL instead -- this is what a native form would do.

Callers 1

FormFunction · 0.70

Calls 7

checkFormActionUrlFunction · 0.90
getAttributeMethod · 0.80
onSubmitFunction · 0.50

Tested by

no test coverage detected