-
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.
Add otherProps util and DangerouslySetHTML component (#242)
* Export component specifically for rendering html * Export utility for passing props for higher order components
- Loading branch information
1 parent
d49b2cc
commit d1f9b25
Showing
4 changed files
with
159 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,32 @@ | ||
'use strict'; | ||
|
||
import Component from './JSXComponent'; | ||
import { Config } from 'metal-state'; | ||
|
||
/** | ||
* JSXComponent that renders html passed in. | ||
* @class | ||
*/ | ||
class DangerouslySetHTML extends Component { | ||
/** | ||
* @return {Component} | ||
*/ | ||
render() { | ||
const {content, tag} = this.props; | ||
|
||
IncrementalDOM.elementOpen(tag, null, null); | ||
|
||
const node = IncrementalDOM.elementClose(tag); | ||
|
||
node.innerHTML = content; | ||
|
||
return node; | ||
} | ||
} | ||
|
||
DangerouslySetHTML.PROPS = { | ||
content: Config.string(), | ||
tag: Config.string().value('span'), | ||
}; | ||
|
||
export default DangerouslySetHTML; |
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,48 @@ | ||
'use strict'; | ||
|
||
import DangerouslySetHTML from '../src/DangerouslySetHTML'; | ||
import JSXComponent from '../src/JSXComponent'; | ||
|
||
describe('JSXComponent', function() { | ||
var component; | ||
|
||
afterEach(function() { | ||
if (component) { | ||
component.dispose(); | ||
} | ||
}); | ||
|
||
it('should render', function() { | ||
component = new DangerouslySetHTML(); | ||
assert.strictEqual('SPAN', component.element.tagName); | ||
}); | ||
|
||
it('should render with custom tag', function() { | ||
component = new DangerouslySetHTML({tag: 'div'}); | ||
assert.strictEqual('DIV', component.element.tagName); | ||
}); | ||
|
||
it('should render with html content', function() { | ||
var content = '<h2>hello</h2><div><span>world</span></div>'; | ||
|
||
component = new DangerouslySetHTML({content: content}); | ||
assert.strictEqual(content, component.element.innerHTML); | ||
}); | ||
|
||
it('should render inside of another component', function() { | ||
var content = '<h2>hello</h2><div><span>world</span></div>'; | ||
|
||
class TestComponent extends JSXComponent { | ||
render() { | ||
return ( | ||
<div> | ||
<DangerouslySetHTML content={content} ref="inner" /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
component = new TestComponent(); | ||
assert.strictEqual(content, component.refs.inner.element.innerHTML); | ||
}); | ||
}); |
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