fn

useStepper

v0.0.14testeddemo

A composable for building wizards/steppers over a list or record of steps

Examples

ts
const { current, goToNext, isLast } = useStepper(['first', 'second', 'last']);
ts
const { current, stepNames, goTo } = useStepper({
  account: { title: 'Account' },
  billing: { title: 'Billing' },
});

Demo

Loading demo…

Signatures

ts
export function useStepper<T extends string | number>(
  steps: MaybeRef<T[]>,
  initialStep?: T,
): UseStepperReturn<T, T[], T>;
ts
export function useStepper<T extends Record<string, unknown>>(
  steps: MaybeRef<T>,
  initialStep?: keyof T,
): UseStepperReturn<Exclude<keyof T, symbol>, T, T[keyof T]>;

Parameters

ParameterTypeDescription
stepsMaybeRef<StepName[] | Record<string, StepValue>>The list of steps, or a record keyed by step name
initialStep?StepNameThe step to start on (defaults to the first step)

Returns

UseStepperReturn<StepName, StepName[] | Record<string, StepValue>, StepValue>The stepper state and navigation helpers
PropertyTypeDescription
stepsReadonly<Ref<Steps>>List of steps.
stepNamesReadonly<Ref<StepName[]>>List of step names.
indexRef<number>Index of the current step.
currentComputedRef<Step>Current step.
nextComputedRef<StepName | undefined>Next step name, or undefined if the current step is the last one.
previousComputedRef<StepName | undefined>Previous step name, or undefined if the current step is the first one.
isFirstComputedRef<boolean>Whether the current step is the first one.
isLastComputedRef<boolean>Whether the current step is the last one.
at(index: number) => Step | undefinedGet the step at the specified index.
get(step: StepName) => Step | undefinedGet a step by the specified name.
goTo(step: StepName) => voidGo to the specified step.
goToNext() => voidGo to the next step. Does nothing if the current step is the last one.
goToPrevious() => voidGo to the previous step. Does nothing if the current step is the first one.
goBackTo(step: StepName) => voidGo back to the given step, only if the current step is after it.
isNext(step: StepName) => booleanChecks whether the given step is the next step.
isPrevious(step: StepName) => booleanChecks whether the given step is the previous step.
isCurrent(step: StepName) => booleanChecks whether the given step is the current step.
isBefore(step: StepName) => booleanChecks if the current step is before the given step.
isAfter(step: StepName) => booleanChecks if the current step is after the given step.