From ec7d8eabf8abf295db29557cf180424dbe6c94ea Mon Sep 17 00:00:00 2001 From: "stephen.kingsley" Date: Sat, 6 Aug 2016 11:14:01 +0800 Subject: [PATCH] add mouseUp and mouseDown --- modules/PointTarget.js | 40 +++++++++++++++++++-------- modules/__tests__/PointTarget-test.js | 13 +++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/modules/PointTarget.js b/modules/PointTarget.js index 16939ae..2a0834e 100644 --- a/modules/PointTarget.js +++ b/modules/PointTarget.js @@ -10,7 +10,9 @@ class PointTarget extends React.Component { static propTypes = { children: PropTypes.node, tolerance: PropTypes.number, - onPoint: PropTypes.func + onPoint: PropTypes.func, + mouseDown: PropTypes.func, + mouseUp: PropTypes.func } static defaultProps = { @@ -21,20 +23,20 @@ class PointTarget extends React.Component { if (!this.usingTouch && this.props.onPoint) this.props.onPoint() } - + handleTouchStart = (event) => { this.usingTouch = true - + if (this.touchStarted) return - + this.touchStarted = true - + this.touchMoved = false this.startX = touchX(event) this.startY = touchY(event) } - + handleTouchMove = (event) => { if (!this.touchMoved) { const { tolerance } = this.props @@ -43,23 +45,35 @@ class PointTarget extends React.Component { Math.abs(this.startY - touchY(event)) > tolerance } } - + handleTouchCancel = () => { this.touchStarted = this.touchMoved = false this.startX = this.startY = 0 } - + handleTouchEnd = () => { this.touchStarted = false - + if (!this.touchMoved && this.props.onPoint) this.props.onPoint() } - + + handleMouseDown = () => { + if (this.props.mouseDown) { + this.props.mouseDown() + } + } + + handleMouseUp = () => { + if (this.props.mouseUp) { + this.props.mouseUp() + } + } + componentWillMount() { this.usingTouch = false } - + render() { const { children } = this.props @@ -70,7 +84,9 @@ class PointTarget extends React.Component { onTouchStart: this.handleTouchStart, onTouchMove: this.handleTouchMove, onTouchCancel: this.handleTouchCancel, - onTouchEnd: this.handleTouchEnd + onTouchEnd: this.handleTouchEnd, + onMouseDown: this.handleMouseDown, + onMouseUp: this.handleMouseUp }) } } diff --git a/modules/__tests__/PointTarget-test.js b/modules/__tests__/PointTarget-test.js index 9b5e472..1b71924 100644 --- a/modules/__tests__/PointTarget-test.js +++ b/modules/__tests__/PointTarget-test.js @@ -82,4 +82,17 @@ describe('A ', () => { }) }) }) + + describe('when a mouse up and down', () => { + it('does not call the onPoint callback', () => { + let called = false + + render( called = true}/>, node, () => { + Simulate.mouseDown(node.firstChild) + Simulate.mouseUp(node.firstChild) + + expect(called).toBe(false) + }) + }) + }) })