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

219 lines
7.3 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:42 +00:00
<nav :id="id" :class="cx('root')" v-bind="ptmi('root')">
<ol ref="list" :class="cx('list')" v-bind="ptm('list')">
<template v-for="(item, index) of model" :key="label(item) + '_' + index.toString()">
<li
v-if="visible(item)"
:class="[cx('item', { item, index }), item.class]"
:style="item.style"
:aria-current="isActive(index) ? 'step' : undefined"
@click="onItemClick($event, item, index)"
@keydown="onItemKeydown($event, item, index)"
v-bind="getPTOptions('item', item, index)"
:data-p-active="isActive(index)"
:data-p-disabled="isItemDisabled(item, index)"
>
2022-09-06 12:03:37 +00:00
<template v-if="!$slots.item">
<span :class="cx('itemLink')" v-bind="getPTOptions('itemLink', item, index)">
<span :class="cx('itemNumber')" v-bind="getPTOptions('itemNumber', item, index)">{{ index + 1 }}</span>
<span :class="cx('itemLabel')" v-bind="getPTOptions('itemLabel', item, index)">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</span>
</template>
<component v-else :is="$slots.item" :item="item" :index="index" :active="index === d_activeStep" :label="label(item)" :props="getMenuItemProps(item, index)"></component>
2022-09-06 12:03:37 +00:00
</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';
2023-08-30 14:29:26 +00:00
import { mergeProps } from 'vue';
2023-05-30 11:58:41 +00:00
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,
2024-02-11 23:48:42 +00:00
inheritAttrs: false,
emits: ['update:activeStep', 'step-change'],
data() {
return {
d_activeStep: this.activeStep
};
},
watch: {
activeStep(newValue) {
this.d_activeStep = newValue;
2023-08-30 14:29:26 +00:00
}
},
2022-12-08 11:04:25 +00:00
mounted() {
const firstItem = this.findFirstItem();
2023-12-28 09:09:51 +00:00
firstItem && (firstItem.tabIndex = '0');
2022-12-08 11:04:25 +00:00
},
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: {
2023-07-25 09:11:49 +00:00
item,
2023-07-24 10:57:49 +00:00
index,
active: this.isActive(index),
disabled: this.isItemDisabled(item, index)
2023-07-24 10:57:49 +00:00
}
});
},
onItemClick(event, item, index) {
2022-09-06 12:03:37 +00:00
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 (index !== this.d_activeStep) {
this.d_activeStep = index;
this.$emit('update:activeStep', this.d_activeStep);
2022-09-06 12:03:37 +00:00
}
this.$emit('step-change', {
originalEvent: event,
index: index
});
2022-09-06 12:03:37 +00:00
},
onItemKeydown(event, item) {
2022-12-08 11:04:25 +00:00
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 'NumpadEnter':
2022-12-08 11:04:25 +00:00
case 'Space': {
this.onItemClick(event, item);
2022-12-08 11:04:25 +00:00
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() {
const firstSibling = DomHandler.findSingle(this.$refs.list, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
return firstSibling ? firstSibling.children[0] : null;
},
findLastItem() {
const siblings = DomHandler.find(this.$refs.list, '[data-pc-section="item"]');
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();
},
isActive(index) {
return index === this.d_activeStep;
2022-09-06 12:03:37 +00:00
},
isItemDisabled(item, index) {
return this.disabled(item) || (this.readonly && !this.isActive(index));
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;
2023-08-30 14:29:26 +00:00
},
getMenuItemProps(item, index) {
return {
action: mergeProps(
{
class: this.cx('itemLink'),
2023-08-30 14:29:26 +00:00
onClick: ($event) => this.onItemClick($event, item),
onKeyDown: ($event) => this.onItemKeydown($event, item)
},
this.getPTOptions('itemLink', item, index)
2023-08-30 14:29:26 +00:00
),
step: mergeProps(
{
class: this.cx('itemNumber')
2023-08-30 14:29:26 +00:00
},
this.getPTOptions('itemNumber', item, index)
2023-08-30 14:29:26 +00:00
),
label: mergeProps(
{
class: this.cx('itemLabel')
2023-08-30 14:29:26 +00:00
},
this.getPTOptions('itemLabel', item, index)
2023-08-30 14:29:26 +00:00
)
};
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>