Migrate most React class components to function components
This commit is contained in:
parent
4c9baaef80
commit
558b5a4a38
23 changed files with 1444 additions and 1775 deletions
|
@ -13,43 +13,39 @@ export type Props = {
|
|||
|
||||
const defaultRenderNonNewLine: RenderTextCallbackType = ({ text }) => text;
|
||||
|
||||
export class AddNewLines extends React.Component<Props> {
|
||||
public override render():
|
||||
| JSX.Element
|
||||
| string
|
||||
| null
|
||||
| Array<JSX.Element | string | null> {
|
||||
const { text, renderNonNewLine = defaultRenderNonNewLine } = this.props;
|
||||
const results: Array<JSX.Element | string> = [];
|
||||
const FIND_NEWLINES = /\n/g;
|
||||
export function AddNewLines({
|
||||
text,
|
||||
renderNonNewLine = defaultRenderNonNewLine,
|
||||
}: Props): JSX.Element {
|
||||
const results: Array<JSX.Element | string> = [];
|
||||
const FIND_NEWLINES = /\n/g;
|
||||
|
||||
let match = FIND_NEWLINES.exec(text);
|
||||
let last = 0;
|
||||
let count = 1;
|
||||
let match = FIND_NEWLINES.exec(text);
|
||||
let last = 0;
|
||||
let count = 1;
|
||||
|
||||
if (!match) {
|
||||
return renderNonNewLine({ text, key: 0 });
|
||||
}
|
||||
|
||||
while (match) {
|
||||
if (last < match.index) {
|
||||
const textWithNoNewline = text.slice(last, match.index);
|
||||
count += 1;
|
||||
results.push(renderNonNewLine({ text: textWithNoNewline, key: count }));
|
||||
}
|
||||
|
||||
count += 1;
|
||||
results.push(<br key={count} />);
|
||||
|
||||
last = FIND_NEWLINES.lastIndex;
|
||||
match = FIND_NEWLINES.exec(text);
|
||||
}
|
||||
|
||||
if (last < text.length) {
|
||||
count += 1;
|
||||
results.push(renderNonNewLine({ text: text.slice(last), key: count }));
|
||||
}
|
||||
|
||||
return results;
|
||||
if (!match) {
|
||||
return <>{renderNonNewLine({ text, key: 0 })}</>;
|
||||
}
|
||||
|
||||
while (match) {
|
||||
if (last < match.index) {
|
||||
const textWithNoNewline = text.slice(last, match.index);
|
||||
count += 1;
|
||||
results.push(renderNonNewLine({ text: textWithNoNewline, key: count }));
|
||||
}
|
||||
|
||||
count += 1;
|
||||
results.push(<br key={count} />);
|
||||
|
||||
last = FIND_NEWLINES.lastIndex;
|
||||
match = FIND_NEWLINES.exec(text);
|
||||
}
|
||||
|
||||
if (last < text.length) {
|
||||
count += 1;
|
||||
results.push(renderNonNewLine({ text: text.slice(last), key: count }));
|
||||
}
|
||||
|
||||
return <>{results}</>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue