0.0.22 • Published 5 years ago

react-native-parsed-text v0.0.22

Weekly downloads
29,507
License
MIT
Repository
github
Last release
5 years ago

React Native Parsed Text

This library allows you to parse a text and extract parts using a RegExp or predefined patterns. Currently there are 3 predefined types: url, phone and email.

All the props are passed down to a new Text Component if there is a matching text. If those are functions they will receive as param the value of the text.

Proptypes

ParsedText can receive Text PropTypes.

parse: Array of parsed text.

  • to use the predefined type: {type: 'url'}.
  • to use your own RegExp: {pattern: /something/}.

renderText: Function called to change the displayed children.

childrenProps : Properties to pass to the children elements generated by react-native-parsed-text.

eg: Your str is 'Mention [@michel:5455345]' where 5455345 is ID of this user and @michel the value to display on interface. Your pattern for ID & username extraction : /\[(@[^:]+):([^\]]+)\]/i Your renderText method :

renderText(matchingString, matches) {
  // matches => ["[@michel:5455345]", "@michel", "5455345"]
  let pattern = /\[(@[^:]+):([^\]]+)\]/i;
  let match = matchingString.match(pattern);
  return `^^${match[1]}^^`;
}

Displayed text will be : Mention ^^@michel^^

Example

import ParsedText from 'react-native-parsed-text';

class Example extends React.Component {
  static displayName = 'Example';

  handleUrlPress(url, matchIndex /*: number*/) {
    LinkingIOS.openURL(url);
  }

  handlePhonePress(phone, matchIndex /*: number*/) {
    AlertIOS.alert(`${phone} has been pressed!`);
  }

  handleNamePress(name, matchIndex /*: number*/) {
    AlertIOS.alert(`Hello ${name}`);
  }

  handleEmailPress(email, matchIndex /*: number*/) {
    AlertIOS.alert(`send email to ${email}`);
  }

  renderText(matchingString, matches) {
    // matches => ["[@michel:5455345]", "@michel", "5455345"]
    let pattern = /\[(@[^:]+):([^\]]+)\]/i;
    let match = matchingString.match(pattern);
    return `^^${match[1]}^^`;
  }

  render() {
    return (
      <View style={styles.container}>
        <ParsedText
          style={styles.text}
          parse={
            [
              {type: 'url',                       style: styles.url, onPress: this.handleUrlPress},
              {type: 'phone',                     style: styles.phone, onPress: this.handlePhonePress},
              {type: 'email',                     style: styles.email, onPress: this.handleEmailPress},
              {pattern: /Bob|David/,              style: styles.name, onPress: this.handleNamePress},
              {pattern: /\[(@[^:]+):([^\]]+)\]/i, style: styles.username, onPress: this.handleNamePress, renderText: this.renderText},
              {pattern: /42/,                     style: styles.magicNumber},
              {pattern: /#(\w+)/,                 style: styles.hashTag},
            ]
          }
          childrenProps={{allowFontScaling: false}}
        >
          Hello this is an example of the ParsedText, links like http://www.google.com or http://www.facebook.com are clickable and phone number 444-555-6666 can call too.
          But you can also do more with this package, for example Bob will change style and David too. foo@gmail.com
          And the magic number is 42!
          #react #react-native
        </ParsedText>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },

  url: {
    color: 'red',
    textDecorationLine: 'underline',
  },

  email: {
    textDecorationLine: 'underline',
  },

  text: {
    color: 'black',
    fontSize: 15,
  },

  phone: {
    color: 'blue',
    textDecorationLine: 'underline',
  },

  name: {
    color: 'red',
  },

  username: {
    color: 'green',
    fontWeight: 'bold'
  },

  magicNumber: {
    fontSize: 42,
    color: 'pink',
  },

  hashTag: {
    fontStyle: 'italic',
  },

});

npm.io

Install

npm install --save react-native-parsed-text

TODO

  • Add nested text parsing
react-native-pkrs-gifted-chatrn-gifted-chat-git@ubaids/react-native-gifted-chatreact-native-gifted-chat-ac@gimwee81/react-native-gifted-chatalex-frontend-sdk-staging@infinitebrahmanuniverse/nolb-react-native-pareact-native-gifted-chat-18@everything-registry/sub-chunk-2586syntx-chatwonderpool-app-driverwonderpool-app-rider@cuongweallnet/rn-gifted-chat@cookiespam/react-native-gifted-chatreact-native-kora-botsdkubiatar-react-native-material-uitmt-react-native-gifted-chat@applications-developer/react-native-gifted-chat@anastely/rn-gifted-chat@autopedia/react-native-ui@ahmedfouad2255/react-native-gifted-chat@aexer/react-native-gifted-chat@alaneu/react-native-gifted-chat@alexal1/react-native-gifted-chat@beaudinn/react-native-gifted-chat@bravemobile/react-native-gifted-chat@2060.io/react-native-gifted-chat@deelzat/react-native-gifted-chat@dasubh/nxmobile@dasubh/sdk-uimobile@dasubh/uimobilezero-wolf-gifted-chatzy-react-native-gifted-chatwecare-react-web-gifted-chatzl-react-native-gifted-chat@hammadj/react-native-gifted-chat@dytesdk/mobile@flyerhq/react-native-chat-ui@foreword/react-native-gifted-chat@haflinger/react-native-gifted-chat@hatkom/react-native-gifted-chat@hunglmtb/react-native-chat-ui@ignaciofalco/react-native-gifted-chat@michaelkitson/react-native-gifted-chat@matoz/react-native-gifted-chat@marblex/react-native-component@nabeel91/react-native-chat-ui@ndl_studio/react-native-gifted-chat@nois/react-native-gifted-chat@eohjsc/react-native-smart-city@eohjsc/react-native-hts@epnsproject/frontend-sdk@epnsproject/frontend-sdk-staging@epnsproject/sdk-uimobile@echobind/react-native-gifted-chat@kbhardwaj/react-native-gifted-chat@karan-push/react-native-package@jamesism/react-native-gifted-chat@jesses/react-native-gifted-chat@iwheels/react-native-chat-ui@knockmsg/chat-ui@imatis/react-native-gifted-chat@jemmyphan/react-native-gifted-chat@jielu/react-native-gifted-chat@raydeck/react-native-gifted-chat@platformbuilders/react-native-chatter@pushprotocol/reactnative@starling-tech/react-native-gifted-chat@sherwinw/react-native-gifted-chat@sharecare/react-native-gifted-chat@smuxx/react-native-gifted-chatpapua-react-native-gifted-chatonboarding-widget-usnusendra-react-native-gifted-chatreact-native-dolphin-chatreact-native-dialog-chatbotreact-native-dialogflow-messengerreact-native-code-editreact-native-chatvtreact-native-chatwhatreact-native-extended-gifted-chatreact-native-customizable-chatreact-native-easy-chatreact-native-lifetime-livechatreact-native-kore-bot-69-devreact-native-kore-bot-template-devreact-native-kore-botsdkreact-native-kore-botsdk-devreact-native-kore-botsdk-qareact-native-kore-botsdk-testreact-native-gifted-chatreact-native-gifted-chat-1mgreact-native-gifted-chat-assacreact-native-gifted-chat-bbwreact-native-gifted-chat-changwoolabreact-native-gifted-chat-custom-textinputreact-native-gifted-chat-do-not-usereact-native-gifted-chat-exporeact-native-gifted-chat-fallreact-native-gifted-chat-fix
0.0.22

5 years ago

0.0.22-beta.3

5 years ago

0.0.22-beta.2

5 years ago

0.0.22-beta.1

5 years ago

0.0.21

6 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago