Skip to content

Commit 54ce8ff

Browse files
committed
Handle touch event
1 parent 84f03dd commit 54ce8ff

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

src/js/InputRangeSlider.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22
import { autobind, extend } from 'InputRangeUtil';
33

4+
// Enable touch
5+
React.initializeTouchEvents(true);
6+
47
class InputRangeSlider extends React.Component {
58
constructor(props) {
69
super(props);
@@ -14,6 +17,9 @@ class InputRangeSlider extends React.Component {
1417
'handleMouseDown',
1518
'handleMouseUp',
1619
'handleMouseMove',
20+
'handleTouchStart',
21+
'handleTouchEnd',
22+
'handleTouchMove',
1723
'handleKeyDown',
1824
], this);
1925
}
@@ -49,6 +55,10 @@ class InputRangeSlider extends React.Component {
4955
}
5056

5157
// Handlers
58+
handleClick(event) {
59+
event.preventDefault();
60+
}
61+
5262
handleMouseDown() {
5363
const document = this.document;
5464

@@ -65,15 +75,32 @@ class InputRangeSlider extends React.Component {
6575
document.removeEventListener('mouseup', this.handleMouseUp);
6676
}
6777

68-
handleClick(event) {
78+
handleMouseMove(event) {
79+
this.props.onSliderMouseMove(this, event);
80+
}
81+
82+
handleTouchStart(event) {
83+
const document = this.document;
84+
6985
event.preventDefault();
86+
87+
document.addEventListener('touchmove', this.handleTouchMove);
88+
document.addEventListener('touchend', this.handleTouchEnd);
7089
}
7190

72-
handleMouseMove(event) {
73-
// Delegate
91+
handleTouchMove(event) {
7492
this.props.onSliderMouseMove(this, event);
7593
}
7694

95+
handleTouchEnd() {
96+
const document = this.document;
97+
98+
event.preventDefault();
99+
100+
document.removeEventListener('touchmove', this.handleTouchMove);
101+
document.removeEventListener('touchend', this.handleTouchEnd);
102+
}
103+
77104
handleKeyDown(event) {
78105
this.props.onSliderKeyDown(this, event);
79106
}
@@ -101,6 +128,7 @@ class InputRangeSlider extends React.Component {
101128
onClick={ this.handleClick }
102129
onKeyDown={ this.handleKeyDown }
103130
onMouseDown={ this.handleMouseDown }
131+
onTouchStart={ this.handleTouchStart }
104132
role="slider">
105133
</a>
106134
</span>

src/js/InputRangeTrack.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22
import { autobind, extend } from 'InputRangeUtil';
33

4+
// Enable touch
5+
React.initializeTouchEvents(true);
6+
47
class InputRangeTrack extends React.Component {
58
constructor(props) {
69
super(props);
@@ -11,6 +14,7 @@ class InputRangeTrack extends React.Component {
1114
// Auto-bind
1215
autobind([
1316
'handleMouseDown',
17+
'handleTouchStart',
1418
], this);
1519
}
1620

@@ -49,7 +53,7 @@ class InputRangeTrack extends React.Component {
4953
// Handlers
5054
handleMouseDown(event) {
5155
const trackClientRect = this.clientRect;
52-
const { clientX } = event;
56+
const { clientX } = event.touches ? event.touches[0] : event;
5357
const position = {
5458
x: clientX - trackClientRect.left,
5559
y: 0,
@@ -58,13 +62,20 @@ class InputRangeTrack extends React.Component {
5862
this.props.onTrackMouseDown(this, position);
5963
}
6064

65+
handleTouchStart(event) {
66+
event.preventDefault();
67+
68+
this.handleMouseDown(event);
69+
}
70+
6171
// Render
6272
render() {
6373
const activeTrackStyle = this.state.activeTrackStyle || {};
6474

6575
return (
6676
<div
6777
onMouseDown={ this.handleMouseDown }
78+
onTouchStart={ this.handleTouchStart }
6879
className="InputRange-track InputRange-track--container">
6980
<div
7081
style={ activeTrackStyle }

src/js/InputRangeUtil.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,8 @@ function clamp(value, min, max) {
22
return Math.min(Math.max(value, min), max);
33
}
44

5-
function assign(target) {
6-
const sources = Array.prototype.slice.call(arguments, 1);
7-
8-
sources.forEach((source) => {
9-
if (!source) {
10-
return;
11-
}
12-
13-
const keys = Object.keys(source);
14-
15-
keys.forEach((key) => {
16-
target[key] = source[key];
17-
});
18-
});
19-
20-
return target;
21-
}
22-
235
function extend() {
24-
return assign.apply(Object, arguments);
6+
return Object.assign.apply(Object, arguments);
257
}
268

279
function captialize(string) {

src/js/InputRangeValueTransformer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class InputRangeValueTransformer {
2727
positionFromEvent(event) {
2828
const trackClientRect = this.component.trackClientRect;
2929
const length = trackClientRect.width;
30+
const { clientX } = event.touches ? event.touches[0] : event;
3031
const position = {
31-
x: clamp(event.clientX - trackClientRect.left, 0, length),
32+
x: clamp(clientX - trackClientRect.left, 0, length),
3233
y: 0,
3334
};
3435

0 commit comments

Comments
 (0)