-
Notifications
You must be signed in to change notification settings - Fork 0
/
useObjectField.ts
86 lines (75 loc) · 2.94 KB
/
useObjectField.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
import { useCallback, useEffect, useReducer, useRef, Dispatch, Reducer } from 'react';
import isEqual from 'react-fast-compare';
import {
initObjectFieldState,
objectFieldReducer,
ObjectFieldAction,
ObjectFieldState,
} from '../reducers';
import { useFormState } from './useFormState';
import { useParentField } from './useParentField';
import { useValues } from './useValues';
import { useError } from './useError';
export function useObjectField<TValue extends { [key: string]: any } = { [key: string]: any }>(
name: string,
removeOnUnmount: boolean = false,
): [ObjectFieldState<TValue>, Dispatch<ObjectFieldAction<TValue>>] {
const [formState] = useFormState();
const [parentFieldState, parentFieldDispatch] = useParentField();
const [initialValue, parentsValue] = useValues<TValue>(name, parentFieldState);
const error = useError(name, parentFieldState);
const [fieldState, fieldDispatch] = useReducer(
objectFieldReducer as Reducer<ObjectFieldState<TValue>, ObjectFieldAction<TValue>>,
[initialValue, parentsValue] as [TValue | undefined, TValue | undefined],
initObjectFieldState,
);
const currentStateRef = useRef(fieldState);
const currentParentsValueRef = useRef(parentsValue);
const dispatch: Dispatch<ObjectFieldAction<TValue>> = useCallback(
action => {
if (formState.status === 'IDLE' || formState.status === 'CHANGING') {
fieldDispatch(action);
}
},
[formState.status],
);
if (currentStateRef.current !== fieldState) {
// propagate change
if (currentStateRef.current.changing !== fieldState.changing) {
if (fieldState.changing) {
parentFieldDispatch({ type: 'CHANGING', name });
} /* else {
parentFieldDispatch({ type: 'CHANGE_FIELD', name, value: fieldState.value });
} */
}
// propagate value change
if (!isEqual(currentStateRef.current.value, fieldState.value)) {
// this also sets the field as not changing
parentFieldDispatch({ type: 'CHANGE_FIELD', name, value: fieldState.value });
}
currentStateRef.current = fieldState;
}
// if initial value changes, set it
if (!isEqual(initialValue, fieldState.initialValue)) {
fieldDispatch({ type: 'SET_INITIAL_VALUE', value: initialValue as TValue });
} else if (!isEqual(currentParentsValueRef.current, parentsValue)) {
currentParentsValueRef.current = parentsValue;
if (parentsValue !== fieldState.value) {
fieldDispatch({ type: 'SET_VALUE', value: parentsValue as TValue });
}
}
// set field as changed on unmount
useEffect(() => {
return () => {
parentFieldDispatch({ type: 'CHANGE_FIELD', name, value: currentStateRef.current.value });
if (removeOnUnmount) {
parentFieldDispatch({ type: 'REMOVE_FIELD', name });
}
};
}, [name, removeOnUnmount]);
// if error is changed, propagate down
if (error !== fieldState.error) {
fieldDispatch({ type: 'SET_ERROR', error });
}
return [fieldState, dispatch];
}