in src/components/FormBuilderField.js [87:124]
_checkAndClearValue() {
// Here we are getting every field that has the same path as this field.
const fields = this.props.pathsToTreeMap[this.props.path];
// Before we remove this fields value, we first need to check if there are any other fields with the
// same path still visible. If there is another field visible, we don't want to remove the value.
const isPathShown = (fields || []).reduce((acc, field) => {
// Don't check this field
if (field.treePath === this.props.treePath) {
return acc;
}
const fieldComputedProperties = getComputedProperties(this.props.computed, field.treePath);
return acc || fieldComputedProperties.shown;
}, false);
// If we are safe to remove the value: set the field as untouched and reset back
// to the fields defaultValue.
if (!isPathShown) {
let defaultValue;
if (typeof this.props.config.defaultValue !== 'undefined') {
defaultValue = this.props.config.defaultValue;
} else {
defaultValue = this.props.registeredComponents[this.props.config.type].defaultValue;
}
const currentValue = this.state.value;
this.props.formik.setFieldTouched(this.props.path, false, false);
if (!this.props.config.retainValue && currentValue !== defaultValue) {
this.props.formik.setFieldValue(this.props.path, defaultValue, false);
}
}
}