Advertisement
Guest User

Untitled

a guest
Sep 21st, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useContext } from 'react';
  2. import { makeStyles } from '@material-ui/core/styles';
  3. import Stepper from '@material-ui/core/Stepper';
  4. import Step from '@material-ui/core/Step';
  5. import StepLabel from '@material-ui/core/StepLabel';
  6. import Button from '@material-ui/core/Button';
  7. import Typography from '@material-ui/core/Typography';
  8. import AuthStepperContext from '../../Context/AuthContext';
  9.  
  10. const useStyles = makeStyles((theme) => ({
  11.   root: {
  12.     width: '100%',
  13.   },
  14.   button: {
  15.     marginRight: theme.spacing(1),
  16.   },
  17.   instructions: {
  18.     marginTop: theme.spacing(1),
  19.     marginBottom: theme.spacing(1),
  20.   },
  21. }));
  22.  
  23. export default function HorizontalOptionalStepper(props) {
  24.  
  25.     const {
  26.         getSteps,
  27.         getStepContent,
  28.         isStepOptional,
  29.         activeStep,
  30.         setActiveStep,
  31.         handleSubmit
  32.        
  33.     } = props;
  34.  
  35.     const classes = useStyles();
  36.     // const [activeStep, setActiveStep] = useState(0);
  37.     const [skipped, setSkipped] = useState(new Set());
  38.     const steps = getSteps();
  39.  
  40.     const isStepSkipped = (step) => {
  41.         return skipped.has(step);
  42.     };
  43.  
  44.     const handleNext = () => {
  45.         let newSkipped = skipped;
  46.         if (isStepSkipped(activeStep)) {
  47.         newSkipped = new Set(newSkipped.values());
  48.         newSkipped.delete(activeStep);
  49.         }
  50.  
  51.         setActiveStep((prevActiveStep) => prevActiveStep + 1);
  52.         setSkipped(newSkipped);
  53.  
  54.         console.log(activeStep)
  55.     };
  56.  
  57.     const handleBack = () => {
  58.         setActiveStep((prevActiveStep) => prevActiveStep - 1);
  59.     };
  60.  
  61.     const handleSkip = () => {
  62.         if (!isStepOptional(activeStep)) {
  63.         // You probably want to guard against something like this,
  64.         // it should never occur unless someone's actively trying to break something.
  65.         throw new Error("You can't skip a step that isn't optional.");
  66.         }
  67.  
  68.         setActiveStep((prevActiveStep) => prevActiveStep + 1);
  69.         setSkipped((prevSkipped) => {
  70.         const newSkipped = new Set(prevSkipped.values());
  71.         newSkipped.add(activeStep);
  72.         return newSkipped;
  73.         });
  74.     };
  75.  
  76.     // const handleReset = () => {
  77.     //     setActiveStep(0);
  78.     // };
  79.  
  80.     return (
  81.         <div className={classes.root}>
  82.         <Stepper activeStep={activeStep}>
  83.             {steps.map((label, index) => {
  84.             const stepProps = {};
  85.             const labelProps = {};
  86.             if (isStepOptional(index)) {
  87.                 labelProps.optional = (
  88.                 <Typography variant="caption">Optional</Typography>
  89.                 );
  90.             }
  91.             if (isStepSkipped(index)) {
  92.                 stepProps.completed = false;
  93.             }
  94.             return (
  95.                 <Step key={label} {...stepProps}>
  96.                 <StepLabel {...labelProps}>{label}</StepLabel>
  97.                 </Step>
  98.             );
  99.             })}
  100.         </Stepper>
  101.         <div>
  102.             {activeStep === steps.length ? (
  103.             <div>
  104.                 <Typography className={classes.instructions}>
  105.                 All steps completed - you&apos;re finished
  106.                 </Typography>
  107.                 <Button
  108.                     onClick={handleSubmit}
  109.                     variant="contained"
  110.                     color="primary"
  111.                     className={classes.button}
  112.                 >
  113.                     Complete Registration
  114.                 </Button>
  115.             </div>
  116.             ) : (
  117.             <div>
  118.                 <Typography className={classes.instructions}>
  119.                 {getStepContent(activeStep)}
  120.                 </Typography>
  121.                 <div>
  122.                 <Button
  123.                     disabled={activeStep === 0}
  124.                     onClick={handleBack}
  125.                     className={classes.button}
  126.                 >
  127.                     Back
  128.                 </Button>
  129.                 {isStepOptional(activeStep) && (
  130.                     <Button
  131.                     variant="contained"
  132.                     color="primary"
  133.                     onClick={handleSkip}
  134.                     className={classes.button}
  135.                     >
  136.                     Skip
  137.                     </Button>
  138.                 )}
  139.  
  140.                 <Button
  141.                     variant="contained"
  142.                     color="primary"
  143.                     onClick={handleNext}
  144.                     className={classes.button}
  145.                 >
  146.                     {activeStep === steps.length - 1 ? 'Finish' : 'Next'}
  147.                 </Button>
  148.                 </div>
  149.             </div>
  150.             )}
  151.         </div>
  152.         </div>
  153.     );
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement