Skip to content

add mouseUp and mouseDown #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions modules/PointTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions modules/__tests__/PointTarget-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ describe('A <PointTarget>', () => {
})
})
})

describe('when a mouse up and down', () => {
it('does not call the onPoint callback', () => {
let called = false

render(<PointTarget onPoint={() => called = true}/>, node, () => {
Simulate.mouseDown(node.firstChild)
Simulate.mouseUp(node.firstChild)

expect(called).toBe(false)
})
})
})
})