diff --git a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx index 9ba8da9..790c8cb 100644 --- a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx +++ b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx @@ -97,16 +97,39 @@ const SearchableSelect = ({ }, [options, searchTerm]); // Get selected option label + const [internalDisplayValue, setInternalDisplayValue] = React.useState(displayValue || ''); const selectedOption = options.find(opt => opt.value === value); - const displayValueToShow = displayValue !== undefined - ? displayValue - : (selectedOption ? selectedOption.label : ''); + + // Update internal display value when value, displayValue prop or options change + React.useEffect(() => { + if (displayValue !== undefined && displayValue !== '') { + setInternalDisplayValue(displayValue); + } else if (selectedOption) { + setInternalDisplayValue(selectedOption.label); + } else if (value) { + // If we have a value but no matching option, try to find it in the options + const matchedOption = options.find(opt => opt.value === value); + setInternalDisplayValue(matchedOption?.label || ''); + } else { + setInternalDisplayValue(''); + } + }, [value, displayValue, selectedOption, options]); + + const displayValueToShow = internalDisplayValue; // Handle option selection const handleSelect = (option) => { if (onChange) { - const event = { target: { value: option.value } }; + const event = { + target: { + value: option.value, + name: name, + selectedDisplay: option.label, + option: option + } + }; onChange(event); + setInternalDisplayValue(option.label); } setSearchTerm(''); setIsOpen(false); @@ -1235,140 +1258,135 @@ const location = useLocation();
{formErrors[`product_${idx}`]}
- )} -{formErrors[`product_${idx}`]}
+ )}{productsError}
- )}