Skip to content

Commit 05fa2b8

Browse files
author
Marek Rozmus
committed
Add code review fixes for list item component
1 parent 0f2edf9 commit 05fa2b8

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/SwipeableListItem.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import PropTypes from 'prop-types';
33

44
import './SwipeableListItem.css';
55

6+
const SwipeActionPropType = PropTypes.shape({
7+
action: PropTypes.func.isRequired,
8+
background: PropTypes.node.isRequired
9+
});
10+
611
class SwipeableListItem extends PureComponent {
712
constructor(props) {
813
super(props);
@@ -21,24 +26,24 @@ class SwipeableListItem extends PureComponent {
2126
}
2227

2328
componentDidMount() {
24-
window.addEventListener('mouseup', this.onDragEndMouse);
25-
window.addEventListener('touchend', this.onDragEndTouch);
29+
this.wrapper.addEventListener('mouseup', this.onDragEndMouse);
30+
this.wrapper.addEventListener('touchend', this.onDragEndTouch);
2631
}
2732

2833
componentWillUnmount() {
29-
window.removeEventListener('mouseup', this.onDragEndMouse);
30-
window.removeEventListener('touchend', this.onDragEndTouch);
34+
this.wrapper.removeEventListener('mouseup', this.onDragEndMouse);
35+
this.wrapper.removeEventListener('touchend', this.onDragEndTouch);
3136
}
3237

33-
onDragStartMouse = evt => {
34-
this.onDragStart(evt);
35-
window.addEventListener('mousemove', this.onMouseMove);
38+
onDragStartMouse = event => {
39+
this.onDragStart(event);
40+
this.wrapper.addEventListener('mousemove', this.onMouseMove);
3641
};
3742

38-
onDragStartTouch = evt => {
39-
const touch = evt.targetTouches[0];
43+
onDragStartTouch = event => {
44+
const touch = event.targetTouches[0];
4045
this.onDragStart(touch);
41-
window.addEventListener('touchmove', this.onTouchMove);
46+
this.wrapper.addEventListener('touchmove', this.onTouchMove);
4247
};
4348

4449
onDragStart = ({ clientX }) => {
@@ -56,12 +61,12 @@ class SwipeableListItem extends PureComponent {
5661
};
5762

5863
onDragEndMouse = () => {
59-
window.removeEventListener('mousemove', this.onMouseMove);
64+
this.wrapper.removeEventListener('mousemove', this.onMouseMove);
6065
this.onDragEnd();
6166
};
6267

6368
onDragEndTouch = () => {
64-
window.removeEventListener('touchmove', this.onTouchMove);
69+
this.wrapper.removeEventListener('touchmove', this.onTouchMove);
6570
this.onDragEnd();
6671
};
6772

@@ -117,8 +122,8 @@ class SwipeableListItem extends PureComponent {
117122
}
118123
};
119124

120-
onTouchMove = evt => {
121-
const touch = evt.targetTouches[0];
125+
onTouchMove = event => {
126+
const touch = event.targetTouches[0];
122127
const delta = touch.clientX - this.dragStartX;
123128

124129
if (this.shouldMoveItem(delta)) {
@@ -224,14 +229,8 @@ class SwipeableListItem extends PureComponent {
224229
SwipeableListItem.propTypes = {
225230
blockSwipe: PropTypes.bool,
226231
children: PropTypes.node.isRequired,
227-
swipeLeft: PropTypes.shape({
228-
action: PropTypes.func.isRequired,
229-
background: PropTypes.node.isRequired
230-
}),
231-
swipeRight: PropTypes.shape({
232-
action: PropTypes.func.isRequired,
233-
background: PropTypes.node.isRequired
234-
}),
232+
swipeLeft: SwipeActionPropType,
233+
swipeRight: SwipeActionPropType,
235234
threshold: PropTypes.number
236235
};
237236

0 commit comments

Comments
 (0)