diff --git a/.gitignore b/.gitignore index 3c3629e..576fd3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +yarn.lock +.vscode/ \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index afe5934..f15b4ba 100644 --- a/dist/index.js +++ b/dist/index.js @@ -47,7 +47,9 @@ var LongPressable = function (_React$PureComponent) { args[_key] = arguments[_key]; } - return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = LongPressable.__proto__ || Object.getPrototypeOf(LongPressable)).call.apply(_ref, [this].concat(args))), _this), _this.isLongPressing = false, _this.startingPosition = { x: 0, y: 0 }, _this.onPointerUp = function (e) { + return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = LongPressable.__proto__ || Object.getPrototypeOf(LongPressable)).call.apply(_ref, [this].concat(args))), _this), _this.isLongPressing = false, _this.startingPosition = { x: 0, y: 0 }, _this.cancelEvent = function (e) { + e.stopPropagation(); + }, _this.onPointerUp = function (e) { if (_this.timerID) { _this.cancelLongPress(); } @@ -55,7 +57,7 @@ var LongPressable = function (_React$PureComponent) { var mousePosition = eventToPosition(e); if (!_this.isLongPressing && !_this.exceedDragThreshold(mousePosition)) { - _this.props.onShortPress(); + _this.props.onShortPress && _this.props.onShortPress(); } else { _this.isLongPressing = false; } @@ -70,7 +72,7 @@ var LongPressable = function (_React$PureComponent) { _this.timerID = setTimeout(function () { _this.isLongPressing = true; - _this.props.onLongPress(); + _this.props.onLongPress && _this.props.onLongPress(); }, _this.props.longPressTime); }, _this.onPointerMove = function (e) { var mousePosition = eventToPosition(e); @@ -98,13 +100,19 @@ var LongPressable = function (_React$PureComponent) { }, { key: 'render', value: function render() { + var disabled = this.props.disabled; + return _react2.default.createElement( 'div', { - onPointerUp: this.onPointerUp, - onPointerDown: this.onPointerDown, - onPointerMove: this.onPointerMove, - onPointerLeave: this.onPointerLeave + onContextMenu: function onContextMenu(e) { + e.preventDefault();e.stopPropagation(); + }, + onClick: disabled ? this.cancelEvent : null, + onPointerUp: disabled ? null : this.onPointerUp, + onPointerDown: disabled ? null : this.onPointerDown, + onPointerMove: disabled ? null : this.onPointerMove, + onPointerLeave: disabled ? null : this.onPointerLeave }, this.props.children ); @@ -122,11 +130,13 @@ LongPressable.propTypes = { // Maximum distance (pixels) user is allowed to drag before // click is canceled dragThreshold: _propTypes2.default.number, + disabled: _propTypes2.default.bool, children: _propTypes2.default.node }; LongPressable.defaultProps = { longPressTime: 500, primaryMouseButtonOnly: true, - dragThreshold: 100 + dragThreshold: 100, + disabled: false }; exports.default = LongPressable; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1a3fbf0..a9185bc 100644 --- a/src/index.js +++ b/src/index.js @@ -24,18 +24,24 @@ export default class LongPressable extends React.PureComponent { // Maximum distance (pixels) user is allowed to drag before // click is canceled dragThreshold: PropType.number, + disabled: PropType.bool, children: PropType.node } static defaultProps = { longPressTime: 500, primaryMouseButtonOnly: true, - dragThreshold: 100 + dragThreshold: 100, + disabled: false } isLongPressing = false startingPosition = { x: 0, y: 0 } + cancelEvent = (e) => { + e.stopPropagation(); + } + onPointerUp = (e) => { if (this.timerID) { this.cancelLongPress() @@ -45,7 +51,7 @@ export default class LongPressable extends React.PureComponent { if (!this.isLongPressing && !this.exceedDragThreshold(mousePosition)) { - this.props.onShortPress() + this.props.onShortPress && this.props.onShortPress() } else { this.isLongPressing = false @@ -58,12 +64,12 @@ export default class LongPressable extends React.PureComponent { return } } - + this.startingPosition = eventToPosition(e) this.timerID = setTimeout(() => { this.isLongPressing = true - this.props.onLongPress() + this.props.onLongPress && this.props.onLongPress() }, this.props.longPressTime) } @@ -90,12 +96,16 @@ export default class LongPressable extends React.PureComponent { } render() { + var disabled = this.props.disabled; + return (
{ e.preventDefault(); e.stopPropagation(); }} + onClick={disabled ? this.cancelEvent : null} + onPointerUp={disabled ? null : this.onPointerUp} + onPointerDown={disabled ? null : this.onPointerDown} + onPointerMove={disabled ? null : this.onPointerMove} + onPointerLeave={disabled ? null : this.onPointerLeave} > {this.props.children}