Skip to content

Commit

Permalink
[docs] Add SwipeableTextMobileStepper demo
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Nov 22, 2019
1 parent 847feaa commit 79ea1ae
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
122 changes: 122 additions & 0 deletions docs/src/pages/components/steppers/SwipeableTextMobileStepper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import React from 'react';
import { makeStyles, useTheme } from '@material-ui/core/styles';
import MobileStepper from '@material-ui/core/MobileStepper';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft';
import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';
import SwipeableViews from 'react-swipeable-views';
import { autoPlay } from 'react-swipeable-views-utils';

const AutoPlaySwipeableViews = autoPlay(SwipeableViews);

const tutorialSteps = [
{
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: 'Bird',
imgPath:
'https://images.unsplash.com/photo-1538032746644-0212e812a9e7?auto=format&fit=crop&w=400&h=250&q=60',
},
{
label: 'Bali, Indonesia',
imgPath:
'https://images.unsplash.com/photo-1537996194471-e657df975ab4?auto=format&fit=crop&w=400&h=250&q=80',
},
{
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: 'Goč, Serbia',
imgPath:
'https://images.unsplash.com/photo-1512341689857-198e7e2f3ca8?auto=format&fit=crop&w=400&h=250&q=60',
},
];

const useStyles = makeStyles(theme => ({
root: {
maxWidth: 400,
flexGrow: 1,
},
header: {
display: 'flex',
alignItems: 'center',
height: 50,
paddingLeft: theme.spacing(4),
backgroundColor: theme.palette.background.default,
},
img: {
height: 255,
display: 'block',
maxWidth: 400,
overflow: 'hidden',
width: '100%',
},
}));

function SwipeableTextMobileStepper() {
const classes = useStyles();
const theme = useTheme();
const [activeStep, setActiveStep] = React.useState(0);
const maxSteps = tutorialSteps.length;

const handleNext = () => {
setActiveStep(prevActiveStep => prevActiveStep + 1);
};

const handleBack = () => {
setActiveStep(prevActiveStep => prevActiveStep - 1);
};

const handleStepChange = (step: number) => {
setActiveStep(step);
};

return (
<div className={classes.root}>
<Paper square elevation={0} className={classes.header}>
<Typography>{tutorialSteps[activeStep].label}</Typography>
</Paper>
<AutoPlaySwipeableViews
axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
index={activeStep}
onChangeIndex={handleStepChange}
enableMouseEvents
>
{tutorialSteps.map((step, index) => (
<div key={step.label}>
{Math.abs(activeStep - index) <= 2 ? (
<img className={classes.img} src={step.imgPath} alt={step.label} />
) : null}
</div>
))}
</AutoPlaySwipeableViews>
<MobileStepper
steps={maxSteps}
position="static"
variant="text"
activeStep={activeStep}
nextButton={
<Button size="small" onClick={handleNext} disabled={activeStep === maxSteps - 1}>
Next
{theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />}
</Button>
}
backButton={
<Button size="small" onClick={handleBack} disabled={activeStep === 0}>
{theme.direction === 'rtl' ? <KeyboardArrowRight /> : <KeyboardArrowLeft />}
Back
</Button>
}
/>
</div>
);
}

export default SwipeableTextMobileStepper;
23 changes: 23 additions & 0 deletions docs/types/react-swipeable-views-utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TODO: upstream

declare module 'react-swipeable-views-utils' {
import * as React from 'react';
import { ConsistentWith, Omit, PropInjector } from '@material-ui/types';
import { OnChangeIndexCallback, OnSwitchingCallback } from 'react-swipeable-views';

export interface WithAutoPlay {
index: number;
onChangeIndex: OnChangeIndexCallback;
onSwitching?: OnSwitchingCallback;
}
export interface WithAutoPlayProps {
autoplay?: boolean;
direction?: 'incremental' | 'decremental';
index: number;
interval?: number;
onChangeIndex: OnChangeIndexCallback;
slideCount?: number;
}

export const autoPlay: PropInjector<WithAutoPlay, WithAutoPlayProps>;
}

0 comments on commit 79ea1ae

Please sign in to comment.