Refactor #4739 - For Steps & Fixed #4778 - Steps: new activeStep property

pull/4763/head
tugcekucukoglu 2023-11-08 15:52:43 +03:00
parent ba8f58d1ac
commit ca572864cf
4 changed files with 52 additions and 44 deletions

View File

@ -17,9 +17,9 @@ export default {
type: Boolean, type: Boolean,
default: true default: true
}, },
exact: { activeStep: {
type: Boolean, type: Number,
default: true default: 0
} }
}, },
style: StepsStyle, style: StepsStyle,

View File

@ -141,9 +141,15 @@ export interface StepsProps {
readonly?: boolean | undefined; readonly?: boolean | undefined;
/** /**
* Whether to apply 'router-link-active-exact' class if route exactly matches the item path. * Whether to apply 'router-link-active-exact' class if route exactly matches the item path.
* @deprecated since v3.40.0.
* @defaultValue true * @defaultValue true
*/ */
exact?: boolean | undefined; exact?: boolean | undefined;
/**
* Active step index of menuitem.
* @defaultValue 0
*/
activeStep?: number | undefined;
/** /**
* Used to pass attributes to DOM elements inside the component. * Used to pass attributes to DOM elements inside the component.
* @type {StepsPassThroughOptions} * @type {StepsPassThroughOptions}

View File

@ -1,24 +1,19 @@
<template> <template>
<nav :id="id" :class="cx('root')" v-bind="ptm('root')" data-pc-name="steps"> <nav :id="id" :class="cx('root')" v-bind="ptm('root')" data-pc-name="steps">
<ol ref="list" :class="cx('menu')" v-bind="ptm('menu')"> <ol ref="list" :class="cx('menu')" v-bind="ptm('menu')">
<template v-for="(item, index) of model" :key="item.to"> <template v-for="(item, index) of model" :key="label(item) + '_' + index.toString()">
<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)"> <li
v-if="visible(item)"
:class="[cx('menuitem', { item, index }), item.class]"
:style="item.style"
@click="onItemClick($event, item, index)"
@keydown="onItemKeydown($event, item, index)"
v-bind="getPTOptions('menuitem', item, index)"
:data-p-highlight="isActive(index)"
:data-p-disabled="isItemDisabled(item, index)"
>
<template v-if="!$slots.item"> <template v-if="!$slots.item">
<router-link v-if="!isItemDisabled(item)" v-slot="{ navigate, href, isActive, isExactActive }" :to="item.to" custom> <span :class="cx('action')" v-bind="getPTOptions('action', item, index)">
<a
:href="href"
:class="cx('action', { isActive, isExactActive })"
:tabindex="-1"
:aria-current="isExactActive ? 'step' : undefined"
@click="onItemClick($event, item, navigate)"
@keydown="onItemKeydown($event, item, navigate)"
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>
</a>
</router-link>
<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('step')" v-bind="getPTOptions('step', item, index)">{{ index + 1 }}</span>
<span :class="cx('label')" v-bind="getPTOptions('label', item, index)">{{ label(item) }}</span> <span :class="cx('label')" v-bind="getPTOptions('label', item, index)">{{ label(item) }}</span>
</span> </span>
@ -38,9 +33,15 @@ import BaseSteps from './BaseSteps.vue';
export default { export default {
name: 'Steps', name: 'Steps',
extends: BaseSteps, extends: BaseSteps,
beforeMount() { emits: ['update:activeStep', 'step-change'],
if (!this.$slots.item) { data() {
console.warn('In future versions, vue-router support will be removed. Item templating should be used.'); return {
d_activeStep: this.activeStep
};
},
watch: {
activeStep(newValue) {
this.d_activeStep = newValue;
} }
}, },
mounted() { mounted() {
@ -54,12 +55,12 @@ export default {
context: { context: {
item, item,
index, index,
active: this.isActive(item), active: this.isActive(index),
disabled: this.isItemDisabled(item) disabled: this.isItemDisabled(item, index)
} }
}); });
}, },
onItemClick(event, item, navigate) { onItemClick(event, item, index) {
if (this.disabled(item) || this.readonly) { if (this.disabled(item) || this.readonly) {
event.preventDefault(); event.preventDefault();
@ -73,11 +74,17 @@ export default {
}); });
} }
if (item.to && navigate) { if (index !== this.d_activeStep) {
navigate(event); this.d_activeStep = index;
this.$emit('update:activeStep', this.d_activeStep);
} }
this.$emit('step-change', {
originalEvent: event,
index: index
});
}, },
onItemKeydown(event, item, navigate) { onItemKeydown(event, item) {
switch (event.code) { switch (event.code) {
case 'ArrowRight': { case 'ArrowRight': {
this.navigateToNextItem(event.target); this.navigateToNextItem(event.target);
@ -110,7 +117,7 @@ export default {
case 'Enter': case 'Enter':
case 'Space': { case 'Space': {
this.onItemClick(event, item, navigate); this.onItemClick(event, item);
event.preventDefault(); event.preventDefault();
break; break;
} }
@ -164,11 +171,11 @@ export default {
focusableItem.tabIndex = '0'; focusableItem.tabIndex = '0';
focusableItem.focus(); focusableItem.focus();
}, },
isActive(item) { isActive(index) {
return item.to ? this.$router.resolve(item.to).path === this.$route.path : false; return index === this.d_activeStep;
}, },
isItemDisabled(item) { isItemDisabled(item, index) {
return this.disabled(item) || (this.readonly && !this.isActive(item)); return this.disabled(item) || (this.readonly && !this.isActive(index));
}, },
visible(item) { visible(item) {
return typeof item.visible === 'function' ? item.visible() : item.visible !== false; return typeof item.visible === 'function' ? item.visible() : item.visible !== false;

View File

@ -27,6 +27,7 @@ const css = `
align-items: center; align-items: center;
overflow: hidden; overflow: hidden;
text-decoration: none; text-decoration: none;
cursor: pointer;
} }
.p-steps.p-steps-readonly .p-steps-item { .p-steps.p-steps-readonly .p-steps-item {
@ -59,20 +60,14 @@ const css = `
const classes = { const classes = {
root: ({ props }) => ['p-steps p-component', { 'p-readonly': props.readonly }], root: ({ props }) => ['p-steps p-component', { 'p-readonly': props.readonly }],
menu: 'p-steps-list', menu: 'p-steps-list',
menuitem: ({ instance, item }) => [ menuitem: ({ instance, item, index }) => [
'p-steps-item', 'p-steps-item',
{ {
'p-highlight p-steps-current': instance.isActive(item), 'p-highlight p-steps-current': instance.isActive(index),
'p-disabled': instance.isItemDisabled(item) 'p-disabled': instance.isItemDisabled(item, index)
}
],
action: ({ props, isActive, isExactActive }) => [
'p-menuitem-link',
{
'router-link-active': isActive,
'router-link-active-exact': props.exact && isExactActive
} }
], ],
action: 'p-menuitem-link',
step: 'p-steps-number', step: 'p-steps-number',
label: 'p-steps-title' label: 'p-steps-title'
}; };