primevue-mirror/components/lib/tabmenu/TabMenu.vue

259 lines
9.3 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('root')" v-bind="ptm('root')">
2023-05-30 11:43:52 +00:00
<ul ref="nav" :class="cx('menu')" role="menubar" :aria-labelledby="ariaLabelledby" :aria-label="ariaLabel" v-bind="ptm('menu')">
<template v-for="(item, i) of model" :key="label(item) + '_' + i.toString()">
2023-05-30 11:43:52 +00:00
<li
2023-11-08 12:49:46 +00:00
v-if="visible(item)"
2023-05-30 11:43:52 +00:00
ref="tab"
:class="[cx('menuitem', { item, index: i }), item.class]"
role="presentation"
@click="onItemClick($event, item, i)"
@keydown="onKeydownItem($event, item, i)"
2023-07-25 09:11:57 +00:00
v-bind="getPTOptions('menuitem', item, i)"
2023-05-30 11:43:52 +00:00
:data-p-highlight="d_activeIndex === i"
:data-p-disabled="disabled(item)"
>
2022-09-06 12:03:37 +00:00
<template v-if="!$slots.item">
2023-07-25 09:11:57 +00:00
<a ref="tabLink" v-ripple role="menuitem" :href="item.url" :class="cx('action')" :target="item.target" :aria-label="label(item)" :aria-disabled="disabled(item)" :tabindex="-1" v-bind="getPTOptions('action', item, i)">
2023-05-30 11:43:52 +00:00
<component v-if="$slots.itemicon" :is="$slots.itemicon" :item="item" :class="[cx('icon'), item.icon]" />
2023-07-25 09:11:57 +00:00
<span v-else-if="item.icon" :class="[cx('icon'), item.icon]" v-bind="getPTOptions('icon', item, i)" />
<span :class="cx('label')" v-bind="getPTOptions('label', item, i)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
</template>
<component v-else :is="$slots.item" :item="item" :index="i" :active="i === d_activeIndex" :label="label(item)" :props="getMenuItemProps(item, i)"></component>
2022-09-06 12:03:37 +00:00
</li>
</template>
2023-05-30 11:43:52 +00:00
<li ref="inkbar" role="none" :class="cx('inkbar')" v-bind="ptm('inkbar')"></li>
2022-09-06 12:03:37 +00:00
</ul>
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler } from 'primevue/utils';
2023-08-31 13:08:40 +00:00
import { mergeProps } from 'vue';
2023-05-30 11:43:52 +00:00
import BaseTabMenu from './BaseTabMenu.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'TabMenu',
2023-05-30 11:43:52 +00:00
extends: BaseTabMenu,
2022-09-06 12:03:37 +00:00
emits: ['update:activeIndex', 'tab-change'],
timeout: null,
data() {
return {
d_activeIndex: this.activeIndex
2022-09-14 11:26:01 +00:00
};
},
watch: {
activeIndex(newValue) {
this.d_activeIndex = newValue;
2022-09-06 12:03:37 +00:00
}
},
mounted() {
this.updateInkBar();
const activeItem = this.findActiveItem();
activeItem && (activeItem.tabIndex = '0');
2022-09-06 12:03:37 +00:00
},
updated() {
this.updateInkBar();
},
beforeUnmount() {
clearTimeout(this.timeout);
},
methods: {
2023-07-25 09:11:57 +00:00
getPTOptions(key, item, index) {
2023-04-28 07:23:20 +00:00
return this.ptm(key, {
context: {
2023-07-25 09:11:57 +00:00
item,
2023-07-24 11:12:43 +00:00
index
2023-04-28 07:23:20 +00:00
}
});
},
2023-11-08 12:49:46 +00:00
onItemClick(event, item, index) {
2022-09-06 12:03:37 +00:00
if (this.disabled(item)) {
event.preventDefault();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return;
}
if (item.command) {
item.command({
originalEvent: event,
item: item
});
}
if (index !== this.d_activeIndex) {
this.d_activeIndex = index;
this.$emit('update:activeIndex', this.d_activeIndex);
}
this.$emit('tab-change', {
originalEvent: event,
index: index
});
},
2023-11-08 12:49:46 +00:00
onKeydownItem(event, item, index) {
2022-12-08 11:04:25 +00:00
switch (event.code) {
case 'ArrowRight': {
this.navigateToNextItem(event.target);
event.preventDefault();
2022-12-08 11:04:25 +00:00
break;
}
case 'ArrowLeft': {
this.navigateToPrevItem(event.target);
event.preventDefault();
2022-12-08 11:04:25 +00:00
break;
}
case 'Home': {
this.navigateToFirstItem(event.target);
2022-12-08 11:04:25 +00:00
event.preventDefault();
break;
}
case 'End': {
this.navigateToLastItem(event.target);
2022-12-08 11:04:25 +00:00
event.preventDefault();
break;
}
case 'Space':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
case 'Enter': {
2023-11-08 12:49:46 +00:00
this.onItemClick(event, item, index);
2022-12-08 11:04:25 +00:00
event.preventDefault();
break;
}
case 'Tab': {
this.onTabKey();
2022-12-08 11:04:25 +00:00
break;
}
default:
break;
}
},
navigateToNextItem(target) {
const nextItem = this.findNextItem(target);
2022-12-08 11:04:25 +00:00
nextItem && this.setFocusToMenuitem(target, nextItem);
2022-12-08 11:04:25 +00:00
},
navigateToPrevItem(target) {
const prevItem = this.findPrevItem(target);
2022-12-08 11:04:25 +00:00
prevItem && this.setFocusToMenuitem(target, prevItem);
},
navigateToFirstItem(target) {
const firstItem = this.findFirstItem(target);
2022-12-08 11:04:25 +00:00
firstItem && this.setFocusToMenuitem(target, firstItem);
},
navigateToLastItem(target) {
const lastItem = this.findLastItem(target);
2022-12-08 11:04:25 +00:00
lastItem && this.setFocusToMenuitem(target, lastItem);
2022-12-08 11:04:25 +00:00
},
findNextItem(item) {
const nextItem = item.parentElement.nextElementSibling;
2022-12-08 11:04:25 +00:00
return nextItem ? (DomHandler.getAttribute(nextItem, 'data-p-disabled') === true ? this.findNextItem(nextItem.children[0]) : nextItem.children[0]) : null;
},
findPrevItem(item) {
const prevItem = item.parentElement.previousElementSibling;
return prevItem ? (DomHandler.getAttribute(prevItem, 'data-p-disabled') === true ? this.findPrevItem(prevItem.children[0]) : prevItem.children[0]) : null;
},
findFirstItem() {
const firstSibling = DomHandler.findSingle(this.$refs.nav, '[data-pc-section="menuitem"][data-p-disabled="false"]');
2022-12-08 11:04:25 +00:00
return firstSibling ? firstSibling.children[0] : null;
},
findLastItem() {
const siblings = DomHandler.find(this.$refs.nav, '[data-pc-section="menuitem"][data-p-disabled="false"]');
2022-12-08 11:04:25 +00:00
return siblings ? siblings[siblings.length - 1].children[0] : null;
},
findActiveItem() {
const activeItem = DomHandler.findSingle(this.$refs.nav, '[data-pc-section="menuitem"][data-p-disabled="false"][data-p-highlight="true"]');
return activeItem ? activeItem.children[0] : null;
},
setFocusToMenuitem(target, focusableItem) {
target.tabIndex = '-1';
focusableItem.tabIndex = '0';
focusableItem.focus();
},
onTabKey() {
const activeItem = DomHandler.findSingle(this.$refs.nav, '[data-pc-section="menuitem"][data-p-disabled="false"][data-p-highlight="true"]');
const focusedItem = DomHandler.findSingle(this.$refs.nav, '[data-pc-section="action"][tabindex="0"]');
if (focusedItem !== activeItem.children[0]) {
activeItem && (activeItem.children[0].tabIndex = '0');
focusedItem.tabIndex = '-1';
}
2022-12-08 11:04:25 +00:00
},
2022-09-06 12:03:37 +00:00
visible(item) {
2022-09-14 11:26:01 +00:00
return typeof item.visible === 'function' ? item.visible() : item.visible !== false;
2022-09-06 12:03:37 +00:00
},
disabled(item) {
return typeof item.disabled === 'function' ? item.disabled() : item.disabled === true;
2022-09-06 12:03:37 +00:00
},
label(item) {
2022-09-14 11:26:01 +00:00
return typeof item.label === 'function' ? item.label() : item.label;
2022-09-06 12:03:37 +00:00
},
updateInkBar() {
let tabs = this.$refs.nav.children;
let inkHighlighted = false;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
2022-09-14 11:26:01 +00:00
2023-05-30 11:43:52 +00:00
if (DomHandler.getAttribute(tab, 'data-p-highlight')) {
2022-09-06 12:03:37 +00:00
this.$refs.inkbar.style.width = DomHandler.getWidth(tab) + 'px';
2022-09-14 11:26:01 +00:00
this.$refs.inkbar.style.left = DomHandler.getOffset(tab).left - DomHandler.getOffset(this.$refs.nav).left + 'px';
2022-09-06 12:03:37 +00:00
inkHighlighted = true;
}
}
if (!inkHighlighted) {
this.$refs.inkbar.style.width = '0px';
2022-09-14 11:26:01 +00:00
this.$refs.inkbar.style.left = '0px';
2022-09-06 12:03:37 +00:00
}
2023-08-31 13:08:40 +00:00
},
getMenuItemProps(item, index) {
return {
action: mergeProps(
{
class: this.cx('action'),
tabindex: -1,
onClick: ($event) => this.onItemClick($event, item, index),
onKeyDown: ($event) => this.onKeydownItem($event, item, index)
},
this.getPTOptions('action', item, index)
),
icon: mergeProps(
{
class: [this.cx('icon'), item.icon]
},
this.getPTOptions('icon', item, index)
),
label: mergeProps(
{
class: this.cx('label')
},
this.getPTOptions('label', item, index)
)
};
2022-09-06 12:03:37 +00:00
}
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>