Skip to content

Commit

Permalink
fix: tab component prev/next buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Nov 26, 2024
1 parent dd7f655 commit 33bdc1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
29 changes: 16 additions & 13 deletions src/app/header/tabs/tabs.component.spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TabsComponent } from './tabs.component'
import { TabComponent } from '../tab/tab.component'
import { MountResponse } from 'cypress/angular'
import { By } from '@angular/platform-browser'
import { Component } from '@angular/core'

describe('TabsComponent', () => {
it('should mount', () => {
Expand All @@ -14,9 +15,13 @@ describe('TabsComponent', () => {
describe('when all tabs fit the screen', () => {
beforeEach(() => {
cy.viewport('macbook-16')
cy.mount('<app-tabs><app-tab>Hello World</app-tab></app-tabs>', {
@Component({
template: '<app-tabs><app-tab>Hello World</app-tab></app-tabs>',
imports: [TabsComponent, TabComponent],
})
class HostComponent {}

cy.mount(HostComponent)
})

it('should disable previous paginator', () => {
Expand All @@ -32,20 +37,19 @@ describe('TabsComponent', () => {
const TABS = [...Array(11).keys()]
beforeEach(() => {
cy.viewport('iphone-x')
cy.mount(
`
<app-tabs [style.max-width.%]="100">
@for(tab of ${JSON.stringify(TABS)}; track $index) {
@Component({
template: ` <app-tabs [style.max-width.%]="100">
@for (tab of ${JSON.stringify(TABS)}; track $index) {
<app-tab>
<span style="white-space: nowrap">Tab {{ tab }}</span>
<span style="white-space: nowrap">Tab {{ tab }}</span>
</app-tab>
}
}
</app-tabs>`,
{
imports: [TabsComponent, TabComponent],
},
).then((wrapper) => {
// noinspection CYUnusedAlias (used below)
imports: [TabsComponent, TabComponent],
})
class HostComponent {}

cy.mount(HostComponent).then((wrapper) => {
cy.wrap(wrapper).as('angular')
})
})
Expand Down Expand Up @@ -150,7 +154,6 @@ describe('TabsComponent', () => {

describe('when marking a tab as active', () => {
beforeEach(() => {
// noinspection CYUnresolvedAlias
cy.get<MountResponse<unknown>>('@angular').then((angular) => {
const tabsElement = angular.fixture.debugElement.query(
By.css('app-tabs'),
Expand Down
23 changes: 11 additions & 12 deletions src/app/header/tabs/tabs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,9 @@ export class TabsComponent implements OnDestroy {

private _onTabsChanged() {
const tabs = this._tabs()
if (!this._intersectionObserver || tabs.length === 0) {
if (isDevMode) {
console.log(
'TabsComponent: either intersection observer is not defined or no tabs present. Not updating tabs list',
{ tabs, intersectionObserver: this._intersectionObserver },
)
}
return
}
this._currentTabs = tabs
this._intersectionObserver.disconnect()
;[this._firstTab, this._lastTab] = [tabs.at(0)!.elRef, tabs.at(-1)!.elRef]
this._intersectionObserver.observe(this._firstTab.nativeElement)
this._intersectionObserver.observe(this._lastTab.nativeElement)
this._resetIntersectionObserverTargets()
}

private _setupIntersectionObserver() {
Expand All @@ -148,7 +137,17 @@ export class TabsComponent implements OnDestroy {
threshold: [INTERSECTION_THRESHOLD],
},
)
this._resetIntersectionObserverTargets()
}

private _resetIntersectionObserverTargets(): void {
if (this._intersectionObserver && this._firstTab && this._lastTab) {
this._intersectionObserver.disconnect()
this._intersectionObserver.observe(this._firstTab!.nativeElement)
this._intersectionObserver.observe(this._lastTab!.nativeElement)
}
}

protected _scrollABit(scrollDirection: ScrollDirection) {
const tabListContainer = this._tabList()?.nativeElement
/* istanbul ignore next */
Expand Down

0 comments on commit 33bdc1d

Please sign in to comment.