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

Allow passing ref to Text component in Ignite boilerplate #2758 #2765

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions boilerplate/app/components/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import i18n from "i18n-js"
import React from "react"
import React, { ForwardedRef, PropsWithChildren, forwardRef } from "react"
import { StyleProp, Text as RNText, TextProps as RNTextProps, TextStyle } from "react-native"
import { isRTL, translate, TxKeyPath } from "../i18n"
import { colors, typography } from "../theme"
Expand Down Expand Up @@ -51,7 +51,10 @@ export interface TextProps extends RNTextProps {
* @param {TextProps} props - The props for the `Text` component.
* @returns {JSX.Element} The rendered `Text` component.
*/
export function Text(props: TextProps) {
export const Text = forwardRef(function Text(
props: PropsWithChildren<TextProps>,
ref: ForwardedRef<RNText | null>,
) {
const { weight, size, tx, txOptions, text, children, style: $styleOverride, ...rest } = props

const i18nText = tx && translate(tx, txOptions)
Expand All @@ -67,11 +70,11 @@ export function Text(props: TextProps) {
]

return (
<RNText {...rest} style={$styles}>
<RNText {...rest} style={$styles} ref={ref}>
{content}
</RNText>
)
}
})

const $sizeStyles = {
xxl: { fontSize: 36, lineHeight: 44 } satisfies TextStyle,
Expand Down
8 changes: 5 additions & 3 deletions boilerplate/app/screens/DemoCommunityScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from "react"
import { Image, ImageStyle, TextStyle, View, ViewStyle } from "react-native"
import React, { FC, useRef } from "react"
import { Image, ImageStyle, TextStyle, View, ViewStyle, Text as RNText } from "react-native"
import { ListItem, Screen, Text } from "../components"
import { DemoTabScreenProps } from "../navigators/DemoNavigator"
import { spacing } from "../theme"
Expand All @@ -13,9 +13,11 @@ const reactNativeNewsletterLogo = require("../../assets/images/demo/rnn-logo.png

export const DemoCommunityScreen: FC<DemoTabScreenProps<"DemoCommunity">> =
function DemoCommunityScreen(_props) {
const textRef = useRef<RNText | null>(null)

return (
<Screen preset="scroll" contentContainerStyle={$container} safeAreaEdges={["top"]}>
<Text preset="heading" tx="demoCommunityScreen.title" style={$title} />
<Text preset="heading" tx="demoCommunityScreen.title" style={$title} ref={textRef} />
<Text tx="demoCommunityScreen.tagLine" style={$tagline} />

<Text preset="subheading" tx="demoCommunityScreen.joinUsOnSlackTitle" />
Expand Down