mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
chore(): fix tabs (#19821)
* fix(tabs): do not wait on child tab setActive() lazy loading * whenDefined example * chore(): fix lint
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Build, Component, ComponentInterface, Element, Host, Method, Prop, h } from '@stencil/core';
|
||||
import { Build, Component, ComponentInterface, Element, Host, Method, Prop, Watch, h } from '@stencil/core';
|
||||
|
||||
import { ComponentRef, FrameworkDelegate } from '../../interface';
|
||||
import { attachComponent } from '../../utils/framework-delegate';
|
||||
@@ -30,7 +30,7 @@ export class Tab implements ComponentInterface {
|
||||
*/
|
||||
@Prop() component?: ComponentRef;
|
||||
|
||||
componentWillLoad() {
|
||||
async componentWillLoad() {
|
||||
if (Build.isDev) {
|
||||
if (this.component !== undefined && this.el.childElementCount > 0) {
|
||||
console.error('You can not use a lazy-loaded component in a tab and inlined content at the same time.' +
|
||||
@@ -39,6 +39,9 @@ export class Tab implements ComponentInterface {
|
||||
`- Remove the embedded content inside the ion-tab: <ion-tab></ion-tab>`);
|
||||
}
|
||||
}
|
||||
if (this.active) {
|
||||
await this.setActive();
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the active component for the tab */
|
||||
@@ -48,6 +51,13 @@ export class Tab implements ComponentInterface {
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
@Watch('active')
|
||||
changeActive(isActive: boolean) {
|
||||
if (isActive) {
|
||||
this.prepareLazyLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
private prepareLazyLoaded(): Promise<HTMLElement | undefined> {
|
||||
if (!this.loaded && this.component != null) {
|
||||
this.loaded = true;
|
||||
|
||||
@@ -46,7 +46,9 @@ export class Tabs implements NavOutlet {
|
||||
}
|
||||
if (!this.useRouter) {
|
||||
const tabs = this.tabs;
|
||||
await this.select(tabs[0]);
|
||||
if (tabs.length > 0) {
|
||||
await this.select(tabs[0]);
|
||||
}
|
||||
}
|
||||
this.ionNavWillLoad.emit();
|
||||
}
|
||||
@@ -127,7 +129,8 @@ export class Tabs implements NavOutlet {
|
||||
this.leavingTab = this.selectedTab;
|
||||
this.selectedTab = selectedTab;
|
||||
this.ionTabsWillChange.emit({ tab: selectedTab.tab });
|
||||
return selectedTab.setActive();
|
||||
selectedTab.active = true;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private tabSwitch() {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
|
||||
</head>
|
||||
|
||||
<body onload="selectTab()">
|
||||
<body>
|
||||
<ion-tabs>
|
||||
<ion-tab tab="tab-one">
|
||||
<div class="content-div div-one">
|
||||
@@ -71,11 +71,11 @@
|
||||
</style>
|
||||
|
||||
<script>
|
||||
async function selectTab() {
|
||||
customElements.whenDefined('ion-tabs').then(async () => {
|
||||
const tabs = document.querySelector('ion-tabs');
|
||||
await tabs.componentOnReady();
|
||||
await tabs.select('tab-three');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
||||
@@ -92,8 +92,8 @@ export const createPointerEvents = (
|
||||
stopMouse();
|
||||
};
|
||||
|
||||
const enable = (enable = true) => {
|
||||
if (!enable) {
|
||||
const enable = (isEnabled = true) => {
|
||||
if (!isEnabled) {
|
||||
if (rmTouchStart) {
|
||||
rmTouchStart();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user