react-wrap-text
Version:
A simple utility to wrap matching substrings in React with custom jsx components
25 lines (22 loc) • 967 B
TypeScript
import { ReactNode } from 'react';
/**
* Replaces all instances of a target substring in the input with a custom ReactNode.
*
* @param input - The input string.
* @param target - The exact string to match.
* @param render - A render function that returns a ReactNode for each match.
* @returns An array of strings and ReactNodes.
*/
declare function wrapMatchesInText(input: string, target: string, render: (match: string, index: number) => ReactNode): ReactNode[];
/**
* Applies `wrapMatchesInText` repeatedly over an input string or ReactNode array
* using multiple match/render rules.
*
* @param input - The initial string or ReactNode array
* @param matches - An array of { target, render } match definitions
*/
declare function wrapMultipleMatchesInText(input: string | ReactNode[], matches: {
target: string;
render: (match: string, index: number) => ReactNode;
}[]): ReactNode[];
export { wrapMatchesInText, wrapMultipleMatchesInText };