UNPKG

rn-text-highlight

Version:
1 lines 829 B
import React from"react";import{Text}from"react-native";import propTypes from"prop-types";const RnTextHighlight=e=>{const{originText:r,keyword:l,keywordStyle:o,textStyle:t}=e;return React.createElement(React.Fragment,null,React.createElement(Text,{style:t},(()=>{if(!l||0===l.length)return React.createElement(Text,null,r);var e=l.split("").reduce((e,t,r)=>r===l.length-1?e+`(${t})`:e+`(${t})|`,""),e=new RegExp(e);const t=r.split(e);return t.map((e,t)=>e?l.includes(e)?React.createElement(Text,{style:o,key:t},e):React.createElement(Text,{key:t},e):null)})()))};export default RnTextHighlight;RnTextHighlight.propTypes={originText:propTypes.string,keyword:propTypes.string,keywordStyle:propTypes.object,textStyle:propTypes.object},RnTextHighlight.defaultProps={originText:"",keyword:"",textStyle:{},keywordStyle:{color:"#f40"}};