@d1os/simple-wizard

中文文档
1.2.0发布于 2026/03/28MITTypeScriptESM + CJS零依赖近 30 天下载 8733

A simple wizard component for React

原始使用文档
文档目录19 节
  1. Installation
  2. Quick Start
  3. API
  4. <Wizard />
  5. useWizard(selector)
  6. Examples
  7. Async Step Validation
  8. Skip Steps Conditionally
  9. Custom Progress Indicator
  10. Controlled Navigation
  11. Using with Form Libraries
  12. Using react-hook-form
  13. Using @tanstack/react-form
  14. React 19 Best Practices
  15. Using use() for Data Fetching
  16. Optimistic Updates with useOptimistic()
  17. Form Actions with useActionState()
  18. TypeScript
  19. License

simple-wizard

A lightweight, flexible wizard component for React 18 and 19.

Installation

npm install @d1os/simple-wizard
yarn add @d1os/simple-wizard
pnpm add @d1os/simple-wizard

Quick Start

import { Wizard, useWizard } from "@d1os/simple-wizard";

function App() {
  return (
    <Wizard footer={<WizardFooter />}>
      <StepOne />
      <StepTwo />
      <StepThree />
    </Wizard>
  );
}

function WizardFooter() {
  const { nextStep, previousStep, activeStep, stepCount } = useWizard((state) => ({
    nextStep: state.nextStep,
    previousStep: state.previousStep,
    activeStep: state.activeStep,
    stepCount: state.stepCount,
  }));

  const isFirstStep = activeStep === 0;
  const isLastStep = activeStep === stepCount - 1;

  return (
    <div>
      <button onClick={() => previousStep()} disabled={isFirstStep}>
        Back
      </button>
      <button onClick={() => nextStep()} disabled={isLastStep}>
        {isLastStep ? "Finish" : "Next"}
      </button>
    </div>
  );
}

API

<Wizard />

The main container component.

interface WizardProps {
  children?: ReactNode;    // Step components
  header?: ReactNode;      // Rendered before steps
  footer?: ReactNode;      // Rendered after steps
  startAtEnd?: boolean;    // Start at last step (default: false)
  initialStep?: number;    // Starting step index (default: 0)
}

useWizard(selector)

Hook to access wizard state and actions. Uses selectors for optimal re-renders.

const { activeStep, nextStep } = useWizard((state) => ({
  activeStep: state.activeStep,
  nextStep: state.nextStep,
}));

State

PropertyTypeDescription
activeStepnumberCurrent step index (0-based)
stepCountnumberTotal number of steps
isLoadingbooleanLoading state during async actions
nextButtonLabelstringLabel for next button (default: "Next")
previousButtonLabelstringLabel for previous button (default: "Back")
isNextButtonDisabledbooleanWhether next button is disabled

Actions

ActionTypeDescription
nextStep(skip?)(skip?: number) => voidGo to next step (or skip multiple)
previousStep(skip?)(skip?: number) => voidGo to previous step (or skip multiple)
setActiveStep(step)(step: number) => voidJump to specific step
setIsLoading(loading)(loading: boolean) => voidSet loading state
setStepAction(handler)(handler: () => void | Promise<void>) => voidSet action to run before advancing
setNextButtonLabel(label)(label: string) => voidSet next button label
setPreviousButtonLabel(label)(label: string) => voidSet previous button label
setNextButtonDisabled(disabled)(disabled: boolean) => voidDisable/enable next button

Examples

Async Step Validation

Run async validation before advancing to the next step:

function StepWithValidation() {
  const { setStepAction } = useWizard((state) => ({
    setStepAction: state.setStepAction,
  }));

  useEffect(() => {
    setStepAction(async () => {
      await saveFormData();
      // If this throws, navigation is cancelled
    });
  }, [setStepAction]);

  return <form>...</form>;
}

Skip Steps Conditionally

Use the skip prop to conditionally skip steps:

<Wizard>
  <StepOne />
  <StepTwo skip={!showOptionalStep} />
  <StepThree />
</Wizard>

Custom Progress Indicator

function ProgressBar() {
  const { activeStep, stepCount } = useWizard((state) => ({
    activeStep: state.activeStep,
    stepCount: state.stepCount,
  }));

  const progress = ((activeStep + 1) / stepCount) * 100;

  return (
    <div className="progress-bar">
      <div className="progress-fill" style={{ width: `${progress}%` }} />
      <span>{activeStep + 1} of {stepCount}</span>
    </div>
  );
}

// Usage
<Wizard header={<ProgressBar />}>
  ...
</Wizard>

Controlled Navigation

