Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 347 add disable previous steps to stepper component #348

Merged
merged 10 commits into from
Dec 10, 2024
29 changes: 29 additions & 0 deletions src/components/Stepper/MucStepper.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,32 @@ export const Default = {
activeItem: "2",
},
};

export const DisablePreviousSteps = {
args: {
stepItems: [
{
id: "1",
label: "Order",
icon: "shopping-cart",
},
{
id: "2",
label: "Delivery",
icon: "calendar",
},
{
id: "3",
label: "Contact",
icon: "mail",
},
{
id: "4",
label: "Overview",
icon: "information",
},
],
activeItem: "2",
disablePreviousSteps: true,
},
};
27 changes: 22 additions & 5 deletions src/components/Stepper/MucStepper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
:item="item"
:is-active="isActive(item.id)"
:is-done="isDone(item.id)"
v-on:click="handleChange"
:disabled="disabled(item.id)"
@click="handleChange"
/>
</template>
</ol>
Expand All @@ -28,7 +29,11 @@ import { ref, watch } from "vue";
import MucStepperItem from "./MucStepperItem.vue";
import { StepperItem } from "./MucStepperTypes";

const { stepItems, activeItem } = defineProps<{
const {
stepItems,
activeItem,
disablePreviousSteps = false,
} = defineProps<{
/**
* List of items displayed in the stepper
*/
Expand All @@ -38,15 +43,19 @@ const { stepItems, activeItem } = defineProps<{
* Id of the current step item
*/
activeItem: string;

/**
* Disables the previous steps
*/
disablePreviousSteps?: boolean;
}>();

const emit = defineEmits<{
/**
* Triggered when an item is clicked.
* @param e Click-Event
* @param id
* @param id of the clicked item
*/
(e: "changeStep", id: string): void;
changeStep: [id: string];
}>();

watch(
Expand Down Expand Up @@ -84,6 +93,14 @@ const isActive = (id: string) => id === activeItem;
*/
const isDone = (id: string) => getIndexOfItem(id) < indexOfActivItem.value;

/**
* Checks if an item is disabled
* @param id id of the item
* @return if item is disabled
*/
const disabled = (id: string) =>
disablePreviousSteps && !isActive(id) && isDone(id);

/**
* Handles a click on an item
* @param id id of the item
Expand Down
51 changes: 29 additions & 22 deletions src/components/Stepper/MucStepperItem.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<template>
<li
:class="isActiveStep"
class="m-form-step"
:class="{
'm-form-step--current': isActive,
'show-cursor': isDone && !disabled && !isActive,
}"
@click="handleClick"
>
<div
class="m-form-step__icon"
:class="{ disabled: disabled }"
:tabindex="getTabindex"
:aria-label="getAriaLabel"
>
<MucIcon :icon="getIcon" />
</div>
<div class="m-form-step__title">
<div
class="m-form-step__title"
:class="{ disabled: disabled }"
>
<span aria-disabled="true"> {{ item.label }}</span>
</div>
</li>
Expand All @@ -22,7 +30,7 @@ import { computed } from "vue";
import { MucIcon } from "../Icon";
import { StepperItem } from "./MucStepperTypes";

const { item, isActive, isDone } = defineProps<{
const { item, isActive, isDone, disabled } = defineProps<{
/**
* Individual item to display inside the MucStepper component
*/
Expand All @@ -37,31 +45,21 @@ const { item, isActive, isDone } = defineProps<{
* Show stepper as done
*/
isDone: boolean;

/**
* Disabled stepper
*/
disabled: boolean;
}>();

const emit = defineEmits<{
/**
* Triggered when an item is clicked.
* @param e Click-Event
* @param id
* @param id of the clicked item
*/
(e: "click", id: string): void;
click: [id: string];
}>();

/**
* Computed property set active state of step
*/
const isActiveStep = computed(() =>
isActive
? "m-form-step m-form-step--current"
: "m-form-step" + clickableStep.value
);

/**
* Computed property show courser on clickable step
*/
const clickableStep = computed(() => (isDone ? " show-cursor" : ""));

/**
* Computed property set icon of step
*/
Expand All @@ -70,7 +68,9 @@ const getIcon = computed(() => (isDone ? "check" : item.icon));
/**
* Computed property set tabindex
*/
const getTabindex = computed(() => (isActive || isDone ? 0 : -1));
const getTabindex = computed(() =>
isActive || (isDone && !disabled) ? 0 : -1
);

/**
* Computed property set aria-label
Expand All @@ -82,12 +82,19 @@ const getAriaLabel = computed(() =>
);

const handleClick = () => {
if (isDone) emit("click", item.id);
if (isDone && !disabled) {
emit("click", item.id);
}
};
</script>

<style scoped>
.show-cursor {
cursor: pointer;
}

.disabled {
color: #9ca8b3;
border-color: #9ca8b3;
}
</style>
Loading