Skip to content

Commit

Permalink
Fixed #15973 - TabMenu doesn't scroll to active item when it's set pr…
Browse files Browse the repository at this point in the history
…ogramatically
  • Loading branch information
mehmetcetin01140 committed Jul 8, 2024
1 parent e804cd6 commit 9f36400
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions src/app/components/tabmenu/tabmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
Inject,
Input,
NgModule,
OnDestroy,
Output,
PLATFORM_ID,
QueryList,
SimpleChanges,
TemplateRef,
ViewChild,
ViewChildren,
Expand Down Expand Up @@ -132,7 +132,7 @@ import { filter } from 'rxjs/operators';
class: 'p-element'
}
})
export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecked, OnDestroy {
export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecked {
/**
* An array of menuitems.
* @group Props
Expand Down Expand Up @@ -225,8 +225,6 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke

forwardIsDisabled: boolean = false;

private timerIdForInitialAutoScroll: any = null;

_focusableItems: MenuItem[] | undefined | any;

_model: MenuItem[] | undefined;
Expand Down Expand Up @@ -257,6 +255,19 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
});
}

ngOnChanges(simpleChange: SimpleChanges) {
if (simpleChange.activeItem) {
if (!this.scrollable) {
return;
}
const activeItem = (this.model as MenuItem[]).findIndex((menuItem) => this.isActive(menuItem));

if (activeItem !== -1) {
this.updateScrollBar(activeItem);
}
}
}

ngAfterContentInit() {
this.templates?.forEach((item) => {
switch (item.getType()) {
Expand All @@ -282,7 +293,7 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
ngAfterViewInit(): void {
if (isPlatformBrowser(this.platformId)) {
this.updateInkBar();
this.initAutoScrollForActiveItem();

this.initButtonState();
}
}
Expand All @@ -294,10 +305,6 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
}
}

ngOnDestroy(): void {
this.clearAutoScrollHandler();
}

isActive(item: MenuItem) {
if (item.routerLink) {
const routerLink = Array.isArray(item.routerLink) ? item.routerLink : [item.routerLink];
Expand Down Expand Up @@ -342,6 +349,7 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
}

this.activeItem = item;

this.activeItemChange.emit(item);
this.tabChanged = true;
this.cd.markForCheck();
Expand Down Expand Up @@ -460,7 +468,9 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
return;
}

tabHeader.scrollIntoView({ block: 'nearest', inline: 'center' });
if (tabHeader && typeof tabHeader.scrollIntoView === 'function') {
tabHeader.scrollIntoView({ block: 'nearest', inline: 'center' });
}
}

onScroll(event: Event) {
Expand All @@ -484,29 +494,6 @@ export class TabMenu implements AfterContentInit, AfterViewInit, AfterViewChecke
content.scrollLeft = pos >= lastPos ? lastPos : pos;
}

private initAutoScrollForActiveItem(): void {
if (!this.scrollable) {
return;
}

this.clearAutoScrollHandler();
// We have to wait for the rendering and then can scroll to element.
this.timerIdForInitialAutoScroll = setTimeout(() => {
const activeItem = (this.model as MenuItem[]).findIndex((menuItem) => this.isActive(menuItem));

if (activeItem !== -1) {
this.updateScrollBar(activeItem);
}
});
}

private clearAutoScrollHandler(): void {
if (this.timerIdForInitialAutoScroll) {
clearTimeout(this.timerIdForInitialAutoScroll);
this.timerIdForInitialAutoScroll = null;
}
}

private initButtonState(): void {
if (this.scrollable) {
// We have to wait for the rendering and then retrieve the actual size element from the DOM.
Expand Down

0 comments on commit 9f36400

Please sign in to comment.