Created
August 30, 2015 21:30
-
-
Save peterjmag/1d5b0d6aaa53049142d3 to your computer and use it in GitHub Desktop.
TextWithLineBreaks React component
This file contains hidden or 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
var _ = require('lodash'); | |
var React = require('react'); | |
module.exports = React.createClass({ | |
displayName: 'TextWithLineBreaks', | |
render: function () { | |
var nodes = []; | |
var fragments = this.props.text.split('\n'); | |
var key = 0; | |
_.each(fragments, function (fragment) { | |
nodes.push( | |
<span key={key}>{fragment}</span> | |
); | |
key++; | |
nodes.push( | |
<br key={key} /> | |
); | |
key++; | |
}); | |
// Remove extra last <br /> tag. | |
nodes.pop(); | |
return ( | |
<span className="text-with-line-breaks"> | |
{nodes} | |
</span> | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment