Last active
August 29, 2015 14:22
-
-
Save adjohu/d6d7388dac9a75b8b469 to your computer and use it in GitHub Desktop.
react hold listener wrapper usage <HoldListener onHold={this.handleHold}><Card /></HoldListener>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
class HoldListener extends React.Component { | |
constructor() { | |
super(); | |
} | |
handleMouseDown() { | |
this.timeout = setTimeout(this.props.onHold, this.props.threshold); | |
} | |
handleMouseUp() { | |
clearTimeout(this.timeout); | |
} | |
componentDidMount() { | |
document.addEventListener('mouseup', this.handleMouseUp.bind(this)); | |
} | |
componentWillUnmount() { | |
document.removeEventListener('mouseup', this.handleMouseUp.bind(this)); | |
} | |
render(){ | |
let props = _.omit(this.props, 'children'); | |
return React.cloneElement(this.props.children, _.merge(props, { | |
onMouseDown: this.handleMouseDown.bind(this) | |
})); | |
} | |
} | |
HoldListener.propTypes = { | |
onHold: React.PropTypes.func.isRequired, | |
children: React.PropTypes.object.isRequired, | |
threshold: React.PropTypes.number | |
}; | |
HoldListener.defaultProps = { | |
threshold: 600 | |
}; | |
export default HoldListener; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment