Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
fix swipe bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaSugawara committed Sep 4, 2017
1 parent 1bbe684 commit bce2e0b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 82 deletions.
Empty file removed docs/docs/index.d.ts
Empty file.
4 changes: 2 additions & 2 deletions docs/index.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ class App extends React.Component {
render() {
return (
<div>
<Carousel
style={{
width: 320
}}
>
<img src="//placehold.it/320x160?text=1of2" alt="a" />
<img src="//placehold.it/320x160?text=2of2" alt="a" />
</Carousel>
<Carousel
ref={(instance) => { this.carousel = instance; }}
autoSlideInterval={3000}
useDots={true}
>
{this.state.count.map((c) => {
return (
Expand Down
64 changes: 0 additions & 64 deletions docs/src/index.d.ts

This file was deleted.

10 changes: 7 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ export interface Touch {
export interface CarouselProps {
label?: string;
useDots?: boolean;
dotStyle?: any;
dotStyle?: React.CSSProperties;
duration?: number;
autoSlideInterval?: number;
style?: React.CSSProperties;
nextComponent?: JSX.Element;
prevComponent?: JSX.Element;
}
export interface CarouselState {
animate: boolean;
Expand All @@ -28,6 +31,7 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
container: Element;
swiping: boolean;
moving: boolean;
direction: number;
touch: Touch;
timer: any;
state: CarouselState;
Expand All @@ -40,7 +44,7 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
render(): JSX.Element;
renderInitialRect(): JSX.Element;
renderCarouselChild(): JSX.Element | JSX.Element[];
renderDots(): JSX.Element;
renderControls(): JSX.Element;
autoSlide(): void;
resetAutoSlide(): void;
addEvents(): void;
Expand All @@ -53,7 +57,7 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
getPrevIndex(): number;
getIndexDirection(index: any): 0 | 1 | -1;
updateFrameRect(): void;
move(index: number): void;
move(index: number, direction?: number): void;
next(): void;
prev(): void;
initialize(): void;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"start": "webpack-dev-server",
"prepare": "npm run test && npm run build",
"build": "tsc",
"build": "tsc -d --declarationDir ./",
"docs": "webpack -p",
"test:dom": "USE_DOM=true ava **/*.dom.test.js",
"test:node": "ava **/*.node.test.js",
Expand Down
32 changes: 22 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export interface Touch {
export interface CarouselProps {
label?: string;
useDots?: boolean;
dotStyle?: any;
dotStyle?: React.CSSProperties;
duration?: number;
autoSlideInterval?: number;
style?: React.CSSProperties;
nextComponent?: JSX.Element;
prevComponent?: JSX.Element;
}

export interface CarouselState {
Expand All @@ -32,6 +35,7 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
container: Element;
swiping: boolean;
moving: boolean;
direction: number;
touch: Touch;
timer;

Expand Down Expand Up @@ -99,14 +103,14 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
return null;
}
return (
<div>
<div style={this.props.style}>
<ul
ref={(node) => (this.container = node)}
style={(this.state.canUseDOM) ? this.getFrameStyle() : { width: '100%' }}
>
{this.renderCarouselChild()}
</ul>
{this.renderDots()}
{this.renderControls()}
</div>
);
}
Expand Down Expand Up @@ -136,7 +140,7 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
});
}

renderDots() {
renderControls() {
if (!this.props.useDots || this.state.slideCount <= 1) {
return null;
}
Expand Down Expand Up @@ -242,7 +246,14 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
const canDisplay = this.canDisplaySlide(index);
const duration = this.swiping ? 0 : this.props.duration / 1000; // to milisec
const transition = this.state.animate ? `transform ${duration}s ease` : null;
const direction = this.getIndexDirection(index);
let direction = this.getIndexDirection(index);
if (direction) {
if (this.touch) {
direction = this.touch.swipeDirection;
} else if (this.direction) {
direction = this.direction;
}
}
let transform, msTransform;
if (canDisplay) {
if (direction === 0) {
Expand Down Expand Up @@ -293,8 +304,8 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta

getIndexDirection(index) {
if (this.state.currentIndex === index) return 0;
if (this.getPrevIndex() === index) return -1;
if (this.getNextIndex() === index) return 1;
if (this.getPrevIndex() === index) return -1;
if (this.state.currentIndex < index) return 1;
if (this.state.currentIndex > index) return -1;
}
Expand All @@ -320,18 +331,19 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
});
}

move(index: number) {
move(index: number, direction: number = this.getIndexDirection(index)) {
if (this.swiping || this.moving || this.state.currentIndex === index) {
return;
}
this.direction = direction;
this.moving = true;
this.setState({
animate: true,
showIndex: index
}, () => {
setTimeout(() => {
this.setState({
swipePosition: this.getIndexDirection(index) * -this.state.slideWidth
swipePosition: direction * -this.state.slideWidth
}, () => {
setTimeout(() => {
this.setState({
Expand All @@ -353,11 +365,11 @@ export default class Carousel extends React.Component<CarouselProps, CarouselSta
}

next() {
this.move(this.getNextIndex());
this.move(this.getNextIndex(), 1);
}

prev() {
this.move(this.getPrevIndex());
this.move(this.getPrevIndex(), -1);
}

initialize() {
Expand Down
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./",
"allowUnreachableCode": true,
"allowUnusedLabels": true,
"sourceMap": true,
Expand Down

0 comments on commit bce2e0b

Please sign in to comment.