Merge pull request #6 from react-component/fead-add-touch-support

为 handler 添加 touch 事件
This commit is contained in:
yiminghe
2015-06-24 11:25:20 +08:00
3 changed files with 70 additions and 19 deletions
+6 -2
View File
@@ -3,9 +3,13 @@
// color
@disabledColor: #e7eaec;
* {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0); //remove tap highlight color for mobile safari
}
.@{prefixClass} {
position: relative;
margin: 6px;
margin: 6px auto;
height: 4px;
width: 100%;
border-radius: 2px;
@@ -26,7 +30,7 @@
margin-top: -5px;
width: 10px;
height: 10px;
cursor: default;
cursor: pointer;
border-radius: 50%;
border: solid 2px #d9d9d9;
background-color: #fff;
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "1.2.2",
"version": "1.2.3",
"description": "slider ui component for react",
"keywords": [
"react",
+63 -16
View File
@@ -142,6 +142,11 @@ var Slider = React.createClass({
return e.pageX || (e.clientX + document.documentElement.scrollLeft);
},
_getTouchPosition: function (e) {
var touch = e.touches[0];
return touch.pageX;
},
_triggerEvents: function(event) {
var props = this.props;
var hasMarks = props.marks && props.marks.length > 0;
@@ -150,17 +155,27 @@ var Slider = React.createClass({
}
},
_addEventHandles: function() {
this._onMouseMoveListener = DomUtils.addEventListener(document, 'mousemove', this._onMouseMove);
this._onMouseUpListener = DomUtils.addEventListener(document, 'mouseup', this._onMouseUp);
},
_removeEventHandles: function () {
if (this._onMouseMoveListener) {
this._onMouseMoveListener.remove();
_addEventHandles: function(type) {
if (type === 'touch') {
// just work for chrome iOS Safari and Android Browser
this._onTouchMoveListener = DomUtils.addEventListener(document, 'touchmove', this._onTouchMove);
this._onTouchUpListener = DomUtils.addEventListener(document, 'touchend', this._onTouchUp);
}
if (this._onMouseUpListener) {
if (type === 'mouse') {
this._onMouseMoveListener = DomUtils.addEventListener(document, 'mousemove', this._onMouseMove);
this._onMouseUpListener = DomUtils.addEventListener(document, 'mouseup', this._onMouseUp);
}
},
_removeEventHandles: function (type) {
if (type === 'touch') {
this._onTouchMoveListener.remove();
this._onTouchUpListener.remove();
}
if (type === 'mouse') {
this._onMouseMoveListener.remove();
this._onMouseUpListener.remove();
}
},
@@ -178,18 +193,37 @@ var Slider = React.createClass({
});
},
_end: function() {
this._removeEventHandles();
_end: function(type) {
this._removeEventHandles(type);
this.setState(this._triggerEvents.bind(this, 'onAfterChange'));
},
_onMouseUp: function() {
this._end();
this._end('mouse');
},
_onTouchUp: function() {
this._end('touch');
},
_onMouseMove: function(e) {
pauseEvent(e);
var position = this._getMousePosition(e);
this._handleMove(e, position);
},
_onTouchMove: function(e) {
if (e.touches.length > 1 || (e.type === 'touchend' && e.touches.length > 0)) {
return;
}
var position = this._getTouchPosition(e);
this._handleMove(e, position);
},
_handleMove: function(e, position) {
pauseEvent(e);
// var position = this._getMousePosition(e);
var props = this.props;
var state = this.state;
@@ -222,6 +256,18 @@ var Slider = React.createClass({
});
},
handleTouchStart: function(e) {
if (this.props.disabled || e.touches.length > 1 || (e.type === 'touchend' && e.touches.length > 0)) {
return;
}
var position = this._getTouchPosition(e);
this.startPosition = position;
this._start(position);
this._addEventHandles('touch');
pauseEvent(e);
},
handleMouseDown: function() {
return (e) => {
if (this.props.disabled) {
@@ -229,7 +275,7 @@ var Slider = React.createClass({
}
var position = this._getMousePosition(e);
this._start(position);
this._addEventHandles();
this._addEventHandles('mouse');
pauseEvent(e);
};
},
@@ -243,7 +289,7 @@ var Slider = React.createClass({
() => {
this._triggerEvents('onChange');
this._start(position);
this._addEventHandles();
this._addEventHandles('mouse');
}
);
pauseEvent(e);
@@ -344,7 +390,8 @@ var Slider = React.createClass({
ref = "handle"
style = {handleStyle}
href = "#"
onMouseDown={this.handleMouseDown}></a>
onMouseDown = {this.handleMouseDown}
onTouchStart = {this.handleTouchStart}></a>
);
},