How To Use Material UI Steppers In SharePoint Framework SPFx

Overview

 
This blog will help us setup Material UI in SPFx and we will use the Stepper component from Material UI.
 

Introduction

 
With the introduction of SPFx, form creation in SharePoint has become easy. When creating huge forms it's great when we are able to divide the form into multiple sections. And to display this divided section it's even better to have a great UI experience. There are multiple options available, and a few are listed below:
For demo purposes we will use Material UI Stepper, but we can use any of the above as per our UI needs.
 

Create SPFx Project

 
The complete project can be downloaded from the below location.
 
 
To try in your environment use the below steps.
 
Step 1
 
Create one folder named materialUIStepper using the command below.
  1. mkdir materialUIStepper  
Navigate to the folder using the below command.
  1. cd materialUIStepper  
Run yeoman SharePoint Framework generator to create the project using the below command.
  1. yo @microsoft/sharepoint  
Step 2
 
Provide the values for the project as prompted by the yeoman generator. Below are the values which I provided for the example solution. 
  • What is your solution name? (material-ui-stepper)
  • Which baseline packages do you want to target for your component(s)? SharePoint Online only (latest)
  • Where do you want to place the files? (Use arrow keys) Use the current folder
  • Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites? (y/N) N
  • Will the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant? (y/N) N
  • Which type of client-side component to create? (Use arrow keys) WebPart
  • What is your Web part name? materialuiStepper
  • What is your Web part description? (materialuiStepper description)
  • Which framework would you like to use? React
Step 3
 
Install Material UI with the below command we can even use Yarn to install Material UI.
  1. npm install @material-ui/core  
Step 4
 
Open the src/webparts/materialuiStepper/components/MaterialuiStepper.tsx and make the following changes.
 
Import Statement
  1. import Stepper from '@material-ui/core/Stepper';  
  2. import Step from '@material-ui/core/Step';  
  3. import StepLabel from '@material-ui/core/StepLabel';  
  4. import Typography from '@material-ui/core/Typography';  
  5. import Button from '@material-ui/core/Button';  
Add in State variable
  1. export interface IMaterialuiStepperState {  
  2. activeStep: number;  
  3. skipped: any;  
  4. }  
Update the state value in constructor
  1. public constructor(props: IMaterialuiStepperProps, state: IMaterialuiStepperState) {  
  2. super(props);  
  3. this.state = {  
  4. activeStep: 0,  
  5. skipped: new Set()  
  6. };  
  7. }  
Create Steps Text
  1. private steps = ['Select campaign settings''Create an ad group''Create an ad'];  
Content inside each step
 
We can use any other React component which we can create for each step and import that component and return for each step.
  1. private getStepContent = (step) => {  
  2. switch (step) {  
  3. case 0:  
  4. return 'Select campaign settings...';  
  5. case 1:  
  6. return 'What is an ad group anyways?';  
  7. case 2:  
  8. return 'This is the bit I really care about!';  
  9. default:  
  10. return 'Unknown step';  
  11. }  
  12. }  
HTML for steps (add inside render method) 
  1. <Stepper activeStep={this.state.activeStep}>  
  2.     {this.steps.map((label, index) => {  
  3.     const props = {};  
  4.     const labelProps = {};  
  5.     if (this.isStepOptional(index)) {  
  6.     labelProps["optional"] = <Typography variant="caption">Optional</Typography>;  
  7.     }  
  8.     if (this.isStepSkipped(index)) {  
  9.     props["completed"] = false;  
  10.     }  
  11.     return (  
  12.     <Step key={label} {...props}>  
  13.         <StepLabel {...labelProps}>{label}</StepLabel>  
  14.     </Step>  
  15.     );  
  16.     })}  
  17. </Stepper>  
  18. <div>  
  19.     {this.state.activeStep === this.steps.length ? (  
  20.     <div>  
  21.         <Typography>  
  22.             All steps completed - you're finished  
  23.         </Typography>  
  24.         <Button onClick={this.handleReset}>  
  25.             Reset  
  26.         </Button>  
  27.     </div>  
  28.     ) : (  
  29.     <div>  
  30.         <Typography>{this.getStepContent(this.state.activeStep)}</Typography>  
  31.         <div>  
  32.             <Button disabled={this.state.activeStep===0} onClick={this.handleBack}>  
  33.                 Back  
  34.             </Button>  
  35.             {this.isStepOptional(this.state.activeStep) && (  
  36.             <Button variant="contained" style={{ marginRight: 10 }} color="primary" onClick={this.handleSkip}>  
  37.                 Skip  
  38.             </Button>  
  39.             )}  
  40.             <Button variant="contained" color="primary" onClick={this.handleNext}>  
  41.                 {this.state.activeStep === this.steps.length - 1 ? 'Finish' : 'Next'}  
  42.             </Button>  
  43.         </div>  
  44.     </div>  
  45.     )}  
  46. </div>  
Add all helping methods for button click
  1. private isStepOptional = (step) => {  
  2.     return step === 1;  
  3. }  
  4.   private isStepSkipped = (step) => {  
  5.     return this.state.skipped.has(step);  
  6. }  
  7.   private handleNext = () => {  
  8.     const { activeStep } = this.state;  
  9.     let { skipped } = this.state;  
  10.     if (this.isStepSkipped(activeStep)) {  
  11.         skipped = new Set(skipped.values());  
  12.         skipped.delete(activeStep);  
  13.     }  
  14.     this.setState({  
  15.         activeStep: activeStep + 1,  
  16.         skipped,  
  17.     });  
  18. }  
  19.   private handleBack = () => {  
  20.     this.setState(state => ({  
  21.         activeStep: state.activeStep - 1,  
  22.     }));  
  23. }  
  24.   private handleSkip = () => {  
  25.     const { activeStep } = this.state;  
  26.     if (!this.isStepOptional(activeStep)) {  
  27.         // You probably want to guard against something like this,  
  28.         // it should never occur unless someone's actively trying to break something.  
  29.         throw new Error("You can't skip a step that isn't optional.");  
  30.     }  
  31.     this.setState(state => {  
  32.         const skipped = new Set(state.skipped.values());  
  33.         skipped.add(activeStep);  
  34.         return {  
  35.             activeStep: state.activeStep + 1,  
  36.             skipped,  
  37.         };  
  38.     });  
  39. }  
  40.   private handleReset = () => {  
  41.     this.setState({  
  42.         activeStep: 0,  
  43.     });  
  44. }  
Outcome
 
 How To Use Material UI Steppers In SharePoint Framework SPFx