From 57a275f1cf1a19caf05675dbf6674a99f166e017 Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Fri, 11 Apr 2025 16:27:37 +0100 Subject: [PATCH] Combobox: Fix an issue where custom values would accumulate as typed (#103891) --- packages/grafana-ui/src/components/Combobox/useOptions.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/grafana-ui/src/components/Combobox/useOptions.ts b/packages/grafana-ui/src/components/Combobox/useOptions.ts index ad160d6fb21..d35b8157b7a 100644 --- a/packages/grafana-ui/src/components/Combobox/useOptions.ts +++ b/packages/grafana-ui/src/components/Combobox/useOptions.ts @@ -65,10 +65,12 @@ export function useOptions(rawOptions: AsyncOptions>) => { let currentOptions: Array> = opts; if (createCustomValue && userTypedSearch) { - //Since the label of a normal option does not have to match its value and a custom option has the same value and label, - //we just focus on the value to check if the option already exists + // Since the label of a normal option does not have to match its value and a custom option has the same value and label, + // we just focus on the value to check if the option already exists const customValueExists = opts.some((opt) => opt.value === userTypedSearch); if (!customValueExists) { + // Make sure to clone the array first to avoid mutating the original array! + currentOptions = currentOptions.slice(); currentOptions.unshift({ label: userTypedSearch, value: userTypedSearch as T,