|
1 | | -import React, { useCallback, useEffect, useReducer } from 'react'; |
| 1 | +import { useCallback, useEffect, useMemo, useReducer } from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
3 | 3 | import isEqual from 'lodash/isEqual'; |
4 | 4 |
|
@@ -33,76 +33,73 @@ export const reducer = (state, { type, sets }) => { |
33 | 33 | } |
34 | 34 | }; |
35 | 35 |
|
36 | | -const Condition = React.memo( |
37 | | - ({ condition, children, values, field }) => { |
38 | | - const formOptions = useFormApi(); |
39 | | - const dirty = formOptions.getState().dirty; |
| 36 | +const Condition = ({ condition, children, field }) => { |
| 37 | + const formOptions = useFormApi(); |
| 38 | + const formState = formOptions.getState(); |
40 | 39 |
|
41 | | - const [state, dispatch] = useReducer(reducer, { |
42 | | - sets: [], |
43 | | - initial: true, |
44 | | - }); |
| 40 | + const [state, dispatch] = useReducer(reducer, { |
| 41 | + sets: [], |
| 42 | + initial: true, |
| 43 | + }); |
45 | 44 |
|
46 | | - // It is required to get the context state values from in order to get the latest state. |
47 | | - // Using the trigger values can cause issues with the radio field as each input is registered separately to state and does not yield the actual field value. |
48 | | - const conditionResult = parseCondition(condition, formOptions.getState().values, field); |
| 45 | + // It is required to get the context state values from in order to get the latest state. |
| 46 | + // Using the trigger values can cause issues with the radio field as each input is registered separately to state and does not yield the actual field value. |
| 47 | + const conditionResult = useMemo(() => parseCondition(condition, formState.values, field), [formState.values, condition, field]); |
49 | 48 |
|
50 | | - const setters = conditionResult.set ? [conditionResult.set] : conditionResult.sets; |
| 49 | + const setters = conditionResult.set ? [conditionResult.set] : conditionResult.sets; |
51 | 50 |
|
52 | | - useEffect(() => { |
53 | | - if (!dirty) { |
54 | | - dispatch({ type: 'formResetted' }); |
55 | | - } |
56 | | - }, [dirty]); |
| 51 | + useEffect(() => { |
| 52 | + if (!formState.dirty) { |
| 53 | + dispatch({ type: 'formResetted' }); |
| 54 | + } |
| 55 | + }, [formState.dirty]); |
57 | 56 |
|
58 | | - const setValue = useCallback((setter) => { |
59 | | - Object.entries(setter).forEach(([name, value]) => { |
60 | | - formOptions.change(name, value); |
61 | | - }); |
62 | | - }, []); |
63 | | - |
64 | | - useEffect(() => { |
65 | | - if (setters && setters.length > 0 && (state.initial || !isEqual(setters, state.sets))) { |
66 | | - setters.forEach((setter, index) => { |
67 | | - if (setter && (state.initial || !isEqual(setter, state.sets[index]))) { |
68 | | - setTimeout(() => { |
69 | | - /** |
70 | | - * We have to get the meta in the timetout to wait for state initialization |
71 | | - */ |
72 | | - const meta = formOptions.getFieldState(field.name); |
73 | | - const isFormModified = Object.values(formOptions.getState().modified).some(Boolean); |
74 | | - /** |
75 | | - * Apply setter only |
76 | | - * - field has no initial value |
77 | | - * - form is modified |
78 | | - * - when meta is false = field was unmounted before timeout, we finish the condition |
79 | | - */ |
80 | | - if (!meta || isFormModified || typeof meta.initial === 'undefined') { |
81 | | - formOptions.batch(() => { |
82 | | - if (typeof setter !== 'function') { |
83 | | - setValue(setter); |
| 57 | + const setValue = useCallback((setter) => { |
| 58 | + Object.entries(setter).forEach(([name, value]) => { |
| 59 | + formOptions.change(name, value); |
| 60 | + }); |
| 61 | + }, []); |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + if (setters && setters.length > 0 && (state.initial || !isEqual(setters, state.sets))) { |
| 65 | + setters.forEach((setter, index) => { |
| 66 | + if (setter && (state.initial || !isEqual(setter, state.sets[index]))) { |
| 67 | + setTimeout(() => { |
| 68 | + /** |
| 69 | + * We have to get the meta in the timetout to wait for state initialization |
| 70 | + */ |
| 71 | + const meta = formOptions.getFieldState(field.name); |
| 72 | + const isFormModified = Object.values(formOptions.getState().modified).some(Boolean); |
| 73 | + /** |
| 74 | + * Apply setter only |
| 75 | + * - field has no initial value |
| 76 | + * - form is modified |
| 77 | + * - when meta is false = field was unmounted before timeout, we finish the condition |
| 78 | + */ |
| 79 | + if (!meta || isFormModified || typeof meta.initial === 'undefined') { |
| 80 | + formOptions.batch(() => { |
| 81 | + if (typeof setter !== 'function') { |
| 82 | + setValue(setter); |
| 83 | + } else { |
| 84 | + const setterValue = setter(formOptions.getState(), formOptions.getFieldState); |
| 85 | + |
| 86 | + if (setterValueCheck(setterValue)) { |
| 87 | + setValue(setterValue); |
84 | 88 | } else { |
85 | | - const setterValue = setter(formOptions.getState(), formOptions.getFieldState); |
86 | | - |
87 | | - if (setterValueCheck(setterValue)) { |
88 | | - setValue(setterValue); |
89 | | - } else { |
90 | | - console.error('Received invalid setterValue. Expected object, received: ', setterValue); |
91 | | - } |
| 89 | + console.error('Received invalid setterValue. Expected object, received: ', setterValue); |
92 | 90 | } |
93 | | - }); |
94 | | - } |
95 | | - }); |
96 | | - } |
97 | | - }); |
98 | | - dispatch({ type: 'rememberSets', sets: setters }); |
99 | | - } |
100 | | - }, [setters, state.initial]); |
101 | | - |
102 | | - return conditionResult.visible ? children : null; |
103 | | - }, |
104 | | - (a, b) => isEqual(a.values, b.values) && isEqual(a.condition, b.condition) |
105 | | -); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | + dispatch({ type: 'rememberSets', sets: setters }); |
| 98 | + } |
| 99 | + }, [setters, state.initial]); |
| 100 | + |
| 101 | + return conditionResult.visible ? children : null; |
| 102 | +}; |
106 | 103 |
|
107 | 104 | const conditionProps = { |
108 | 105 | when: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]), |
|
0 commit comments