Skip to content

Commit 457b290

Browse files
Do not mutate original search result in AutoCompleteField
1 parent 257ec10 commit 457b290

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/components/AutocompleteField/AutoCompleteField.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ function AutoCompleteField<T, UPDATE_VALUE>(props: AutoCompleteFieldProps<T, UPD
301301
let enableHighlighting = true;
302302
if (onlySelectItemReturned) {
303303
// If the auto-completion only returns no suggestion or the selected item itself, query with empty string.
304-
const emptyStringResults = await onSearch("");
304+
const emptyStringResults: T[] = await onSearch("");
305305
// Disable highlighting, since we used empty string search
306306
enableHighlighting = false;
307307
// Put selected item at the top if it is not in the result list
308308
if (!!selectedItem && itemIndexOf(emptyStringResults, selectedItem) > -1) {
309-
emptyStringResults.splice(itemIndexOf(emptyStringResults, selectedItem), 1);
310-
result = [selectedItem, ...emptyStringResults];
309+
// Do not mutate original array
310+
const withoutSelected = [...emptyStringResults]
311+
withoutSelected.splice(itemIndexOf(emptyStringResults, selectedItem), 1);
312+
result = [selectedItem, ...withoutSelected];
311313
} else {
312314
result = emptyStringResults;
313315
}

0 commit comments

Comments
 (0)