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

[TS migration] Migrate 'Header.js' component to TypeScript #30233

Merged
merged 8 commits into from
Nov 13, 2023
64 changes: 0 additions & 64 deletions src/components/Header.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, {ReactElement} from 'react';
import {View, TextStyle} from 'react-native';
import styles from '../styles/styles';
import Text from './Text';
import EnvironmentBadge from './EnvironmentBadge';

type HeaderProps = {
/** Title of the Header */
title?: string | ReactElement;

/** Subtitle of the header */
subtitle?: string | ReactElement;

/** Should we show the environment badge (dev/stg)? */
shouldShowEnvironmentBadge?: boolean;

/** Additional text styles */
textStyles?: TextStyle[];
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
};

function Header({title = '', subtitle = '', textStyles = [], shouldShowEnvironmentBadge = false}: HeaderProps) {
Copy link
Contributor

@bondydaa bondydaa Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoping maybe to learn something here, what's the term (or what is it called) for what we're doing here {} : propTypes?

is this a typescript thing or a newer JS language thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bondydaa It is just the way of typing component props using TypeScript.
You can find similar example in the TS_STYLE

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 ah thanks, I'd completely forgot about "object destructing" addition to JS

return (
<View style={[styles.flex1, styles.flexRow]}>
<View style={styles.mw100}>
{typeof title === 'string'
? Boolean(title) && (
<Text
numberOfLines={2}
style={[styles.headerText, styles.textLarge, ...textStyles]}
>
{title}
</Text>
)
: title}
{/* If there's no subtitle then display a fragment to avoid an empty space which moves the main title */}
{typeof subtitle === 'string'
? Boolean(subtitle) && (
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
<Text
style={[styles.mutedTextLabel, styles.pre]}
numberOfLines={1}
>
{subtitle}
</Text>
)
: subtitle}
</View>
{shouldShowEnvironmentBadge && <EnvironmentBadge />}
</View>
);
}

Header.displayName = 'Header';

export default Header;
7 changes: 7 additions & 0 deletions src/components/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ type TextProps = {
/** The size of the text */
fontSize?: number;

/**
* Used to truncate the text with an ellipsis after computing the text
* layout, including line wrapping, such that the total number of lines
* does not exceed this number.
*/
numberOfLines?: number;

VickyStash marked this conversation as resolved.
Show resolved Hide resolved
/** The alignment of the text */
textAlign?: 'left' | 'right' | 'auto' | 'center' | 'justify';

Expand Down
Loading