113 lines
3.8 KiB
JavaScript
113 lines
3.8 KiB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
|
|
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
|
import _createClass from 'babel-runtime/helpers/createClass';
|
|
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
|
import _inherits from 'babel-runtime/helpers/inherits';
|
|
import React from 'react';
|
|
import classNames from 'classnames';
|
|
|
|
var TouchFeedback = function (_React$Component) {
|
|
_inherits(TouchFeedback, _React$Component);
|
|
|
|
function TouchFeedback() {
|
|
_classCallCheck(this, TouchFeedback);
|
|
|
|
var _this = _possibleConstructorReturn(this, (TouchFeedback.__proto__ || Object.getPrototypeOf(TouchFeedback)).apply(this, arguments));
|
|
|
|
_this.state = {
|
|
active: false
|
|
};
|
|
_this.onTouchStart = function (e) {
|
|
_this.triggerEvent('TouchStart', true, e);
|
|
};
|
|
_this.onTouchMove = function (e) {
|
|
_this.triggerEvent('TouchMove', false, e);
|
|
};
|
|
_this.onTouchEnd = function (e) {
|
|
_this.triggerEvent('TouchEnd', false, e);
|
|
};
|
|
_this.onTouchCancel = function (e) {
|
|
_this.triggerEvent('TouchCancel', false, e);
|
|
};
|
|
_this.onMouseDown = function (e) {
|
|
// pc simulate mobile
|
|
_this.triggerEvent('MouseDown', true, e);
|
|
};
|
|
_this.onMouseUp = function (e) {
|
|
_this.triggerEvent('MouseUp', false, e);
|
|
};
|
|
_this.onMouseLeave = function (e) {
|
|
_this.triggerEvent('MouseLeave', false, e);
|
|
};
|
|
return _this;
|
|
}
|
|
|
|
_createClass(TouchFeedback, [{
|
|
key: 'componentDidUpdate',
|
|
value: function componentDidUpdate() {
|
|
if (this.props.disabled && this.state.active) {
|
|
this.setState({
|
|
active: false
|
|
});
|
|
}
|
|
}
|
|
}, {
|
|
key: 'triggerEvent',
|
|
value: function triggerEvent(type, isActive, ev) {
|
|
var eventType = 'on' + type;
|
|
var children = this.props.children;
|
|
|
|
if (children.props[eventType]) {
|
|
children.props[eventType](ev);
|
|
}
|
|
if (isActive !== this.state.active) {
|
|
this.setState({
|
|
active: isActive
|
|
});
|
|
}
|
|
}
|
|
}, {
|
|
key: 'render',
|
|
value: function render() {
|
|
var _props = this.props,
|
|
children = _props.children,
|
|
disabled = _props.disabled,
|
|
activeClassName = _props.activeClassName,
|
|
activeStyle = _props.activeStyle;
|
|
|
|
var events = disabled ? undefined : {
|
|
onTouchStart: this.onTouchStart,
|
|
onTouchMove: this.onTouchMove,
|
|
onTouchEnd: this.onTouchEnd,
|
|
onTouchCancel: this.onTouchCancel,
|
|
onMouseDown: this.onMouseDown,
|
|
onMouseUp: this.onMouseUp,
|
|
onMouseLeave: this.onMouseLeave
|
|
};
|
|
var child = React.Children.only(children);
|
|
if (!disabled && this.state.active) {
|
|
var _child$props = child.props,
|
|
style = _child$props.style,
|
|
className = _child$props.className;
|
|
|
|
if (activeStyle !== false) {
|
|
if (activeStyle) {
|
|
style = _extends({}, style, activeStyle);
|
|
}
|
|
className = classNames(className, activeClassName);
|
|
}
|
|
return React.cloneElement(child, _extends({ className: className,
|
|
style: style }, events));
|
|
}
|
|
return React.cloneElement(child, events);
|
|
}
|
|
}]);
|
|
|
|
return TouchFeedback;
|
|
}(React.Component);
|
|
|
|
export default TouchFeedback;
|
|
|
|
TouchFeedback.defaultProps = {
|
|
disabled: false
|
|
}; |