forked from cypress-io/cypress-react-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse-movement.js
50 lines (44 loc) · 1.28 KB
/
mouse-movement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://medium.com/@pierrehedkvist/renderless-components-in-react-8d663746314c
import React from 'react'
import PropTypes from 'prop-types'
export default class MouseMovement extends React.Component {
constructor(props) {
console.log('MouseMovement constructor')
super(props)
this.state = {
timer: undefined,
}
this.timeout = this.timeout.bind(this)
this.onMouseMove = this.onMouseMove.bind(this)
}
componentWillMount() {
console.log('MouseMovement componentWillMount')
document.addEventListener('mousemove', this.onMouseMove)
const timer = setTimeout(this.timeout, 4000)
this.setState({ timer })
}
componentWillUnmount() {
console.log('MouseMovement componentWillUnmount')
document.removeEventListener('mousemove', this.onMouseMove)
clearTimeout(this.state.timer)
this.setState({ timer: undefined })
}
onMouseMove() {
console.log('MouseMovement onMouseMove')
clearTimeout(this.state.timer)
const timer = setTimeout(this.timeout, 4000)
this.setState({ timer })
this.props.onMoved(true)
}
timeout() {
console.log('timeout')
clearTimeout(this.state.timer)
this.props.onMoved(false)
}
render() {
return null
}
}
MouseMovement.propTypes = {
onMoved: PropTypes.func.isRequired,
}