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

273 lines
9.6 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-30 11:43:52 +00:00
<div :class="cx('root')" v-bind="ptm('root')">
<ul ref="nav" :class="cx('menu')" role="menubar" :aria-labelledby="ariaLabelledby" :aria-label="ariaLabel" v-bind="ptm('menu')">
<template v-for="(item, i) of focusableItems" :key="label(item) + '_' + i.toString()">
2022-09-14 11:26:01 +00:00
<router-link v-if="item.to && !disabled(item)" v-slot="{ navigate, href, isActive, isExactActive }" :to="item.to" custom>
2023-05-30 11:43:52 +00:00
<li
v-if="visible(item)"
ref="tab"
:class="[cx('menuitem', { item, isActive, isExactActive }), item.class]"
:style="item.style"
role="presentation"
v-bind="getPTOptions('menuitem', i)"
:data-p-highlight="exact ? isExactActive : isActive"
:data-p-disabled="disabled(item)"
>
2022-09-06 12:03:37 +00:00
<template v-if="!$slots.item">
2022-12-08 11:04:25 +00:00
<a
ref="tabLink"
v-ripple
role="menuitem"
:href="href"
2023-05-30 11:43:52 +00:00
:class="cx('action')"
2022-12-08 11:04:25 +00:00
:aria-label="label(item)"
:aria-disabled="disabled(item)"
:tabindex="isExactActive ? '0' : '-1'"
@click="onItemClick($event, item, i, navigate)"
@keydown="onKeydownItem($event, i, navigate)"
2023-04-28 07:23:20 +00:00
v-bind="getPTOptions('action', i)"
2022-12-08 11:04:25 +00:00
>
2023-05-30 11:43:52 +00:00
<component v-if="$slots.itemicon" :is="$slots.itemicon" :item="item" :class="[cx('icon'), item.icon]" />
<span v-else-if="item.icon" :class="[cx('icon'), item.icon]" v-bind="getPTOptions('icon', i)" />
<span :class="cx('label')" v-bind="getPTOptions('label', i)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
</template>
<component v-else :is="$slots.item" :item="item" :index="i"></component>
2022-09-06 12:03:37 +00:00
</li>
</router-link>
2023-05-30 11:43:52 +00:00
<li
v-else-if="visible(item)"
ref="tab"
:class="[cx('menuitem', { item, index: i }), item.class]"
role="presentation"
@click="onItemClick($event, item, i)"
@keydown="onKeydownItem($event, i)"
v-bind="getPTOptions('menuitem', i)"
: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-05-30 11:43:52 +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="setTabIndex(i)" v-bind="getPTOptions('action', i)">
<component v-if="$slots.itemicon" :is="$slots.itemicon" :item="item" :class="[cx('icon'), item.icon]" />
<span v-else-if="item.icon" :class="[cx('icon'), item.icon]" v-bind="getPTOptions('icon', i)" />
<span :class="cx('label')" v-bind="getPTOptions('label', i)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
</template>
<component v-else :is="$slots.item" :item="item" :index="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-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: {
$route() {
this.timeout = setTimeout(() => this.updateInkBar(), 50);
},
activeIndex(newValue) {
this.d_activeIndex = newValue;
2022-09-06 12:03:37 +00:00
}
},
mounted() {
this.updateInkBar();
},
updated() {
this.updateInkBar();
},
beforeUnmount() {
clearTimeout(this.timeout);
},
methods: {
2023-04-28 07:23:20 +00:00
getPTOptions(key, index) {
return this.ptm(key, {
context: {
2023-04-28 07:23:20 +00:00
order: index
}
});
},
2022-09-06 12:03:37 +00:00
onItemClick(event, item, index, navigate) {
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 (item.to && navigate) {
navigate(event);
}
if (index !== this.d_activeIndex) {
this.d_activeIndex = index;
this.$emit('update:activeIndex', this.d_activeIndex);
}
this.$emit('tab-change', {
originalEvent: event,
index: index
});
},
onKeydownItem(event, index) {
2022-12-08 11:04:25 +00:00
let i = index;
let foundElement = {};
const tabLinkRef = this.$refs.tabLink;
switch (event.code) {
case 'ArrowRight': {
foundElement = this.findNextItem(this.$refs.tab, i);
i = foundElement.i;
break;
}
case 'ArrowLeft': {
foundElement = this.findPrevItem(this.$refs.tab, i);
i = foundElement.i;
break;
}
case 'End': {
foundElement = this.findPrevItem(this.$refs.tab, this.model.length);
i = foundElement.i;
event.preventDefault();
break;
}
case 'Home': {
foundElement = this.findNextItem(this.$refs.tab, -1);
i = foundElement.i;
event.preventDefault();
break;
}
case 'Space':
case 'Enter': {
if (event.currentTarget) {
event.currentTarget.click();
}
event.preventDefault();
break;
}
case 'Tab': {
this.setDefaultTabIndexes(tabLinkRef);
break;
}
default:
break;
}
if (tabLinkRef[i] && tabLinkRef[index]) {
tabLinkRef[index].tabIndex = '-1';
tabLinkRef[i].tabIndex = '0';
tabLinkRef[i].focus();
}
},
findNextItem(items, index) {
let i = index + 1;
if (i >= items.length) {
return { nextItem: items[items.length], i: items.length };
}
let nextItem = items[i];
2023-05-30 11:43:52 +00:00
if (nextItem) return DomHandler.getAttribute(nextItem, 'data-p-disabled') ? this.findNextItem(items, i) : { nextItem, i };
2022-12-08 11:04:25 +00:00
else return null;
},
findPrevItem(items, index) {
let i = index - 1;
if (i < 0) {
return { nextItem: items[0], i: 0 };
}
let prevItem = items[i];
2023-05-30 11:43:52 +00:00
if (prevItem) return DomHandler.getAttribute(prevItem, 'data-p-disabled') ? this.findPrevItem(items, i) : { prevItem, i };
2022-12-08 11:04:25 +00:00
else return null;
},
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) {
2022-09-14 11:26:01 +00:00
return typeof item.disabled === 'function' ? item.disabled() : item.disabled;
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
},
2022-12-08 11:04:25 +00:00
setDefaultTabIndexes(tabLinkRef) {
setTimeout(() => {
tabLinkRef.forEach((item) => {
2023-05-30 11:43:52 +00:00
item.tabIndex = DomHandler.getAttribute(item.parentElement, 'data-p-highlight') ? '0' : '-1';
2022-12-08 11:04:25 +00:00
});
}, 300);
},
setTabIndex(index) {
return this.d_activeIndex === index ? '0' : '-1';
},
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
}
}
},
computed: {
focusableItems() {
return (this.model || []).reduce((result, item) => {
2023-05-30 11:43:52 +00:00
this.visible(item) && !this.disabled(item) && result.push(item);
return result;
}, []);
}
},
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>