You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
878 B
TypeScript
41 lines
878 B
TypeScript
7 years ago
|
import React from 'react';
|
||
|
|
||
|
interface Props {
|
||
|
text: string;
|
||
|
}
|
||
|
|
||
|
export class AddNewLines extends React.Component<Props, {}> {
|
||
|
public render() {
|
||
|
const { text } = this.props;
|
||
|
const results: Array<any> = [];
|
||
|
const FIND_NEWLINES = /\n/g;
|
||
|
|
||
|
let match = FIND_NEWLINES.exec(text);
|
||
|
let last = 0;
|
||
|
let count = 1;
|
||
|
|
||
|
if (!match) {
|
||
|
return <span>{text}</span>;
|
||
|
}
|
||
|
|
||
|
while (match) {
|
||
|
if (last < match.index) {
|
||
|
const textWithNoNewline = text.slice(last, match.index);
|
||
|
results.push(<span key={count++}>{textWithNoNewline}</span>);
|
||
|
}
|
||
|
|
||
|
results.push(<br key={count++} />);
|
||
|
|
||
|
// @ts-ignore
|
||
|
last = FIND_NEWLINES.lastIndex;
|
||
|
match = FIND_NEWLINES.exec(text);
|
||
|
}
|
||
|
|
||
|
if (last < text.length) {
|
||
|
results.push(<span key={count++}>{text.slice(last)}</span>);
|
||
|
}
|
||
|
|
||
|
return <span>{results}</span>;
|
||
|
}
|
||
|
}
|