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 'InlineCodeBlock' component to TypeScript #32477

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
77 changes: 0 additions & 77 deletions src/components/InlineCodeBlock/WrappedText.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/components/InlineCodeBlock/WrappedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, {Fragment} from 'react';
import {StyleProp, TextStyle, View, ViewStyle} from 'react-native';
import Text from '@components/Text';
import useThemeStyles from '@styles/useThemeStyles';
import CONST from '@src/CONST';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

type WrappedTextProps = ChildrenProps & {
/** Style to be applied to Text */
textStyles?: StyleProp<TextStyle>;

/** Style for each word(Token) in the text, remember that token also includes whitespaces among words */
wordStyles?: StyleProp<ViewStyle>;
};

/**
* Breaks the text into matrix
* for eg: My Name is Rajat
* [
* [My,' ',Name,' ',' ',is,' ',Rajat],
* ]
*/
function getTextMatrix(text: string): string[][] {
return text.split('\n').map((row) => row.split(CONST.REGEX.SPACE_OR_EMOJI).filter((value) => value !== ''));
}

function WrappedText({children, wordStyles, textStyles}: WrappedTextProps) {
const styles = useThemeStyles();

if (typeof children !== 'string') {
return null;
}

const textMatrix = getTextMatrix(children);

return textMatrix.map((rowText, rowIndex) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

I see you removed the outer fragment <></> and are returning the map directly, please let's test this to ensure there is no regressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Retested all of the platforms 👌

<Fragment
// eslint-disable-next-line react/no-array-index-key
key={`${rowText[0]}-${rowIndex}`}
>
{rowText.map((colText, colIndex) => (
// Outer View is important to vertically center the Text
<View
// eslint-disable-next-line react/no-array-index-key
key={`${colText}-${colIndex}`}
style={styles.codeWordWrapper}
>
<View style={[wordStyles, colIndex === 0 && styles.codeFirstWordStyle, colIndex === rowText.length - 1 && styles.codeLastWordStyle]}>
<Text style={textStyles}>{colText}</Text>
</View>
</View>
))}
</Fragment>
));
}

WrappedText.displayName = 'WrappedText';

export default WrappedText;
22 changes: 0 additions & 22 deletions src/components/InlineCodeBlock/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import React from 'react';
import type {TText} from 'react-native-render-html';
import useThemeStyles from '@styles/useThemeStyles';
import inlineCodeBlockPropTypes from './inlineCodeBlockPropTypes';
import type InlineCodeBlockProps from './types';
import WrappedText from './WrappedText';

function InlineCodeBlock(props) {
function InlineCodeBlock<TComponent extends TText>({TDefaultRenderer, defaultRendererProps, textStyle, boxModelStyle}: InlineCodeBlockProps<TComponent>) {
const styles = useThemeStyles();
const TDefaultRenderer = props.TDefaultRenderer;

return (
<TDefaultRenderer
// eslint-disable-next-line react/jsx-props-no-spreading
{...props.defaultRendererProps}
{...defaultRendererProps}
>
<WrappedText
textStyles={[props.textStyle]}
wordStyles={[props.boxModelStyle, styles.codeWordStyle]}
textStyles={textStyle}
wordStyles={[boxModelStyle, styles.codeWordStyle]}
>
{props.defaultRendererProps.tnode.data}
{defaultRendererProps.tnode.data}
</WrappedText>
</TDefaultRenderer>
);
}

InlineCodeBlock.propTypes = inlineCodeBlockPropTypes;
InlineCodeBlock.displayName = 'InlineCodeBlock';

export default InlineCodeBlock;
21 changes: 21 additions & 0 deletions src/components/InlineCodeBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import type {TText} from 'react-native-render-html';
import Text from '@components/Text';
import type InlineCodeBlockProps from './types';

function InlineCodeBlock<TComponent extends TText>({TDefaultRenderer, textStyle, defaultRendererProps, boxModelStyle}: InlineCodeBlockProps<TComponent>) {
const {textDecorationLine, ...textStyles} = textStyle;

return (
<TDefaultRenderer
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
<Text style={{...boxModelStyle, ...textStyles}}>{defaultRendererProps.tnode.data}</Text>
</TDefaultRenderer>
);
}

InlineCodeBlock.displayName = 'InlineCodeBlock';

export default InlineCodeBlock;
10 changes: 0 additions & 10 deletions src/components/InlineCodeBlock/inlineCodeBlockPropTypes.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/components/InlineCodeBlock/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {TextStyle, ViewStyle} from 'react-native';
import type {TDefaultRenderer, TDefaultRendererProps, TText} from 'react-native-render-html';

type InlineCodeBlockProps<TComponent extends TText> = {
TDefaultRenderer: TDefaultRenderer<TComponent>;
textStyle: TextStyle;
defaultRendererProps: TDefaultRendererProps<TComponent>;
boxModelStyle: ViewStyle & TextStyle;
};

export default InlineCodeBlockProps;
2 changes: 1 addition & 1 deletion src/styles/codeStyles/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TextStyle, ViewStyle} from 'react-native';

type CodeWordWrapperStyles = ViewStyle;
type CodeWordStyles = TextStyle;
type CodeWordStyles = ViewStyle;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blazejkustra Otherwise there is a TS error in this line
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Weird typing issues, I'm not sure I get them...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cubuspl42 More detailed clarification, let me know if it makes more sense to you:

  • CodeWordStyles type is used for typing styles.codeWordStyle
  • styles.codeWordStyle is passed to the WrappedText component as a part of wordStyles param
  • wordStyles param in WrappedText is used for View component styling
  • so it expects that styles.codeWordStyle will have ViewStyle type instead of TextStyle, which means CodeWordStyles type should be ViewStyle

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thanks. So I understand that we don't pass any text-specific styles, and we stick to the View-supported style properties subset

type CodeTextStyles = TextStyle;

export type {CodeTextStyles, CodeWordStyles, CodeWordWrapperStyles};