@jdcfe/yep-react
Version:
一套移动端的React组件库
190 lines (171 loc) • 5.99 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import classNames from 'classnames';
/**
* 给组件添加Touch或Mouse下的active状态
*/
var TouchFeedback = /*#__PURE__*/function (_React$PureComponent) {
_inherits(TouchFeedback, _React$PureComponent);
var _super = _createSuper(TouchFeedback);
function TouchFeedback(props) {
var _this;
_classCallCheck(this, TouchFeedback);
_this = _super.call(this, props);
_this.triggerEvent = _this.triggerEvent.bind(_assertThisInitialized(_this));
_this.onTouchStart = _this.onTouchStart.bind(_assertThisInitialized(_this));
_this.onTouchMove = _this.onTouchMove.bind(_assertThisInitialized(_this));
_this.onTouchEnd = _this.onTouchEnd.bind(_assertThisInitialized(_this));
_this.onTouchCancel = _this.onTouchCancel.bind(_assertThisInitialized(_this));
_this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this));
_this.onMouseUp = _this.onMouseUp.bind(_assertThisInitialized(_this));
_this.onMouseLeave = _this.onMouseLeave.bind(_assertThisInitialized(_this));
_this.state = {
active: false
};
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(parameters) {
var type = parameters.type,
isActive = parameters.isActive,
ev = parameters.ev;
var eventType = "on".concat(type);
var children = this.props.children;
if (children && children.props[eventType]) {
children.props[eventType](ev);
}
if (isActive !== this.state.active) {
this.setState({
active: isActive
});
}
}
}, {
key: "onTouchStart",
value: function onTouchStart(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'TouchStart',
isActive: true,
ev: e
});
}
}, {
key: "onTouchMove",
value: function onTouchMove(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'TouchMove',
isActive: false,
ev: e
});
}
}, {
key: "onTouchEnd",
value: function onTouchEnd(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'TouchEnd',
isActive: false,
ev: e
});
}
}, {
key: "onTouchCancel",
value: function onTouchCancel(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'TouchCancel',
isActive: false,
ev: e
});
}
}, {
key: "onMouseDown",
value: function onMouseDown(parameters) {
var e = parameters.e; // pc simulate mobile
this.triggerEvent({
type: 'MouseDown',
isActive: true,
ev: e
});
}
}, {
key: "onMouseUp",
value: function onMouseUp(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'MouseUp',
isActive: false,
ev: e
});
}
}, {
key: "onMouseLeave",
value: function onMouseLeave(parameters) {
var e = parameters.e;
this.triggerEvent({
type: 'MouseLeave',
isActive: false,
ev: e
});
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
children = _this$props.children,
disabled = _this$props.disabled,
activeClassName = _this$props.activeClassName,
activeStyle = _this$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 = Object.assign(Object.assign({}, style), activeStyle);
}
className = classNames(className, activeClassName);
}
return /*#__PURE__*/React.cloneElement(child, Object.assign({
className: className,
style: style
}, events));
}
return /*#__PURE__*/React.cloneElement(child, events);
}
}]);
return TouchFeedback;
}(React.PureComponent);
export { TouchFeedback as default };
TouchFeedback.defaultProps = {
disabled: false
};