Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[StepConnector] Customize connector based on internal states #13023

Merged
merged 9 commits into from
Sep 29, 2018
142 changes: 142 additions & 0 deletions docs/src/pages/demos/steppers/CustomizedStepper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } 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 StepConnector from '@material-ui/core/StepConnector';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';

const styles = theme => ({
root: {
width: '90%',
},
button: {
marginRight: theme.spacing.unit,
},
instructions: {
marginTop: theme.spacing.unit,
marginBottom: theme.spacing.unit,
},
connectorActive: {
'& $line': {
borderColor: theme.palette.secondary.main,
},
},
connectorCompleted: {
'& $line': {
borderColor: theme.palette.primary.main,
},
},
line: {},
});

function getSteps() {
return ['Select campaign settings', 'Create an ad group', 'Create an ad'];
}

function getStepContent(step) {
switch (step) {
case 0:
return 'Select campaign settings...';
case 1:
return 'What is an ad group anyways?';
case 2:
return 'This is the bit I really care about!';
default:
return 'Unknown step';
}
}

class CustomizedStepper extends React.Component {
state = {
activeStep: 0,
};

handleNext = () => {
this.setState(state => ({
activeStep: state.activeStep + 1,
}));
};

handleBack = () => {
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleReset = () => {
this.setState({
activeStep: 0,
});
};

render() {
const { classes } = this.props;
const { activeStep } = this.state;
const steps = getSteps();

return (
<div className={classes.root}>
<Stepper
activeStep={activeStep}
connector={
<StepConnector
classes={{
active: classes.connectorActive,
completed: classes.connectorCompleted,
line: classes.line,
}}
/>
}
>
{steps.map(label => (
<Step key={label}>
<StepLabel>{label}</StepLabel>
</Step>
))}
</Stepper>
<div>
{activeStep === steps.length ? (
<div>
<Typography className={classes.instructions}>
All steps completed - you&quot;re finished
</Typography>
<Button onClick={this.handleReset} className={classes.button}>
Reset
</Button>
</div>
) : (
<div>
<Typography className={classes.instructions}>{getStepContent(activeStep)}</Typography>
<div>
<Button
disabled={activeStep === 0}
onClick={this.handleBack}
className={classes.button}
>
Back
</Button>
<Button
variant="contained"
color="primary"
onClick={this.handleNext}
className={classes.button}
>
{activeStep === steps.length - 1 ? 'Finish' : 'Next'}
</Button>
</div>
</div>
)}
</div>
</div>
);
}
}

CustomizedStepper.propTypes = {
classes: PropTypes.object,
};

export default withStyles(styles)(CustomizedStepper);
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ class HorizontalLabelPositionBelowStepper extends React.Component {
};

handleNext = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep + 1,
});
this.setState(state => ({
activeStep: state.activeStep + 1,
}));
};

handleBack = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep - 1,
});
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleReset = () => {
Expand Down
7 changes: 3 additions & 4 deletions docs/src/pages/demos/steppers/HorizontalLinearStepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ class HorizontalLinearStepper extends React.Component {
};

handleBack = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep - 1,
});
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleSkip = () => {
Expand Down
7 changes: 3 additions & 4 deletions docs/src/pages/demos/steppers/HorizontalNonLinearStepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class HorizontalNonLinearStepper extends React.Component {
};

handleBack = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep - 1,
});
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleStep = step => () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ class HorizontalNonLinearStepperWithError extends React.Component {
};

