Skip to content

Instantly share code, notes, and snippets.

@jkimbo
Last active August 29, 2015 14:15
Show Gist options
  • Save jkimbo/2827e4a7ab7fb0041fab to your computer and use it in GitHub Desktop.
Save jkimbo/2827e4a7ab7fb0041fab to your computer and use it in GitHub Desktop.
React Classname Mixin
"use strict";
import React from 'react/addons';
var cx = React.addons.classSet;
var ClassNameMixin = {
propTypes: {
className: React.PropTypes.string
},
getClassSet: function(classes) {
classes = classes || {};
if (this.props.className) {
classes[this.props.className] = true;
}
return cx(classes);
}
};
export default ClassNameMixin;
import React from 'react/addons';
import ClassNameMixin from './ClassNameMixin';
var Button = React.createClass({
mixins: [ClassNameMixin],
render: function() {
var classes = {
'Button': true,
'Button--primary': true
};
return (
<button className={this.getClassSet({classes})}>{this.props.children}</button>
);
}
});
var Form = React.createClass({
render: function() {
return (
<form>
<Button className="Button--small">Click Me!</Button>
</form>
);
// you can also pass the result of a class set
var classes = {
'Button--small': true
};
var cx = React.addons.classSet;
return (
<form>
<Button className={cx(classes)}>Click Me!</Button>
</form>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment