Skip to content

Commit 8095e78

Browse files
authored
fix: bring back column resize on touch support (#83)
* fix: bring back column resize on touch support * update
1 parent f8936f5 commit 8095e78

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## NEXT VERSION
44

55
- fix: fix possible memory leak in `ColumnResizer`
6+
- fix: bring back column resize on touch events support
67

78
## v1.7.2 (2019-08-26)
89

@@ -35,7 +36,7 @@
3536

3637
## v1.6.1 (2019-07-06)
3738

38-
- feat: support infinite loading with `maxHeight`
39+
- feat: support infinite loading with `maxHeight`
3940
- chore: remove unused `$table-border-radius` variable
4041
- feat: add `sortState` to support multi sort
4142

src/ColumnResizer.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ export function removeUserSelectStyles(doc) {
3333
}
3434
}
3535

36+
const eventsFor = {
37+
touch: {
38+
start: 'touchstart',
39+
move: 'touchmove',
40+
stop: 'touchend',
41+
},
42+
mouse: {
43+
start: 'mousedown',
44+
move: 'mousemove',
45+
stop: 'mouseup',
46+
},
47+
};
48+
49+
let dragEventFor = eventsFor.mouse;
50+
3651
/**
3752
* ColumnResizer for BaseTable
3853
*/
@@ -48,14 +63,20 @@ class ColumnResizer extends React.PureComponent {
4863
this._handleClick = this._handleClick.bind(this);
4964
this._handleMouseDown = this._handleMouseDown.bind(this);
5065
this._handleMouseUp = this._handleMouseUp.bind(this);
51-
this._handleMouseMove = this._handleMouseMove.bind(this);
66+
this._handleTouchStart = this._handleTouchStart.bind(this);
67+
this._handleTouchEnd = this._handleTouchEnd.bind(this);
68+
this._handleDragStart = this._handleDragStart.bind(this);
69+
this._handleDragStop = this._handleDragStop.bind(this);
70+
this._handleDrag = this._handleDrag.bind(this);
5271
}
5372

5473
componentWillUnmount() {
5574
if (this.handleRef) {
5675
const { ownerDocument } = this.handleRef;
57-
ownerDocument.removeEventListener('mousemove', this._handleMouseMove);
58-
ownerDocument.removeEventListener('mouseup', this._handleMouseUp);
76+
ownerDocument.removeEventListener(eventsFor.mouse.move, this._handleDrag);
77+
ownerDocument.removeEventListener(eventsFor.mouse.stop, this._handleDragStop);
78+
ownerDocument.removeEventListener(eventsFor.touch.move, this._handleDrag);
79+
ownerDocument.removeEventListener(eventsFor.touch.stop, this._handleDragStop);
5980
removeUserSelectStyles(ownerDocument);
6081
}
6182
}
@@ -70,6 +91,8 @@ class ColumnResizer extends React.PureComponent {
7091
onClick={this._handleClick}
7192
onMouseDown={this._handleMouseDown}
7293
onMouseUp={this._handleMouseUp}
94+
onTouchStart={this._handleTouchStart}
95+
onTouchEnd={this._handleTouchEnd}
7396
style={{
7497
userSelect: 'none',
7598
touchAction: 'none',
@@ -93,6 +116,26 @@ class ColumnResizer extends React.PureComponent {
93116
}
94117

95118
_handleMouseDown(e) {
119+
dragEventFor = eventsFor.mouse;
120+
this._handleDragStart(e);
121+
}
122+
123+
_handleMouseUp(e) {
124+
dragEventFor = eventsFor.mouse;
125+
this._handleDragStop(e);
126+
}
127+
128+
_handleTouchStart(e) {
129+
dragEventFor = eventsFor.touch;
130+
this._handleDragStart(e);
131+
}
132+
133+
_handleTouchEnd(e) {
134+
dragEventFor = eventsFor.touch;
135+
this._handleDragStop(e);
136+
}
137+
138+
_handleDragStart(e) {
96139
if (typeof e.button === 'number' && e.button !== 0) return;
97140

98141
this.isDragging = true;
@@ -102,26 +145,32 @@ class ColumnResizer extends React.PureComponent {
102145

103146
const { ownerDocument } = this.handleRef;
104147
addUserSelectStyles(ownerDocument);
105-
ownerDocument.addEventListener('mousemove', this._handleMouseMove);
106-
ownerDocument.addEventListener('mouseup', this._handleMouseUp);
148+
ownerDocument.addEventListener(dragEventFor.move, this._handleDrag);
149+
ownerDocument.addEventListener(dragEventFor.stop, this._handleDragStop);
107150
}
108151

109-
_handleMouseUp(e) {
152+
_handleDragStop(e) {
110153
if (!this.isDragging) return;
111154
this.isDragging = false;
112155

113156
this.props.onResizeStop(this.props.column);
114157

115158
const { ownerDocument } = this.handleRef;
116159
removeUserSelectStyles(ownerDocument);
117-
ownerDocument.removeEventListener('mousemove', this._handleMouseMove);
118-
ownerDocument.addEventListener('mouseup', this._handleMouseUp);
160+
ownerDocument.removeEventListener(dragEventFor.move, this._handleDrag);
161+
ownerDocument.removeEventListener(dragEventFor.stop, this._handleDragStop);
119162
}
120163

121-
_handleMouseMove(e) {
164+
_handleDrag(e) {
165+
let clientX = e.clientX;
166+
if (e.type === eventsFor.touch.move) {
167+
e.preventDefault();
168+
if (e.targetTouches && e.targetTouches[0]) clientX = e.targetTouches[0].clientX;
169+
}
170+
122171
const { offsetParent } = this.handleRef;
123172
const offsetParentRect = offsetParent.getBoundingClientRect();
124-
const x = e.clientX + offsetParent.scrollLeft - offsetParentRect.left;
173+
const x = clientX + offsetParent.scrollLeft - offsetParentRect.left;
125174

126175
if (this.lastX === INVALID_VALUE) {
127176
this.lastX = x;

0 commit comments

Comments
 (0)