MCPcopy Create free account
hub / github.com/formkit/formkit / handleSubmit

Function handleSubmit

packages/inputs/src/features/forms.ts:17–83  ·  view source on GitHub ↗

* Handle the submit event. * * @param e - The event * * @internal

(node: FormKitNode, submitEvent: Event)

Source from the content-addressed store, hash-verified

15 * @internal
16 */
17async function handleSubmit(node: FormKitNode, submitEvent: Event) {
18 const submitNonce = Math.random()
19 node.props._submitNonce = submitNonce
20 submitEvent.preventDefault()
21 await node.settled
22
23 if (node.ledger.value('validating')) {
24 // There are validation rules still pending.
25 node.store.set(loading)
26 await node.ledger.settled('validating')
27 node.store.remove('loading')
28 // If this was not the same submit event, bail out.
29 if (node.props._submitNonce !== submitNonce) return
30 }
31 // Set the submitted state on all children
32 const setSubmitted = (n: FormKitNode) =>
33 n.store.set(
34 createMessage({
35 key: 'submitted',
36 value: true,
37 visible: false,
38 })
39 )
40 node.walk(setSubmitted)
41 setSubmitted(node)
42
43 node.emit('submit-raw')
44 if (typeof node.props.onSubmitRaw === 'function') {
45 node.props.onSubmitRaw(submitEvent, node)
46 }
47
48 if (node.ledger.value('blocking')) {
49 if (typeof node.props.onSubmitInvalid === 'function') {
50 node.props.onSubmitInvalid(node)
51 }
52 // There is still a blocking message in the store.
53 if (node.props.incompleteMessage !== false) {
54 setIncompleteMessage(node)
55 }
56 } else {
57 // No blocking messages
58 if (typeof node.props.onSubmit === 'function') {
59 // call onSubmit
60 const retVal = node.props.onSubmit(
61 node.hook.submit.dispatch(clone(node.value as Record<string, any>)),
62 node
63 )
64 if (retVal instanceof Promise) {
65 const autoDisable =
66 node.props.disabled === undefined &&
67 node.props.submitBehavior !== 'live'
68 if (autoDisable) node.props.disabled = true
69 node.store.set(loading)
70 try {
71 await retVal
72 } finally {
73 if (autoDisable) node.props.disabled = false
74 node.store.remove('loading')

Callers

nothing calls this directly

Calls 3

cloneFunction · 0.90
setSubmittedFunction · 0.85
setIncompleteMessageFunction · 0.85

Tested by

no test coverage detected