primevue-mirror/components/lib/tabview/TabView.vue

376 lines
15 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:42 +00:00
<div :class="cx('root')" role="tablist" v-bind="ptmi('root')">
<div :class="cx('navContainer')" v-bind="ptm('navContainer')">
2022-09-14 11:26:01 +00:00
<button
v-if="scrollable && !isPrevButtonDisabled"
ref="prevBtn"
v-ripple
type="button"
2023-05-24 06:24:33 +00:00
:class="cx('previousButton')"
2022-09-14 11:26:01 +00:00
:tabindex="tabindex"
:aria-label="prevButtonAriaLabel"
@click="onPrevButtonClick"
2023-05-24 06:24:33 +00:00
v-bind="{ ...previousButtonProps, ...ptm('previousButton') }"
2023-07-17 10:29:13 +00:00
data-pc-group-section="navbutton"
2022-09-14 11:26:01 +00:00
>
<slot name="previcon">
<component :is="prevIcon ? 'span' : 'ChevronLeftIcon'" aria-hidden="true" :class="prevIcon" v-bind="ptm('previousIcon')" />
</slot>
2022-09-14 11:26:01 +00:00
</button>
<div ref="content" :class="cx('navContent')" @scroll="onScroll" v-bind="ptm('navContent')">
2024-01-12 12:27:10 +00:00
<ul ref="nav" :class="cx('nav')" v-bind="ptm('nav')">
<li
2023-05-24 06:24:33 +00:00
v-for="(tab, index) of tabs"
:key="getKey(tab, index)"
:style="getTabProp(tab, 'headerStyle')"
2023-05-24 06:24:33 +00:00
:class="cx('tab.header', { tab, index })"
role="presentation"
v-bind="{ ...getTabProp(tab, 'headerProps'), ...getTabPT(tab, 'root', index), ...getTabPT(tab, 'header', index) }"
data-pc-name="tabpanel"
2023-05-24 06:24:33 +00:00
:data-p-highlight="d_activeIndex === index"
:data-p-disabled="getTabProp(tab, 'disabled')"
:data-pc-index="index"
:data-p-active="d_activeIndex === index"
>
2022-09-14 11:26:01 +00:00
<a
2023-05-24 06:24:33 +00:00
:id="getTabHeaderActionId(index)"
2022-09-14 11:26:01 +00:00
v-ripple
2023-05-24 06:24:33 +00:00
:class="cx('tab.headerAction')"
:tabindex="getTabProp(tab, 'disabled') || !isTabActive(index) ? -1 : tabindex"
2022-09-14 11:26:01 +00:00
role="tab"
:aria-disabled="getTabProp(tab, 'disabled')"
2023-05-24 06:24:33 +00:00
:aria-selected="isTabActive(index)"
:aria-controls="getTabContentId(index)"
@click="onTabClick($event, tab, index)"
@keydown="onTabKeyDown($event, tab, index)"
2023-05-24 07:07:13 +00:00
v-bind="{ ...getTabProp(tab, 'headerActionProps'), ...getTabPT(tab, 'headerAction', index) }"
2022-09-14 11:26:01 +00:00
>
<span v-if="tab.props && tab.props.header" :class="cx('tab.headerTitle')" v-bind="getTabPT(tab, 'headerTitle', index)">{{ tab.props.header }}</span>
2022-09-14 11:26:01 +00:00
<component v-if="tab.children && tab.children.header" :is="tab.children.header"></component>
2022-09-06 12:03:37 +00:00
</a>
</li>
<li ref="inkbar" :class="cx('inkbar')" role="presentation" aria-hidden="true" v-bind="ptm('inkbar')"></li>
2022-09-06 12:03:37 +00:00
</ul>
</div>
2022-09-14 11:26:01 +00:00
<button
v-if="scrollable && !isNextButtonDisabled"
ref="nextBtn"
v-ripple
type="button"
2023-05-24 06:24:33 +00:00
:class="cx('nextButton')"
2022-09-14 11:26:01 +00:00
:tabindex="tabindex"
:aria-label="nextButtonAriaLabel"
@click="onNextButtonClick"
2023-05-24 06:24:33 +00:00
v-bind="{ ...nextButtonProps, ...ptm('nextButton') }"
2023-07-17 10:29:13 +00:00
data-pc-group-section="navbutton"
2022-09-14 11:26:01 +00:00
>
<slot name="nexticon">
<component :is="nextIcon ? 'span' : 'ChevronRightIcon'" aria-hidden="true" :class="nextIcon" v-bind="ptm('nextIcon')" />
</slot>
2022-09-14 11:26:01 +00:00
</button>
2022-09-06 12:03:37 +00:00
</div>
<div :class="cx('panelContainer')" v-bind="ptm('panelContainer')">
2023-05-24 06:24:33 +00:00
<template v-for="(tab, index) of tabs" :key="getKey(tab, index)">
2022-09-14 11:26:01 +00:00
<div
2023-05-24 06:24:33 +00:00
v-if="lazy ? isTabActive(index) : true"
v-show="lazy ? true : isTabActive(index)"
:id="getTabContentId(index)"
2022-09-14 11:26:01 +00:00
:style="getTabProp(tab, 'contentStyle')"
2023-05-24 06:24:33 +00:00
:class="cx('tab.content', { tab })"
2022-09-14 11:26:01 +00:00
role="tabpanel"
2023-05-24 06:24:33 +00:00
:aria-labelledby="getTabHeaderActionId(index)"
v-bind="{ ...getTabProp(tab, 'contentProps'), ...getTabPT(tab, 'root', index), ...getTabPT(tab, 'content', index) }"
data-pc-name="tabpanel"
:data-pc-index="index"
:data-p-active="d_activeIndex === index"
2022-09-14 11:26:01 +00:00
>
2022-09-06 12:03:37 +00:00
<component :is="tab"></component>
</div>
</template>
</div>
</div>
</template>
<script>
import ChevronLeftIcon from 'primevue/icons/chevronleft';
import ChevronRightIcon from 'primevue/icons/chevronright';
import Ripple from 'primevue/ripple';
import { DomHandler, UniqueComponentId } from 'primevue/utils';
import { mergeProps } from 'vue';
2023-05-24 06:24:33 +00:00
import BaseTabView from './BaseTabView.vue';
2023-04-14 08:04:15 +00:00
2022-09-06 12:03:37 +00:00
export default {
name: 'TabView',
2023-05-24 06:24:33 +00:00
extends: BaseTabView,
2024-02-11 23:48:42 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:activeIndex', 'tab-change', 'tab-click'],
data() {
return {
id: this.$attrs.id,
2022-09-06 12:03:37 +00:00
d_activeIndex: this.activeIndex,
2022-09-14 11:26:01 +00:00
isPrevButtonDisabled: true,
isNextButtonDisabled: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
activeIndex(newValue) {
this.d_activeIndex = newValue;
2022-09-14 11:26:01 +00:00
this.scrollInView({ index: newValue });
2022-09-06 12:03:37 +00:00
}
},
mounted() {
2024-01-23 18:06:31 +00:00
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
this.updateInkBar();
2022-12-08 11:04:25 +00:00
this.scrollable && this.updateButtonState();
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
updated() {
2022-09-06 12:03:37 +00:00
this.updateInkBar();
this.scrollable && this.updateButtonState();
2022-09-06 12:03:37 +00:00
},
methods: {
isTabPanel(child) {
return child.type.name === 'TabPanel';
},
2022-09-14 11:26:01 +00:00
isTabActive(index) {
return this.d_activeIndex === index;
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
getTabProp(tab, name) {
return tab.props ? tab.props[name] : undefined;
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
getKey(tab, index) {
return this.getTabProp(tab, 'header') || index;
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
getTabHeaderActionId(index) {
return `${this.id}_${index}_header_action`;
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
getTabContentId(index) {
return `${this.id}_${index}_content`;
2022-09-06 12:03:37 +00:00
},
2023-05-24 07:07:13 +00:00
getTabPT(tab, key, index) {
const count = this.tabs.length;
2023-05-24 07:07:13 +00:00
const tabMetaData = {
props: tab.props,
parent: {
instance: this,
props: this.$props,
state: this.$data
2023-05-24 07:07:13 +00:00
},
context: {
index,
count,
first: index === 0,
last: index === count - 1,
active: this.isTabActive(index)
}
2023-05-24 07:07:13 +00:00
};
return mergeProps(this.ptm(`tab.${key}`, { tab: tabMetaData }), this.ptm(`tabpanel.${key}`, { tabpanel: tabMetaData }), this.ptm(`tabpanel.${key}`, tabMetaData), this.ptmo(this.getTabProp(tab, 'pt'), key, tabMetaData));
},
2022-09-06 12:03:37 +00:00
onScroll(event) {
this.scrollable && this.updateButtonState();
event.preventDefault();
},
2022-09-14 11:26:01 +00:00
onPrevButtonClick() {
2022-09-06 12:03:37 +00:00
const content = this.$refs.content;
const width = DomHandler.getWidth(content);
2022-09-06 12:03:37 +00:00
const pos = content.scrollLeft - width;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
content.scrollLeft = pos <= 0 ? 0 : pos;
},
2022-09-14 11:26:01 +00:00
onNextButtonClick() {
2022-09-06 12:03:37 +00:00
const content = this.$refs.content;
const width = DomHandler.getWidth(content) - this.getVisibleButtonWidths();
const pos = content.scrollLeft + width;
const lastPos = content.scrollWidth - width;
content.scrollLeft = pos >= lastPos ? lastPos : pos;
2022-09-14 11:26:01 +00:00
},
onTabClick(event, tab, index) {
this.changeActiveIndex(event, tab, index);
this.$emit('tab-click', { originalEvent: event, index });
},
onTabKeyDown(event, tab, index) {
switch (event.code) {
case 'ArrowLeft':
this.onTabArrowLeftKey(event);
break;
case 'ArrowRight':
this.onTabArrowRightKey(event);
break;
case 'Home':
this.onTabHomeKey(event);
break;
case 'End':
this.onTabEndKey(event);
break;
case 'PageDown':
this.onPageDownKey(event);
break;
case 'PageUp':
this.onPageUpKey(event);
break;
case 'Enter':
case 'NumpadEnter':
2022-09-14 11:26:01 +00:00
case 'Space':
this.onTabEnterKey(event, tab, index);
break;
default:
break;
}
},
onTabArrowRightKey(event) {
const nextHeaderAction = this.findNextHeaderAction(event.target.parentElement);
nextHeaderAction ? this.changeFocusedTab(event, nextHeaderAction) : this.onTabHomeKey(event);
event.preventDefault();
},
onTabArrowLeftKey(event) {
const prevHeaderAction = this.findPrevHeaderAction(event.target.parentElement);
prevHeaderAction ? this.changeFocusedTab(event, prevHeaderAction) : this.onTabEndKey(event);
event.preventDefault();
},
onTabHomeKey(event) {
const firstHeaderAction = this.findFirstHeaderAction();
this.changeFocusedTab(event, firstHeaderAction);
event.preventDefault();
},
onTabEndKey(event) {
const lastHeaderAction = this.findLastHeaderAction();
this.changeFocusedTab(event, lastHeaderAction);
event.preventDefault();
},
onPageDownKey(event) {
this.scrollInView({ index: this.$refs.nav.children.length - 2 });
event.preventDefault();
},
onPageUpKey(event) {
this.scrollInView({ index: 0 });
event.preventDefault();
},
onTabEnterKey(event, tab, index) {
this.changeActiveIndex(event, tab, index);
event.preventDefault();
},
findNextHeaderAction(tabElement, selfCheck = false) {
const headerElement = selfCheck ? tabElement : tabElement.nextElementSibling;
return headerElement
2023-05-24 06:24:33 +00:00
? DomHandler.getAttribute(headerElement, 'data-p-disabled') || DomHandler.getAttribute(headerElement, 'data-pc-section') === 'inkbar'
2022-09-14 11:26:01 +00:00
? this.findNextHeaderAction(headerElement)
2023-05-24 06:24:33 +00:00
: DomHandler.findSingle(headerElement, '[data-pc-section="headeraction"]')
2022-09-14 11:26:01 +00:00
: null;
},
findPrevHeaderAction(tabElement, selfCheck = false) {
const headerElement = selfCheck ? tabElement : tabElement.previousElementSibling;
return headerElement
2023-05-24 06:24:33 +00:00
? DomHandler.getAttribute(headerElement, 'data-p-disabled') || DomHandler.getAttribute(headerElement, 'data-pc-section') === 'inkbar'
2022-09-14 11:26:01 +00:00
? this.findPrevHeaderAction(headerElement)
2023-05-24 06:24:33 +00:00
: DomHandler.findSingle(headerElement, '[data-pc-section="headeraction"]')
2022-09-14 11:26:01 +00:00
: null;
},
findFirstHeaderAction() {
return this.findNextHeaderAction(this.$refs.nav.firstElementChild, true);
},
findLastHeaderAction() {
return this.findPrevHeaderAction(this.$refs.nav.lastElementChild, true);
},
changeActiveIndex(event, tab, index) {
if (!this.getTabProp(tab, 'disabled') && this.d_activeIndex !== index) {
this.d_activeIndex = index;
this.$emit('update:activeIndex', index);
this.$emit('tab-change', { originalEvent: event, index });
this.scrollInView({ index });
}
},
changeFocusedTab(event, element) {
if (element) {
DomHandler.focus(element);
this.scrollInView({ element });
if (this.selectOnFocus) {
const index = parseInt(element.parentElement.dataset.pcIndex, 10);
2022-09-14 11:26:01 +00:00
const tab = this.tabs[index];
this.changeActiveIndex(event, tab, index);
}
}
},
scrollInView({ element, index = -1 }) {
const currentElement = element || this.$refs.nav.children[index];
if (currentElement) {
currentElement.scrollIntoView && currentElement.scrollIntoView({ block: 'nearest' });
}
},
updateInkBar() {
let tabHeader = this.$refs.nav.children[this.d_activeIndex];
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';
},
updateButtonState() {
const content = this.$refs.content;
const { scrollLeft, scrollWidth } = content;
const width = DomHandler.getWidth(content);
this.isPrevButtonDisabled = scrollLeft === 0;
this.isNextButtonDisabled = parseInt(scrollLeft) === scrollWidth - width;
},
getVisibleButtonWidths() {
const { prevBtn, nextBtn } = this.$refs;
return [prevBtn, nextBtn].reduce((acc, el) => (el ? acc + DomHandler.getWidth(el) : acc), 0);
2022-09-06 12:03:37 +00:00
}
},
computed: {
tabs() {
return this.$slots.default().reduce((tabs, child) => {
if (this.isTabPanel(child)) {
tabs.push(child);
} else if (child.children && child.children instanceof Array) {
child.children.forEach((nestedChild) => {
if (this.isTabPanel(nestedChild)) {
tabs.push(nestedChild);
}
});
}
return tabs;
}, []);
2022-09-14 11:26:01 +00:00
},
prevButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.previous : undefined;
},
nextButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.next : undefined;
2022-09-06 12:03:37 +00:00
}
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
},
components: {
ChevronLeftIcon,
ChevronRightIcon
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>