From 511d206e639fcf181a11cb05a51088e847407b83 Mon Sep 17 00:00:00 2001 From: Felix Beceic Date: Wed, 3 May 2023 12:04:29 +0200 Subject: [PATCH] #110 Improve change consistency when sendOptionValue is disabled --- libs/formik-elements/src/AutocompleteField.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libs/formik-elements/src/AutocompleteField.tsx b/libs/formik-elements/src/AutocompleteField.tsx index 5b78bdc..3d1ecea 100644 --- a/libs/formik-elements/src/AutocompleteField.tsx +++ b/libs/formik-elements/src/AutocompleteField.tsx @@ -86,11 +86,12 @@ export default function AutocompleteField({ } else { helpers.setValue(value, shouldValidate); } + // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const lastFieldValue = React.useRef(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); @@ -101,9 +102,13 @@ export default function AutocompleteField({ } 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]);