Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): angular, fire willChange event before selected tab changes #24910

Merged
merged 3 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion angular/src/directives/navigation/ion-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export class IonTabs {
onPageSelected(detail: StackEvent): void {
const stackId = detail.enteringView.stackId;
if (detail.tabSwitch && stackId !== undefined) {
this.ionTabsWillChange.emit({ tab: stackId });
if (this.tabBar) {
this.tabBar.selectedTab = stackId;
}
this.ionTabsWillChange.emit({ tab: stackId });
this.ionTabsDidChange.emit({ tab: stackId });
}
}
Expand Down
24 changes: 24 additions & 0 deletions angular/test/test-app/e2e/src/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ describe('Tabs', () => {
tab.find('.segment-changed').should('have.text', 'false');
});

describe('when navigating between tabs', () => {

it('should emit ionTabsWillChange before setting the selected tab', () => {
cy.get('#ionTabsWillChangeCounter').should('have.text', '1');
cy.get('#ionTabsWillChangeEvent').should('have.text', 'account');
cy.get('#ionTabsWillChangeSelectedTab').should('have.text', '');

cy.get('#ionTabsDidChangeCounter').should('have.text', '1');
cy.get('#ionTabsDidChangeEvent').should('have.text', 'account');
cy.get('#ionTabsDidChangeSelectedTab').should('have.text', 'account');

cy.get('#tab-button-contact').click();

cy.get('#ionTabsWillChangeCounter').should('have.text', '2');
cy.get('#ionTabsWillChangeEvent').should('have.text', 'contact');
cy.get('#ionTabsWillChangeSelectedTab').should('have.text', 'account');

cy.get('#ionTabsDidChangeCounter').should('have.text', '2');
cy.get('#ionTabsDidChangeEvent').should('have.text', 'contact');
cy.get('#ionTabsDidChangeSelectedTab').should('have.text', 'contact');
})

});

it('should simulate stack + double tab click', () => {
let tab = getSelectedTab();
tab.find('#goto-tab1-page2').click();
Expand Down
5 changes: 5 additions & 0 deletions angular/test/test-app/src/app/tabs/tabs.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#test {
position: absolute;
bottom: 100px;
left: 0;
}
28 changes: 26 additions & 2 deletions angular/test/test-app/src/app/tabs/tabs.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ion-tabs (ionTabsDidChange)="tabChanged($event)">
<ion-tabs (ionTabsDidChange)="tabChanged($event)" (ionTabsWillChange)="tabsWillChange($event)">
<ion-tab-bar>
<ion-tab-button tab="account">
<ion-label>Tab One</ion-label>
Expand All @@ -18,5 +18,29 @@
</ion-tab-bar>
</ion-tabs>
<ion-fab horizontal="end" vertical="top">
<ion-fab-button id="tabs-state" style="width: 100px;">{{tabCounter}}.{{tabEvent}}</ion-fab-button>
<ion-fab-button id="tabs-state" style="width: 100px;">{{tabsDidChangeCounter}}.{{tabsDidChangeEvent}}</ion-fab-button>
</ion-fab>
<div id="test">
<ul>
<li>
ionTabsWillChange counter: <span id="ionTabsWillChangeCounter">{{ tabsWillChangeCounter }}</span>
</li>
<li>
ionTabsWillChange event: <span id="ionTabsWillChangeEvent">{{ tabsWillChangeEvent }}</span>
</li>
<li>
ionTabsWillChange selectedTab: <span id="ionTabsWillChangeSelectedTab">{{ tabsWillChangeSelectedTab }}</span>
</li>
</ul>
<ul>
<li>
ionTabsDidChange counter: <span id="ionTabsDidChangeCounter">{{ tabsDidChangeCounter }}</span>
</li>
<li>
ionTabsDidChange event: <span id="ionTabsDidChangeEvent">{{ tabsDidChangeEvent }}</span>
</li>
<li>
ionTabsDidChange selectedTab: <span id="ionTabsDidChangeSelectedTab">{{ tabsDidChangeSelectedTab }}</span>
</li>
</ul>
</div>
30 changes: 24 additions & 6 deletions angular/test/test-app/src/app/tabs/tabs.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { Component } from '@angular/core';
import { Component, OnInit, ViewChild } from '@angular/core';
import { IonTabBar } from '@ionic/angular';

@Component({
selector: 'app-tabs',
templateUrl: './tabs.component.html',
styleUrls: ['./tabs.component.css']
})
export class TabsComponent {
tabCounter = 0;
tabEvent = '';
tabsDidChangeCounter = 0;
tabsDidChangeEvent = '';
tabsDidChangeSelectedTab = '';

tabChanged(ev: {tab: string}) {
this.tabCounter++;
this.tabEvent = ev.tab;
tabsWillChangeCounter = 0;
tabsWillChangeEvent = '';
tabsWillChangeSelectedTab = '';

@ViewChild(IonTabBar) tabBar: IonTabBar;

tabChanged(ev: { tab: string }) {
console.log('ionTabsDidChange', this.tabBar.selectedTab);
this.tabsDidChangeCounter++;
this.tabsDidChangeEvent = ev.tab;
this.tabsDidChangeSelectedTab = this.tabBar.selectedTab;
}

tabsWillChange(ev: { tab: string }) {
console.log('ionTabsWillChange', this.tabBar.selectedTab);
this.tabsWillChangeCounter++;
this.tabsWillChangeEvent = ev.tab;
this.tabsWillChangeSelectedTab = this.tabBar.selectedTab;
}
}