fix: ensures failed conditions send path to form

This commit is contained in:
James
2021-04-02 11:53:03 -04:00
parent 988d0a4b08
commit dff72fbf2f
2 changed files with 4 additions and 2 deletions

View File

@@ -119,6 +119,7 @@ function fieldReducer(state: Fields, action): Fields {
initialValue: action.initialValue,
stringify: action.stringify,
validate: action.validate,
condition: action.condition,
};
return {

View File

@@ -33,18 +33,19 @@ const withCondition = <P extends Record<string, unknown>>(Field: React.Component
const { getData, getSiblingData, getField, dispatchFields } = useWatchForm();
const data = getData();
const field = getField(path);
const siblingData = getSiblingData(path);
const passesCondition = condition ? condition(data, siblingData) : true;
useEffect(() => {
if (!passesCondition) {
const field = getField(path);
dispatchFields({
...field,
path,
valid: true,
});
}
}, [passesCondition, field, dispatchFields]);
}, [passesCondition, getField, dispatchFields, path]);
if (passesCondition) {
return <Field {...props} />;