Stepper wizard

Form split across steps, each step validates its own fields

  • StepperWizard.tsx
  • AccountStep.tsx
  • ProfileStep.tsx
  • SummaryStep.tsx
  • types.ts
import { useState } from 'react';
import { Button, Group, Stepper, useMatches } from '@mantine/core';
import { hasLength, isEmail, isNotEmpty, useForm } from '@mantine/form';
import { notifications } from '@mantine/notifications';
import { AccountStep } from './AccountStep';
import { ProfileStep } from './ProfileStep';
import { SummaryStep } from './SummaryStep';
import { WizardValues } from './types';

const STEP_FIELDS: (keyof WizardValues)[][] = [
  ['username', 'password'],
  ['name', 'email'],
];

function createAccount(values: WizardValues) {
  return new Promise<WizardValues>((resolve) => {
    setTimeout(() => resolve(values), 1000);
  });
}

export function StepperWizard() {
  const [active, setActive] = useState(0);
  const mobile = useMatches({ base: true, xs: false });

  const form = useForm<WizardValues>({
    initialValues: {
      username: '',
      password: '',
      name: '',
      email: '',
    },
    validate: {
      username: hasLength({ min: 3 }, 'Username must be at least 3 characters'),
      password: hasLength({ min: 8 }, 'Password must be at least 8 characters'),
      name: isNotEmpty('Name is required'),
      email: isEmail('Invalid email'),
    },
  });

  const nextStep = () => {
    // Validate every field of the current step so all errors are shown at once
    const invalidFields = STEP_FIELDS[active].filter((field) => form.validateField(field).hasError);
    if (invalidFields.length > 0) {
      form.focusInput(invalidFields[0]);
    } else {
      setActive((current) => current + 1);
    }
  };

  const prevStep = () => setActive((current) => current - 1);

  const handleSubmit = async (values: WizardValues) => {
    const user = await createAccount(values);
    notifications.show({
      title: 'Account created',
      message: `${user.name} (${user.email}) can now sign in as ${user.username}`,
    });
  };

  return (
    <form onSubmit={form.onSubmit(handleSubmit)}>
      <Stepper active={active} size="sm" labelPosition={mobile ? 'bottom' : 'right'} wrap={!mobile}>
        <Stepper.Step label="Account" description={!mobile && 'Username and password'}>
          <AccountStep form={form} />
        </Stepper.Step>

        <Stepper.Step label="Profile" description={!mobile && 'Name and email'}>
          <ProfileStep form={form} />
        </Stepper.Step>

        <Stepper.Step label="Done" description={!mobile && 'Review and submit'}>
          <SummaryStep form={form} />
        </Stepper.Step>
      </Stepper>

      <Group justify="flex-end" mt="xl">
        {active > 0 && (
          <Button size="md" variant="default" onClick={prevStep} disabled={form.submitting}>
            Back
          </Button>
        )}
        {active < 2 && (
          <Button size="md" onClick={nextStep}>
            Next
          </Button>
        )}
        {active === 2 && (
          <Button size="md" type="submit" loading={form.submitting}>
            Create account
          </Button>
        )}
      </Group>
    </form>
  );
}