Skip to content

Commit

Permalink
feat: add onPress handler props
Browse files Browse the repository at this point in the history
  • Loading branch information
kyunkakata committed Jul 4, 2022
1 parent afe2dab commit 3279a8c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ return (
| metadataContainerStyle | [ViewStyle](https://reactnative.dev/docs/view-style-props) | Title, description and minimized image container style |
| metadataTextContainerStyle | [ViewStyle](https://reactnative.dev/docs/view-style-props) | Title and description container style |
| onPreviewDataFetched | (PreviewData) => void | Callback to get the fetched preview data |
| onPress | (string) => void | Custom handler when click LinkPreview |
| previewData | PreviewData | Data to render instead of parsing the provided text |
| renderDescription | (string) => ReactNode | Custom description render prop |
| renderHeader | (string) => ReactNode | Custom header render prop |
Expand Down
10 changes: 9 additions & 1 deletion src/LinkPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface LinkPreviewProps {
text: string
textContainerStyle?: StyleProp<ViewStyle>
touchableWithoutFeedbackProps?: TouchableWithoutFeedbackProps
onPress?: (link?: string) => void
}

export const LinkPreview = React.memo(
Expand All @@ -61,6 +62,7 @@ export const LinkPreview = React.memo(
text,
textContainerStyle,
touchableWithoutFeedbackProps,
onPress,
}: LinkPreviewProps) => {
const [containerWidth, setContainerWidth] = React.useState(0)
const [data, setData] = React.useState(previewData)
Expand Down Expand Up @@ -110,7 +112,13 @@ export const LinkPreview = React.memo(
[]
)

const handlePress = () => data?.link && Linking.openURL(data.link)
const handlePress = () => {
if (typeof onPress === 'function') {
onPress(data?.link)
} else {
data?.link && Linking.openURL(data.link)
}
}

const renderDescriptionNode = (description: string) => {
return oneOf(
Expand Down

0 comments on commit 3279a8c

Please sign in to comment.