Skip to content

Commit

Permalink
Merge pull request Expensify#30907 from software-mansion-labs/ts-migr…
Browse files Browse the repository at this point in the history
…ation/TextLink

[TS migration] Migrate TextLink to Typescript
  • Loading branch information
jasperhuangg authored Dec 7, 2023
2 parents 749e6e3 + 83504ed commit 291da7b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 115 deletions.
32 changes: 18 additions & 14 deletions src/components/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import React, {ForwardedRef} from 'react';
// eslint-disable-next-line no-restricted-imports
import {Text as RNText, TextProps as RNTextProps, StyleSheet} from 'react-native';
import type {TextStyle} from 'react-native';
import fontFamily from '@styles/fontFamily';
import useTheme from '@styles/themes/useTheme';
import variables from '@styles/variables';
import ChildrenProps from '@src/types/utils/ChildrenProps';

type TextProps = RNTextProps & {
/** The color of the text */
color?: string;
type TextProps = RNTextProps &
ChildrenProps & {
/** The color of the text */
color?: string;

/** The size of the text */
fontSize?: number;
/** The size of the text */
fontSize?: number;

/** The alignment of the text */
textAlign?: 'left' | 'right' | 'auto' | 'center' | 'justify';
/** The alignment of the text */
textAlign?: TextStyle['textAlign'];

/** Any children to display */
children: React.ReactNode;
/** Any children to display */
children: React.ReactNode;

/** The family of the font to use */
family?: keyof typeof fontFamily;
};
/** The family of the font to use */
family?: keyof typeof fontFamily;
};

function Text({color, fontSize = variables.fontSizeNormal, textAlign = 'left', children = null, family = 'EXP_NEUE', style = {}, ...props}: TextProps, ref: ForwardedRef<RNText>) {
function Text({color, fontSize = variables.fontSizeNormal, textAlign = 'left', children, family = 'EXP_NEUE', style = {}, ...props}: TextProps, ref: ForwardedRef<RNText>) {
const theme = useTheme();

const componentStyle: TextStyle = {
color: color ?? theme.text,
fontSize,
Expand All @@ -36,6 +38,7 @@ function Text({color, fontSize = variables.fontSizeNormal, textAlign = 'left', c
if (!componentStyle.lineHeight && componentStyle.fontSize === variables.fontSizeNormal) {
componentStyle.lineHeight = variables.fontSizeNormalHeight;
}

return (
<RNText
allowFontScaling={false}
Expand All @@ -52,3 +55,4 @@ function Text({color, fontSize = variables.fontSizeNormal, textAlign = 'left', c
Text.displayName = 'Text';

export default React.forwardRef(Text);
export type {TextProps};
101 changes: 0 additions & 101 deletions src/components/TextLink.js

This file was deleted.

79 changes: 79 additions & 0 deletions src/components/TextLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, {ForwardedRef, forwardRef, KeyboardEventHandler, MouseEventHandler} from 'react';
import {GestureResponderEvent, Text as RNText, StyleProp, TextStyle} from 'react-native';
import useEnvironment from '@hooks/useEnvironment';
import useThemeStyles from '@styles/useThemeStyles';
import * as Link from '@userActions/Link';
import CONST from '@src/CONST';
import Text, {TextProps} from './Text';

type LinkProps = {
/** Link to open in new tab */
href: string;

onPress?: undefined;
};

type PressProps = {
href?: undefined;

/** Overwrites the default link behavior with a custom callback */
onPress: () => void;
};

type TextLinkProps = (LinkProps | PressProps) &
TextProps & {
/** Additional style props */
style?: StyleProp<TextStyle>;

/** Callback that is called when mousedown is triggered */
onMouseDown?: MouseEventHandler;
};

function TextLink({href, onPress, children, style, onMouseDown = (event) => event.preventDefault(), ...rest}: TextLinkProps, ref: ForwardedRef<RNText>) {
const {environmentURL} = useEnvironment();
const styles = useThemeStyles();

const openLink = () => {
if (onPress) {
onPress();
} else {
Link.openLink(href, environmentURL);
}
};

const openLinkOnTap = (event: GestureResponderEvent) => {
event.preventDefault();

openLink();
};

const openLinkOnEnterKey: KeyboardEventHandler = (event) => {
if (event.key !== 'Enter') {
return;
}
event.preventDefault();

openLink();
};

return (
<Text
style={[styles.link, style]}
role={CONST.ACCESSIBILITY_ROLE.LINK}
href={href}
onPress={openLinkOnTap}
onKeyDown={openLinkOnEnterKey}
onMouseDown={onMouseDown}
ref={ref}
suppressHighlighting
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{children}
</Text>
);
}

TextLink.displayName = 'TextLink';

export default forwardRef(TextLink);

0 comments on commit 291da7b

Please sign in to comment.