@render-props/paragraphs
Version:
A function as child component which provides an interface for creating paragraphs with line breaks from raw text with `\n` new lines. The default render child is: ```js const defaultParagraph = props => <p key={props.key} children={props.text}/> ```
67 lines (52 loc) • 1.43 kB
JavaScript
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault')
exports.__esModule = true
exports.default = Paragraphs
var _react = _interopRequireDefault(require('react'))
var _propTypes = _interopRequireDefault(require('prop-types'))
var _limitLines = _interopRequireDefault(require('./limitLines'))
var _toBreaks = _interopRequireDefault(require('./toBreaks'))
/*
import Paragraphs from '@render-props/paragraphs'
<Paragraphs text={text}>
{({key, text, n, count}) => (
<p
className={n === count - 1 ? 'm--b0' : 'm--b3'}
children={text}
/>
)}
</Paragraphs>
*/
const _pRe = /(\n{2})/g
const defaultParagraph = props =>
_react.default.createElement('p', {
key: props.key,
children: props.text,
})
function _ref(p) {
return !p.match(_pRe) && p.length
}
function Paragraphs(props) {
const lines = (0, _limitLines.default)(props.text)
.split(_pRe)
.filter(_ref)
const paragraphs = []
const children = props.children || defaultParagraph
const count = lines.length
for (let x = 0; x < lines.length; x++) {
const line = (0, _toBreaks.default)(lines[x])
paragraphs.push(
children({
key: x + '--' + line,
text: line,
n: x,
count,
})
)
}
return paragraphs
}
Paragraphs.propTypes = {
text: _propTypes.default.string.isRequired,
children: _propTypes.default.func.isRequired,
}