Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Commit

Permalink
Add classSet property as a shortcut for React.addons.classSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
cain-wang committed Jan 12, 2015
1 parent aa40b26 commit 18f88a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = React.createClass({
r.h2('This is React.js markup')
r(AnotherComponent, {foo: 'bar'}),
r.div({
className: { // automatically use React classSet
classSet: { // automatically use React classSet
foo: this.props.foo,
bar: this.props.bar
},
Expand Down Expand Up @@ -52,4 +52,4 @@ Returns a React element

- **isRendered** `Boolean` - If strictly to false, React will skip rendering the target component.

- **className** `String|Object` - If the className value is an object, apply React.addons.classSet() automatically.
- **classSet** `String|Object` - If the classSet value is an object, apply React.addons.classSet() automatically and assign to className.
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,23 @@ function r(component, properties, children) {
return React.createElement(component, properties);
}

// Wraps the className property value with React classSet if it's an object.
// Wraps the classSet property value with React.addons.classSet
// and merge into className.
function processClasses(properties) {
var className = properties.className;
var classSetConfig = properties.classSet;
if (classSetConfig) {
var className = properties.className;
if (className && typeof className === 'string') {
var names = className.match(/\S+/g);

if (className && typeof className === 'object') {
properties.className = classSet(className);
if (names) {
for (var i = 0; i < names.length; i++) {
classSetConfig[names[i]] = true;
}
}
}

properties.className = classSet(classSetConfig);
}
}

Expand Down
7 changes: 4 additions & 3 deletions test/fixtures/render-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ module.exports = {
)
},
componentWithDynamicClassNames: {
html: '<div><h1></h1><div class="class1 class2"></div></div>',
html: '<div><h1></h1><div class="class1 class3 class4"></div></div>',
dom: (
r(Component, [
r.div({
className: {
className: ' class3 class4 ',
classSet: {
class1: true,
class2: true,
class2: false,
class3: false
}
})
Expand Down

0 comments on commit 18f88a0

Please sign in to comment.