Total Complexity | 2 |
Complexity/F | 0 |
Lines of Code | 35 |
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 | * @param indexedProps Maps objects of attributes to the paragraph index they apply to, allowing different attributes for specific paragraphs. |
||
8 | * @returns One or more <p> elements wrapped in a fragment. |
||
9 | */ |
||
10 | export const textToParagraphs = ( |
||
11 | text: string, |
||
12 | props?: { [attribute: string]: any }, |
||
13 | indexedProps?: { [index: number]: any }, |
||
14 | ): React.ReactFragment => { |
||
15 | const items = text.split("\n"); |
||
16 | return ( |
||
17 | <> |
||
18 | {items.map( |
||
19 | (item, index): React.ReactElement => { |
||
20 | const getIndexedProps = |
||
21 | indexedProps && indexedProps[index] ? indexedProps[index] : ""; |
||
22 | return ( |
||
23 | // eslint-disable-next-line react/no-array-index-key |
||
24 | <p key={index} {...props} {...getIndexedProps}> |
||
25 | {item.trim().length > 0 ? item : <br />} |
||
26 | </p> |
||
27 | ); |
||
28 | }, |
||
29 | )} |
||
30 | </> |
||
31 | ); |
||
32 | }; |
||
33 | |||
34 | export default textToParagraphs; |
||
35 |