0.0.4 • Published 4 years ago

string2jsx v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

String2JSX

npm version

String2JSX converts an input string into a JSX Element. For an approach that does not return a Fragment or that has more control over its matches, try using react-process-string.

Usage

let conversion = [
  {
    from: /Hello/,
    to: "Goodbye"
  },
  {
    from: /, world/,
    to: <span style={{color: 'red'}} />,
    isChild: true
  },
  {
    from: /!/,
    to: <span>...</span>,
    props: {
      className: 'kinda-dark-tbh'
    }
  }
]

<String2JSX map={conversion}>
    Hello, world!
</String2JSX>
  
// Output: 
<>
  <span>Goodbye</span><span style={{color: 'red'}}>world</span><span className={'kinda-dark-tbh'}>...</span>
</>

Installation

Install the module using NPM:

npm i string2jsx

Then simply import it with:

import String2JSX from 'string2JSX'

Documentation

Props

Required PropsTypeRequiredDescription
childrenstring or string[]yesContains the string (or array of strings) to be converted.
mapobjectyesContains all of the conversion mappins and their respective options.
parentJSX.ElementnoContains the conversions in the given parent element. Props on the given element will be maintained. Any props passed to the String2JSX element will be passed to this parent.Defaults to React.Fragment.
defaultMatchGroupnumbernoOverrides all maps to default to the given regex match group.Defaults to 0.

Map Options

KeyTypeRequiredDescription
fromRegExpyesThe regex used to find all matches within the string.
toJSX.Element or stringyesThe JSX Element to replace any found matches.
propsobjectnoThe props to pass to the new JSX Element (these can also go on the given "to" element). Defaults to {}.
isChildbooleannoSets the match (or the specified matchGroup) as the new JSX Element's "children" prop.Defaults to false.
useForPropsstring[]noSets the match (or the specified matchGroup) to each prop in this list for the new JSX Element.Defaults to [].
matchGroupnumbernoSpecifies the regex match group to use for the above options.Defaults to defaultMatchGroup.
0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago