Giter VIP home page Giter VIP logo

react-text-format's Introduction

react-text-format

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

NPM JavaScript Style Guide

Installation

yarn add react-text-format

or

npm install react-text-format --save

Props

Name Type Default
allowedFormats Array ['URL', 'Email', 'Image', 'Phone', 'CreditCard'] ['URL', 'Email', 'Phone']
linkTarget String (_blank | _self | _parent | _top | framename) _self
LinkDecorator React.Node (decoratedHref: string, decoratedText: string, linkTarget: string, key: number) Output Format: <a href="{URL}" target="{target}" rel='noopener' className='rtfLink'> <URL> </a>
EmailDecorator React.Node (decoratedHref: string, decoratedText: string,key: number) Output Format:<a href="mailto: {EMAIL ADDRESS}" className='rtfEmail'> {EMAIL ADDRESS} </a>
PhoneDecorator React.Node (decoratedText: string, key: number) Output Format<a href="tel:{PHONE NUMBER}" className='rtfEmail'> {PHONE NUMBER} </a>
CreditCardDecorator React.Node (decoratedText: string, key: number) Output Format: <span className='rtfCreditCard'> {CREDIT CARD NUMBER} </span>
ImageDecorator React.Node (decoratedURL: string, key: number) Output Format: <img src="{URL OF IMAGE}" rel='noopener' className='rtfImage' />

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 protected]">[email protected]</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 contact Number 2125551212.In this example, we will use this for demo.<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>
    )
}

React.render(
    <ReactTextFormat
          allowedFormats={['URL', 'Email', 'Image', 'Phone', 'CreditCard']}
          LinkDecorator={customLinkDecorator}
          EmailDecorator={customEmailDecorator}
          PhoneDecorator={customPhoneDecorator}
          CreditCardDecorator={customCreditCardDecorator}
          ImageDecorator={customImageDecorator}
          >
            This is demo link http://www.google.com
            <br/><br/>
            This is demo email <span data-email="[email protected]">[email protected]</span>
            <br /><br />
            This is demo image https://preview.ibb.co/hqhoyA/lexie-barnhorn-1114350-unsplash.jpg
            <br /><br />
            This is demo credit Card 4111111111111111
            <br /><br />
            This is contact Number 2125551212.In this example, we will use this for demo.<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

react-text-format's People

Contributors

ahsan-bilal-786 avatar ahsanbilalas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.