Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-text-format

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-text-format

React Component to find and parse links, emails, phone numbers, credit cards and keywords to required format.

  • 2.0.21
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
897
decreased by-12.74%
Maintainers
1
Weekly downloads
 
Created
Source

react-text-format

React Component to find and parse links, emails, phone numbers, image's URL, credit cards and keywords to required format.

NPM JavaScript Style Guide

View live demo here

Installation

yarn add react-text-format

or

npm install react-text-format --save

Props

NameTypeDefault
allowedFormatsArray ['URL', 'Email', 'Image', 'Phone', 'CreditCard']['URL', 'Email', 'Phone']
linkTargetString (_blank | _self | _parent | _top | framename)_self
termsArray of strings[]
linkDecoratorReact.Node (decoratedHref: string, decoratedText: string, linkTarget: string, key: number)Output Format: <a href="{URL}" target="{target}" rel='noopener' className='rtfLink'> <URL> </a>
emailDecoratorReact.Node (decoratedHref: string, decoratedText: string,key: number)Output Format:<a href="mailto: {EMAIL ADDRESS}" className='rtfEmail'> {EMAIL ADDRESS} </a>
phoneDecoratorReact.Node (decoratedText: string, key: number)Output Format<a href="tel:{PHONE NUMBER}" className='rtfEmail'> {PHONE NUMBER} </a>
creditCardDecoratorReact.Node (decoratedText: string, key: number)Output Format: <span className='rtfCreditCard'> {CREDIT CARD NUMBER} </span>
imageDecoratorReact.Node (decoratedURL: string, key: number)Output Format: <img src="{URL OF IMAGE}" rel='noopener' className='rtfImage' />
termDecoratorReact.Node (decoratedText: string, key: number)Output Format: <span key={key} className='rtfTerm'>{decoratedText}</span>

Usage

Basic Implementation

import ReactTextFormat from 'react-text-format';

React.render(
    <ReactTextFormat>
      This is demo link http://www.google.com
      <br/><br/>
      This is demo email <span data-email="email@span.com">jago@yahoo.com</span>
      <br /><br />
      This is demo image https://preview.ibb.co/hqhoyA/lexie-barnhorn-1114350-unsplash.jpg
      <br /><br />
      This is demo credit Card 5555555555554444
      <br /><br />
      This is demo phone Number 123.456.7890 <br />
      This is demo phone Number (212) 555 1212 <br />
      This is demo phone Number (212) 555-1212 <br />
      This is demo phone Number 212-555-1212 ext. 101 <br />
      This is demo phone Number 212 555 1212 x101 <br />
      <br /><br />
      This is an anchor <a href="http://formatter.com">http://formatter.com</a>;
    </ReactTextFormat>,
  document.body
);
Output:

Generated Avatar

Advance Implementation

import ReactTextFormat from 'react-text-format';

customLinkDecorator = (
    decoratedHref: string,
    decoratedText: string,
    linkTarget: string,
    key: number
  ): React.Node => {
    return (
      <a
        href={decoratedHref}
        key={key}
        target={linkTarget}
        rel='noopener'
        className='customLink'
      >
        {decoratedText}
      </a>
    )
  }

customImageDecorator = (
    decoratedURL: string,
    key: number
    ): React.Node => {
    return (
      <div>
        <img src={decoratedURL} key={key} rel='noopener' width="100" className='customImage' />
      </div>
)
}

customEmailDecorator = (
    decoratedHref: string,
    decoratedText: string,
    key: number
    ): React.Node => {
    return (
      <a href={decoratedHref} key={key} className='customEmail'>
        {decoratedText}
      </a>
    )
}

customPhoneDecorator = (
    decoratedText: string,
    key: number
    ): React.Node => {
    return (
      <a href={`tel:${decoratedText}`} key={key} className='customPhone'>
        {decoratedText}
      </a>
    )
}

customCreditCardDecorator = (
    decoratedText: string,
    key: number
    ): React.Node => {
    return (
      <i key={key} className='customCreditCard'>
        <b>{decoratedText}</b>
      </i>
    )
}

customTermDecorator = (decoratedText: string, key: number): React.Node => {
  return (
    <b key={key} className="keyword">
      {decoratedText}
    </b>
  );
};

React.render(
    <ReactTextFormat
          allowedFormats={['URL', 'Email', 'Image', 'Phone', 'CreditCard']}
          linkDecorator={customLinkDecorator}
          emailDecorator={customEmailDecorator}
          phoneDecorator={customPhoneDecorator}
          creditCardDecorator={customCreditCardDecorator}
          imageDecorator={customImageDecorator}
          terms={["Link", "phone", "image", "Anchor", "email", "Credit"]}
          termDecorator={customTermDecorator}
          >
            This is demo link http://www.google.com
            <br/><br/>
            This is encoded Link http://go%2Emsn%2Ecom/nl/133942%2Easp
            <br />
            This is demo email <span data-email="email@span.com">jago@yahoo.com</span>
            <br /><br />
            This is demo image https://preview.ibb.co/hqhoyA/lexie-barnhorn-1114350-unsplash.jpg
            <br /><br />
            This is demo credit Card 5555555555554444
            <br /><br />
            This is demo phone Number 123.456.7890 <br />
            This is demo phone Number (212) 555 1212 <br />
            This is demo Phone Number (212) 555-1212 <br />
            This is demo phone Number 212-555-1212 ext. 101 <br />
            This is demo phone Number 212 555 1212 x101 <br />
            <br /><br />
            This is an anchor <a href="http://formatter.com">http://formatter.com</a>;
        </ReactTextFormat>,
        document.body
);
Output:

Generated Avatar

Keywords

FAQs

Package last updated on 09 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc