Skip to content

Commit

Permalink
Merge pull request #32477 from VickyStash/ts-migration/inlineCodeBloc…
Browse files Browse the repository at this point in the history
…k-component

[TS migration] Migrate 'InlineCodeBlock' component to TypeScript
  • Loading branch information
marcochavezf authored Dec 12, 2023
2 parents 6b1460f + 729ae12 commit d5ff8f8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 118 deletions.
77 changes: 0 additions & 77 deletions src/components/InlineCodeBlock/WrappedText.js

This file was deleted.

66 changes: 66 additions & 0 deletions src/components/InlineCodeBlock/WrappedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 individual word (token) in the text. Note that a token can also include whitespace characters between words.
*/
wordStyles?: StyleProp<ViewStyle>;
};

/**
* Breaks the text into matrix
*
* @example
* const text = "My Name is Rajat";
* const resultMatrix = getTextMatrix(text);
* console.log(resultMatrix);
* // Output:
* // [
* // ['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) => (
<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;
23 changes: 23 additions & 0 deletions src/components/InlineCodeBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import {StyleSheet} from 'react-native';
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 flattenTextStyle = StyleSheet.flatten(textStyle);
const {textDecorationLine, ...textStyles} = flattenTextStyle;

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 {StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {TDefaultRenderer, TDefaultRendererProps, TText} from 'react-native-render-html';

type InlineCodeBlockProps<TComponent extends TText> = {
TDefaultRenderer: TDefaultRenderer<TComponent>;
textStyle: StyleProp<TextStyle>;
defaultRendererProps: TDefaultRendererProps<TComponent>;
boxModelStyle: StyleProp<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;
type CodeTextStyles = TextStyle;

export type {CodeTextStyles, CodeWordStyles, CodeWordWrapperStyles};

0 comments on commit d5ff8f8

Please sign in to comment.