Migrated TabView to V3

pull/496/head
Cagatay Civici 2020-09-22 11:23:15 +03:00
parent 86360a6df1
commit 50f5fd7bc2
4 changed files with 88 additions and 152 deletions

View File

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

View File

@ -1,16 +1,18 @@
<template> <template>
<div class="p-tabview p-component"> <div class="p-tabview p-component">
<ul ref="nav" class="p-tabview-nav" role="tablist"> <ul ref="nav" class="p-tabview-nav" role="tablist">
<li role="presentation" v-for="(tab, i) of tabs" :key="tab.header || i" :class="[{'p-highlight': (tab.d_active), 'p-disabled': tab.disabled}]"> <li role="presentation" v-for="(tab, i) of tabs" :key="getKey(tab,i)" :class="[{'p-highlight': (d_activeIndex === i), 'p-disabled': isTabDisabled(tab)}]">
<a role="tab" class="p-tabview-nav-link" @click="onTabClick($event, tab)" @keydown="onTabKeydown($event, tab)" :tabindex="tab.disabled ? null : '0'" :aria-selected="tab.d_active" v-ripple> <a role="tab" class="p-tabview-nav-link" @click="onTabClick($event, i)" @keydown="onTabKeydown($event, i)" :tabindex="isTabDisabled(tab) ? null : '0'" :aria-selected="d_activeIndex === i" v-ripple>
<span class="p-tabview-title" v-if="tab.header">{{tab.header}}</span> <span class="p-tabview-title" v-if="tab.props && tab.props.header">{{tab.props.header}}</span>
<TabPanelHeaderSlot :tab="tab" v-if="tab.$scopedSlots.header" /> <component :is="tab.children.header" v-if="tab.children && tab.children.header"></component>
</a> </a>
</li> </li>
<li ref="inkbar" class="p-tabview-ink-bar"></li> <li ref="inkbar" class="p-tabview-ink-bar"></li>
</ul> </ul>
<div class="p-tabview-panels"> <div class="p-tabview-panels">
<slot></slot> <div v-for="(tab, i) of tabs" :key="getKey(tab,i)" class="p-tabview-panel" role="tabpanel" v-show="(d_activeIndex === i)">
<component :is="tab"></component>
</div>
</div> </div>
</div> </div>
</template> </template>
@ -19,84 +21,63 @@
import DomHandler from '../utils/DomHandler'; import DomHandler from '../utils/DomHandler';
import Ripple from '../ripple/Ripple'; import Ripple from '../ripple/Ripple';
const TabPanelHeaderSlot = { export default {
functional: true,
props: { props: {
tab: { activeIndex: {
type: null, type: Number,
default: null default: 0
} }
}, },
render(createElement, context) {
return [context.props.tab.$scopedSlots['header']()];
}
};
export default {
data() { data() {
return { return {
d_children: [] d_activeIndex: this.activeIndex
}; }
}, },
mounted() { watch: {
this.d_children = this.$children; activeIndex(newValue) {
this.d_activeIndex = newValue;
}
}, },
updated() { updated() {
let activeTab = this.tabs[this.findActiveTabIndex()]; this.updateInkBar();
if (!activeTab && this.tabs.length) { },
this.tabs[0].d_active = true; mounted() {
}
this.updateInkBar(); this.updateInkBar();
}, },
methods: { methods: {
onTabClick(event, tab) { onTabClick(event, i) {
if (!tab.disabled && !tab.d_active) { if (!this.isTabDisabled(this.tabs[i]) && i !== this.d_activeIndex) {
this.activateTab(tab); this.d_activeIndex = i;
this.$emit('update:activeIndex', this.d_activeIndex);
this.$emit('tab-change', { this.$emit('tab-change', {
originalEvent: event, originalEvent: event,
tab: tab index: i
}); });
} }
}, },
activateTab(tab) { onTabKeydown(event, i) {
for (let i = 0; i < this.tabs.length; i++) {
let active = this.tabs[i] === tab;
this.tabs[i].d_active = active;
this.tabs[i].$emit('update:active', active);
}
this.updateInkBar();
},
onTabKeydown(event, tab) {
if (event.which === 13) { if (event.which === 13) {
this.onTabClick(event, tab); this.onTabClick(event, i);
} }
}, },
findActiveTabIndex() {
for (let i = 0; i < this.tabs.length; i++) {
let tab = this.tabs[i];
if (tab.d_active) {
return i;
}
}
return null;
},
updateInkBar() { updateInkBar() {
let tabHeader = this.$refs.nav.children[this.findActiveTabIndex()]; let tabHeader = this.$refs.nav.children[this.d_activeIndex];
this.$refs.inkbar.style.width = DomHandler.getWidth(tabHeader) + 'px'; this.$refs.inkbar.style.width = DomHandler.getWidth(tabHeader) + 'px';
this.$refs.inkbar.style.left = DomHandler.getOffset(tabHeader).left - DomHandler.getOffset(this.$refs.nav).left + 'px'; this.$refs.inkbar.style.left = DomHandler.getOffset(tabHeader).left - DomHandler.getOffset(this.$refs.nav).left + 'px';
},
getKey(tab, i) {
return tab.props && tab.props.header ? tab.props.header : i;
},
isTabDisabled(tab) {
return tab.props && tab.props.disabled;
} }
}, },
computed: { computed: {
tabs() { tabs() {
return this.d_children.filter(child => child.$vnode.tag.indexOf('tabpanel') !== -1); return this.$slots.default().filter(child => child.type.name === 'tabpanel');
} }
}, },
components: {
'TabPanelHeaderSlot': TabPanelHeaderSlot
},
directives: { directives: {
'ripple': Ripple 'ripple': Ripple
} }

View File

@ -31,24 +31,24 @@
<div class="card"> <div class="card">
<h5>Programmatic</h5> <h5>Programmatic</h5>
<div style="padding: .5rem 0 1rem 0"> <div style="padding: .5rem 0 1rem 0" class="p-t-2 p-b-3">
<Button @click="activate(0)" class="p-button-text" label="Activate 1st" /> <Button @click="active = 0" class="p-button-text" label="Activate 1st" />
<Button @click="activate(1)" class="p-button-text" label="Activate 2nd" /> <Button @click="active = 1" class="p-button-text" label="Activate 2nd" />
<Button @click="activate(2)" class="p-button-text" label="Activate 3rd" /> <Button @click="active = 2" class="p-button-text" label="Activate 3rd" />
</div> </div>
<TabView ref="tabview2"> <TabView ref="tabview2" v-model:activeIndex="active">
<TabPanel header="Header I" v-model:active="active[0]"> <TabPanel header="Header I">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</TabPanel> </TabPanel>
<TabPanel header="Header II" v-model:active="active[1]"> <TabPanel header="Header II">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione
voluptatem sequi nesciunt. Consectetur, adipisci velit, sed quia non numquam eius modi.</p> voluptatem sequi nesciunt. Consectetur, adipisci velit, sed quia non numquam eius modi.</p>
</TabPanel> </TabPanel>
<TabPanel header="Header III" v-model:active="active[2]"> <TabPanel header="Header III">
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati <p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati
cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio.
Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus.</p> Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus.</p>
@ -124,7 +124,7 @@ import EventBus from '@/EventBus';
export default { export default {
data() { data() {
return { return {
active: [true, false, false] active: 0
} }
}, },
timeout: null, timeout: null,
@ -142,15 +142,6 @@ export default {
clearTimeout(this.timeout); clearTimeout(this.timeout);
EventBus.off('change-theme'); EventBus.off('change-theme');
}, },
methods: {
activate(index) {
let activeArray = [...this.active];
for (let i = 0 ; i < activeArray.length; i++) {
activeArray[i] = (i === index);
}
this.active = activeArray;
}
},
components: { components: {
'TabViewDoc': TabViewDoc 'TabViewDoc': TabViewDoc
} }

View File

@ -25,13 +25,13 @@ import TabPanel from 'primevue/tabpanel';
</CodeHighlight> </CodeHighlight>
<h5>Active</h5> <h5>Active</h5>
<p>Visibility of the content is specified with the active property that supports one or two-way binding.</p> <p>Visibility of the content is specified with the <i>activeIndex</i> property that supports one or two-way binding.</p>
<CodeHighlight> <CodeHighlight>
&lt;TabView&gt; &lt;TabView :activeIndex="activeIndex"&gt;
&lt;TabPanel header="Header I"&gt; &lt;TabPanel header="Header I"&gt;
Content I Content I
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header II" :active="true"&gt; &lt;TabPanel header="Header II"&gt;
Content II Content II
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header III"&gt; &lt;TabPanel header="Header III"&gt;
@ -40,16 +40,16 @@ import TabPanel from 'primevue/tabpanel';
&lt;/TabView&gt; &lt;/TabView&gt;
</CodeHighlight> </CodeHighlight>
<p>Two-way binding requires the sync operator.</p> <p>Two-way binding requires v-model.</p>
<CodeHighlight> <CodeHighlight>
&lt;TabView&gt; &lt;TabView v-model:activeIndex="activeIndex"&gt;
&lt;TabPanel header="Header I" :active.sync="active1"&gt; &lt;TabPanel header="Header I"&gt;
Content I Content I
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header II" :active.sync="active2"&gt; &lt;TabPanel header="Header II"&gt;
Content II Content II
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header III" :active.sync="active3"&gt; &lt;TabPanel header="Header III""&gt;
Content III Content III
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;/TabView&gt; &lt;/TabView&gt;
@ -74,7 +74,7 @@ import TabPanel from 'primevue/tabpanel';
<h5>Header Template</h5> <h5>Header Template</h5>
<p>Custom content for the title section of a panel is defined using the header template.</p> <p>Custom content for the title section of a panel is defined using the header template.</p>
<CodeHighlight> <CodeHighlight>
&lt;TabView class="tabview-custom"&gt; &lt;TabView&gt;
&lt;TabPanel&gt; &lt;TabPanel&gt;
&lt;template #header&gt; &lt;template #header&gt;
&lt;i class="pi pi-calendar"&gt;&lt;/i&gt; &lt;i class="pi pi-calendar"&gt;&lt;/i&gt;
@ -95,11 +95,11 @@ import TabPanel from 'primevue/tabpanel';
<h5>Programmatic Control</h5> <h5>Programmatic Control</h5>
<p>Tabs can be controlled programmatically using active property that defines the active tab.</p> <p>Tabs can be controlled programmatically using active property that defines the active tab.</p>
<CodeHighlight> <CodeHighlight>
&lt;Button @click="activate(0)" class="p-button-text" label="Activate 1st" /&gt; &lt;Button @click="active = 0" class="p-button-text" label="Activate 1st" /&gt;
&lt;Button @click="activate(1)" class="p-button-text" label="Activate 2st" /&gt; &lt;Button @click="active = 1" class="p-button-text" label="Activate 2nd" /&gt;
&lt;Button @click="activate(2)" class="p-button-text" label="Activate 3st" /&gt; &lt;Button @click="active = 2" class="p-button-text" label="Activate 3rd" /&gt;
&lt;TabView&gt; &lt;TabView v-model:activeIndex="active"&gt;
&lt;TabPanel header="Header I" :active.sync="active[0]"&gt; &lt;TabPanel header="Header I" :active.sync="active[0]"&gt;
Content I Content I
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
@ -117,18 +117,9 @@ import TabPanel from 'primevue/tabpanel';
export default { export default {
data() { data() {
return { return {
active: [true, false, false] active: 0
} }
}, }
methods: {
activate(index) {
let activeArray = [...this.active];
for (let i = 0 ; i &lt; activeArray.length; i++) {
activeArray[i] = (i === index);
}
this.active = activeArray;
}
},
} }
</CodeHighlight> </CodeHighlight>
@ -136,32 +127,26 @@ export default {
<div class="doc-tablewrapper"> <div class="doc-tablewrapper">
<table class="doc-table"> <table class="doc-table">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Type</th> <th>Type</th>
<th>Default</th> <th>Default</th>
<th>Description</th> <th>Description</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>header</td> <td>header</td>
<td>string</td> <td>string</td>
<td>null</td> <td>null</td>
<td>Orientation of tab headers.</td> <td>Orientation of tab headers.</td>
</tr> </tr>
<tr> <tr>
<td>active</td> <td>disabled</td>
<td>boolean</td> <td>boolean</td>
<td>null</td> <td>null</td>
<td>Visibility of the content.</td> <td>Whether the tab is disabled.</td>
</tr> </tr>
<tr>
<td>disabled</td>
<td>boolean</td>
<td>null</td>
<td>Whether the tab is disabled.</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
@ -259,24 +244,24 @@ export default {
&lt;div class="card"&gt; &lt;div class="card"&gt;
&lt;h5&gt;Programmatic&lt;/h5&gt; &lt;h5&gt;Programmatic&lt;/h5&gt;
&lt;div style="padding: .5rem 0 1rem 0"&gt; &lt;div style="padding: .5rem 0 1rem 0" class="p-t-2 p-b-3"&gt;
&lt;Button @click="activate(0)" class="p-button-text" label="Activate 1st" /&gt; &lt;Button @click="active = 0" class="p-button-text" label="Activate 1st" /&gt;
&lt;Button @click="activate(1)" class="p-button-text" label="Activate 2nd" /&gt; &lt;Button @click="active = 1" class="p-button-text" label="Activate 2nd" /&gt;
&lt;Button @click="activate(2)" class="p-button-text" label="Activate 3rd" /&gt; &lt;Button @click="active = 2" class="p-button-text" label="Activate 3rd" /&gt;
&lt;/div&gt; &lt;/div&gt;
&lt;TabView&gt; &lt;TabView ref="tabview2" v-model:activeIndex="active"&gt;
&lt;TabPanel header="Header I" :active.sync="active[0]"&gt; &lt;TabPanel header="Header I"&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt; Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header II" :active.sync="active[1]"&gt; &lt;TabPanel header="Header II"&gt;
&lt;p&gt;Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi &lt;p&gt;Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione
voluptatem sequi nesciunt. Consectetur, adipisci velit, sed quia non numquam eius modi.&lt;/p&gt; voluptatem sequi nesciunt. Consectetur, adipisci velit, sed quia non numquam eius modi.&lt;/p&gt;
&lt;/TabPanel&gt; &lt;/TabPanel&gt;
&lt;TabPanel header="Header III" :active.sync="active[2]"&gt; &lt;TabPanel header="Header III"&gt;
&lt;p&gt;At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati &lt;p&gt;At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati
cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio.
Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus.&lt;/p&gt; Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus.&lt;/p&gt;
@ -346,18 +331,10 @@ export default {
export default { export default {
data() { data() {
return { return {
active: [true, false, false] active: 0
}
},
methods: {
activate(index) {
let activeArray = [...this.active];
for (let i = 0 ; i &lt; activeArray.length; i++) {
activeArray[i] = (i === index);
}
this.active = activeArray;
} }
} }
}
</CodeHighlight> </CodeHighlight>
<CodeHighlight lang="css"> <CodeHighlight lang="css">