Skip to content

Commit 2bfdd77

Browse files
authored
Merge pull request #1 from cilice/add-handle-submit
Add handleSubmit handler
2 parents 5d14f2f + abab643 commit 2bfdd77

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/useForm.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useRef } from 'react'
1+
import { useState, useEffect, useRef, useCallback } from 'react'
22
import { createForm, formSubscriptionItems } from 'final-form'
33

44
export const all = formSubscriptionItems.reduce((result, key) => {
@@ -12,7 +12,19 @@ const useForm = ({ subscription, ...config }) => {
1212
useEffect(() => form.current.subscribe(setState, subscription || all), [
1313
subscription
1414
])
15-
return { ...state, form: form.current }
15+
const handleSubmit = useCallback(event => {
16+
if (event) {
17+
if (typeof event.preventDefault === 'function') {
18+
event.preventDefault()
19+
}
20+
if (typeof event.stopPropagation === 'function') {
21+
event.stopPropagation()
22+
}
23+
}
24+
return form.current.submit()
25+
}, [])
26+
27+
return { ...state, form: form.current, handleSubmit }
1628
}
1729

1830
export default useForm

0 commit comments

Comments
 (0)