/
tab.tsx
79 lines (68 loc) · 2.08 KB
/
tab.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Build, Component, ComponentInterface, Element, Host, Method, Prop, h } from '@stencil/core';
import { ComponentRef, FrameworkDelegate } from '../../interface';
import { attachComponent } from '../../utils/framework-delegate';
@Component({
tag: 'ion-tab',
styleUrl: 'tab.scss',
shadow: true
})
export class Tab implements ComponentInterface {
private loaded = false;
@Element() el!: HTMLIonTabElement;
/** @internal */
@Prop({ mutable: true }) active = false;
/** @internal */
@Prop() delegate?: FrameworkDelegate;
/**
* A tab id must be provided for each `ion-tab`. It's used internally to reference
* the selected tab or by the router to switch between them.
*/
@Prop() tab!: string;
/**
* The component to display inside of the tab.
*/
@Prop() component?: ComponentRef;
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.' +
`- Remove the component attribute in: <ion-tab component="${this.component}">` +
` or` +
`- Remove the embedded content inside the ion-tab: <ion-tab></ion-tab>`);
}
}
}
/** Set the active component for the tab */
@Method()
async setActive(): Promise<void> {
await this.prepareLazyLoaded();
this.active = true;
}
private prepareLazyLoaded(): Promise<HTMLElement | undefined> {
if (!this.loaded && this.component != null) {
this.loaded = true;
try {
return attachComponent(this.delegate, this.el, this.component, ['ion-page']);
} catch (e) {
console.error(e);
}
}
return Promise.resolve(undefined);
}
render() {
const { tab, active, component } = this;
return (
<Host
role="tabpanel"
aria-hidden={!active ? 'true' : null}
aria-labelledby={`tab-button-${tab}`}
class={{
'ion-page': component === undefined,
'tab-hidden': !active
}}
>
<slot></slot>
</Host>
);
}
}