From 74cce164a329cc110d8eb2a959391d7b3bbba2ab Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 8 Feb 2018 17:05:16 +0100 Subject: [PATCH] feat(tabs): adds lazy loading --- packages/core/src/components.d.ts | 5 +++ .../core/src/components/content/content.tsx | 2 +- .../components/nav/test/routing/index.html | 3 +- .../core/src/components/router/router.tsx | 4 ++ packages/core/src/components/tab/readme.md | 24 +++++++++++ packages/core/src/components/tab/tab.tsx | 40 ++++++++++++++++++- .../src/components/tabs/test/basic/index.html | 20 +++------- 7 files changed, 80 insertions(+), 18 deletions(-) diff --git a/packages/core/src/components.d.ts b/packages/core/src/components.d.ts index 040d8f7ca5..3ab8c6e870 100644 --- a/packages/core/src/components.d.ts +++ b/packages/core/src/components.d.ts @@ -40,6 +40,9 @@ import { import { SelectPopoverOption, } from './components/select-popover/select-popover'; +import { + FrameworkDelegate as FrameworkDelegate2, +} from '.'; import { DomRenderFn, HeaderFn, @@ -2995,6 +2998,8 @@ declare global { badge?: string; badgeStyle?: string; btnId?: string; + component?: any; + delegate?: FrameworkDelegate; disabled?: boolean; icon?: string; path?: string; diff --git a/packages/core/src/components/content/content.tsx b/packages/core/src/components/content/content.tsx index 29a48415f2..58914fd51b 100644 --- a/packages/core/src/components/content/content.tsx +++ b/packages/core/src/components/content/content.tsx @@ -102,7 +102,7 @@ export class Content { }); } else { this.cTop = this.cBottom = null; - this.dom.write(() => this.scrollEl.removeAttribute('style')); + this.dom.write(() => this.scrollEl && this.scrollEl.removeAttribute('style')); } } diff --git a/packages/core/src/components/nav/test/routing/index.html b/packages/core/src/components/nav/test/routing/index.html index e1017d1a46..38160b8371 100644 --- a/packages/core/src/components/nav/test/routing/index.html +++ b/packages/core/src/components/nav/test/routing/index.html @@ -13,11 +13,12 @@ + - + diff --git a/packages/core/src/components/router/router.tsx b/packages/core/src/components/router/router.tsx index 1c2db9f5f8..5e1076a0b4 100644 --- a/packages/core/src/components/router/router.tsx +++ b/packages/core/src/components/router/router.tsx @@ -88,4 +88,8 @@ export class Router { private readPath(): string[] | null { return readPath(window.location, this.base, this.useHash); } + + render() { + return ; + } } diff --git a/packages/core/src/components/tab/readme.md b/packages/core/src/components/tab/readme.md index 83a70114fd..f4679940bd 100644 --- a/packages/core/src/components/tab/readme.md +++ b/packages/core/src/components/tab/readme.md @@ -71,6 +71,18 @@ string Set the root page for this tab. +#### component + +any + +The badge for the tab button. + + +#### delegate + + + + #### disabled boolean @@ -142,6 +154,18 @@ string Set the root page for this tab. +#### component + +any + +The badge for the tab button. + + +#### delegate + + + + #### disabled boolean diff --git a/packages/core/src/components/tab/tab.tsx b/packages/core/src/components/tab/tab.tsx index 6c80c4a17f..6fe90e2d13 100644 --- a/packages/core/src/components/tab/tab.tsx +++ b/packages/core/src/components/tab/tab.tsx @@ -1,12 +1,14 @@ import { Component, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core'; import { getNavAsChildIfExists } from '../../utils/helpers'; +import { FrameworkDelegate } from '../..'; @Component({ tag: 'ion-tab', }) export class Tab { + private loaded = false; @Element() el: HTMLElement; @State() init = false; @@ -37,6 +39,11 @@ export class Tab { */ @Prop() badge: string; + /** + * The badge for the tab button. + */ + @Prop() component: any; + /** * The badge color for the tab button. */ @@ -58,6 +65,7 @@ export class Tab { */ @Prop() tabsHideOnSubPages = false; + @Prop() delegate: FrameworkDelegate; @Prop({ mutable: true }) selected = false; @@ -76,6 +84,25 @@ export class Tab { @Method() setActive(active: boolean): Promise { this.active = active; + if (this.loaded || !active) { + return Promise.resolve(); + } + + this.loaded = true; + + let promise: Promise; + if (this.component) { + promise = (this.delegate) + ? this.delegate.attachViewToDom(this.el, this.component) + : attachViewToDom(this.el, this.component); + + } else { + promise = Promise.resolve(); + } + return promise.then(() => this.fireChildren()); + } + + fireChildren() { const nav = getNavAsChildIfExists(this.el); if (nav && nav.getViews().length === 0 && nav.root) { // we need to initialize @@ -99,7 +126,7 @@ export class Tab { hostData() { const visible = this.active && this.selected; return { - 'aria-hidden': !visible ? 'true' : null, + 'aria-hidden': !visible, 'aria-labelledby': this.btnId, 'role': 'tabpanel', class: { @@ -109,10 +136,19 @@ export class Tab { } render() { - return ; + return ; } } +function attachViewToDom(container: HTMLElement, cmp: string): Promise { + const el = document.createElement(cmp) as HTMLStencilElement; + container.appendChild(el); + if (el.componentOnReady ) { + return el.componentOnReady(); + } + return Promise.resolve(); +} + export interface TabEvent extends CustomEvent { detail: TabEventDetail; } diff --git a/packages/core/src/components/tabs/test/basic/index.html b/packages/core/src/components/tabs/test/basic/index.html index e53daae581..f800a74591 100644 --- a/packages/core/src/components/tabs/test/basic/index.html +++ b/packages/core/src/components/tabs/test/basic/index.html @@ -5,9 +5,7 @@ Tab - Basic - + @@ -52,17 +50,11 @@ - - - - - Tab Four - - - - Tab Four - - +