React Stepper - Implement a Wizard

This tutorial uses a Stepper widget to create a wizard-like application. The created application integrates the Form and MultiView components.

This wizard includes three key parts:

  1. The Stepper component.
  2. Step content where the MultiView component shows content for each step. Every view with input fields contains a Form.
  3. A navigation panel with an active step caption ("Step 1 of 5") and buttons for moving between steps ("Next" and "Back").

Stepper wizard elements

To view the complete code of this example, refer to the following demo:

Stepper - Form Integration Demo

Set Up App Layout

To get started, add a Stepper and a MultiView to your project. Create a navigation panel - a container with child items. Child items are a <div> element that displays the active step caption and two Buttons:

App.tsx
  • import { Stepper, Item } from 'devextreme-react/stepper'
  • import type { IItemProps } from 'devextreme-react/stepper'
  • import Button from 'devextreme-react/button';
  • import { MultiView } from 'devextreme-react/multi-view';
  • import { initialSteps } from './data.ts';
  •  
  • // ...
  • export default function App () {
  • const [steps, setSteps] = useState<IItemProps[]>(initialSteps);
  •  
  • // ...
  • return (
  • <>
  • <Stepper ... >
  • {steps.map((step) => <Item key={step.label} {...step} />)}
  • </Stepper>
  • <div className="content">
  • <MultiView ...
  • focusStateEnabled={false}
  • animationEnabled={false}
  • swipeEnabled={false}
  • >
  • </MultiView>
  • <div className="nav-panel">
  • <div className="current-step">
  • Step <span className="selected-index">1</span> of {steps.length}
  • </div>
  • <div className="nav-buttons">
  • <Button ...
  • id="prevButton"
  • text="Back"
  • visible={false}
  • >
  • <Button
  • id="nextButton"
  • text="Next"
  • />
  • </div>
  • </div>
  • </div>
  • </>
  • )
  • }

The "Back" button is initially hidden. A later step in this tutorial implements dynamic visibility logic (see Synchronize Steps and Navigation Panel).

Configure forms for each step with MultiView items[].render:

App.tsx
DatesForm.tsx
  • import DatesForm from './DatesForm.tsx';
  •  
  • // ...
  • export default function App () {
  • const renderDatesForm = useCallback(() => {
  • return <DatesForm ... />;
  • }, [formData]);
  •  
  • // ...
  • return (
  • <>
  • <div className="content">
  • <MultiView ... >
  • <Item render={renderDatesForm} />
  • <!-- Configure the rest of the forms -->
  • </MultiView>
  • </div>
  • </>
  • )
  • }
  • import { Form, SimpleItem } from 'devextreme-react/form';
  •  
  • // ...
  • const DatesForm: FC<FormProps> = memo(({ formData, validationGroup }) => (
  • <>
  • <Form ... >
  • <SimpleItem ... />
  • </Form>
  • </>
  • ));

This tutorial implements custom components as MultiView item templates and a custom type for form data. For more details on how this example is configured, refer to the Stepper - Form Integration demo.

Synchronize Steps and Navigation Panel

To synchronize the selectedIndex properties of Stepper and MultiView, update the MultiView.selectedIndex property in the Stepper.onSelectionChanged event handler.

App.tsx
  • // ...
  • export default function App () {
  • const [selectedIndex, setSelectedIndex] = useState(0);
  •  
  • const onSelectionChanged = useCallback(({ component }: SelectionChangedEvent) => {
  • setSelectedIndex(component.option('selectedIndex') ?? 0);
  • }, []);
  •  
  • // ...
  • return (
  • <>
  • <Stepper ...
  • selectedIndex={selectedIndex}
  • onSelectionChanged={onSelectionChanged}
  • >
  • </Stepper>
  • <div className="content">
  • <MultiView ...
  • selectedIndex={selectedIndex}
  • >
  • </MultiView>
  • </div>
  • </>
  • )
  • }

This example utilizes the selectedIndex variable to implement navigation panel functionality:

  • The onPrevButtonClick and onNextButtonClick handlers move to the previous/next step by modifying selectedIndex.
  • The nextButtonText function changes the text of the "Next" button to "Confirm" on the last step.

The "Confirm" button submits the form and disables user interactions with the Stepper. To learn how to disable Stepper interactions, refer to the following help topic: Configure a Readonly Stepper.

After users submit the form, they can reset the wizard and start over.

