Skip to content

Commit 79ea1ae

Browse files
committed
[docs] Add SwipeableTextMobileStepper demo
1 parent 847feaa commit 79ea1ae

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import React from 'react';
2+
import { makeStyles, useTheme } from '@material-ui/core/styles';
3+
import MobileStepper from '@material-ui/core/MobileStepper';
4+
import Paper from '@material-ui/core/Paper';
5+
import Typography from '@material-ui/core/Typography';
6+
import Button from '@material-ui/core/Button';
7+
import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft';
8+
import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';
9+
import SwipeableViews from 'react-swipeable-views';
10+
import { autoPlay } from 'react-swipeable-views-utils';
11+
12+
const AutoPlaySwipeableViews = autoPlay(SwipeableViews);
13+
14+
const tutorialSteps = [
15+
{
16+
label: 'San Francisco – Oakland Bay Bridge, United States',
17+
imgPath:
18+
'https://images.unsplash.com/photo-1537944434965-cf4679d1a598?auto=format&fit=crop&w=400&h=250&q=60',
19+
},
20+
{
21+
label: 'Bird',
22+
imgPath:
23+
'https://images.unsplash.com/photo-1538032746644-0212e812a9e7?auto=format&fit=crop&w=400&h=250&q=60',
24+
},
25+
{
26+
label: 'Bali, Indonesia',
27+
imgPath:
28+
'https://images.unsplash.com/photo-1537996194471-e657df975ab4?auto=format&fit=crop&w=400&h=250&q=80',
29+
},
30+
{
31+
label: 'NeONBRAND Digital Marketing, Las Vegas, United States',
32+
imgPath:
33+
'https://images.unsplash.com/photo-1518732714860-b62714ce0c59?auto=format&fit=crop&w=400&h=250&q=60',
34+
},
35+
{
36+
label: 'Goč, Serbia',
37+
imgPath:
38+
'https://images.unsplash.com/photo-1512341689857-198e7e2f3ca8?auto=format&fit=crop&w=400&h=250&q=60',
39+
},
40+
];
41+
42+
const useStyles = makeStyles(theme => ({
43+
root: {
44+
maxWidth: 400,
45+
flexGrow: 1,
46+
},
47+
header: {
48+
display: 'flex',
49+
alignItems: 'center',
50+
height: 50,
51+
paddingLeft: theme.spacing(4),
52+
backgroundColor: theme.palette.background.default,
53+
},
54+
img: {
55+
height: 255,
56+
display: 'block',
57+
maxWidth: 400,
58+
overflow: 'hidden',
59+
width: '100%',
60+
},
61+
}));
62+
63+
function SwipeableTextMobileStepper() {
64+
const classes = useStyles();
65+
const theme = useTheme();
66+
const [activeStep, setActiveStep] = React.useState(0);
67+
const maxSteps = tutorialSteps.length;
68+
69+
const handleNext = () => {
70+
setActiveStep(prevActiveStep => prevActiveStep + 1);
71+
};
72+
73+
const handleBack = () => {
74+
setActiveStep(prevActiveStep => prevActiveStep - 1);
75+
};
76+
77+
const handleStepChange = (step: number) => {
78+
setActiveStep(step);
79+
};
80+
81+
return (
82+
<div className={classes.root}>
83+
<Paper square elevation={0} className={classes.header}>
84+
<Typography>{tutorialSteps[activeStep].label}</Typography>
85+
</Paper>
86+
<AutoPlaySwipeableViews
87+
axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
88+
index={activeStep}
89+
onChangeIndex={handleStepChange}
90+
enableMouseEvents
91+
>
92+
{tutorialSteps.map((step, index) => (
93+
<div key={step.label}>
94+
{Math.abs(activeStep - index) <= 2 ? (
95+
<img className={classes.img} src={step.imgPath} alt={step.label} />
96+
) : null}
97+
</div>
98+
))}
99+
</AutoPlaySwipeableViews>
100+
<MobileStepper
101+
steps={maxSteps}
102+
position="static"
103+
variant="text"
104+
activeStep={activeStep}
105+
nextButton={
106+
<Button size="small" onClick={handleNext} disabled={activeStep === maxSteps - 1}>
107+
Next
108+
{theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />}
109+
</Button>
110+
}
111+
backButton={
112+
<Button size="small" onClick={handleBack} disabled={activeStep === 0}>
113+
{theme.direction === 'rtl' ? <KeyboardArrowRight /> : <KeyboardArrowLeft />}
114+
Back
115+
</Button>
116+
}
117+
/>
118+
</div>
119+
);
120+
}
121+
122+
export default SwipeableTextMobileStepper;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// TODO: upstream
2+
3+
declare module 'react-swipeable-views-utils' {
4+
import * as React from 'react';
5+
import { ConsistentWith, Omit, PropInjector } from '@material-ui/types';
6+
import { OnChangeIndexCallback, OnSwitchingCallback } from 'react-swipeable-views';
7+
8+
export interface WithAutoPlay {
9+
index: number;
10+
onChangeIndex: OnChangeIndexCallback;
11+
onSwitching?: OnSwitchingCallback;
12+
}
13+
export interface WithAutoPlayProps {
14+
autoplay?: boolean;
15+
direction?: 'incremental' | 'decremental';
16+
index: number;
17+
interval?: number;
18+
onChangeIndex: OnChangeIndexCallback;
19+
slideCount?: number;
20+
}
21+
22+
export const autoPlay: PropInjector<WithAutoPlay, WithAutoPlayProps>;
23+
}

0 commit comments

Comments
 (0)