Stepper 分步向导
把步骤条、面板和导航按钮放在一起,支持逐步校验、禁用步骤和懒渲染。每个面板用 HuiStepperPanel 声明。
只展示订单、审批等流程进度时使用 Steps。Stepper 在 Steps 的基础上组合内容面板和导航,适合分步填写;步骤条直接复用 Steps,外观和基础交互共用。
引入
import { HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
自持状态与受控回填
未传active或仅监听更新时内部从0保存;传入属性(含undefined)即受控,undefined按0显示。父拒绝导航时实际面板、懒挂载和已走过范围不前进。next()返回true表示校验及请求放行,最终当前步骤仍由父回填;reset()同样请求父更新,保留输入内容。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiStepper, HuiStepperPanel, HuiInput, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref(0)
const q1Bound = ref<InstanceType<typeof HuiStepper>>()
</script>
<template>
<div class="stepper-q1-demo">
<HuiSpace
><HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton
><HuiButton class="q1-reset" @click="q1Bound?.reset()">重置双向绑定步骤</HuiButton></HuiSpace
><HuiSpace direction="vertical" fill>
<div>
<p>未传属性,自行保存</p>
<div class="q1-local">
<HuiStepper lazy
><HuiStepperPanel title="甲"><HuiInput placeholder="步骤内草稿" /></HuiStepperPanel
><HuiStepperPanel title="乙">乙内容</HuiStepperPanel
><HuiStepperPanel title="丙">丙内容</HuiStepperPanel></HuiStepper
>
</div>
</div>
<div>
<p>仅监听更新,仍自行保存</p>
<div class="q1-listener">
<HuiStepper lazy @update:active="q1Last = String($event)"
><HuiStepperPanel title="甲"><HuiInput placeholder="步骤内草稿" /></HuiStepperPanel
><HuiStepperPanel title="乙">乙内容</HuiStepperPanel
><HuiStepperPanel title="丙">丙内容</HuiStepperPanel></HuiStepper
>
</div>
</div>
<div>
<p>显式undefined,父值优先</p>
<div class="q1-empty">
<HuiStepper lazy :active="undefined"
><HuiStepperPanel title="甲"><HuiInput placeholder="步骤内草稿" /></HuiStepperPanel
><HuiStepperPanel title="乙">乙内容</HuiStepperPanel
><HuiStepperPanel title="丙">丙内容</HuiStepperPanel></HuiStepper
>
</div>
</div>
<div>
<p>固定受控,父拒绝更新</p>
<div class="q1-fixed">
<HuiStepper lazy :active="1" @update:active="q1Last = String($event)"
><HuiStepperPanel title="甲"><HuiInput placeholder="步骤内草稿" /></HuiStepperPanel
><HuiStepperPanel title="乙">乙内容</HuiStepperPanel
><HuiStepperPanel title="丙">丙内容</HuiStepperPanel></HuiStepper
>
</div>
</div>
<div>
<p>双向绑定</p>
<div class="q1-bound">
<HuiStepper lazy ref="q1Bound" v-model:active="q1Model"
><HuiStepperPanel title="甲"><HuiInput placeholder="步骤内草稿" /></HuiStepperPanel
><HuiStepperPanel title="乙">乙内容</HuiStepperPanel
><HuiStepperPanel title="丙">丙内容</HuiStepperPanel></HuiStepper
>
</div>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p></HuiSpace
>
</div>
</template>
基础用法
不传 active 时组件自行管理步骤。默认只能点击已经走过的步骤,前进要点“下一步”。最后一个可用步骤显示“完成”,验证通过后触发 finish。
<script setup lang="ts">
import { HuiInput, HuiMessage, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
const finish = () => HuiMessage.success('流程已完成')
</script>
<template>
<HuiStepper @finish="finish">
<HuiStepperPanel title="基本信息" description="填写个人资料">
<HuiInput placeholder="姓名,切换步骤后保留" />
</HuiStepperPanel>
<HuiStepperPanel title="上传附件" description="准备材料">
<p>请检查所需材料是否齐全。</p>
</HuiStepperPanel>
<HuiStepperPanel title="确认提交" description="核对信息">
<p>核对无误后点“完成”。</p>
</HuiStepperPanel>
</HuiStepper>
</template>
双向绑定和切换事件
v-model:active 使用从 0 开始的序号。导航使步骤变化时,先发 update:active,再发 change(新序号, { previous: 旧序号 })。外部直接修改 active 不调用 before-next,也不触发这两个事件,适合恢复草稿进度。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
const active = ref(0)
const event = ref('尚未切换')
const onChange = (next: number, { previous }: { previous: number }) => {
event.value = `change:${previous} → ${next}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiStepper v-model:active="active" @change="onChange" @finish="event = 'finish:完成'">
<HuiStepperPanel title="资料"><p>填写基本资料。</p></HuiStepperPanel>
<HuiStepperPanel title="复核"><p>复核填写结果。</p></HuiStepperPanel>
<HuiStepperPanel title="提交"><p>确认并完成。</p></HuiStepperPanel>
</HuiStepper>
<HuiSpace>
<HuiButton @click="active = 2">从草稿恢复到第 3 步</HuiButton>
<span>当前序号:{{ active }};{{ event }}</span>
</HuiSpace>
</HuiSpace>
</template>
接表单校验
before-next 接收当前序号。返回 false、抛错或 Promise 拒绝时停在原步骤;返回 true 或不返回值则继续。只在前进和完成时校验,回退不校验。
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiForm,
HuiFormItem,
HuiInput,
HuiMessage,
HuiStepper,
HuiStepperPanel
} from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = ref({ name: '', phone: '' })
const rules = {
name: [{ required: true, message: '请填写姓名', trigger: 'blur' }],
phone: [{ required: true, message: '请填写手机号', trigger: 'blur' }]
}
const validate = async (index: number) => {
if (index === 0) return (await formRef.value!.validate()).valid
}
</script>
<template>
<HuiStepper :before-next="validate" @finish="HuiMessage.success('资料验证通过')">
<HuiStepperPanel title="基本信息">
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px">
<HuiFormItem label="姓名" prop="name"><HuiInput v-model="model.name" /></HuiFormItem>
<HuiFormItem label="手机号" prop="phone"><HuiInput v-model="model.phone" /></HuiFormItem>
</HuiForm>
</HuiStepperPanel>
<HuiStepperPanel title="确认">
<p>姓名:{{ model.name }};手机号:{{ model.phone }}</p>
</HuiStepperPanel>
</HuiStepper>
</template>
异步验证和等待状态
返回 Promise 时,下一步或完成按钮显示加载状态,导航按钮、步骤条和导航方法暂时锁定。重复点下一步只调用一次验证。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiStepper, HuiStepperPanel, HuiSwitch } from '@nscode/handy-ui'
const allowed = ref(true)
const result = ref('等待操作')
const validate = async (index: number) => {
result.value = `正在验证第 ${index + 1} 步`
await new Promise((resolve) => setTimeout(resolve, 1000))
result.value = allowed.value ? '验证通过' : '验证失败,停留当前步骤'
return allowed.value
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><span>允许通过</span><HuiSwitch v-model="allowed" /></HuiSpace>
<HuiStepper :before-next="validate" @finish="result = '全部完成'">
<HuiStepperPanel title="资料"><p>点下一步,等待 1 秒。</p></HuiStepperPanel>
<HuiStepperPanel title="提交"><p>点完成时也会验证。</p></HuiStepperPanel>
</HuiStepper>
<span>{{ result }}</span>
</HuiSpace>
</template>
等待期间外部改变 active、当前面板被移除、禁用或被换到别的位置、调用 reset()、组件卸载或被 KeepAlive 缓存失活,会取消这次导航。调整其他面板不取消“下一步”,验证通过后按最新面板查找下一可用步骤;goTo(index) 则要求原目标仍在该位置且可用。旧 Promise 结束后不会切换新的流程,也不会解除新一轮等待。验证请求本身由业务决定是否取消。
等待会话与取消结果
beforeNext 整条业务回调(包括回调内的 form.validate)结束前不接受重复前进。undefined 放行;false、抛错和拒绝拦截,错误在开发环境输出带 Stepper.beforeNext 的诊断。重置、外部 active 变化或当前面板失效后,等待结果不能继续导航,业务请求本身仍可结束;导航应用前到达的父回填优先。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
import type { StepperExpose } from '@nscode/handy-ui'
interface WaitingStep {
id: number
index: number
resolve: (value: boolean | void) => void
}
const wizard = ref<StepperExpose>()
const active = ref(0)
const pending = ref<WaitingStep[]>([])
const outcome = ref('尚未请求')
let seed = 0
const beforeNext = (index: number) =>
new Promise<boolean | void>((resolve) => {
pending.value.push({ id: ++seed, index, resolve })
})
const start = async () => {
outcome.value = '导航结果: ' + (await wizard.value!.next())
}
const release = (request: WaitingStep, allowed: boolean) => {
pending.value = pending.value.filter((item) => item.id !== request.id)
request.resolve(allowed ? undefined : false)
}
</script>
<template>
<HuiSpace direction="vertical" fill class="stepper-guard-demo">
<HuiSpace wrap>
<HuiButton @click="start">发起下一步</HuiButton>
<HuiButton @click="wizard?.reset()">reset并作废等待</HuiButton>
<HuiButton @click="active = 2">外部切到第三步</HuiButton>
</HuiSpace>
<HuiStepper ref="wizard" v-model:active="active" :before-next="beforeNext">
<HuiStepperPanel title="资料"><p>资料</p></HuiStepperPanel>
<HuiStepperPanel title="复核"><p>复核</p></HuiStepperPanel>
<HuiStepperPanel title="提交"><p>提交</p></HuiStepperPanel>
</HuiStepper>
<HuiSpace v-for="request in pending" :key="request.id" wrap>
<span>#{{ request.id }} / {{ request.index }}</span>
<HuiButton @click="release(request, true)">允许(undefined)</HuiButton>
<HuiButton @click="release(request, false)">拦截(false)</HuiButton>
</HuiSpace>
<span class="stepper-guard-result">当前步骤: {{ active }}; {{ outcome }}</span>
<span>当前序号从0开始。先发起、reset,再放行旧请求,旧结果不改变步骤。</span>
</HuiSpace>
</template>
校验后按条件跳过步骤
before-next 可以根据业务结果禁用后续面板。同步或异步验证都只调用一次,点一次下一步就进入最新的下一可用步骤。下面的开关决定是否跳过“补充材料”。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiStepper, HuiStepperPanel, HuiSwitch } from '@nscode/handy-ui'
const stepperRef = ref<InstanceType<typeof HuiStepper>>()
const skip = ref(true)
const asynchronous = ref(false)
const disabled = ref(false)
const calls = ref(0)
const validate = (index: number) => {
calls.value++
const apply = () => {
if (index === 0) disabled.value = skip.value
return true
}
return asynchronous.value
? new Promise<boolean>((resolve) => setTimeout(() => resolve(apply()), 500))
: apply()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><span>跳过补充材料</span><HuiSwitch v-model="skip" /><span>等待接口</span
><HuiSwitch v-model="asynchronous" /><HuiButton @click="stepperRef?.reset()"
>重置进度再试</HuiButton
></HuiSpace
>
<HuiStepper ref="stepperRef" :before-next="validate">
<HuiStepperPanel title="基本信息"><p>点一次下一步,根据结果决定去哪里。</p></HuiStepperPanel>
<HuiStepperPanel title="补充材料" :disabled="disabled"><p>仅需要时填写。</p></HuiStepperPanel>
<HuiStepperPanel title="确认提交"><p>验证后已跳过不需要的面板。</p></HuiStepperPanel>
</HuiStepper>
<span>验证调用 {{ calls }} 次</span>
</HuiSpace>
</template>
非线性和禁用步骤
:linear="false" 允许直接点击任意未禁用步骤。往后点仍然只验证当前步骤。禁用的面板由上一步和下一步跳过;最后一个面板禁用时,前一个可用面板就是完成步骤。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiStepper, HuiStepperPanel, HuiSwitch } from '@nscode/handy-ui'
const disabled = ref(true)
const message = ref('可直接点击第 3 步')
const validate = (index: number) => {
message.value = `验证当前第 ${index + 1} 步后前进`
return true
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><span>禁用中间一步</span><HuiSwitch v-model="disabled" /></HuiSpace>
<HuiStepper :linear="false" :before-next="validate" @finish="message = '完成'">
<HuiStepperPanel title="资料"><p>填写资料。</p></HuiStepperPanel>
<HuiStepperPanel title="可选复核" :disabled="disabled"
><p>这个步骤可以禁用。</p></HuiStepperPanel
>
<HuiStepperPanel title="提交"><p>提交前确认。</p></HuiStepperPanel>
</HuiStepper>
<span>{{ message }}</span>
</HuiSpace>
</template>
方向、居中和简洁风格
direction="horizontal" 是默认值,步骤条在面板上方;vertical 时在左侧。align-center 只影响横向步骤条,simple 使用简洁风格。
<script setup lang="ts">
import { HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<div>
<p>horizontal,默认左对齐</p>
<HuiStepper direction="horizontal">
<HuiStepperPanel title="资料"><p>普通样式。</p></HuiStepperPanel>
<HuiStepperPanel title="完成"><p>确认完成。</p></HuiStepperPanel>
</HuiStepper>
</div>
<div>
<p>align-center + simple</p>
<HuiStepper align-center simple>
<HuiStepperPanel title="资料"><p>居中、简洁样式。</p></HuiStepperPanel>
<HuiStepperPanel title="完成"><p>确认完成。</p></HuiStepperPanel>
</HuiStepper>
</div>
<div>
<p>vertical</p>
<HuiStepper direction="vertical">
<HuiStepperPanel title="资料" description="填写资料"><p>步骤条在左边。</p></HuiStepperPanel>
<HuiStepperPanel title="完成" description="确认结果"><p>确认完成。</p></HuiStepperPanel>
</HuiStepper>
</div>
</HuiSpace>
</template>
标题、描述、图标和状态
面板的 title 同时作为步骤标题和面板的读屏名称。description、图标名或 VNode 通过步骤条显示。status 可以覆盖自动状态,支持 wait、process、finish、error、success。
<script setup lang="ts">
import { h } from 'vue'
import { HuiIcon, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
const star = h(HuiIcon, { name: 'star' })
</script>
<template>
<HuiStepper :active="2" :linear="false">
<HuiStepperPanel title="等待" description="wait" status="wait" icon="user"
><p>等待状态。</p></HuiStepperPanel
>
<HuiStepperPanel title="处理中" description="process" status="process"
><p>处理状态。</p></HuiStepperPanel
>
<HuiStepperPanel title="已完成" description="finish" status="finish"
><p>完成状态。</p></HuiStepperPanel
>
<HuiStepperPanel title="有错误" description="error" status="error"
><p>错误状态由业务指定。</p></HuiStepperPanel
>
<HuiStepperPanel title="成功" description="success + VNode 图标" status="success" :icon="star"
><p>成功状态。</p></HuiStepperPanel
>
</HuiStepper>
</template>
自定义按钮和操作插槽
三个文字属性覆盖语言包,空字符串也是显式覆盖。actions 插槽可接管按钮区,获取当前步骤、首尾状态、加载状态以及导航函数。即使 show-actions="false",显式提供的插槽仍会显示。
<script setup lang="ts">
import { HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
</script>
<template>
<HuiStepper
prev-button-text="返回修改"
next-button-text="保存并继续"
finish-button-text="确认提交"
>
<HuiStepperPanel title="填写"><p>点“保存并继续”前进。</p></HuiStepperPanel>
<HuiStepperPanel title="核对"><p>按钮变成“确认提交”。</p></HuiStepperPanel>
</HuiStepper>
</template>
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
</script>
<template>
<HuiStepper :show-actions="false" @finish="HuiMessage.success('已提交')">
<HuiStepperPanel title="填写"><p>自定义操作区。</p></HuiStepperPanel>
<HuiStepperPanel title="提交"><p>确认并提交。</p></HuiStepperPanel>
<template #actions="{ active, isFirst, isLast, loading, prev, next }">
<HuiSpace>
<span>当前第 {{ active + 1 }} 步</span>
<HuiButton :disabled="loading" @click="HuiMessage.success('草稿已保存')"
>保存草稿</HuiButton
>
<HuiButton :disabled="isFirst || loading" @click="prev">返回</HuiButton>
<HuiButton type="primary" :loading="loading" @click="next">{{
isLast ? '提交' : '继续'
}}</HuiButton>
</HuiSpace>
</template>
</HuiStepper>
</template>
懒渲染和内容保留
默认立即挂载全部面板,只隐藏非当前面板。lazy 时第一次走到才挂载内容,离开后保留。下面每个面板显示首次渲染时间,返回后时间和输入值都不变。
<script setup lang="ts">
import { defineComponent, h } from 'vue'
import { HuiInput, HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
const RenderTime = defineComponent({
setup() {
const time = new Date().toLocaleTimeString()
return () => h('p', `首次渲染时间:${time}`)
}
})
</script>
<template>
<HuiStepper lazy>
<HuiStepperPanel title="第一步">
<HuiSpace direction="vertical" fill
><RenderTime /><HuiInput placeholder="填入内容后切换再返回"
/></HuiSpace>
</HuiStepperPanel>
<HuiStepperPanel title="第二步">
<HuiSpace direction="vertical" fill
><RenderTime /><HuiInput placeholder="本面板第一次显示时才挂载"
/></HuiSpace>
</HuiStepperPanel>
<HuiStepperPanel title="第三步">
<HuiSpace direction="vertical" fill
><RenderTime />
<p>返回可查看之前的状态。</p></HuiSpace
>
</HuiStepperPanel>
</HuiStepper>
</template>
方法和隐藏内置按钮
show-actions="false" 可隐藏内置按钮,由外部按钮调用方法。goTo 在默认线性模式下只能访问已经走过的范围,next 才能解锁新步骤。reset() 回到第一个可用步骤并重新锁住后续步骤,保留面板内容和已挂载的组件。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiSpace, HuiStepper, HuiStepperPanel } from '@nscode/handy-ui'
const stepperRef = ref<InstanceType<typeof HuiStepper>>()
const active = ref(0)
const result = ref('用 next 解锁步骤,再尝试 goTo')
const next = async () => {
result.value = `next:${await stepperRef.value!.next()}`
}
const jump = async () => {
result.value = `goTo(2):${await stepperRef.value!.goTo(2)}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiStepper
ref="stepperRef"
v-model:active="active"
:show-actions="false"
@finish="result = 'finish:完成'"
>
<HuiStepperPanel title="一"
><HuiInput placeholder="reset 不会清除此处内容"
/></HuiStepperPanel>
<HuiStepperPanel title="二"><p>第二步。</p></HuiStepperPanel>
<HuiStepperPanel title="三"><p>第三步。</p></HuiStepperPanel>
</HuiStepper>
<HuiSpace wrap>
<HuiButton @click="stepperRef?.prev()">prev()</HuiButton>
<HuiButton @click="next">next()</HuiButton>
<HuiButton @click="jump">goTo(2)</HuiButton>
<HuiButton @click="stepperRef?.reset()">reset()</HuiButton>
<span>当前序号:{{ active }};{{ result }}</span>
</HuiSpace>
</HuiSpace>
</template>
HuiStepper Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| active / v-model:active | number | undefined | 当前序号,不传时自行管理;外部赋值绕过校验 未传内部初始0,传入即受控。 |
| linear | boolean | true | 步骤条和 goTo 只能访问已走过的范围 |
| beforeNext | (active: number) => boolean | void | Promise<boolean | void> | — | 独占前进和完成前验证;undefined 放行,false、抛错或拒绝拦截,过期结果忽略 |
| direction | 'horizontal' | 'vertical' | 'horizontal' | 步骤条在上方或左侧 |
| alignCenter | boolean | false | 横向步骤内容居中 |
| simple | boolean | false | 简洁步骤条 |
| lazy | boolean | false | 面板内容首次激活才渲染,之后保留 |
| showActions | boolean | true | 显示内置按钮;不影响显式 actions 插槽 |
| prevButtonText | string | 语言包“上一步” | 上一步文字,空字符串保留 |
| nextButtonText | string | 语言包“下一步” | 下一步文字,空字符串保留 |
| finishButtonText | string | 语言包“完成” | 完成文字,空字符串保留 |
HuiStepperPanel Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '' | 步骤标题和面板读屏名称,建议填写 |
| description | string | '' | 步骤描述 |
| icon | string | VNode | — | 步骤图标名或 VNode |
| status | StepsStatus | 按 active 计算 | wait、process、finish、error、success |
| disabled | boolean | false | 禁止选择该面板,前后导航自动跳过 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| update:active | (active: number) | 内部导航改变步骤时触发 |
| change | (active: number, context: StepperChangeContext) | 随后通知新序号,context.previous是旧序号 |
| finish | () | 最后一个可用步骤验证通过后触发 |
Slots
| 组件 | 插槽 | 作用域 | 说明 |
|---|---|---|---|
| HuiStepper | default | — | 放置 HuiStepperPanel |
| HuiStepper | actions | StepperActionsSlotProps | 自定义操作区 |
| HuiStepperPanel | default | — | 面板内容 |
方法和暴露属性
| 名称 | 类型 | 说明 |
|---|---|---|
| next | () => Promise<boolean> | 前进或完成;验证通过且导航未失效时返回 true |
| prev | () => void | 回到上一个可用步骤,不验证 |
| goTo | (index: number) => Promise<boolean> | 跳到指定可用步骤;前进会验证,线性模式受已走过范围限制 |
| reset | () => void | 取消等待,回到首个可用步骤并重置前进范围,保留内容 |
| active | number | 当前内部序号,只读 |
类型
import type {
StepperProps,
StepperEmits,
StepperChangeContext,
StepperPanelProps,
StepperActionsSlotProps,
StepperExpose,
StepperPanelInstance,
StepperContext
} from '@nscode/handy-ui'
interface StepperChangeContext {
previous: number
}
interface StepperActionsSlotProps {
active: number
isFirst: boolean
isLast: boolean
loading: boolean
prev: () => void
next: () => Promise<boolean>
}
注入键 STEPPER_INJECTION_KEY 也公开导出,供需要向向导登记的自定义面板使用。