Skip to content
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
6 changes: 6 additions & 0 deletions src/components/RenderHTML.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useMemo} from 'react';
import {RenderHTMLConfigProvider, RenderHTMLSource} from 'react-native-render-html';
import type {RenderersProps} from 'react-native-render-html';
import useHasTextAncestor from '@hooks/useHasTextAncestor';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Parser from '@libs/Parser';

Expand All @@ -22,6 +23,11 @@ type RenderHTMLProps = {
// context to RenderHTMLSource components. See https://git.io/JRcZb
// The provider is available at src/components/HTMLEngineProvider/
function RenderHTML({html: htmlParam, onLinkPress, isSelectable}: RenderHTMLProps) {
const hasTextAncestor = useHasTextAncestor();
if (__DEV__ && hasTextAncestor) {
throw new Error('RenderHTML must not be rendered inside a <Text> component, as it will break the layout on iOS. Render it as a sibling instead.');
}

const {windowWidth} = useWindowDimensions();
const html = useMemo(() => {
return (
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useHasTextAncestor.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {useContext} from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

Use index.tsx or index.native format

// eslint-disable-next-line no-restricted-imports
import {unstable_TextAncestorContext as TextAncestorContext} from 'react-native';

export default function useHasTextAncestor(): boolean {
return useContext(TextAncestorContext);
}
4 changes: 4 additions & 0 deletions src/hooks/useHasTextAncestor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TextAncestorContext is not available on web (react-native-web), so always return false.
export default function useHasTextAncestor(): boolean {
return false;
}
17 changes: 9 additions & 8 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,14 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
shouldShow={!hasDependentTags && !hasIndependentTags}
/>
) : (
<Text style={[styles.textNormal, styles.colorMuted]}>
{!hasDependentTags && !hasIndependentTags && !!policyTagLists.at(0)?.name ? (
<EmployeesSeeTagsAsText customTagName={policyTagLists.at(0)?.name ?? ''} />
) : (
translate('workspace.tags.subtitle')
)}

<>
<Text style={[styles.textNormal, styles.colorMuted]}>
{!hasDependentTags && !hasIndependentTags && !!policyTagLists.at(0)?.name ? (
<EmployeesSeeTagsAsText customTagName={policyTagLists.at(0)?.name ?? ''} />
) : (
translate('workspace.tags.subtitle')
)}
</Text>
{hasDependentTags && (
<View style={[styles.renderHTML]}>
<RenderHTML
Expand All @@ -706,7 +707,7 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
/>
</View>
)}
</Text>
</>
)}
</View>
{tagList.length > CONST.SEARCH_ITEM_LIMIT && (
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/RenderHTMLTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {render} from '@testing-library/react-native';
import React from 'react';
import {View} from 'react-native';
import RenderHTML from '@components/RenderHTML';

jest.mock('@hooks/useWindowDimensions', () => () => ({windowWidth: 400}));
jest.mock('react-native-render-html', () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-assignment
const {View: MockView} = require('react-native');
return {
RenderHTMLConfigProvider: ({children}: {children: React.ReactNode}) => children,
RenderHTMLSource: () => <MockView />,
};
});

const mockUseHasTextAncestor = jest.fn(() => false);
jest.mock('@hooks/useHasTextAncestor', () => () => mockUseHasTextAncestor());

describe('RenderHTML', () => {
it('throws when rendered inside a Text ancestor', () => {
mockUseHasTextAncestor.mockReturnValue(true);
expect(() =>
render(
<View>
Copy link
Member

Choose a reason for hiding this comment

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

Text

<RenderHTML html="<p>test</p>" />
</View>,
),
).toThrow('RenderHTML must not be rendered inside a <Text> component');
});

it('does not throw when rendered outside a Text ancestor', () => {
mockUseHasTextAncestor.mockReturnValue(false);
expect(() =>
render(
<View>
<RenderHTML html="<p>test</p>" />
</View>,
),
).not.toThrow();
});
});
Loading