Steps 步骤条
把一个流程拆成几步显示,标出已完成、进行中和未开始的步骤。每一步用一个 HuiStepItem。
只展示流程进度或自行控制页面内容时用 Steps;需要内容面板、上一步/下一步、逐步校验和线性导航时用 Stepper。Stepper 内部复用 Steps 绘制步骤条,两者共用基础外观,分别提供展示和向导入口。
引入
import { HuiSteps, HuiStepItem } from '@nscode/handy-ui'
自持状态与受控回填
active未传或仅监听时内部从0保存当前步骤;传入属性(含undefined)即受控,undefined按0显示。父拒绝更新时当前步骤不变,同值不重复发事件。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSteps, HuiStepItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref(0)
</script>
<template>
<div class="steps-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>未传属性,自行保存</p>
<div class="q1-local">
<HuiSteps clickable
><HuiStepItem title="甲" /><HuiStepItem title="乙" /><HuiStepItem title="丙" disabled
/></HuiSteps>
</div>
</div>
<div>
<p>仅监听更新,仍自行保存</p>
<div class="q1-listener">
<HuiSteps clickable @update:active="q1Last = JSON.stringify($event)"
><HuiStepItem title="甲" /><HuiStepItem title="乙" /><HuiStepItem title="丙" disabled
/></HuiSteps>
</div>
</div>
<div>
<p>显式undefined,父值优先</p>
<div class="q1-empty">
<HuiSteps clickable :active="undefined"
><HuiStepItem title="甲" /><HuiStepItem title="乙" /><HuiStepItem title="丙" disabled
/></HuiSteps>
</div>
</div>
<div>
<p>固定受控,父拒绝更新</p>
<div class="q1-fixed">
<HuiSteps clickable :active="1" @update:active="q1Last = JSON.stringify($event)"
><HuiStepItem title="甲" /><HuiStepItem title="乙" /><HuiStepItem title="丙" disabled
/></HuiSteps>
</div>
</div>
<div>
<p>双向绑定</p>
<div class="q1-bound">
<HuiSteps clickable v-model:active="q1Model"
><HuiStepItem title="甲" /><HuiStepItem title="乙" /><HuiStepItem title="丙" disabled
/></HuiSteps>
</div>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p>
</HuiSpace>
</div>
</template>
基础用法
active 是当前步骤的序号,从 0 开始。序号小于 active 的步骤是已完成,等于的是进行中,大于的是等待。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSteps, HuiStepItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
const active = ref(0)
const prev = () => {
active.value = Math.max(active.value - 1, 0)
}
const next = () => {
active.value = active.value >= 3 ? 0 : active.value + 1
}
</script>
<template>
<HuiSteps :active="active">
<HuiStepItem title="填写信息" description="填写基本资料" />
<HuiStepItem title="验证身份" description="上传证件照片" />
<HuiStepItem title="完成注册" description="注册成功" />
</HuiSteps>
<HuiSpace style="margin-top: 16px">
<HuiButton :disabled="active === 0" @click="prev">上一步</HuiButton>
<HuiButton type="primary" @click="next">下一步</HuiButton>
<span>active = {{ active }}</span>
</HuiSpace>
</template>
active 等于步骤总数时,所有步骤都是已完成。上面点到 active = 3 就能看到。
方向
direction 可选 horizontal(横向,默认)和 vertical(纵向)。纵向时步骤从上往下排,图标在左、文字在右,竖线把相邻的步骤连起来。
<template>
<HuiSpace direction="vertical" fill size="large">
<div>
<p class="row-label">horizontal</p>
<HuiSteps :active="1" direction="horizontal">
<HuiStepItem title="提交申请" description="2026-11-01 10:00" />
<HuiStepItem title="部门审批" description="等待部门经理审批" />
<HuiStepItem title="归档" description="审批通过后自动归档" />
</HuiSteps>
</div>
<div>
<p class="row-label">vertical</p>
<HuiSteps :active="1" direction="vertical">
<HuiStepItem title="提交申请" description="2026-11-01 10:00" />
<HuiStepItem title="部门审批" description="等待部门经理审批" />
<HuiStepItem title="归档" description="审批通过后自动归档" />
</HuiSteps>
</div>
</HuiSpace>
</template>
<style scoped>
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
居中对齐
align-center 让图标、标题和描述在每一步里居中,连接线连在相邻两个图标之间。只对横向步骤条起作用。上面是默认的左对齐,下面是居中:
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSteps :active="1">
<HuiStepItem title="填写信息" description="填写基本资料" />
<HuiStepItem title="验证身份" description="上传证件照片" />
<HuiStepItem title="完成注册" description="注册成功" />
</HuiSteps>
<HuiSteps :active="1" align-center>
<HuiStepItem title="填写信息" description="填写基本资料" />
<HuiStepItem title="验证身份" description="上传证件照片" />
<HuiStepItem title="完成注册" description="注册成功" />
</HuiSteps>
</HuiSpace>
</template>
简洁风格
simple 给整个步骤条加上浅色背景,图标换成更小的 24px 实心圆:未开始是灰底,进行中是主色底,已完成是浅主色底加对勾,success 是成功色底,error 是危险色底。可以和居中对齐、纵向一起用。
<template>
<HuiSpace direction="vertical" fill>
<HuiSteps :active="1" simple>
<HuiStepItem title="选择商品" />
<HuiStepItem title="确认订单" />
<HuiStepItem title="支付" />
</HuiSteps>
<HuiSteps :active="2" simple align-center finish-status="success">
<HuiStepItem title="选择商品" />
<HuiStepItem title="确认订单" />
<HuiStepItem title="支付" />
</HuiSteps>
<HuiSteps :active="1" simple process-status="error">
<HuiStepItem title="选择商品" />
<HuiStepItem title="确认订单" description="库存不足" />
<HuiStepItem title="支付" />
</HuiSteps>
<HuiSteps :active="1" simple direction="vertical">
<HuiStepItem title="选择商品" description="已选 3 件" />
<HuiStepItem title="确认订单" description="请核对收货地址" />
<HuiStepItem title="支付" />
</HuiSteps>
</HuiSpace>
</template>
每一步的尺寸
space 是每一步占的尺寸:横向时是宽度,纵向时是高度。数字按 px 算,字符串原样使用(比如 '25%')。最后一步不受 space 影响,大小等于它的内容。不设 space 时,横向的步骤(最后一步除外)平分剩余宽度,纵向的步骤高度等于内容。
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSteps :active="1" :space="200">
<HuiStepItem title="宽 200px" />
<HuiStepItem title="宽 200px" />
<HuiStepItem title="步骤三" />
</HuiSteps>
<HuiSteps :active="1" space="25%">
<HuiStepItem title="宽 25%" />
<HuiStepItem title="宽 25%" />
<HuiStepItem title="步骤三" />
</HuiSteps>
<HuiSteps :active="1" direction="vertical" space="80px">
<HuiStepItem title="高 80px" />
<HuiStepItem title="高 80px" />
<HuiStepItem title="步骤三" />
</HuiSteps>
</HuiSpace>
</template>
步骤状态
状态有 wait(等待)、process(进行中)、finish(已完成)、error(出错)、success(成功)五种。已完成(finish、success)的步骤图标显示对勾,error 显示叉号,其余显示序号。
HuiStepItem 的 status 可以单独指定某一步的状态,写了以后不再按 active 计算。下面每一步是一种状态:
<template>
<HuiSteps>
<HuiStepItem title="已完成(finish)" status="finish" />
<HuiStepItem title="成功(success)" status="success" />
<HuiStepItem title="出错(error)" status="error" />
<HuiStepItem title="进行中(process)" status="process" />
<HuiStepItem title="等待(wait)" status="wait" />
</HuiSteps>
</template>
finish-status 设置已完成步骤的状态,process-status 设置当前步骤的状态。比如当前步骤出错了,把 process-status 设成 error。
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSteps :active="1" process-status="error">
<HuiStepItem title="提交" />
<HuiStepItem title="审核" description="审核不通过" />
<HuiStepItem title="完成" />
</HuiSteps>
<HuiSteps :active="2" finish-status="success">
<HuiStepItem title="提交" />
<HuiStepItem title="审核" />
<HuiStepItem title="完成" />
</HuiSteps>
<HuiSteps :active="1">
<HuiStepItem title="下单" />
<HuiStepItem title="发货" status="error" description="仓库缺货,单独指定 status" />
<HuiStepItem title="签收" />
</HuiSteps>
</HuiSpace>
</template>
两个属性都可以取五种状态中的任意一种,切换看看:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRadio, HuiRadioGroup, HuiSpace, HuiStepItem, HuiSteps } from '@nscode/handy-ui'
import type { StepsStatus } from '@nscode/handy-ui'
const statuses: StepsStatus[] = ['wait', 'process', 'finish', 'error', 'success']
const finishStatus = ref<StepsStatus>('finish')
const processStatus = ref<StepsStatus>('process')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap>
<span class="row-label">finish-status</span>
<HuiRadioGroup v-model="finishStatus" type="button" size="small">
<HuiRadio v-for="item in statuses" :key="item" :value="item">{{ item }}</HuiRadio>
</HuiRadioGroup>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">process-status</span>
<HuiRadioGroup v-model="processStatus" type="button" size="small">
<HuiRadio v-for="item in statuses" :key="item" :value="item">{{ item }}</HuiRadio>
</HuiRadioGroup>
</HuiSpace>
<HuiSteps :active="2" :finish-status="finishStatus" :process-status="processStatus">
<HuiStepItem title="第一步" description="已完成" />
<HuiStepItem title="第二步" description="已完成" />
<HuiStepItem title="第三步" description="当前步骤" />
<HuiStepItem title="第四步" description="等待" />
</HuiSteps>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 104px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
自定义图标
icon 传图标名(HuiIcon 的 name)或者 VNode,图标会代替序号和状态图标。需要更多控制时用 icon 插槽。
<script setup lang="ts">
import { h } from 'vue'
import { HuiSteps, HuiStepItem, HuiIcon } from '@nscode/handy-ui'
// icon 也可以传 VNode
const payIcon = h(HuiIcon, { name: 'star' })
</script>
<template>
<HuiSteps :active="1">
<HuiStepItem title="登录" icon="user" />
<HuiStepItem title="编辑" icon="edit" />
<HuiStepItem title="上传">
<template #icon>
<HuiIcon name="upload" />
</template>
</HuiStepItem>
<HuiStepItem title="评价" :icon="payIcon" />
</HuiSteps>
</template>
自定义标题和描述
title 和 description 插槽可以放任意内容。没写 description 属性也没写 description 插槽时,不渲染描述区域。
<template>
<HuiSteps :active="1">
<HuiStepItem>
<template #title>
<strong>提交申请</strong>
</template>
<template #description>
<span style="color: var(--hui-color-success)">已于 11 月 1 日提交</span>
</template>
</HuiStepItem>
<HuiStepItem title="审批中" description="预计 3 个工作日" />
<HuiStepItem title="完成" />
</HuiSteps>
</template>
点击切换
clickable 为 true 时,点另一个未禁用步骤会依次触发 update:active 和 change,参数是被点步骤的序号。可以用 v-model:active 自动接收更新,也可以像下面这样用 change 更新受控值。横向、纵向都可以点。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSteps, HuiStepItem, HuiSpace } from '@nscode/handy-ui'
const active = ref(0)
const handleChange = (index: number) => {
active.value = index
}
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSteps :active="active" clickable @change="handleChange">
<HuiStepItem title="基本信息" />
<HuiStepItem title="联系方式" />
<HuiStepItem title="确认提交" />
</HuiSteps>
<HuiSteps :active="active" direction="vertical" clickable @change="handleChange">
<HuiStepItem title="基本信息" description="点我切换" />
<HuiStepItem title="联系方式" description="点我切换" />
<HuiStepItem title="确认提交" description="点我切换" />
</HuiSteps>
</HuiSpace>
<p style="margin-top: 12px">change 返回的序号:{{ active }}</p>
</template>
可点击且未禁用的步骤能用 Tab 键聚焦,聚焦后按回车或空格等同于点击。
双向绑定和禁用步骤
v-model:active 同步当前步骤;HuiStepItem 的 disabled 阻止鼠标和键盘切换。下面通过 maxReached 限制只能点击已走过的步骤,点“下一步”解锁后面的步骤。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiStepItem, HuiSteps } from '@nscode/handy-ui'
const titles = ['基本信息', '联系方式', '确认提交']
const active = ref(0)
const maxReached = ref(0)
const lastChange = ref('尚未切换')
const next = () => {
active.value = Math.min(active.value + 1, titles.length - 1)
maxReached.value = Math.max(maxReached.value, active.value)
}
const onChange = (index: number) => {
lastChange.value = `change:${index}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSteps v-model:active="active" clickable @change="onChange">
<HuiStepItem
v-for="(title, index) in titles"
:key="title"
:title="title"
:disabled="index > maxReached"
/>
</HuiSteps>
<HuiSpace>
<HuiButton type="primary" :disabled="active === titles.length - 1" @click="next">
下一步
</HuiButton>
<span>当前序号:{{ active }};已走到:{{ maxReached }}</span>
</HuiSpace>
<span>{{ lastChange }}</span>
</HuiSpace>
</template>
点击当前步骤不重复触发事件;外部直接修改 active 只更新显示,不触发 update:active 或 change。禁用步骤保留原来的状态颜色,不进入 Tab 聚焦顺序。
动态增删步骤
步骤可以用 v-for、v-if 动态生成。增删或重新排序后,序号和状态按步骤在页面上的顺序重新计算。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSteps, HuiStepItem, HuiButton } from '@nscode/handy-ui'
const needReview = ref(true)
</script>
<template>
<HuiSteps :active="1">
<HuiStepItem title="提交" />
<HuiStepItem v-if="needReview" title="复核" />
<HuiStepItem title="审批" />
<HuiStepItem title="完成" />
</HuiSteps>
<HuiButton style="margin-top: 16px" @click="needReview = !needReview">
{{ needReview ? '去掉复核' : '加上复核' }}
</HuiButton>
</template>
读取步骤信息
HuiSteps 通过 ref 暴露 steps(当前所有步骤,顺序和页面上一致)和 active。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiStepItem, HuiSteps } from '@nscode/handy-ui'
const stepsRef = ref<InstanceType<typeof HuiSteps>>()
const info = ref('')
const read = () => {
const steps = stepsRef.value?.steps ?? []
const titles = steps.map((step) => step.title).join('、')
info.value = `共 ${steps.length} 步:${titles};当前序号 ${stepsRef.value?.active}`
}
</script>
<template>
<HuiSteps ref="stepsRef" :active="1">
<HuiStepItem title="提交" />
<HuiStepItem title="审批" />
<HuiStepItem title="完成" />
</HuiSteps>
<HuiSpace style="margin-top: 16px">
<HuiButton @click="read">读取步骤信息</HuiButton>
<span>{{ info }}</span>
</HuiSpace>
</template>
HuiSteps Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| active / v-model:active | number | undefined | 当前步骤的序号,从 0 开始,可双向绑定 |
| space | number | string | - | 每一步的尺寸,横向是宽度、纵向是高度。数字按 px,字符串原样使用。最后一步不受影响 |
| direction | 'horizontal' | 'vertical' | 'horizontal' | 排列方向 |
| alignCenter | boolean | false | 每一步的内容是否居中,只对横向起作用 |
| simple | boolean | false | 是否使用简洁风格 |
| finishStatus | 'wait' | 'process' | 'finish' | 'error' | 'success' | 'finish' | 已完成步骤的状态 |
| processStatus | 'wait' | 'process' | 'finish' | 'error' | 'success' | 'process' | 当前步骤的状态 |
| clickable | boolean | false | 为 true 时,操作另一个未禁用步骤依次触发 update:active、change,可用键盘聚焦 |
HuiStepItem Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '' | 标题。写了 title 插槽时不显示 |
| description | string | '' | 描述。写了 description 插槽时不显示 |
| icon | string | VNode | - | 图标,字符串是 HuiIcon 的 name。设了以后代替序号和状态图标 |
| status | 'wait' | 'process' | 'finish' | 'error' | 'success' | - | 这一步的状态。设了以后不再按 active 计算 |
| disabled | boolean | false | 禁用该步骤,不响应点击、回车或空格,也不能用 Tab 聚焦 |
HuiSteps Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:active | (index: number) | clickable 时操作另一个未禁用步骤触发,用于 v-model:active,序号从 0 开始 |
| change | (index: number) | 与 update:active 同时触发;点击当前步骤、禁用步骤或外部改 active 不触发 |
HuiSteps Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| default | - | 放 HuiStepItem |
HuiStepItem Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| title | - | 标题 |
| description | - | 描述 |
| icon | - | 图标,代替序号、状态图标和 icon 属性 |
HuiStepItem 没有默认插槽,描述只能用 description 属性或 description 插槽。
HuiSteps 暴露的属性
通过组件的 ref 读取,只用来读:
| 属性 | 类型 | 说明 |
|---|---|---|
| steps | StepItemInstance[] | 当前所有步骤的信息,顺序和页面上的顺序一致 |
| active | number | 实际当前步骤序号;受控时遵从父值,未绑定时读内部值 |
类型
import type {
StepsProps,
StepItemProps,
StepsDirection,
StepsStatus,
StepItemInstance
} from '@nscode/handy-ui'
import type { VNode } from 'vue'
type StepsDirection = 'horizontal' | 'vertical'
type StepsStatus = 'wait' | 'process' | 'finish' | 'error' | 'success'
interface StepsProps {
active?: number
space?: number | string
direction?: StepsDirection
alignCenter?: boolean
simple?: boolean
finishStatus?: StepsStatus
processStatus?: StepsStatus
clickable?: boolean
}
interface StepItemProps {
title?: string
description?: string
icon?: string | VNode
status?: StepsStatus
disabled?: boolean
}
/** ref 上 steps 里的每一项 */
interface StepItemInstance {
uid: number
/** 步骤的根元素 */
getEl: () => HTMLElement | undefined
readonly title: string
readonly description: string
readonly icon: string | VNode | undefined
readonly status: StepsStatus | undefined
readonly disabled: boolean
}
另外还导出了 StepsContext 和注入用的 STEPS_INJECTION_KEY,自己写和 HuiSteps 配合的子组件时才用得到。