UNPKG

@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}/> ```

28 lines (23 loc) 468 B
import React from 'react' import limitLines from './limitLines' const _breakRe = /(\n)/g function breaker(line, x) { return line.match(_breakRe) ? React.createElement('br', { key: x + '--' + line, }) : line } function _ref(line) { return line } export default function toBreaks(rawText) { if (!rawText) { return rawText } rawText = limitLines(rawText) return rawText .split(_breakRe) .map(breaker) .filter(_ref) }