Skip to content

Commit 18bafc7

Browse files
committed
feat(FormProvider): submit, onNotify in FormProvider
It is now possible to pass onNotify and submit functions to FormProvider, instead of defining them in each useForm. The handleSubmit function now also gives the name of the form it was called from, to be able to identify what form called the submit function if it was passed to FormProvider.
1 parent 519800b commit 18bafc7

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/FormContext.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useReducer, useEffect, useRef } from 'react'
1+
import React, { createContext, useReducer, useEffect, useRef, useMemo } from 'react'
22

33
const FormContext = createContext()
44

@@ -8,7 +8,13 @@ function reducer(state, { type, payload }) {
88
{ ...state, ...payload }
99
}
1010

11-
function FormProvider({ children, initialState = {}, validators = {} }) {
11+
function FormProvider({
12+
children,
13+
initialState = {},
14+
validators = {},
15+
onNotify = null,
16+
submit = null
17+
}) {
1218
const [forms, dispatch] = useReducer(reducer, initialState)
1319

1420

@@ -23,8 +29,16 @@ function FormProvider({ children, initialState = {}, validators = {} }) {
2329
}
2430
}, [initialState])
2531

32+
const value = useMemo(() => ({
33+
forms,
34+
dispatch,
35+
validators,
36+
onNotify,
37+
submit
38+
}), [forms, onNotify, submit, validators])
39+
2640
return (
27-
<FormContext.Provider value={{ forms, dispatch, validators }}>
41+
<FormContext.Provider value={value}>
2842
{children}
2943
</FormContext.Provider>
3044
)

src/useForm.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const isObject = o => Object.prototype.toString.call(o) === '[object Object]'
77

88
export default function useForm ({
99
name,
10-
submit,
10+
submit = null,
1111
validators = undefined,
1212
onFinished = null,
1313
onNotify = null,
@@ -16,14 +16,22 @@ export default function useForm ({
1616
}) {
1717

1818

19-
const { dispatch, forms, validators: _validators } = useContext(context || FormContext)
19+
const {
20+
dispatch,
21+
forms,
22+
validators: _validators,
23+
onNotify: _onNotify,
24+
submit: _submit
25+
} = useContext(context || FormContext)
2026

2127
const form = forms[name]
2228

2329
validators = validators || (_validators[name] ? { ..._validators[name] } : undefined)
30+
onNotify = onNotify || _onNotify
2431

2532
const [errors, setErrors] = useState({})
2633
const [loading, setLoading] = useState(false)
34+
submit = submit || _submit
2735

2836

2937
if (process.env.NODE_ENV !== 'production') {
@@ -142,6 +150,7 @@ export default function useForm ({
142150
}
143151
else {
144152
return submit({
153+
name,
145154
fields: form,
146155
setLoading,
147156
finish: (...args) => {
@@ -153,7 +162,7 @@ export default function useForm ({
153162
}
154163
}, [
155164
// REVIEW: Find a better way to optimize here.
156-
form, loading, onNotify, validators, submit, onFinished,
165+
name, form, loading, onNotify, validators, submit, onFinished,
157166
])
158167

159168
return ({

0 commit comments

Comments
 (0)