1

I am new to Ract and building a multi step form in Next.js, where I also use Context. So my project structure is pretty wild and I don't get how / where to change the steps.status when moving to next step in the stepper. So far I have my context, managing half of states, basically the formData, but also the 'original' state of my stepper:

import { useState, createContext, useContext } from "react";

export const FormContext = createContext();

export default function FormProvider({ children }) {
  const [data, setData] = useState({});
  const [steps, setSteps] = useState([
    { name: 'Vertrag', status: 'current' },
    { name: 'Dateneingabe', status: 'upcoming' },
    { name: 'Bestätigung', status: 'upcoming' },
  ]);

  
  const setFormValues = (values) => {
    setData((prevValues) => ({
      ...prevValues,
      ...values,
    }));
  };

  return (
    <FormContext.Provider value={{ steps, data, setFormValues }}>
      {children}
    </FormContext.Provider>
  );
}

export const useFormData = () => useContext(FormContext);

In Stepper.js I therefore import my formData:

import { CheckIcon } from '@heroicons/react/solid'
import { useContext } from 'react'
import { FormContext } from "../context";
// status: 'complete', 'current', 'upcoming'


function classNames(...classes) {
  return classes.filter(Boolean).join(' ')
}

export default function Stepper() {
const formData = useContext(FormContext);
const steps = formData.steps

 
  return (
    <nav aria-label="Progress">
      <div className="flex items-center flex-col">
        <ol className="flex items-center sm:flex-col md:flex-row mx-auto mt-32 mb-8">
          {steps.map((step, stepIdx) => (
            <li key={step.name} className={classNames(stepIdx !== steps.length - 1 ? 'pr-16 sm:pr-32' : '', 'relative')}>
              {step.status === 'complete' ? (
                <>
                  <div className="absolute inset-0 flex items-center" aria-hidden="true">
                    <div className="h-0.5 w-full bg-yellow-500" />
                  </div>
                  <a
                    href="#"
                    className="relative w-8 h-8 flex items-center justify-center bg-yellow-500 rounded-full hover:bg-yellow-500"
                  >
                    <span className="h-9 flex flex-col items-center">
                      <span className="relative top-2 z-10 w-8 h-8 flex items-center justify-center rounded-full group-hover:bg-indigo-800">
                        <CheckIcon className="w-5 h-5 text-white" aria-hidden="true" />
                      </span>
                      <span className="text-xs font-semibold tracking-wide text-gray-600 mt-8">{step.name}</span>
                    </span>
                  </a>
                </>
              ) : step.status === 'current' ? (
                <>
                  <div className="absolute inset-0 flex items-center" aria-hidden="true">
                    <div className="h-0.5 w-full bg-gray-200" />
                  </div>
                  <a
                    href="#"
                    className="relative w-8 h-8 flex items-center justify-center bg-white border-2 border-yellow-500 rounded-full"
                    aria-current="step"
                  >
                    <span className="h-9 flex flex-col items-center">
                      <span className="z-10 w-8 h-8 flex items-center justify-center rounded-full group-hover:bg-indigo-800">
                        <span className="relative h-2.5 w-2.5 bg-yellow-500 rounded-full relative" style={{top: '0.8rem'}} />
                      </span>
                      <span className="text-xs font-semibold tracking-wide text-gray-600" style={{marginTop: '2.72rem'}}>{step.name}</span>
                    </span>
                  </a>
                </>
              ) : (
                <>
                  <div className="absolute inset-0 flex items-center" aria-hidden="true">
                    <div className="h-0.5 w-full bg-gray-200" />
                  </div>
                  <a
                    href="#"
                    className="group relative w-8 h-8 flex items-center justify-center bg-white border-2 border-gray-300 rounded-full hover:border-gray-400"
                  >
                    <span className="h-9 flex flex-col items-center">
                      <span className="z-10 w-8 h-8 flex items-center justify-center rounded-full">
                        
                        <span className="relative h-2.5 w-2.5 bg-transparent rounded-full group-hover:bg-gray-300" style={{top: '0.8rem'}} />
                      </span>
                      <span className="text-xs font-semibold tracking-wide text-gray-600" style={{marginTop: '2.72rem'}}>{step.name}</span>
                    </span>
                  </a>
                </>
                
              )}
            </li>
          ))}
        </ol>
        
      </div>
    </nav>
  )

Moreover I have index.js page, where all the components come together

import { useState } from "react";
import Head from "next/head";
import Stepper from '../components/Stepper'
import styles from "../styles/styles.module.scss";
import FormCard from "../components/FormCard";
import Navbar from "../components/Navbar";
import { CheckIcon } from '@heroicons/react/solid'

import {
  PersonalInfo,
  ConfirmPurchase,
  ContractInfo,
} from "../components/Forms";

import FormCompleted from "../components/FormCompleted";

function classNames(...classes) {
  return classes.filter(Boolean).join(' ')
}

const App = () => {
  const [formStep, setFormStep] = useState(0);
  const nextFormStep = () => setFormStep((currentStep) => currentStep + 1);
  const prevFormStep = () => setFormStep((currentStep) => currentStep - 1);

  const [activeStep, setActiveStep] = useState(0);
  
  const handleNextStep = () => {
    setActiveStep((prevActiveStep) => prevActiveStep + 1);
  };

  const handlePrevoiusStep = () => {
    setActiveStep((prevActiveStep) => prevActiveStep - 1);
  };



  return (
    
    <div>
      <Head>
        <title>Next.js Multi Step Form</title>
      </Head>
      < Navbar />
      < Stepper activeStep={activeStep} />
        <div className={styles.container}>
          <FormCard currentStep={formStep} prevFormStep={prevFormStep}>
            {formStep >= 0 && (
              <ContractInfo formStep={formStep} nextFormStep={nextFormStep} />
            )}
            {formStep >= 1 && (
              <PersonalInfo formStep={formStep} nextFormStep={nextFormStep} />
            )}
            {formStep >= 2 && (
              <ConfirmPurchase formStep={formStep} nextFormStep={nextFormStep} />
            )}
            {formStep > 2 && <FormCompleted />}
          </FormCard>
        </div>
        <div className="mt-1 mb-5 sm:mt-8 sm:flex sm:justify-center lg:justify-center">
          <div className="rounded-md shadow">
            <a role="button" tabIndex={0}
            onClick={ () => { prevFormStep(); handlePrevoiusStep() }}
            className="w-full flex items-center justify-center px-8 py-3 border border-transparent text-base font-medium rounded-md text-white bg-yellow-500 hover:bg-yallow-600 md:py-4 md:text-lg md:px-10"
            >
              Back
            </a>
          </div>
          <div className="mt-3 sm:mt-0 sm:ml-3">
            <a
              onClick={ () => { nextFormStep(); handleNextStep() }}
              className="w-full flex items-center justify-center px-8 py-3 border border-transparent text-base font-medium rounded-md text-white bg-yellow-500 hover:bg-yallow-600 md:py-4 md:text-lg md:px-10"
            >
              Next
            </a>
          </div>
      </div>
    </div>
  );
};

export default App;

As you see, Stepper is managed in three different files. But I am at least capable to change the activeStep index when clicking on Buttons, which already was a huge challenge for me. So now I also need the design to change. So that activeStep gets the step.status === 'current'. All stepps index < activeStep index should get step.status === 'complete' and logically all stepps index > activeStep index - step.status === 'upcoming'. Now I tried to handle this in index.js, but of course get back step is undefined, even though it is defined in Stepper.js through context.

Katharina Schreiber
  • 1,187
  • 2
  • 11
  • 38
  • I would like to help, but this is an incredibly complex example to follow without a [mcve](https://stackoverflow.com/help/minimal-reproducible-example). Please create a stripped down example of your stepper (doesn't need any styling/code not relevant to the issue) by forking a [nextjs codesandbox](https://codesandbox.io/s/eager-noether-f8hic) OR please share a github repository with your code. – Matt Carlotta Aug 08 '21 at 15:35
  • Sorry, it took some time to prepare it. You would really save my life! https://github.com/katharinasch/stepper-with-form – Katharina Schreiber Aug 08 '21 at 15:58
  • Do me a favor: Create a `.gitignore` file, add the `node_modules` folder to it, save it, commit it and then push it up to github (`node_modules` is an incredibly large folder that shouldn't be tracked by git/added to version control). You may also need to remove the folder from git tracking before committing: [remove folder from git tracking](https://stackoverflow.com/questions/24290358/remove-a-folder-from-git-tracking) – Matt Carlotta Aug 08 '21 at 16:11
  • @KatharinaSchreiber Where are you using `FormProvider`? Is it also wrapping your `index.js` page? Could you not expose `setSteps` in `FormProvider` and get both `steps` and `setSteps` through context in the `index.js` page? – juliomalves Aug 08 '21 at 16:27
  • Here again repository without node_modules : https://github.com/katharinasch/stepper-with-form – Katharina Schreiber Aug 08 '21 at 16:33
  • FormProvider wraps all pages comeponents in _app.js ` ` – Katharina Schreiber Aug 08 '21 at 16:36
  • @KatharinaSchreiber I created a working example [here](https://github.com/mattcarlotta/stepper-with-form-refactored) (see `README` for instructions). Unfortunately, this is a complex example and I just can't cover everything I did within this short comment box, so if you're interested, I have a [slack](https://slack.com/) channel that I can invite you to and can explain more in detail. Let me know. – Matt Carlotta Aug 08 '21 at 22:04
  • sure, that would be amazing! and thank you so much for your help! 'Back' Button doesn't work yet, but I am looking into it right now and trying to undertand how you did it :) For slack: the email in on my GitHub Profile. – Katharina Schreiber Aug 09 '21 at 07:14
  • @KatharinaSchreiber Invite sent. – Matt Carlotta Aug 09 '21 at 16:35

0 Answers0