Reimplement TabView active tab handling

pull/5/head
cagataycivici 2018-12-24 23:16:54 +03:00
parent 1140ebe31f
commit 6908614ec9
4 changed files with 48 additions and 44 deletions

View File

@ -5,16 +5,16 @@ export default {
active: Boolean, active: Boolean,
disabled: Boolean disabled: Boolean
}, },
watch: {
active(newValue) {
this.d_active = newValue;
}
},
data() { data() {
return { return {
d_active: this.active d_active: this.active
} }
}, },
watch: {
active(newValue) {
this.d_active = newValue;
}
},
render() { render() {
return null; return null;
} }

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="p-tabview-panel" role="tabpanel" v-show="active"> <div class="p-tabview-panel" role="tabpanel" v-show="d_active">
<slot></slot> <slot></slot>
</div> </div>
</template> </template>
@ -8,11 +8,17 @@
export default { export default {
props: { props: {
header: null, header: null,
active: Boolean,
disabled: Boolean disabled: Boolean
}, },
data() { data() {
return { return {
active: false d_active: this.active
}
},
watch: {
active(newValue) {
this.d_active = newValue;
} }
} }
} }

View File

@ -1,63 +1,61 @@
<script> <script>
export default { export default {
props: {
activeIndex: {
type: Number,
default: 0,
},
orientation: {
type: String,
default: 'top'
}
},
data() { data() {
return { return {
d_activeTabIndex: this.activeIndex,
tabs: [] tabs: []
}; };
}, },
watch: {
activeIndex(newValue) {
this.activateTab(newValue);
}
},
mounted() { mounted() {
this.tabs = this.$children; this.tabs = this.$children;
this.tabs[this.activeIndex].active = true;
let activeTab = this.findActiveTab();
if (!activeTab && this.tabs.length) {
this.tabs[0].d_active = true;
}
}, },
methods: { methods: {
onTabClick(event, tab, index) { onTabClick(event, tab) {
if (!tab.disabled && index !== this.d_activeTabIndex) { if (!tab.disabled && !tab.d_active) {
this.activateTab(index); this.activateTab(tab);
this.$emit('tabchange', { this.$emit('tabchange', {
originalEvent: event, originalEvent: event,
tab: tab, tab: tab
index: index
}); });
} }
}, },
activateTab(index) { activateTab(tab) {
this.d_activeTabIndex = index;
for (let i = 0; i < this.tabs.length; i++) { for (let i = 0; i < this.tabs.length; i++) {
this.tabs[i].active = (i === index); this.tabs[i].d_active = this.tabs[i] === tab;
} }
}, },
onTabKeydown(event, tab, index) { onTabKeydown(event, tab) {
if (event.which === 13) { if (event.which === 13) {
this.onTabClick(event, tab, index); this.onTabClick(event, tab);
} }
},
findActiveTab() {
let activeTab;
for (let i = 0; i < this.tabs.length; i++) {
let tab = this.tabs[i];
if (tab.d_active) {
activeTab = tab;
break;
}
}
return activeTab;
} }
}, },
render() { render() {
return ( return (
<div class="p-tabview p-component p-tabview-top"> <div class="p-tabview p-component p-tabview-top">
<ul class="p-tabview-nav p-resest" role="tablist"> <ul class="p-tabview-nav p-reset" role="tablist">
{ {
this.tabs.map((tab, i) => { this.tabs.map(tab => {
return ( return (
<li role="presentation" key={tab.header||i} class={{'p-highlight': (this.d_activeTabIndex === i), 'p-disabled': tab.disabled}}> <li role="presentation" key={tab.header} class={['p-unselectable-text', {'p-highlight': (tab.d_active), 'p-disabled': tab.disabled}]}>
<a role="tab" on-click={event => this.onTabClick(event, tab, i)} on-keydown={event => this.onTabKeydown(event, tab, i)} tabindex={tab.disabled ? null : '0'}> <a role="tab" on-click={event => this.onTabClick(event, tab)} on-keydown={event => this.onTabKeydown(event, tab)} tabindex={tab.disabled ? null : '0'}>
{tab.header && <span class="p-tabview-title">{tab.header}</span>} {tab.header && <span class="p-tabview-title">{tab.header}</span>}
{tab.$slots.header} {tab.$slots.header}
</a> </a>

View File

@ -28,23 +28,23 @@
</p-tabView> </p-tabView>
<h3>Programmatic</h3> <h3>Programmatic</h3>
<div style="padding: .25em"> <div style="padding: .5em 0">
<p-button icon="pi pi-chevron-left" @click="prev" class="p-button-secondary" /> <p-button icon="pi pi-chevron-left" @click="prev" class="p-button-secondary" />
<p-button icon="pi pi-chevron-right" @click="next" style="margin-left: .5em" class="p-button-secondary"/> <p-button icon="pi pi-chevron-right" @click="next" style="margin-left: .5em" class="p-button-secondary"/>
</div> </div>
<p-tabView :activeIndex="activeIndex"> <p-tabView>
<p-tabPanel header="Godfather I"> <p-tabPanel header="Godfather I" :active="activeIndex === 0">
The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war,
but does not intend to become part of his father's business. Through Michael's life the nature of the family business becomes clear. The business of the family is but does not intend to become part of his father's business. Through Michael's life the nature of the family business becomes clear. The business of the family is
just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family. just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-tabPanel> </p-tabPanel>
<p-tabPanel header="Godfather II"> <p-tabPanel header="Godfather II" :active="activeIndex === 1">
Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, TheGodfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, TheGodfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall,
deepening The Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills deepening The Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills
his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy. his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy.
</p-tabPanel> </p-tabPanel>
<p-tabPanel header="Godfather III"> <p-tabPanel header="Godfather III" :active="activeIndex === 2">
The Godfather Part III is set in 1979 and 1980. Michael has moved back to New York and taken great strides to remove the family from crime. He turns over his New York criminal The Godfather Part III is set in 1979 and 1980. Michael has moved back to New York and taken great strides to remove the family from crime. He turns over his New York criminal
interests to longtime enforcer Joey Zasa. He uses his wealth in an attempt to rehabilitate his reputation through numerous philanthropic acts, administered by a foundation named after his father. interests to longtime enforcer Joey Zasa. He uses his wealth in an attempt to rehabilitate his reputation through numerous philanthropic acts, administered by a foundation named after his father.
A decade earlier, he gave custody of his two children to Kay, who has since remarried. A decade earlier, he gave custody of his two children to Kay, who has since remarried.
@ -110,7 +110,7 @@
export default { export default {
data() { data() {
return { return {
activeIndex: 1 activeIndex: 0
} }
}, },
methods: { methods: {