Comments (4)
I have a pretty simple use case, but require nested matching nonetheless. After playing around with react-native-parsed-text
, it was easier to roll my own component that meets my specific needs. The entire component and pattern matching logic is shown below and its simplicity matches that of my use case but perhaps it can be used as inspiration to provide more comprehensive nested pattern matching.
import React, { FunctionComponent, useState } from 'react';
import { StyleProp, Text, TextProps, TextStyle } from 'react-native';
interface P9TextParserConfig {
pattern: RegExp;
style?: StyleProp<TextStyle>;
transform?(text: string): string;
}
interface P9TextParseResult {
match: string;
style?: StyleProp<TextStyle>;
}
export interface P9ParsedTextProps extends TextProps {
parsers: P9TextParserConfig[];
}
export const P9ParsedText: FunctionComponent<P9ParsedTextProps> = ({
children,
parsers,
style: rootStyle,
...rest
}) => {
const [parsed] = useState(parseText([{ match: children as string }], parsers, 0, []));
return (
<>
<Text {...rest} style={rootStyle}>
{parsed.map(({ match, style }, index) =>
style ? (
<Text key={index.toString()} style={[rootStyle, style]}>
{match}
</Text>
) : (
match
)
)}
</Text>
</>
);
};
function parseText(
input: P9TextParseResult[],
parsers: P9TextParserConfig[],
current: number,
matches: P9TextParseResult[]
): P9TextParseResult[] {
if (current === parsers.length) {
return matches;
}
const { pattern, style, transform } = parsers[current];
matches = input
.map(({ match: parent, style: parentStyle }) =>
parent
.split(pattern)
.map((match) =>
pattern.test(match) ? { match: transform ? transform(match) : match, style } : { match, style: parentStyle }
)
)
.reduce((output, next) => output.concat(next), []);
return parseText(matches, parsers, ++current, matches);
}
from react-native-parsed-text.
@sellmeadog -- this is great! Were you proposing this as a way to rewrite this library? or just as an example on how to do it if people need this feature?
A. I'm just the steward of react-native-parsed-text, not the originator, so I'm probably not going to completely rewrite this library.
B. If it's just an example on how to do this if your application needs it, then I'd be comfortable closing this Feature request then!
from react-native-parsed-text.
It's not a strict proposal, I was just trying to showcase what worked for me and see what value it might add to the overall discussion. I think it could be the base of a rewrite, but react-native-parsed-text
seems to do a bit more like supporting click interaction, limits on matching, etc., which I have not taken into consideration.
from react-native-parsed-text.
Can I propose a simple way to fix this would be to allow renderText
to return a React Element? Then you could do like:
{
pattern: /\w+-\d+/,
renderText: (str, matches) => (
<ParsedText
parse={[ ... ]}
>{str}</ParsedText>
),
}
This avoid complicating the parsing logic and moves the recursion onto the caller.
from react-native-parsed-text.
Related Issues (20)
- how to implements using Hashtag and Bold text patterns together? HOT 3
- How to get number of matches? HOT 1
- Change words in text? HOT 1
- Android_all format was change follow the first character when copy paste a string include #, url, text
- could I use it in react for a website HOT 2
- ERROR TypeError: undefined is not an object (evaluating '_reactNative.Text.propTypes.style') in react native 0.70
- URL parser with single quote not working HOT 3
- Bank card number is not phone number
- Pattern for Bold and Italic is not working HOT 3
- parse the state HOT 5
- New release? HOT 1
- Google maps URL not getting parsed due to comma HOT 1
- [Android] Incorrect bold font processing HOT 7
- this.state cannot be parsed HOT 1
- renderText's style does not work after I adding any character inside TextInput tag HOT 2
- Multiple hashtags HOT 1
- how to make ellipsizeMode working?
- Hey Everyone We are supported emoji or emoji parser?
- selectable works on iOS but not on Android. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-parsed-text.