-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseForm.ts
94 lines (87 loc) · 3.18 KB
/
useForm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import isEqual from 'react-fast-compare';
import { Dispatch, Reducer, useReducer, useRef, useCallback } from 'react';
import {
ArrayFieldAction,
ArrayFieldState,
FormState,
FormAction,
formReducer,
initFormState,
ObjectFieldAction,
ObjectFieldState,
} from '../reducers';
import { ValidationError } from './ValidationError';
export {
ArrayFieldAction,
ArrayFieldState,
FormState,
FormAction,
ObjectFieldState,
ObjectFieldAction,
};
export function useForm<TValue extends { [key: string]: any } = { [key: string]: any }>(
initialValue?: TValue,
onChange?: (value: Partial<TValue> | undefined) => Promise<void>,
onSubmit?: (value: TValue) => Promise<void>,
onValidate?: (value: Partial<TValue> | undefined) => Promise<TValue | void>,
validateOnChange: boolean = false,
): [FormState<TValue>, Dispatch<FormAction<TValue>>] {
const [state, dispatch] = useReducer(
formReducer as Reducer<FormState<TValue>, FormAction<TValue>>,
initialValue,
initFormState,
);
const previousFormState = useRef(state);
const formDispatch: typeof dispatch = useCallback(
action => {
dispatch(action.type === 'CHANGE_FIELD' ? { ...action, validateOnChange } : action);
},
[validateOnChange],
);
if (previousFormState.current !== state) {
if (previousFormState.current.status !== state.status) {
if (state.status === 'VALIDATING' || state.status === 'VALIDATING_ON_CHANGE') {
Promise.resolve(onValidate ? onValidate(state.value) : state.value)
.then(value => dispatch({ type: 'VALIDATING_DONE', value: value || state.value }))
.catch(e => {
// process validation error
if (e instanceof ValidationError) {
dispatch({ type: 'VALIDATING_FAILED', error: e.errors });
} else if (e instanceof Error) {
dispatch({ type: 'VALIDATING_FAILED', error: { '': e.message } });
} else {
dispatch({ type: 'VALIDATING_FAILED', error: { '': '' + e } });
}
});
} else if (state.status === 'SUBMITTING') {
// SUBMITTING won't be called if value of form is undefined
Promise.resolve(onSubmit ? onSubmit(state.value!) : undefined)
.then(() => {
dispatch({ type: 'SUBMITTING_DONE' });
})
.catch(e => {
// process validation error
if (e instanceof ValidationError) {
dispatch({ type: 'SUBMITTING_FAILED', error: e.errors });
} else if (e instanceof Error) {
dispatch({ type: 'SUBMITTING_FAILED', error: { '': e.message } });
} else {
dispatch({ type: 'SUBMITTING_FAILED', error: { '': '' + e } });
}
});
}
}
if (onChange && !isEqual(previousFormState.current.value, state.value)) {
onChange(state.value);
}
previousFormState.current = state;
}
// if form is idle or changing and initial value changed, reset everything
if (
!isEqual(state.initialValue, initialValue) &&
(state.status === 'IDLE' || state.status === 'CHANGING')
) {
dispatch({ type: 'SET_INITIAL_VALUE', value: initialValue as TValue });
}
return [state, formDispatch];
}