From 31607bf16a480929d909ae531bf05f3a90fb57b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87etin?= <69278826+cetincakiroglu@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:09:40 +0300 Subject: [PATCH] Fixed #14137 - AutoComplete | Refactor onAnimationStart method --- .../components/autocomplete/autocomplete.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/app/components/autocomplete/autocomplete.ts b/src/app/components/autocomplete/autocomplete.ts index d981d4cb3e4..5451b6d90f9 100755 --- a/src/app/components/autocomplete/autocomplete.ts +++ b/src/app/components/autocomplete/autocomplete.ts @@ -197,7 +197,6 @@ export const AUTOCOMPLETE_VALUE_ACCESSOR: any = { *ngIf="virtualScroll" #scroller [items]="visibleOptions()" - [tabindex]="-1" [style]="{ height: scrollHeight }" [itemSize]="virtualScrollItemSize || _itemSize" [autoSize]="true" @@ -1568,7 +1567,26 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr onOverlayAnimationStart(event: AnimationEvent) { if (event.toState === 'visible') { this.itemsWrapper = DomHandler.findSingle(this.overlayViewChild.overlayViewChild?.nativeElement, this.virtualScroll ? '.p-scroller' : '.p-autocomplete-panel'); - this.virtualScroll && this.scroller?.setContentEl(this.itemsViewChild?.nativeElement); + + if(this.virtualScroll) { + this.scroller?.setContentEl(this.itemsViewChild?.nativeElement); + this.scroller.viewInit(); + } + if(this.visibleOptions() && this.visibleOptions().length) { + if(this.virtualScroll) { + const selectedIndex = this.modelValue() ? this.focusedOptionIndex() : -1; + + if (selectedIndex !== -1) { + this.scroller?.scrollToIndex(selectedIndex); + } + } else { + let selectedListItem = DomHandler.findSingle(this.itemsWrapper, '.p-autocomplete-item.p-highlight'); + + if (selectedListItem) { + selectedListItem.scrollIntoView({ block: 'nearest', inline: 'center' }); + } + } + } } }