Skip to content

Commit 4a4180a

Browse files
author
Marek Rozmus
committed
Closes issue 77
1 parent 22e191d commit 4a4180a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

examples/src/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ function App() {
8585
<SwipeableListItem
8686
swipeRight={swipeRightDataSimple('Item with both swipes')}
8787
swipeLeft={swipeLeftDataSimple('Item with both swipes')}
88+
onSwipeStart={() => console.log('Swipe started')}
89+
onSwipeEnd={() => console.log('Swipe ended')}
90+
onSwipeStateChange={state => console.log(state)}
8891
>
8992
{itemContentSimple('Item with both swipes')}
9093
</SwipeableListItem>

src/SwipeableListItem.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ class SwipeableListItem extends PureComponent {
2929

3030
this.startTime = null;
3131

32+
this.prevSwipeDistancePercent = 0;
33+
3234
this.resetState();
3335
}
3436

3537
resetState = () => {
3638
this.dragStartPoint = { x: -1, y: -1 };
3739
this.dragDirection = DragDirection.UNKNOWN;
3840
this.left = 0;
41+
this.prevSwipeDistancePercent = 0;
3942
};
4043

4144
get dragHorizontalDirectionThreshold() {
@@ -169,6 +172,10 @@ class SwipeableListItem extends PureComponent {
169172
} else if (this.left > this.listElement.offsetWidth * threshold) {
170173
this.handleSwipedRight();
171174
}
175+
176+
if (this.props.onSwipeEnd) {
177+
this.props.onSwipeEnd();
178+
}
172179
}
173180

174181
this.resetState();
@@ -250,6 +257,14 @@ class SwipeableListItem extends PureComponent {
250257
}
251258
break;
252259
}
260+
261+
if (
262+
this.props.onSwipeStart &&
263+
(this.dragDirection === DragDirection.LEFT ||
264+
this.dragDirection === DragDirection.RIGHT)
265+
) {
266+
this.props.onSwipeStart();
267+
}
253268
}
254269
};
255270

@@ -279,6 +294,24 @@ class SwipeableListItem extends PureComponent {
279294

280295
const opacity = (Math.abs(this.left) / 100).toFixed(2);
281296

297+
if (this.props.onSwipeStateChange) {
298+
const swipeDistance = Math.max(
299+
0,
300+
this.listElement.offsetWidth - Math.abs(this.left)
301+
);
302+
303+
const swipeDistancePercent =
304+
100 -
305+
Math.round((100 * swipeDistance) / this.listElement.offsetWidth);
306+
307+
if (this.prevSwipeDistancePercent !== swipeDistancePercent) {
308+
this.props.onSwipeStateChange({
309+
swipeDistancePercent
310+
});
311+
this.prevSwipeDistancePercent = swipeDistancePercent;
312+
}
313+
}
314+
282315
if (opacity < 1 && opacity.toString() !== contentToShow.style.opacity) {
283316
contentToShow.style.opacity = opacity.toString();
284317

@@ -361,7 +394,11 @@ SwipeableListItem.propTypes = {
361394
swipeRight: SwipeActionPropType,
362395
scrollStartThreshold: PropTypes.number,
363396
swipeStartThreshold: PropTypes.number,
364-
threshold: PropTypes.number
397+
threshold: PropTypes.number,
398+
399+
onSwipeStart: PropTypes.func,
400+
onSwipeEnd: PropTypes.func,
401+
onSwipeStateChange: PropTypes.func
365402
};
366403

367404
export default SwipeableListItem;

0 commit comments

Comments
 (0)