App.tsx
styles.css
  • // ...
  • export default function App () {
  • const [isConfirmed, setIsConfirmed] = useState(false);
  • const [isStepperReadonly, setIsStepperReadonly] = useState(false);
  •  
  • const nextButtonText = useMemo(() => {
  • if (selectedIndex < steps.length - 1) {
  • return 'Next';
  • }
  •  
  • if (isConfirmed) {
  • return 'Reset';
  • }
  •  
  • return 'Confirm';
  • }, [selectedIndex, isConfirmed]);
  •  
  • const onPrevButtonClick = useCallback(() => {
  • setSelectedIndex((prev) => prev - 1);
  • }, []);
  •  
  • const moveNext = useCallback(() => {
  • setSelectedIndex(selectedIndex + 1);
  • }, [selectedIndex]);
  •  
  • const onConfirm = useCallback(() => {
  • setIsConfirmed(true);
  • setIsStepperReadonly(true)
  • }, []);
  •  
  • const onReset = useCallback(() => {
  • setIsConfirmed(false);
  • setSelectedIndex(0);
  • setIsStepperReadonly(false);
  • // ...
  • }, []);
  •  
  • const onNextButtonClick = useCallback(() => {
  • if (selectedIndex < initialSteps.length -1) {
  • moveNext();
  • } else if (isConfirmed) {
  • onReset();
  • } else {
  • onConfirm();
  • }
  • }, [selectedIndex, isConfirmed, onConfirm, onReset]);
  •  
  • // ...
  • return (
  • <>
  • <Stepper
  • className={isStepperReadonly ? 'readonly' : ''}
  • focusStateEnabled={!isStepperReadonly}
  • />
  • <div className="nav-panel">
  • <div className="current-step">
  • {!isConfirmed && (
  • <>
  • Step <span className="selected-index">{selectedIndex + 1}</span> of {steps.length}
  • </>
  • )}
  • </div>
  •  
  • <div className="nav-buttons">
  • <Button ...
  • id="prevButton"
  • onClick={onPrevButtonClick}
  • visible={selectedIndex !== 0 && !isConfirmed}
  • />
  • <Button ...
  • id="nextButton"
  • text={nextButtonText}
  • onClick={onNextButtonClick}
  • />
  • </div>
  • </div>
  • </>
  • )
  • }
  • .readonly {
  • pointer-events: none;
  • }

Implement Validation

To configure validation, assign validation groups and specify validation rules in each MultiView form that requires validation:

DatesForm.tsx
App.tsx
  • import { Form, SimpleItem } from 'devextreme-react/form';
  •  
  • // ...
  • const DatesForm: FC<FormProps> = memo(({ formData, validationGroup }) => (
  • <>
  • <Form ...
  • validationGroup={validationGroup}
  • >
  • <SimpleItem ...
  • isRequired
  • />
  • </Form>
  • </>
  • ));
  • // ...
  • const validationGroups = ['dates', 'guests', 'roomAndMealPlan'];
  •  
  • export default function App () {
  • const renderDatesForm = useCallback(() => {
  • return <DatesForm validationGroup={validationGroups[0]} ... />;
  • }, [formData]);
  • // ...
  • }

This tutorial validates steps only when users move forward. If a step fails validation, navigation operation is cancelled:

App.tsx
  • import validationEngine from 'devextreme/ui/validation_engine';
  •  
  • // ...
  • export default function App () {
  • const onSelectionChanging = useCallback((args: SelectionChangingEvent) => {
  • const { component, addedItems, removedItems } = args;
  • const { items = [] } = component.option();
  •  
  • const addedIndex = items.findIndex((item: IItemProps) => item === addedItems[0]);
  • const removedIndex = items.findIndex((item: IItemProps) => item === removedItems[0]);
  • const isMoveForward = addedIndex > removedIndex;
  •  
  • if (isMoveForward) {
  • const isValid = getValidationResult(removedIndex);
  •  
  • setStepValidationResult(removedIndex, isValid);
  •  
  • if (isValid === false) {
  • args.cancel = true;
  • }
  • }
  • }, [setStepValidationResult, isConfirmed]);
  •  
  • const moveNext = useCallback(() => {
  • const isValid = getValidationResult(selectedIndex);
  •  
  • setStepValidationResult(selectedIndex, isValid);
  •  
  • if (isValid){
  • setSelectedIndex(selectedIndex + 1);
  • }
  • }, [selectedIndex]);
  •  
  • const getValidationResult = useCallback((index: number) => {
  • if (index >= validationGroups.length) {
  • return true;
  • }
  •  
  • return validationEngine.validateGroup(validationGroups[index]).isValid;
  • }, []);
  •  
  • const setStepValidationResult = useCallback((index: number, isValid: boolean | undefined) => {
  • setSteps((prev) => prev.map((step, i) => {
  • if (i === index) {
  • return {
  • ...step,
  • isValid,
  • }
  • }
  •  
  • return step;
  • }));
  • }, []);
  •  
  • return (
  • <>
  • <Stepper ...
  • onSelectionChanging={onSelectionChanging}
  • >
  • </Stepper>
  • </>
  • )
  • }

Review the full code of this tutorial in the following demo:

Stepper - Form Integration Demo