-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from bryceosterhaus/fragments
Adds Fragment component
- Loading branch information
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
import Component from './JSXComponent'; | ||
|
||
/** | ||
* JSXComponent that renders children passed in. | ||
* @class | ||
*/ | ||
class Fragment extends Component { | ||
/** | ||
* @return {Component} | ||
*/ | ||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
|
||
Fragment.PROPS = { | ||
elementClasses: { | ||
setter: () => undefined, | ||
validator: () => { | ||
return new Error( | ||
`Warning: passing 'elementClasses' to 'Fragment' will add class | ||
to first child element. This is not recommended.` | ||
); | ||
}, | ||
}, | ||
}; | ||
|
||
export default Fragment; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
import Fragment from '../src/Fragment'; | ||
import JSXComponent from '../src/JSXComponent'; | ||
|
||
describe('Fragment', function() { | ||
let component; | ||
|
||
afterEach(function() { | ||
if (component) { | ||
component.dispose(); | ||
} | ||
}); | ||
|
||
it('should render children with no wrapping element', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return ( | ||
<div> | ||
<Fragment> | ||
<span>foo</span> | ||
<span>bar</span> | ||
<span>baz</span> | ||
</Fragment> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
component = new TestComponent(); | ||
|
||
assert.strictEqual( | ||
'<span>foo</span><span>bar</span><span>baz</span>', | ||
component.element.innerHTML | ||
); | ||
}); | ||
|
||
it('should render be able to map through fragments', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return ( | ||
<div> | ||
{[0, 1, 2].map(i => ( | ||
<Fragment key={i}> | ||
<span>foo {i}</span> | ||
<span>bar {i}</span> | ||
<span>baz {i}</span> | ||
</Fragment> | ||
))} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
component = new TestComponent(); | ||
|
||
assert.strictEqual( | ||
'<span>foo 0</span><span>bar 0</span><span>baz 0</span><span>foo 1</span><span>bar 1</span><span>baz 1</span><span>foo 2</span><span>bar 2</span><span>baz 2</span>', | ||
component.element.innerHTML | ||
); | ||
}); | ||
|
||
it('should not set elementClasses prop', function() { | ||
class TestComponent extends JSXComponent { | ||
render() { | ||
return ( | ||
<Fragment elementClasses="test"> | ||
<span>foo</span> | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
component = new TestComponent(); | ||
|
||
assert.strictEqual('<span>foo</span>', component.element.outerHTML); | ||
}); | ||
}); |