# Steps

> 按步骤组织任务，明确当前位置、校验反馈与返回路径。

网页来源：/components/steps

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

## 推进、完成与重新开始

默认 linear 逐步推进；完成过的位置可从标题返回，未来位置通过 next 前进。索引从 0 开始，items.length 表示流程结束；complete 仅通知到达结束位置，不代表服务端业务已成功。actions 插槽自由组合已有 Button，不限定操作顺序。

### 示例：steps/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { nextTick, ref } from 'vue';
import { QuireSteps, QuireButton } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
const finished = ref(0);
const host = ref<HTMLElement>();
/** Transfers focus when finishing removes the Next button. */
async function finish(): Promise<void> { finished.value++; await nextTick(); host.value?.querySelector<HTMLElement>('[data-completed-heading]')?.focus(); }
/** @param reset - Public Ark-backed navigation reset; host drafts are not implicitly cleared. */
async function restart(reset:()=>void): Promise<void> { reset(); await nextTick(); host.value?.querySelector<HTMLElement>('[data-state=open] p')?.focus(); }
</script>
<template><div ref="host" data-steps-basic class="grid gap-4 p-qk-surface"><QuireSteps label="文档发布流程" :items="items" @complete="finish"><template #content="{item}"><p tabindex="-1" class="m-0">当前任务：{{ item.title }}</p></template><template #completed><p data-completed-heading tabindex="-1" class="m-0">流程已走完，请确认宿主提交结果。</p></template><template #actions="{next,previous,reset,canNext,canPrevious,completed}"><div class="flex flex-wrap gap-qk-inline"><QuireButton :disabled="!canPrevious" @click="previous">上一步</QuireButton><QuireButton color="primary" variant="solid" v-if="!completed" :disabled="!canNext" @click="next">下一步</QuireButton><QuireButton v-else @click="restart(reset)">重新开始</QuireButton></div></template></QuireSteps><output class="text-qk-helper text-qk-text-secondary">结束通知 {{ finished }} 次</output></div></template>
```

## 自由选择与受控状态

`linear=false` 允许选择任意可用步骤；v-model 由宿主管理。标题是原生按钮，使用 Tab 移动焦点，Enter/Space 激活。步骤导航不冒充页签，不承诺方向键切换。

### 示例：steps/nonlinear

文件：`nonlinear.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireSteps, QuireButton } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
const index = ref(0);
</script>
<template><div data-steps-nonlinear class="grid gap-4 p-qk-surface"><QuireSteps v-model="index" label="自由检查流程" :items="items" :linear="false"><template #content="{item}"><p class="m-0">正在检查：{{ item.title }}</p></template></QuireSteps><QuireButton @click="index=0">返回第一步</QuireButton><output>当前索引 {{ index }}</output></div></template>
```

## 校验与保留草稿

isStepValid 是同步判断；不通过时触发 invalid 并停留，error 文本提供恢复说明。示例聚焦未填写的输入，成功后聚焦新标题。所有步骤内容保持挂载，仅隐藏非当前区域，后退不会因为内容销毁而丢失草稿。

### 示例：steps/validation

文件：`validation.vue`

```vue
<script setup lang="ts">
import { computed, nextTick, ref } from 'vue';
import { QuireSteps, QuireButton, QuireInput, QuireField } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
const index = ref(0), name = ref(''), error = ref('');
const host = ref<HTMLElement>();
const steps = computed(() => items.map((item,i)=>({...item,...(i===0 && error.value ? {error:error.value} : {})})));
/** @param step - Current task. @returns Whether synchronous input is valid. */
function valid(step:number): boolean { return step !== 0 || !!name.value.trim(); }
/** Focuses the retained invalid field after its recovery message becomes visible. */
async function invalid(): Promise<void> { error.value='请填写文档名称后继续'; await nextTick(); host.value?.querySelector<HTMLInputElement>('input')?.focus(); }
/** @param value - Accepted new position; focus follows the host's new task heading. */
async function change(value:number): Promise<void> { index.value=value; error.value=''; await nextTick(); requestAnimationFrame(() => host.value?.querySelector<HTMLElement>(`[data-step-heading="${value}"]`)?.focus()); }
</script>
<template><div ref="host" data-steps-validation class="p-qk-surface"><QuireSteps :model-value="index" label="带校验的发布流程" :items="steps" :is-step-valid="valid" @update:model-value="change" @invalid="invalid"><template #content="{item,index:step}"><h3 :data-step-heading="step" tabindex="-1" class="m-0 mb-qk-inline">{{item.title}}</h3><QuireField v-if="step===0" label="文档名称" :invalid="!!error" :error-text="error" width="100%"><template #default="{controlProps}"><QuireInput v-bind="controlProps" v-model="name" width="100%" /></template></QuireField><p v-else class="m-0">待发布：{{name}}</p></template><template #actions="{next,previous,canNext,canPrevious}"><div class="flex gap-qk-inline"><QuireButton :disabled="!canPrevious" @click="previous">上一步</QuireButton><QuireButton :disabled="!canNext" @click="next">验证并继续</QuireButton></div></template></QuireSteps></div></template>
```

## 异步失败与恢复

宿主执行异步校验或提交，busy 期间暂停导航。成功后显式更新位置；失败保持当前任务与恢复说明。组件不发请求，不自动判定业务成功，也不把步骤数解释为后台处理百分比。

### 示例：steps/async

文件：`async.vue`

```vue
<script setup lang="ts">
import { computed, onUnmounted, ref } from 'vue';
import { QuireSteps, QuireButton, QuireSwitch } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
const index=ref(0), busy=ref(false), fail=ref(true), message=ref('等待校验');
const errors=ref('');
const steps=computed(()=>items.map((item,i)=>({...item,...(i===index.value && errors.value ? {error:errors.value} : {})})));
let timer:ReturnType<typeof setTimeout>|undefined;
/** Host-owned async validation; failure preserves this position and its task content. */
function proceed():void { if(busy.value)return; busy.value=true;errors.value='';message.value='正在校验';timer=setTimeout(()=>{busy.value=false;if(fail.value){errors.value='校验暂时失败，请重试';message.value=errors.value;}else{index.value++;message.value='校验成功';}},350); }
onUnmounted(()=>clearTimeout(timer));
</script>
<template><div data-steps-async class="grid gap-4 p-qk-surface"><QuireSwitch v-model:checked="fail" label="模拟校验失败" /><QuireSteps v-model="index" label="异步校验流程" :items="steps" :busy="busy"><template #content="{item}"><p class="m-0">{{item.title}}：草稿由宿主保存</p></template><template #actions="{previous,canPrevious,completed}"><div class="flex gap-qk-inline"><QuireButton :disabled="!canPrevious" @click="previous">上一步</QuireButton><QuireButton :disabled="busy || completed" @click="proceed">校验后继续</QuireButton></div></template></QuireSteps><output>{{message}}</output></div></template>
```

## 禁用与可用路径

整组 disabled 暂停所有导航；单项 disabled 保留标题及 disabledReason，但不能进入。下一位置不可用时不会自动跳过；宿主负责安排合法流程。权限是业务规则，步骤状态不是权限校验替代品。

### 示例：steps/disabled

文件：`disabled.vue`

```vue
<script setup lang="ts">
import { computed, ref } from 'vue';
import { QuireSteps, QuireButton, QuireSwitch } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
const disabled=ref(false), blocked=ref(true);
const steps=computed(()=>items.map((item,i)=>({...item, disabled:blocked.value && i===1, disabledReason:'需要审核权限，未自动跳过此步骤'})));
</script>
<template><div data-steps-disabled class="grid gap-4 p-qk-surface"><div class="flex flex-wrap gap-4"><QuireSwitch v-model:checked="disabled" label="禁用流程导航" /><QuireSwitch v-model:checked="blocked" label="禁用审核步骤" /></div><QuireSteps label="权限受限流程" :items="steps" :disabled="disabled" :linear="false"><template #content="{item}"><p class="m-0">{{item.title}}</p></template><template #actions="{next,canNext}"><QuireButton :disabled="!canNext" @click="next">下一步</QuireButton></template></QuireSteps></div></template>
```

## 方向与密度

`density="compact|comfortable"` 调整步骤间距和指示器，操作按钮仍使用自己的 `size`。`orientation="vertical"` 适合窄侧栏或长说明；横向空间不足时可滚动。

### 示例：steps/density

文件：`density.vue`

```vue
<script setup lang="ts">
import { QuireSteps } from '@quirekit/ui/vue';
import type { QuireKitStepsItem } from '@quirekit/ui/vue';