handleBack = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep - 1,
});
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleSkip = () => {
Expand Down
38 changes: 23 additions & 15 deletions docs/src/pages/demos/steppers/SwipeableTextMobileStepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,29 @@ import SwipeableViews from 'react-swipeable-views';

const tutorialSteps = [
{
label: 'How to be happy :)',
imgPath: '/static/images/steppers/1-happy.jpg',
label: 'San Francisco – Oakland Bay Bridge, United States',
imgPath:
'https://images.unsplash.com/photo-1537944434965-cf4679d1a598?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '1. Work with something that you like, like…',
imgPath: '/static/images/steppers/2-work.jpg',
label: 'Bird',
imgPath:
'https://images.unsplash.com/photo-1538032746644-0212e812a9e7?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '2. Keep your friends close to you and hangout with them',
imgPath: '/static/images/steppers/3-friends.jpg',
label: 'Bali, Indonesia',
imgPath:
'https://images.unsplash.com/photo-1537996194471-e657df975ab4?auto=format&fit=crop&w=400&h=250&q=80',
},
{
label: '3. Travel everytime that you have a chance',
imgPath: '/static/images/steppers/4-travel.jpg',
label: 'NeONBRAND Digital Marketing, Las Vegas, United States',
imgPath:
'https://images.unsplash.com/photo-1518732714860-b62714ce0c59?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '4. And contribute to Material-UI :D',
imgPath: '/static/images/steppers/5-mui.png',
label: 'Goč, Serbia',
imgPath:
'https://images.unsplash.com/photo-1512341689857-198e7e2f3ca8?auto=format&fit=crop&w=400&h=250&q=60',
},
];

Expand All @@ -42,11 +47,11 @@ const styles = theme => ({
alignItems: 'center',
height: 50,
paddingLeft: theme.spacing.unit * 4,
marginBottom: 20,
backgroundColor: theme.palette.background.default,
},
img: {
height: 255,
display: 'block',
maxWidth: 400,
overflow: 'hidden',
width: '100%',
Expand Down Expand Up @@ -77,7 +82,6 @@ class SwipeableTextMobileStepper extends React.Component {
render() {
const { classes, theme } = this.props;
const { activeStep } = this.state;

const maxSteps = tutorialSteps.length;

return (
Expand All @@ -87,12 +91,16 @@ class SwipeableTextMobileStepper extends React.Component {
</Paper>
<SwipeableViews
axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
index={this.state.activeStep}
index={activeStep}
onChangeIndex={this.handleStepChange}
enableMouseEvents
>
{tutorialSteps.map(step => (
<img key={step.label} className={classes.img} src={step.imgPath} alt={step.label} />
{tutorialSteps.map((step, index) => (
<div>
{Math.abs(activeStep - index) <= 2 ? (
<img key={step.label} className={classes.img} src={step.imgPath} alt={step.label} />
) : null}
</div>
))}
</SwipeableViews>
<MobileStepper
Expand Down
28 changes: 16 additions & 12 deletions docs/src/pages/demos/steppers/TextMobileStepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';

const tutorialSteps = [
{
label: 'How to be happy :)',
imgPath: '/static/images/steppers/1-happy.jpg',
label: 'San Francisco – Oakland Bay Bridge, United States',
imgPath:
'https://images.unsplash.com/photo-1537944434965-cf4679d1a598?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '1. Work with something that you like, like…',
imgPath: '/static/images/steppers/2-work.jpg',
label: 'Bird',
imgPath:
'https://images.unsplash.com/photo-1538032746644-0212e812a9e7?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '2. Keep your friends close to you and hangout with them',
imgPath: '/static/images/steppers/3-friends.jpg',
label: 'Bali, Indonesia',
imgPath:
'https://images.unsplash.com/photo-1537996194471-e657df975ab4?auto=format&fit=crop&w=400&h=250&q=80',
},
{
label: '3. Travel everytime that you have a chance',
imgPath: '/static/images/steppers/4-travel.jpg',
label: 'NeONBRAND Digital Marketing, Las Vegas, United States',
imgPath:
'https://images.unsplash.com/photo-1518732714860-b62714ce0c59?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: '4. And contribute to Material-UI :D',
imgPath: '/static/images/steppers/5-mui.png',
label: 'Goč, Serbia',
imgPath:
'https://images.unsplash.com/photo-1512341689857-198e7e2f3ca8?auto=format&fit=crop&w=400&h=250&q=60',
},
];

Expand All @@ -41,13 +46,13 @@ const styles = theme => ({
alignItems: 'center',
height: 50,
paddingLeft: theme.spacing.unit * 4,
marginBottom: 20,
backgroundColor: theme.palette.background.default,
},
img: {
height: 255,
maxWidth: 400,
overflow: 'hidden',
display: 'block',
width: '100%',
},
});
Expand All @@ -72,7 +77,6 @@ class TextMobileStepper extends React.Component {
render() {
const { classes, theme } = this.props;
const { activeStep } = this.state;

const maxSteps = tutorialSteps.length;

return (
Expand Down
6 changes: 6 additions & 0 deletions docs/src/pages/demos/steppers/steppers.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Labels can be placed below the step icon by setting the `alternativeLabel` prope

{{"demo": "pages/demos/steppers/VerticalLinearStepper.js"}}

## Customized Stepper

The example also uses a customized `StepConnector` element that changes border color based on the `active` and `completed` state.
Copy link
Contributor Author

@spirosikmd spirosikmd Sep 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text is a bit off now that we moved it to its own section. Maybe

This component uses a customized `StepConnector` element that changes border color based on the `active` and `completed` state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!


{{"demo": "pages/demos/steppers/CustomizedStepper.js"}}

## Mobile Stepper

This component implements a compact stepper suitable for a mobile device. See [mobile steps](https://material.io/archive/guidelines/components/steppers.html#steppers-types-of-steps) for its inspiration.
Expand Down
14 changes: 6 additions & 8 deletions docs/src/pages/page-layout-examples/checkout/Checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,15 @@ class Checkout extends React.Component {
};

handleNext = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep + 1,
});
this.setState(state => ({
activeStep: state.activeStep + 1,
}));
};

handleBack = () => {
const { activeStep } = this.state;
this.setState({
activeStep: activeStep - 1,
});
this.setState(state => ({
activeStep: state.activeStep - 1,
}));
};

handleReset = () => {
Expand Down
Loading