Skip to content

Commit

Permalink
#110 Improve change consistency when sendOptionValue is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Beceic committed May 3, 2023
1 parent e435527 commit 511d206
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libs/formik-elements/src/AutocompleteField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ export default function AutocompleteField<T>({
} else {
helpers.setValue(value, shouldValidate);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const lastFieldValue = React.useRef<unknown>(null);
React.useEffect(() => {
if (getOptionValue && field.value && !isEqual(lastFieldValue.current, field.value)) {
if (getOptionValue && field.value && !isEqual(lastFieldValue.current, field.value) && sendOptionValue) {
if (Array.isArray(field.value)) {
helpers.setValue(field.value.map(getOptionValue), shouldValidate);
lastFieldValue.current = field.value.map(getOptionValue);
Expand All @@ -101,9 +102,13 @@ export default function AutocompleteField<T>({
}
setValue(field.value);
initialError.current = undefined;
} else if (!field.value || (Array.isArray(field.value) && field.value.length === 0)) {
} else if ((!field.value || (Array.isArray(field.value) && field.value.length === 0)) && sendOptionValue) {
lastFieldValue.current = undefined;
setValue(undefined);
} else if (!sendOptionValue) {
helpers.setValue(field.value, shouldValidate);
lastFieldValue.current = field.value;
setValue(field.value);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [field.value]);
Expand Down

0 comments on commit 511d206

Please sign in to comment.