Я использую response-native-gifted-chat (версия 0.13.0, последняя версия) и свойство parsePatterns, например:
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
alwaysShowSend={true}
user={user}
showUserAvatar={true}
renderUsernameOnMessage={true}
parsePatterns={(linkStyle) => [
{ pattern: /#(\w+)/, style: { textDecorationLine: 'underline', color: '#A4A4A4' }, onPress :
(key, index) => this.onPressHashtag(key, index) }
]}
/>
parsePatterns используется renderMessageText (react-native-gifted- чат). Вот рендер в классе MessageText.
render() {
const linkStyle = [
styles[this.props.position].link,
this.props.linkStyle && this.props.linkStyle[this.props.position],
]
return (
<View
style={[
styles[this.props.position].container,
this.props.containerStyle &&
this.props.containerStyle[this.props.position],
]}
>
<ParsedText
style={[
styles[this.props.position].text,
this.props.textStyle && this.props.textStyle[this.props.position],
this.props.customTextStyle,
]}
parse={[
...this.props.parsePatterns!(linkStyle as TextStyle),
{ type: 'url', style: linkStyle, onPress: this.onUrlPress },
{ type: 'phone', style: linkStyle, onPress: this.onPhonePress },
{ type: 'email', style: linkStyle, onPress: this.onEmailPress },
]}
childrenProps={{ ...this.props.textProps }}
>
{this.props.currentMessage!.text}
</ParsedText>
</View>
)
}
Вот часть кода ParsedText. js (response-native-parsed-text)
getPatterns() {
return this.props.parse.map((option) => {
const {type, ...patternOption} = option;
if (type) {
if (!PATTERNS[type]) {
throw new Error(`${option.type} is not a supported type`);
}
patternOption.pattern = PATTERNS[type];
}
return patternOption;
});
}
getParsedText() {
if (!this.props.parse) { return this.props.children; }
if (typeof this.props.children !== 'string') { return this.props.children; }
const textExtraction = new TextExtraction(this.props.children, this.getPatterns());
return textExtraction.parse().map((props, index) => {
const { style: parentStyle } = this.props
const { style, ...remainder } = props
return (
<Text
key={`parsedText-${index}`}
style={[parentStyle, style]}
{...this.props.childrenProps}
{...remainder}
/>
);
});
}
render() {
// Discard custom props before passing remainder to Text
const { parse, childrenProps, ...remainder } = { ...this.props };
return (
<Text ref={ref => (this._root = ref)} {...remainder}>
{this.getParsedText()}
</Text>
);
}
}
Я бы хотел бы получить currentMessage._id, когда я вызываю метод onPressHashTag, но я получаю только сопоставленный текст и индекс.
Как я могу получить currentMessage._id из parsepatterns, например "/ # (\ w +) / "после рендеринга?