primevue-mirror/components/lib/steps/Steps.vue

178 lines
6.5 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-30 11:58:41 +00:00
<nav :id="id" :class="cx('root')" v-bind="ptm('root')" data-pc-name="steps">
<ol ref="list" :class="cx('menu')" v-bind="ptm('menu')">
2022-09-14 11:26:01 +00:00
<template v-for="(item, index) of model" :key="item.to">
2023-07-24 10:57:49 +00:00
<li v-if="visible(item)" :class="[cx('menuitem', { item }), item.class]" :style="item.style" v-bind="getPTOptions('menuitem', item, index)" :data-p-highlight="isActive(item)" :data-p-disabled="isItemDisabled(item)">
2022-09-06 12:03:37 +00:00
<template v-if="!$slots.item">
2022-09-14 11:26:01 +00:00
<router-link v-if="!isItemDisabled(item)" v-slot="{ navigate, href, isActive, isExactActive }" :to="item.to" custom>
2022-12-08 11:04:25 +00:00
<a
:href="href"
2023-05-30 11:58:41 +00:00
:class="cx('action', { isActive, isExactActive })"
2022-12-08 11:04:25 +00:00
:tabindex="-1"
:aria-current="isExactActive ? 'step' : undefined"
@click="onItemClick($event, item, navigate)"
@keydown="onItemKeydown($event, item, navigate)"
2023-07-24 10:57:49 +00:00
v-bind="getPTOptions('action', item, index)"
2022-12-08 11:04:25 +00:00
>
2023-07-24 10:57:49 +00:00
<span :class="cx('step')" v-bind="getPTOptions('step', item, index)">{{ index + 1 }}</span>
<span :class="cx('label')" v-bind="getPTOptions('label', item, index)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
</router-link>
2023-07-24 10:57:49 +00:00
<span v-else :class="cx('action')" @keydown="onItemKeydown($event, item)" v-bind="getPTOptions('action', item, index)">
<span :class="cx('step')" v-bind="getPTOptions('step', item, index)">{{ index + 1 }}</span>
<span :class="cx('label')" v-bind="getPTOptions('label', item, index)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</span>
</template>
<component v-else :is="$slots.item" :item="item"></component>
</li>
</template>
2022-12-08 11:04:25 +00:00
</ol>
</nav>
2022-09-06 12:03:37 +00:00
</template>
<script>
2023-05-30 11:58:41 +00:00
import { DomHandler } from 'primevue/utils';
import BaseSteps from './BaseSteps.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'Steps',
2023-05-30 11:58:41 +00:00
extends: BaseSteps,
2022-12-08 11:04:25 +00:00
mounted() {
const firstItem = this.findFirstItem();
firstItem.tabIndex = '0';
},
2022-09-06 12:03:37 +00:00
methods: {
2023-07-24 10:57:49 +00:00
getPTOptions(key, item, index) {
return this.ptm(key, {
context: {
index,
active: this.isActive(item),
disabled: this.isItemDisabled(item)
}
});
},
2022-09-06 12:03:37 +00:00
onItemClick(event, item, navigate) {
if (this.disabled(item) || this.readonly) {
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);
}
},
2022-12-08 11:04:25 +00:00
onItemKeydown(event, item, navigate) {
switch (event.code) {
case 'ArrowRight': {
this.navigateToNextItem(event.target);
event.preventDefault();
break;
}
case 'ArrowLeft': {
this.navigateToPrevItem(event.target);
event.preventDefault();
break;
}
case 'Home': {
this.navigateToFirstItem(event.target);
event.preventDefault();
break;
}
case 'End': {
this.navigateToLastItem(event.target);
event.preventDefault();
break;
}
case 'Tab':
//no op
break;
case 'Enter':
case 'Space': {
this.onItemClick(event, item, navigate);
event.preventDefault();
break;
}
default:
break;
}
},
navigateToNextItem(target) {
const nextItem = this.findNextItem(target);
nextItem && this.setFocusToMenuitem(target, nextItem);
},
navigateToPrevItem(target) {
const prevItem = this.findPrevItem(target);
prevItem && this.setFocusToMenuitem(target, prevItem);
},
navigateToFirstItem(target) {
const firstItem = this.findFirstItem(target);
firstItem && this.setFocusToMenuitem(target, firstItem);
},
navigateToLastItem(target) {
const lastItem = this.findLastItem(target);
lastItem && this.setFocusToMenuitem(target, lastItem);
},
findNextItem(item) {
const nextItem = item.parentElement.nextElementSibling;
return nextItem ? nextItem.children[0] : null;
},
findPrevItem(item) {
const prevItem = item.parentElement.previousElementSibling;
return prevItem ? prevItem.children[0] : null;
},
findFirstItem() {
2023-05-30 11:58:41 +00:00
const firstSibling = DomHandler.findSingle(this.$refs.list, '[data-pc-section="menuitem"]');
2022-12-08 11:04:25 +00:00
return firstSibling ? firstSibling.children[0] : null;
},
findLastItem() {
2023-05-30 11:58:41 +00:00
const siblings = DomHandler.find(this.$refs.list, '[data-pc-section="menuitem"]');
2022-12-08 11:04:25 +00:00
return siblings ? siblings[siblings.length - 1].children[0] : null;
},
setFocusToMenuitem(target, focusableItem) {
target.tabIndex = '-1';
focusableItem.tabIndex = '0';
focusableItem.focus();
},
2022-09-06 12:03:37 +00:00
isActive(item) {
return item.to ? this.$router.resolve(item.to).path === this.$route.path : false;
},
isItemDisabled(item) {
2022-09-14 11:26:01 +00:00
return this.disabled(item) || (this.readonly && !this.isActive(item));
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-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>