const items: readonly QuireKitStepsItem[] = [
  { key: 'details', title: '填写信息', description: '确认文档名称与用途' },
  { key: 'review', title: '检查内容', description: '核对本次修改' },
  { key: 'publish', title: '确认发布', description: '提交后由宿主呈现结果' }
];
</script>
<template><div data-steps-density class="grid gap-qk-group p-qk-surface"><section v-for="density in ['compact','comfortable'] as const" :key="density"><h3 class="m-0 mb-qk-surface">{{density==='compact'?'紧凑流程':'常规流程'}}</h3><QuireSteps :label="density" :items="items" :density="density" :default-value="1" /></section><section><h3 class="m-0 mb-qk-surface">垂直流程</h3><QuireSteps label="垂直流程" :items="items" orientation="vertical" :default-value="1" /></section></div></template>
```

## 长内容、动态数据与阅读方向

名称和说明完整换行，RTL 由 LocaleProvider 和宿主方向共同控制。空数组呈现空态，不报告完成；索引越界时投影到有效范围。items 使用稳定 key，重排时宿主同步更新当前索引，避免相同位置被误当作同一任务。

### 示例：steps/long-content

文件：`long-content.vue`

```vue
<script setup lang="ts">
import { computed, ref } from 'vue';
import { LocaleProvider } from '@ark-ui/vue/locale';
import { QuireSteps, QuireButton, QuireSwitch } from '@quirekit/ui/vue';
const rtl=ref(false), empty=ref(false),short=ref(false),index=ref(2);
const items=computed(()=>empty.value?[]:[{key:'first',title:'ReviewLongDocumentMetadataAndPublishingPermissions',description:'长名称与详细说明保持完整；窄屏使用垂直流程，不缩小文字。'},{key:'second',title:'确认数据与协作者的访问范围'},{key:'third',title:'完成并查看结果'}].slice(0,short.value?1:3));
</script>
<template><LocaleProvider :locale="rtl?'ar':'zh-CN'"><div data-steps-long :dir="rtl?'rtl':'ltr'" class="grid gap-4 p-qk-surface"><div class="flex flex-wrap gap-4"><QuireSwitch v-model:checked="rtl" label="从右向左" /><QuireSwitch v-model:checked="empty" label="空步骤" /><QuireSwitch v-model:checked="short" label="缩短步骤" /></div><QuireSteps v-model="index" label="长内容流程" :items="items" orientation="vertical" :linear="false"><template #content="{item}"><p class="m-0">{{item.title}}</p></template><template #actions="{reset,completed}"><QuireButton :disabled="empty" @click="reset">{{completed?'重新开始':'返回开始'}}</QuireButton></template></QuireSteps></div></LocaleProvider></template>
```

## API

### QuireSteps

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| label | 是 | — | Accessible name of the ordered process. |
| items | 是 | — | Ordered steps; hosts synchronize the index when reordering. |
| modelValue | 否 | — | Zero-based current position; items.length is the completed position. |
| defaultValue | 否 | 0 | Initial uncontrolled position. |
| linear | 否 | true | Future steps require next(); completed steps can be revisited. |
| orientation | 否 | "horizontal" | Layout direction, selected by the host for its available space. |
| density | 否 | "comfortable" | Indicator and process spacing, separate from Button size. |
| disabled | 否 | false | Disable all navigation. |
| busy | 否 | false | Pause navigation while the host validates or submits asynchronously. |
| isStepValid | 否 | — | Synchronous current-step validity checked by Ark before forward navigation. |

**label**

```ts
string
```

**items**

```ts
readonly QuireKitStepsItem[]
```

```ts
readonly ({
  /** Stable identity, unique within the sequence. */
  key: string;
  /** Visible step name. */
  title: string;
  /** Supporting task guidance. */
  description?: undefined | string;
  /** Visible error and recovery guidance, independent of position. */
  error?: string | undefined;
  /** Prevent navigation to this step; never implicitly skip it. */
  disabled?: boolean | undefined;
  /** Visible explanation of the unavailable step. */
  disabledReason?: string | undefined;
})[]
```

**modelValue**

```ts
number | undefined
```

```ts
undefined | number
```

**defaultValue**

```ts
number | undefined
```

```ts
undefined | number
```

**linear**

```ts
boolean | undefined
```

**orientation**

```ts
"horizontal" | "vertical" | undefined
```

```ts
undefined | "horizontal" | "vertical"
```

**density**

```ts
QuireKitStepsDensity | undefined
```

```ts
undefined | "comfortable" | "compact"
```

**disabled**

```ts
boolean | undefined
```

**busy**

```ts
boolean | undefined
```

**isStepValid**

```ts
((index: number) => boolean) | undefined
```

```ts
undefined | { (index: number): boolean; }
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:modelValue | 否 | — |  |
| complete | 否 | — |  |
| invalid | 否 | — |  |

