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

260 lines
9.4 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')" v-bind="ptmi('root')">
<ul ref="nav" :class="cx('tablist')" role="menubar" :aria-labelledby="ariaLabelledby" :aria-label="ariaLabel" v-bind="ptm('tablist')">
<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('item', { item, index: i }), item.class]"
2023-05-30 11:43:52 +00:00
role="presentation"
@click="onItemClick($event, item, i)"
@keydown="onKeydownItem($event, item, i)"
v-bind="getPTOptions('item', 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">
<a ref="tabLink" v-ripple role="menuitem" :href="item.url" :class="cx('itemLink')" :target="item.target" :aria-label="label(item)" :aria-disabled="disabled(item)" :tabindex="-1" v-bind="getPTOptions('itemLink', item, i)">
<component v-if="$slots.itemicon" :is="$slots.itemicon" :item="item" :class="cx('itemIcon')" />
<span v-else-if="item.icon" :class="[cx('itemIcon'), item.icon]" v-bind="getPTOptions('itemIcon', item, i)" />
<span :class="cx('itemLabel')" v-bind="getPTOptions('itemLabel', 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>
2024-05-11 09:30:44 +00:00
<li ref="inkbar" role="none" :class="cx('activeBar')" v-bind="ptm('activeBar')"></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,
2024-02-11 23:48:42 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:activeIndex', 'tab-change'],
data() {
return {
d_activeIndex: this.activeIndex
2022-09-14 11:26:01 +00:00
};
},
watch: {
2024-05-13 12:20:44 +00:00
activeIndex: {
flush: 'post',
handler(newValue) {
this.d_activeIndex = newValue;
this.updateInkBar();
}
2022-09-06 12:03:37 +00:00
}
},
mounted() {
2024-05-13 12:20:44 +00:00
this.$nextTick(() => {
this.updateInkBar();
});
const activeItem = this.findActiveItem();
activeItem && (activeItem.tabIndex = '0');
2022-09-06 12:03:37 +00:00
},
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="item"][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="item"][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="item"][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="item"][data-p-disabled="false"][data-p-highlight="true"]');
const focusedItem = DomHandler.findSingle(this.$refs.nav, '[data-pc-section="itemlink"][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('itemLink'),
2023-08-31 13:08:40 +00:00
tabindex: -1,
onClick: ($event) => this.onItemClick($event, item, index),
onKeyDown: ($event) => this.onKeydownItem($event, item, index)
},
this.getPTOptions('itemLink', item, index)
2023-08-31 13:08:40 +00:00
),
icon: mergeProps(
{
class: [this.cx('itemIcon'), item.icon]
2023-08-31 13:08:40 +00:00
},
this.getPTOptions('itemIcon', item, index)
2023-08-31 13:08:40 +00:00
),
label: mergeProps(
{
class: this.cx('itemLabel')
2023-08-31 13:08:40 +00:00
},
this.getPTOptions('itemLabel', item, index)
2023-08-31 13:08:40 +00:00
)
};
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>