|
| 1 | +// CStepper.ts |
| 2 | +import { |
| 3 | + defineComponent, |
| 4 | + h, |
| 5 | + ref, |
| 6 | + watch, |
| 7 | + computed, |
| 8 | + nextTick, |
| 9 | + onMounted, |
| 10 | + toRefs, |
| 11 | + shallowRef, |
| 12 | + watchEffect, |
| 13 | +} from 'vue' |
| 14 | +import { CCollapse } from '../collapse' |
| 15 | +import type { StepperStepData, StepperStepValidationResult } from './types' |
| 16 | + |
| 17 | +export const CStepper = defineComponent({ |
| 18 | + name: 'CStepper', |
| 19 | + inheritAttrs: false, |
| 20 | + props: { |
| 21 | + modelValue: Number, |
| 22 | + defaultActiveStepIndex: { |
| 23 | + type: Number, |
| 24 | + default: 0, |
| 25 | + }, |
| 26 | + layout: { |
| 27 | + type: String as () => 'horizontal' | 'vertical', |
| 28 | + default: 'horizontal', |
| 29 | + }, |
| 30 | + linear: { |
| 31 | + type: Boolean, |
| 32 | + default: true, |
| 33 | + }, |
| 34 | + steps: { |
| 35 | + type: Array as () => StepperStepData[], |
| 36 | + required: true, |
| 37 | + }, |
| 38 | + stepButtonLayout: { |
| 39 | + type: String as () => 'horizontal' | 'vertical', |
| 40 | + default: 'horizontal', |
| 41 | + }, |
| 42 | + validation: { |
| 43 | + type: Boolean, |
| 44 | + default: true, |
| 45 | + }, |
| 46 | + id: String, |
| 47 | + }, |
| 48 | + emits: ['update:modelValue', 'finish', 'reset', 'stepChange', 'stepValidationComplete'], |
| 49 | + setup(props, { emit, slots, attrs, expose }) { |
| 50 | + const { modelValue, defaultActiveStepIndex, validation, steps, layout, linear } = toRefs(props) |
| 51 | + |
| 52 | + const activeStepIndex = ref<number>(modelValue.value ?? defaultActiveStepIndex.value ?? 0) |
| 53 | + const isControlled = computed(() => modelValue.value !== undefined) |
| 54 | + const isFinished = ref(false) |
| 55 | + const stepsRef = ref<HTMLOListElement | null>(null) |
| 56 | + const stepButtonRefs = shallowRef<(HTMLButtonElement | null)[]>([]) |
| 57 | + |
| 58 | + watch(modelValue, (val) => { |
| 59 | + if (val !== undefined) activeStepIndex.value = val |
| 60 | + }) |
| 61 | + |
| 62 | + watch(activeStepIndex, (val) => { |
| 63 | + if (isControlled.value) emit('update:modelValue', val) |
| 64 | + }) |
| 65 | + |
| 66 | + const isStepValid = (index: number): boolean => { |
| 67 | + if (!validation.value) return true |
| 68 | + |
| 69 | + const form = steps.value[index]?.formRef?.value |
| 70 | + if (!form) return true |
| 71 | + |
| 72 | + const valid = form.checkValidity() |
| 73 | + emit('stepValidationComplete', { stepNumber: index + 1, isValid: valid }) |
| 74 | + if (!valid) form.reportValidity() |
| 75 | + return valid |
| 76 | + } |
| 77 | + |
| 78 | + const setActiveStep = (index: number, bypassValidation = false) => { |
| 79 | + if (index < 0 || index >= steps.value.length || index === activeStepIndex.value) return |
| 80 | + if (!bypassValidation && index > activeStepIndex.value && !isStepValid(activeStepIndex.value)) |
| 81 | + return |
| 82 | + |
| 83 | + activeStepIndex.value = index |
| 84 | + emit('stepChange', index + 1) |
| 85 | + } |
| 86 | + |
| 87 | + const next = () => { |
| 88 | + if (activeStepIndex.value < steps.value.length - 1) { |
| 89 | + setActiveStep(activeStepIndex.value + 1) |
| 90 | + } else { |
| 91 | + finish() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const prev = () => { |
| 96 | + if (activeStepIndex.value > 0) { |
| 97 | + setActiveStep(activeStepIndex.value - 1, true) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + const finish = () => { |
| 102 | + if (activeStepIndex.value === steps.value.length - 1 && isStepValid(activeStepIndex.value)) { |
| 103 | + isFinished.value = true |
| 104 | + emit('finish') |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const reset = () => { |
| 109 | + if (validation.value) { |
| 110 | + steps.value.forEach((s) => s.formRef?.value?.reset?.()) |
| 111 | + } |
| 112 | + activeStepIndex.value = defaultActiveStepIndex.value |
| 113 | + isFinished.value = false |
| 114 | + emit('reset') |
| 115 | + emit('stepChange', defaultActiveStepIndex.value) |
| 116 | + nextTick(() => { |
| 117 | + stepButtonRefs.value[defaultActiveStepIndex.value]?.focus() |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 122 | + const buttons = stepButtonRefs.value |
| 123 | + const current = event.target as HTMLButtonElement |
| 124 | + const index = buttons.findIndex((b) => b === current) |
| 125 | + if (index === -1) return |
| 126 | + |
| 127 | + let nextIndex = index |
| 128 | + switch (event.key) { |
| 129 | + case 'ArrowRight': |
| 130 | + case 'ArrowDown': |
| 131 | + nextIndex = (index + 1) % buttons.length |
| 132 | + break |
| 133 | + case 'ArrowLeft': |
| 134 | + case 'ArrowUp': |
| 135 | + nextIndex = (index - 1 + buttons.length) % buttons.length |
| 136 | + break |
| 137 | + case 'Home': |
| 138 | + nextIndex = 0 |
| 139 | + break |
| 140 | + case 'End': |
| 141 | + nextIndex = buttons.length - 1 |
| 142 | + break |
| 143 | + default: |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + event.preventDefault() |
| 148 | + buttons[nextIndex]?.focus() |
| 149 | + } |
| 150 | + |
| 151 | + expose({ next, prev, finish, reset }) |
| 152 | + |
| 153 | + return () => { |
| 154 | + const isVertical = layout.value === 'vertical' |
| 155 | + stepButtonRefs.value = [] |
| 156 | + |
| 157 | + return h( |
| 158 | + 'div', |
| 159 | + { |
| 160 | + ...attrs, |
| 161 | + class: ['stepper', { 'stepper-vertical': isVertical }, attrs.class], |
| 162 | + }, |
| 163 | + [ |
| 164 | + h( |
| 165 | + 'ol', |
| 166 | + { |
| 167 | + class: 'stepper-steps', |
| 168 | + role: 'tablist', |
| 169 | + 'aria-orientation': isVertical ? 'vertical' : 'horizontal', |
| 170 | + onKeydown: handleKeyDown, |
| 171 | + ref: stepsRef, |
| 172 | + }, |
| 173 | + steps.value.map((step, index) => { |
| 174 | + const isActive = !isFinished.value && index === activeStepIndex.value |
| 175 | + const isComplete = isFinished.value || index < activeStepIndex.value |
| 176 | + const isDisabled = |
| 177 | + isFinished.value || (linear.value && index > activeStepIndex.value + 1) |
| 178 | + const stepId = `step-${props.id || 'stepper'}-${index}` |
| 179 | + const panelId = `panel-${props.id || 'stepper'}-${index}` |
| 180 | + |
| 181 | + return h( |
| 182 | + 'li', |
| 183 | + { |
| 184 | + key: index, |
| 185 | + class: ['stepper-step', props.stepButtonLayout], |
| 186 | + role: 'presentation', |
| 187 | + }, |
| 188 | + [ |
| 189 | + h( |
| 190 | + 'button', |
| 191 | + { |
| 192 | + type: 'button', |
| 193 | + class: ['stepper-step-button', { active: isActive, complete: isComplete }], |
| 194 | + disabled: isDisabled, |
| 195 | + id: stepId, |
| 196 | + role: 'tab', |
| 197 | + 'aria-selected': isActive, |
| 198 | + tabindex: isActive ? 0 : -1, |
| 199 | + 'aria-controls': step.content ? panelId : undefined, |
| 200 | + onClick: () => |
| 201 | + setActiveStep(index, !linear.value || index <= activeStepIndex.value), |
| 202 | + ref: (el) => (stepButtonRefs.value[index] = el as HTMLButtonElement), |
| 203 | + }, |
| 204 | + [ |
| 205 | + h('span', { class: 'stepper-step-indicator' }, [ |
| 206 | + isComplete |
| 207 | + ? h('span', { class: 'stepper-step-indicator-icon' }) |
| 208 | + : h( |
| 209 | + 'span', |
| 210 | + { class: 'stepper-step-indicator-text' }, |
| 211 | + step.indicator ?? index + 1 |
| 212 | + ), |
| 213 | + ]), |
| 214 | + h('span', { class: 'stepper-step-label' }, step.label), |
| 215 | + ] |
| 216 | + ), |
| 217 | + index < steps.value.length - 1 && h('div', { class: 'stepper-step-connector' }), |
| 218 | + step.content && |
| 219 | + isVertical && |
| 220 | + h( |
| 221 | + CCollapse, |
| 222 | + { |
| 223 | + class: 'stepper-step-content', |
| 224 | + id: panelId, |
| 225 | + role: 'tabpanel', |
| 226 | + visible: isActive, |
| 227 | + 'aria-hidden': !isActive, |
| 228 | + 'aria-labelledby': stepId, |
| 229 | + 'aria-live': 'polite', |
| 230 | + }, |
| 231 | + () => step.content |
| 232 | + ), |
| 233 | + ] |
| 234 | + ) |
| 235 | + }) |
| 236 | + ), |
| 237 | + !isVertical && |
| 238 | + steps.value.some((s) => s.content != null) && |
| 239 | + h( |
| 240 | + 'div', |
| 241 | + { class: 'stepper-content' }, |
| 242 | + steps.value.map((step, index) => { |
| 243 | + const isActive = !isFinished.value && index === activeStepIndex.value |
| 244 | + const stepId = `step-${props.id || 'stepper'}-${index}` |
| 245 | + const panelId = `panel-${props.id || 'stepper'}-${index}` |
| 246 | + |
| 247 | + return h( |
| 248 | + 'div', |
| 249 | + { |
| 250 | + key: index, |
| 251 | + id: panelId, |
| 252 | + role: 'tabpanel', |
| 253 | + 'aria-hidden': !isActive, |
| 254 | + 'aria-labelledby': stepId, |
| 255 | + 'aria-live': 'polite', |
| 256 | + class: ['stepper-pane', { active: isActive, show: isActive }], |
| 257 | + }, |
| 258 | + step.content |
| 259 | + ) |
| 260 | + }) |
| 261 | + ), |
| 262 | + ] |
| 263 | + ) |
| 264 | + } |
| 265 | + }, |
| 266 | +}) |
0 commit comments