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

Function onFormSubmit

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

Source from the content-addressed store, hash-verified

88export default Form
89
90function onFormSubmit(
91 event: SubmitEvent<HTMLFormElement>,
92 {
93 actionHref,
94 onSubmit,
95 replace,
96 scroll,
97 router,
98 }: {
99 actionHref: string
100 onSubmit: FormProps['onSubmit']
101 replace: FormProps['replace']
102 scroll: FormProps['scroll']
103 router: NextRouter | null
104 }
105) {
106 if (typeof onSubmit === 'function') {
107 onSubmit(event)
108
109 // if the user called event.preventDefault(), do nothing.
110 // (this matches what Link does for `onClick`)
111 if (event.defaultPrevented) {
112 return
113 }
114 }
115
116 if (!router) {
117 // Form was somehow used outside of the router (but not in app/, the implementation is forked!).
118 // We can't perform a soft navigation, so let the native submit handling do its thing.
119 return
120 }
121
122 const formElement = event.currentTarget
123 const submitter = event.nativeEvent.submitter
124
125 let action = actionHref
126
127 if (submitter) {
128 // this is page-router-only, so we don't need to worry about false positives
129 // from the attributes that react adds for server actions.
130 if (hasUnsupportedSubmitterAttributes(submitter)) {
131 return
132 }
133
134 // client actions have `formAction="javascript:..."`. We obviously can't prefetch/navigate to that.
135 if (hasReactClientActionAttributes(submitter)) {
136 return
137 }
138
139 // If the submitter specified an alternate formAction,
140 // use that URL instead -- this is what a native form would do.
141 // NOTE: `submitter.formAction` is unreliable, because it will give us `location.href` if it *wasn't* set
142 // NOTE: this should not have `basePath` added, because we can't add it before hydration
143 const submitterFormAction = submitter.getAttribute('formAction')
144 if (submitterFormAction !== null) {
145 if (process.env.NODE_ENV === 'development') {
146 checkFormActionUrl(submitterFormAction, 'formAction')
147 }

Callers 1

form.tsxFile · 0.70

Calls 6

checkFormActionUrlFunction · 0.90
getAttributeMethod · 0.80
onSubmitFunction · 0.50

Tested by

no test coverage detected