**update:modelValue**

```ts
[index: number]
```

**complete**

```ts
[]
```

**invalid**

```ts
[index: number]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| indicator | 否 | — | Static indicator content inside the title button. |
| content | 否 | — | Mounted content for every step, hidden while inactive. |
| completed | 否 | — | Final task result; hosts arrange focus after successful submission. |
| actions | 否 | — | Compose existing Buttons freely using the public controls. |

**indicator**

```ts
StepsItemContext
```

```ts
{
  /** Original host item. */
  item: {
    /** Stable identity, unique within the sequence. */
    key: string;
    /** Visible step name. */
    title: string;
    /** Supporting task guidance. */
    description?: undefined | string;
    /** Visible error and recovery guidance, independent of position. */
    error?: string | undefined;
    /** Prevent navigation to this step; never implicitly skip it. */
    disabled?: boolean | undefined;
    /** Visible explanation of the unavailable step. */
    disabledReason?: string | undefined;
  };
  /** Zero-based item position. */
  index: number;
  /** Whether this is the currently visible step. */
  current: boolean;
  /** Whether this position precedes the current step. */
  completed: boolean;
}
```

**content**

```ts
StepsItemContext
```

```ts
{
  /** Original host item. */
  item: {
    /** Stable identity, unique within the sequence. */
    key: string;
    /** Visible step name. */
    title: string;
    /** Supporting task guidance. */
    description?: undefined | string;
    /** Visible error and recovery guidance, independent of position. */
    error?: string | undefined;
    /** Prevent navigation to this step; never implicitly skip it. */
    disabled?: boolean | undefined;
    /** Visible explanation of the unavailable step. */
    disabledReason?: string | undefined;
  };
  /** Zero-based item position. */
  index: number;
  /** Whether this is the currently visible step. */
  current: boolean;
  /** Whether this position precedes the current step. */
  completed: boolean;
}
```

**completed**

```ts
any
```

**actions**

```ts
StepsActions
```

```ts
{
  /** Current normalized position. */
  index: number;
  /** Whether the next position is available; validation runs upon activation. */
  canNext: boolean;
  /** Whether the previous position is available. */
  canPrevious: boolean;
  /** Whether a nonempty sequence reached its ending position. */
  completed: boolean;
  /** Request the next step or the completed position. */
  next: { (): void; };
  /** Request the previous position without discarding drafts. */
  previous: { (): void; };
  /** Request a permitted title-navigation target. */
  goTo: { (index: number): void; };
  /** Return to the first step; the host owns clearing business data. */
  reset: { (): void; };
}
```

#### 公开实例成员

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| next | 否 | — |  |
| previous | 否 | — |  |
| goTo | 否 | — |  |
| reset | 否 | — |  |
| canNext | 否 | — |  |
| canPrevious | 否 | — |  |
| completed | 否 | — |  |

**next**

```ts
() => void
```

```ts
{ (): void; }
```

**previous**

```ts
() => void
```

```ts
{ (): void; }
```

**goTo**

```ts
(index: number) => void
```

```ts
{ (index: number): void; }
```

**reset**

```ts
() => void
```

```ts
{ (): void; }
```

**canNext**

```ts
boolean
```

**canPrevious**

```ts
boolean
```

**completed**

```ts
boolean
```

#### 相关类型

```ts
type QuireKitStepsItem = {
  /** Stable identity, unique within the sequence. */
  key: string;
  /** Visible step name. */
  title: string;
  /** Supporting task guidance. */
  description?: undefined | string;
  /** Visible error and recovery guidance, independent of position. */
  error?: string | undefined;
  /** Prevent navigation to this step; never implicitly skip it. */
  disabled?: boolean | undefined;
  /** Visible explanation of the unavailable step. */
  disabledReason?: string | undefined;
};
```
