Skip to content

Commit 519800b

Browse files
committed
feat(FormProvider): validators in FormProvider
It is now possible to send a group of validators as a prop through FormProvider, much like initialState.
1 parent 8ee4c48 commit 519800b

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/FormContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function reducer(state, { type, payload }) {
88
{ ...state, ...payload }
99
}
1010

11-
function FormProvider({ children, initialState = {} }) {
11+
function FormProvider({ children, initialState = {}, validators = {} }) {
1212
const [forms, dispatch] = useReducer(reducer, initialState)
1313

1414

@@ -24,7 +24,7 @@ function FormProvider({ children, initialState = {} }) {
2424
}, [initialState])
2525

2626
return (
27-
<FormContext.Provider value={{ forms, dispatch }}>
27+
<FormContext.Provider value={{ forms, dispatch, validators }}>
2828
{children}
2929
</FormContext.Provider>
3030
)

src/useForm.js

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

88
export default function useForm ({
99
name,
10-
validators,
1110
submit,
11+
validators = undefined,
1212
onFinished = null,
1313
onNotify = null,
1414
context = null,
1515
...rest
1616
}) {
1717

1818

19-
const { dispatch, forms } = useContext(context || FormContext)
19+
const { dispatch, forms, validators: _validators } = useContext(context || FormContext)
2020

2121
const form = forms[name]
2222

23+
validators = validators || (_validators[name] ? { ..._validators[name] } : undefined)
24+
2325
const [errors, setErrors] = useState({})
2426
const [loading, setLoading] = useState(false)
2527

typings/FormContext.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ export {
2020
}
2121

2222

23-
interface FormProviderProps {
23+
interface FormProviderProps<T extends {[name: string]: Object}>{
2424
children: React.ReactElement,
2525
/** Give the initial state of the forms.
2626
*
2727
* NOTE: It "can change", meaning you can set a default value,
2828
* and replace it with another value if you receive
2929
* this data asynchronously.
3030
*/
31-
initialState: {
32-
[name: string]: Object
33-
}
31+
initialState: T
32+
/**
33+
* The validators for the forms. Mirrors the structure of `initialState`.
34+
*/
35+
validators?: T
3436
}
3537

3638
interface Action {

typings/useForm.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ interface UseFormParams {
2929
*
3030
* A validator function returns `true`
3131
* if the field(s) it is testing are valid.
32+
* @note You can also pass the validators to the
33+
* FormProvider.
3234
*/
33-
validators: Validators
35+
validators?: Validators
3436
/** Function called to submit the form. */
3537
submit: SubmitFunction
3638
/**

0 commit comments

Comments
 (0)