Skip to content

Instantly share code, notes, and snippets.

@slorber
Forked from brigand/gist:c648e589b34b1d023ec5
Created January 20, 2015 17:56
Show Gist options
  • Save slorber/955ca0dcb41d48ff7198 to your computer and use it in GitHub Desktop.
Save slorber/955ca0dcb41d48ff7198 to your computer and use it in GitHub Desktop.
var WithStopPropagation = React.createClass({
propTypes: {
children: React.PropTypes.node.isRequired,
eventNames: React.PropTypes.arrayOf(React.PropTypes.string).isRequired
},
stopPropagation: function(e){
e.stopPropagation();
},
// returns something like { onClick: stopPropagation, onFocus: stopPropagation , ... }
makeEventHandlers: function(){
return this.props.eventNames.reduce(function(props, name) {
props[name] = this.stopPropagation;
return props
}.bind(this),{});
},
render: function() {
return React.createElement(
'span', // Use span as wrapper node to avoid unwanted CSS effects
this.makeEventHandlers(),
React.Children.only(this.props.children)
);
}
});
module.exports = WithStopPropagation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment