0.1.6 • Published 5 years ago

react-substring v0.1.6

Weekly downloads
465
License
MIT
Repository
github
Last release
5 years ago

react-substring

Decorating substrings with components. Useful to highlight something in your text content.

NPM registry NPM license

Demo

Here is an examples page

Install

# Yarn
yarn add react-substring

# NPM
npm install --save react-substring

Features

  • Search substrings by simple string or regular expressions.
  • Decorate substrings with custom components or just add some style or className to them.
  • Personal decorating for each match pattern.

Usage

import React from 'react';
import Substring from 'react-substring';

const RedString = ({ children }) => <span style={{ color: 'red' }}>{children}</span>;

const DemoComponent = () => (
    <Substring
        substrings={[
            {
                match: new RegExp('Decorate', 'gi'),
                component: 'b',
            },
            {
                match: 'custom',
                caseSensitive: true,
                component: RedString,
            },
            {
                match: ['just', 'some', /className/gi],
                props: { style: { backgroundColor: 'red' } },
            },
        ]}
    >
        Decorate substrings with custom components or just add some style or className to them
    </Substring>
);

API

Props

PropertyTypeRequired?Description
substringsArrayArray on Substring objects.
outerTagNameStringTag name passed to document.createElement to create the outer container element. (Default: span)
styleObjectOptional inline style to attach to outermost element.
classNameObjectOptional inline style to attach to outermost element.

Substring object fields

Field nameTypeRequired?Description
matchRegExp or StringPattern to search substrings for processing. Can use array of patterns here.
componentReact.Component or StringReact component or tag name taking matching content. (Default: mark)
caseSensitiveBoolIf use match as string - it allows to switch on case sensitive search substring (Default: false)
propsObjectAdditional props for matching component (can use style, className or something else)

License

MIT © avin

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago