| Total Complexity | 1 |
| Complexity/F | 0 |
| Lines of Code | 29 |
| Function Count | 0 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import React from "react"; |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Splits a string on newlines and creates a list of <p> elements with the results. |
||
| 5 | * @param text A string that may or may not contain newlines. |
||
| 6 | * @param props An object with attributes to add to each <p> element. |
||
| 7 | * @returns One or more <p> elements wrapped in a fragment. |
||
| 8 | */ |
||
| 9 | export const textToParagraphs = ( |
||
| 10 | text: string, |
||
| 11 | props?: any, |
||
|
|
|||
| 12 | ): React.ReactFragment => { |
||
| 13 | const items = text.split("\n"); |
||
| 14 | return ( |
||
| 15 | <> |
||
| 16 | {items.map( |
||
| 17 | (item, index): React.ReactElement => ( |
||
| 18 | // eslint-disable-next-line react/no-array-index-key |
||
| 19 | <p key={index} {...props}> |
||
| 20 | {item.trim().length > 0 ? item : <br />} |
||
| 21 | </p> |
||
| 22 | ), |
||
| 23 | )} |
||
| 24 | </> |
||
| 25 | ); |
||
| 26 | }; |
||
| 27 | |||
| 28 | export default textToParagraphs; |
||
| 29 |