Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { useState, useContext } from 'react';
- import { makeStyles } from '@material-ui/core/styles';
- import Stepper from '@material-ui/core/Stepper';
- import Step from '@material-ui/core/Step';
- import StepLabel from '@material-ui/core/StepLabel';
- import Button from '@material-ui/core/Button';
- import Typography from '@material-ui/core/Typography';
- import AuthStepperContext from '../../Context/AuthContext';
- const useStyles = makeStyles((theme) => ({
- root: {
- width: '100%',
- },
- button: {
- marginRight: theme.spacing(1),
- },
- instructions: {
- marginTop: theme.spacing(1),
- marginBottom: theme.spacing(1),
- },
- }));
- export default function HorizontalOptionalStepper(props) {
- const {
- getSteps,
- getStepContent,
- isStepOptional,
- activeStep,
- setActiveStep,
- handleSubmit
- } = props;
- const classes = useStyles();
- // const [activeStep, setActiveStep] = useState(0);
- const [skipped, setSkipped] = useState(new Set());
- const steps = getSteps();
- const isStepSkipped = (step) => {
- return skipped.has(step);
- };
- const handleNext = () => {
- let newSkipped = skipped;
- if (isStepSkipped(activeStep)) {
- newSkipped = new Set(newSkipped.values());
- newSkipped.delete(activeStep);
- }
- setActiveStep((prevActiveStep) => prevActiveStep + 1);
- setSkipped(newSkipped);
- console.log(activeStep)
- };
- const handleBack = () => {
- setActiveStep((prevActiveStep) => prevActiveStep - 1);
- };
- const handleSkip = () => {
- if (!isStepOptional(activeStep)) {
- // You probably want to guard against something like this,
- // it should never occur unless someone's actively trying to break something.
- throw new Error("You can't skip a step that isn't optional.");
- }
- setActiveStep((prevActiveStep) => prevActiveStep + 1);
- setSkipped((prevSkipped) => {
- const newSkipped = new Set(prevSkipped.values());
- newSkipped.add(activeStep);
- return newSkipped;
- });
- };
- // const handleReset = () => {
- // setActiveStep(0);
- // };
- return (
- <div className={classes.root}>
- <Stepper activeStep={activeStep}>
- {steps.map((label, index) => {
- const stepProps = {};
- const labelProps = {};
- if (isStepOptional(index)) {
- labelProps.optional = (
- <Typography variant="caption">Optional</Typography>
- );
- }
- if (isStepSkipped(index)) {
- stepProps.completed = false;
- }
- return (
- <Step key={label} {...stepProps}>
- <StepLabel {...labelProps}>{label}</StepLabel>
- </Step>
- );
- })}
- </Stepper>
- <div>
- {activeStep === steps.length ? (
- <div>
- <Typography className={classes.instructions}>
- All steps completed - you're finished
- </Typography>
- <Button
- onClick={handleSubmit}
- variant="contained"
- color="primary"
- className={classes.button}
- >
- Complete Registration
- </Button>
- </div>
- ) : (
- <div>
- <Typography className={classes.instructions}>
- {getStepContent(activeStep)}
- </Typography>
- <div>
- <Button
- disabled={activeStep === 0}
- onClick={handleBack}
- className={classes.button}
- >
- Back
- </Button>
- {isStepOptional(activeStep) && (
- <Button
- variant="contained"
- color="primary"
- onClick={handleSkip}
- className={classes.button}
- >
- Skip
- </Button>
- )}
- <Button
- variant="contained"
- color="primary"
- onClick={handleNext}
- className={classes.button}
- >
- {activeStep === steps.length - 1 ? 'Finish' : 'Next'}
- </Button>
- </div>
- </div>
- )}
- </div>
- </div>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement