Skip to content

Instantly share code, notes, and snippets.

@adjohu
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adjohu/d6d7388dac9a75b8b469 to your computer and use it in GitHub Desktop.
Save adjohu/d6d7388dac9a75b8b469 to your computer and use it in GitHub Desktop.
react hold listener wrapper usage <HoldListener onHold={this.handleHold}><Card /></HoldListener>
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