Skip to content

Commit 5a56672

Browse files
committed
fix(tab): handling edge cases
1 parent d5d0774 commit 5a56672

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/src/components/tab-bar/tab-bar.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { TabBarChangedEventDetail } from './tab-bar-interface';
2222
})
2323
export class TabBar implements ComponentInterface {
2424
private keyboardCtrl: KeyboardController | null = null;
25+
private didLoad = false;
2526

2627
@Element() el!: HTMLElement;
2728

@@ -40,6 +41,12 @@ export class TabBar implements ComponentInterface {
4041
@Prop() selectedTab?: string;
4142
@Watch('selectedTab')
4243
selectedTabChanged() {
44+
// Skip the initial watcher call that happens during component load
45+
// We handle that in componentDidLoad to ensure children are ready
46+
if (!this.didLoad) {
47+
return;
48+
}
49+
4350
if (this.selectedTab !== undefined) {
4451
this.ionTabBarChanged.emit({
4552
tab: this.selectedTab,
@@ -67,9 +74,17 @@ export class TabBar implements ComponentInterface {
6774

6875
componentDidLoad() {
6976
this.ionTabBarLoaded.emit();
77+
// Set the flag to indicate the component has loaded
78+
// This allows the watcher to emit changes from this point forward
79+
this.didLoad = true;
80+
7081
// Emit the initial selected tab after the component is fully loaded
7182
// This ensures all child components (ion-tab-button) are ready
72-
this.selectedTabChanged();
83+
if (this.selectedTab !== undefined) {
84+
this.ionTabBarChanged.emit({
85+
tab: this.selectedTab,
86+
});
87+
}
7388
}
7489

7590
async connectedCallback() {

core/src/components/tab-button/tab-button.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ export class TabButton implements ComponentInterface, AnchorInterface {
103103
if (this.layout === undefined) {
104104
this.layout = config.get('tabButtonLayout', 'icon-top');
105105
}
106+
107+
// Check if this tab button should be initially selected based on parent tab-bar's selectedTab prop
108+
// This handles the case where selected-tab is set via HTML attribute before events fire
109+
const tabBar = this.el.parentElement as HTMLIonTabBarElement | null;
110+
if (tabBar && tabBar.tagName === 'ION-TAB-BAR' && tabBar.selectedTab !== undefined) {
111+
this.selected = this.tab === tabBar.selectedTab;
112+
}
106113
}
107114

108115
private selectTab(ev: Event | KeyboardEvent) {

0 commit comments

Comments
 (0)