-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseArrayField.ts
99 lines (87 loc) · 3.11 KB
/
useArrayField.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
95
96
97
98
99
import { useCallback, useEffect, useReducer, useRef, Dispatch, Reducer } from 'react';
import isEqual from 'react-fast-compare';
import {
initArrayFieldState,
arrayFieldReducer,
ArrayFieldAction,
ArrayFieldState,
} from '../reducers';
import { useFormState } from './useFormState';
import { useParentField } from './useParentField';
import { useValues } from './useValues';
import { useError } from './useError';
export function useArrayField<TValue extends any[] = any[]>(
name: string | number,
removeOnUnmount: boolean = false,
): [ArrayFieldState<TValue>, Dispatch<ArrayFieldAction<TValue>>] {
const [formState] = useFormState();
const [parentFieldState, parentFieldDispatch] = useParentField();
const [initialValue, parentsValue] = useValues<TValue>(name.toString(), parentFieldState);
const error = useError(name.toString(), parentFieldState);
const [fieldState, fieldDispatch] = useReducer(
arrayFieldReducer as Reducer<ArrayFieldState<TValue>, ArrayFieldAction<TValue>>,
[initialValue, parentsValue] as [TValue | undefined, TValue | undefined],
initArrayFieldState,
);
const currentState = useRef(fieldState);
const previousParentsValue = useRef(parentsValue);
const dispatch: Dispatch<ArrayFieldAction<TValue>> = useCallback(
action => {
if (formState.status === 'IDLE' || formState.status === 'CHANGING') {
fieldDispatch(action);
}
},
[formState.status],
);
if (currentState.current !== fieldState) {
// propagate change
if (currentState.current.changing !== fieldState.changing) {
if (fieldState.changing) {
parentFieldDispatch({ type: 'CHANGING', name: name.toString() });
} /* else {
parentFieldDispatch({
type: 'CHANGE_FIELD',
name: name.toString(),
value: fieldState.value,
});
} */
}
if (!isEqual(currentState.current.value, fieldState.value)) {
// this also sets the field as not changing
parentFieldDispatch({
type: 'CHANGE_FIELD',
name: name.toString(),
value: fieldState.value,
});
}
currentState.current = fieldState;
}
// if initial value changes, set it
if (!isEqual(initialValue, fieldState.initialValue)) {
previousParentsValue.current = initialValue;
fieldDispatch({ type: 'SET_INITIAL_VALUE', value: initialValue as TValue });
} else if (!isEqual(previousParentsValue.current, parentsValue)) {
previousParentsValue.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: name.toString(),
value: currentState.current.value,
});
if (removeOnUnmount) {
parentFieldDispatch({ type: 'REMOVE_FIELD', name: name.toString() });
}
};
}, [name, removeOnUnmount]);
// if error is changed, propagate down
if (error !== fieldState.error) {
fieldDispatch({ type: 'SET_ERROR', error });
}
return [fieldState, dispatch];
}