function StepButtons() {
  const { activeStep, stepCount, setActiveStep } = useWizard((state) => ({
    activeStep: state.activeStep,
    stepCount: state.stepCount,
    setActiveStep: state.setActiveStep,
  }));

  return (
    <div className="step-buttons">
      {Array.from({ length: stepCount }, (_, i) => (
        <button
          key={i}
          onClick={() => setActiveStep(i)}
          className={activeStep === i ? "active" : ""}
        >
          {i + 1}
        </button>
      ))}
    </div>
  );
}

Using with Form Libraries

simple-wizard is form-library agnostic. You can use it with any form library you like, such as react-hook-form, formik, @tanstack/react-form, etc.

Use setStepAction() to integrate validation:

Using react-hook-form

import { useForm } from "react-hook-form";

function StepWithValidation() {
  const { register, trigger } = useForm();
  const { setStepAction } = useWizard((state) => ({
    setStepAction: state.setStepAction,
  }));


  useEffect(() => {
    setStepAction(async () => {
      const valid = await trigger();
      if (!valid) {
        throw new Error("Form is invalid");
      }
    });
  }, [setStepAction, trigger]);

  return (
    <input {...register("email", { required: true })} placeholder="Email" />
  );
}

Using @tanstack/react-form

import { useForm } from "@tanstack/react-form";

function StepWithValidation() {
  const form = useForm({
    defaultValues: {
      email: "",
    },
  });
  const { setStepAction } = useWizard((state) => ({
    setStepAction: state.setStepAction,
  }));

  useEffect(() => {
    setStepAction(async () => {
      await form.validateAllFields("change");
      if (!form.state.isFormValid) {
        throw new Error("Form is invalid");
      }
    });
  }, [setStepAction, form]);

  return (
    <form.Field name="email">
      {({ field }) => (
        <input {...field.getInputProps()} placeholder="Email" />
      )}
    </form.Field>
  )
}

A more advanced example using @tanstack/react-form can be found in this stackblitz.

React 19 Best Practices

This library is fully compatible with React 19. Here are patterns to leverage React 19 features in your step components:

Using use() for Data Fetching

import { use, Suspense } from "react";

function StepWithData({ dataPromise }) {
  const data = use(dataPromise);
  return <div>{data.content}</div>;
}

// In your wizard
<Wizard>
  <Suspense fallback={<Loading />}>
    <StepWithData dataPromise={fetchStepData()} />
  </Suspense>
</Wizard>

Optimistic Updates with useOptimistic()

import { useOptimistic, useState } from "react";

function StepWithOptimisticSave() {
  const [saved, setSaved] = useState(false);
  const [optimisticSaved, setOptimisticSaved] = useOptimistic(saved);

  async function handleSave() {
    setOptimisticSaved(true);  // Instant UI feedback
    await saveToServer();
    setSaved(true);
  }

  return (
    <div>
      <button onClick={handleSave}>
        {optimisticSaved ? "Saved!" : "Save"}
      </button>
    </div>
  );
}

Form Actions with useActionState()

import { useActionState } from "react";

function FormStep() {
  const [state, submitAction, isPending] = useActionState(
    async (prevState, formData) => {
      const result = await submitForm(formData);
      return { success: result.ok, error: result.error };
    },
    { success: false, error: null }
  );

  return (
    <form action={submitAction}>
      <input name="email" type="email" required />
      {state.error && <p className="error">{state.error}</p>}
      <button type="submit" disabled={isPending}>
        {isPending ? "Submitting..." : "Continue"}
      </button>
    </form>
  );
}

TypeScript

Full TypeScript support with exported types:

import type {
  WizardProps,
  WizardStore,
  WizardState,
  WizardActions
} from "@d1os/simple-wizard";

License

MIT

版本与发布记录

@d1os/simple-wizard 共发布 3 个版本,首次发布于 2026/01/04;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.2.0latest2026/03/287 个月前
1.1.02026/03/277 个月前
1.0.02026/01/0410 个月前

关于 @d1os/simple-wizard 的常见问题

@d1os/simple-wizard 是什么?

A simple wizard component for React

如何安装 @d1os/simple-wizard?

在项目目录运行 npm install @d1os/simple-wizard,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@d1os/simple-wizard 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@d1os/simple-wizard 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@d1os/simple-wizard 可以商用吗?

@d1os/simple-wizard 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@d1os/simple-wizard 的下载量有多少?

根据 npm 官方下载统计,@d1os/simple-wizard 在 2026/10/11 – 2026/11/09 这 30 天内被下载 8,733 次,日均约 291 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@d1os/simple-wizard 还在维护吗?

最新版本 1.2.0 发布于 2026/03/28(7 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/01/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。