From 30bfb955c5e7a2b0c6831585e31c639d41815146 Mon Sep 17 00:00:00 2001 From: Md Ashik Date: Wed, 30 Aug 2023 16:25:11 +0600 Subject: [PATCH] fix: #13007 || AutoComplete control inappropriately fires onClear event when force selection true --- .../components/autocomplete/autocomplete.ts | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/src/app/components/autocomplete/autocomplete.ts b/src/app/components/autocomplete/autocomplete.ts index 6a546df7d56..2bb28df4808 100755 --- a/src/app/components/autocomplete/autocomplete.ts +++ b/src/app/components/autocomplete/autocomplete.ts @@ -659,6 +659,8 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr overlayVisible: boolean = false; + isSuggestionOptionSelect: boolean = false; + suggestionsUpdated: Nullable; highlightOption: any; @@ -902,6 +904,8 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr } selectItem(option: any, focus: boolean = true) { + this.isSuggestionOptionSelect = true; + if (this.forceSelectionUpdateModelTimeout) { clearTimeout(this.forceSelectionUpdateModelTimeout); this.forceSelectionUpdateModelTimeout = null; @@ -1161,42 +1165,48 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr } onInputChange(event: Event) { - if (this.forceSelection) { - let valid = false; - const target = event.target as HTMLTextAreaElement; - let inputValue = target.value.trim(); - - if (this.suggestions) { - let suggestions = [...this.suggestions]; - if (this.group) { - let groupedSuggestions = this.suggestions.filter((s) => s[this.optionGroupChildren]).flatMap((s) => s[this.optionGroupChildren]); - suggestions = suggestions.concat(groupedSuggestions); - } + this.isSuggestionOptionSelect = false; - for (let suggestion of suggestions) { - let itemValue = this.field ? ObjectUtils.resolveFieldData(suggestion, this.field) : suggestion; - if (itemValue && inputValue.toLowerCase() === itemValue.toLowerCase().trim()) { - valid = true; - this.forceSelectionUpdateModelTimeout = setTimeout(() => { - this.selectItem(suggestion, false); - }, 250); - break; - } - } + setTimeout(() => { + if (this.forceSelection && !this.isSuggestionOptionSelect) this.onForceSelectOrClearInput(event); + }, 200); + } + + onForceSelectOrClearInput(event: Event) { + let valid = false; + const target = event.target as HTMLTextAreaElement; + let inputValue = target.value.trim(); + + if (this.suggestions) { + let suggestions = [...this.suggestions]; + if (this.group) { + let groupedSuggestions = this.suggestions.filter((s) => s[this.optionGroupChildren]).flatMap((s) => s[this.optionGroupChildren]); + suggestions = suggestions.concat(groupedSuggestions); } - if (!valid) { - if (this.multiple) { - (this.multiInputEl).nativeElement.value = ''; - } else { - this.value = null; - (this.inputEL).nativeElement.value = ''; + for (let suggestion of suggestions) { + let itemValue = this.field ? ObjectUtils.resolveFieldData(suggestion, this.field) : suggestion; + if (itemValue && inputValue.toLowerCase() === itemValue.toLowerCase().trim()) { + valid = true; + this.forceSelectionUpdateModelTimeout = setTimeout(() => { + this.selectItem(suggestion, false); + }, 250); + break; } + } + } - this.onClear.emit(event); - this.onModelChange(this.value); - this.updateFilledState(); + if (!valid) { + if (this.multiple) { + (this.multiInputEl).nativeElement.value = ''; + } else { + this.value = null; + (this.inputEL).nativeElement.value = ''; } + + this.onClear.emit(event); + this.onModelChange(this.value); + this.updateFilledState(); } }