Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing for parsePatterns prop in MessageText.tsx, extracted ParsedShape type #2259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/MessageText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import PropTypes from 'prop-types'
import React from 'react'
import {
Linking,
StyleProp,
StyleSheet,
View,
TextProps,
StyleProp,
ViewStyle,
TextStyle,
View,
ViewStyle,
} from 'react-native'

// @ts-ignore
import ParsedText from 'react-native-parsed-text'
import { LeftRightStyle, IMessage } from './Models'
import { StylePropType } from './utils'
import ParsedText, { ParsedTextProps } from 'react-native-parsed-text'
import { useChatContext } from './GiftedChatContext'
import { error } from './logging'
import { IMessage, LeftRightStyle } from './Models'
import { StylePropType } from './utils'

const WWW_URL_PATTERN = /^www\./i

Expand Down Expand Up @@ -57,6 +56,9 @@ const styles = {

const DEFAULT_OPTION_TITLES = ['Call', 'Text', 'Cancel']

// ParsedShape type extraction
type ParsedShape = ParsedTextProps['parse']

export interface MessageTextProps<TMessage extends IMessage> {
position?: 'left' | 'right'
optionTitles?: string[]
Expand All @@ -66,7 +68,7 @@ export interface MessageTextProps<TMessage extends IMessage> {
linkStyle?: LeftRightStyle<TextStyle>
textProps?: TextProps
customTextStyle?: StyleProp<TextStyle>
parsePatterns?(linkStyle: TextStyle): any
parsePatterns?(linkStyle: StyleProp<TextStyle>): ParsedShape
}

export function MessageText<TMessage extends IMessage = IMessage>({
Expand Down Expand Up @@ -141,7 +143,8 @@ export function MessageText<TMessage extends IMessage = IMessage>({
const linkStyle = [
styles[position].link,
linkStyleProp && linkStyleProp[position],
]
] as StyleProp<TextStyle>

return (
<View
style={[
Expand All @@ -156,12 +159,12 @@ export function MessageText<TMessage extends IMessage = IMessage>({
customTextStyle,
]}
parse={[
...parsePatterns!(linkStyle as TextStyle),
...parsePatterns!(linkStyle)!,
{ type: 'url', style: linkStyle, onPress: onUrlPress },
{ type: 'phone', style: linkStyle, onPress: onPhonePress },
{ type: 'email', style: linkStyle, onPress: onEmailPress },
]}
childrenProps={{ ...textProps }}
childrenProps={textProps}
>
{currentMessage!.text}
</ParsedText>
Expand Down