Skip to content

Commit

Permalink
fix(Text): make vivinho char replacement for screen readers work when…
Browse files Browse the repository at this point in the history
… Text have multiple children (#1090)

WEB-1837 

Before this PR, `<Text>Ħ something</Text>` was working but `<Text>Ħ
{"something"}</Text>` wasn't
  • Loading branch information
atabel authored Apr 22, 2024
1 parent 5175b5c commit e5c239c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/text-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ test.each([
expect(screen.getByRole('link', {name: expected})).toBeInTheDocument();
});

test('vivinho char replacement works in Texts with multiple children', () => {
const someVar = 'something';
const vivinhoVar = 'Ħ';
render(
<ThemeContextProvider theme={makeTheme({skin: getVivoNewSkin()})}>
<a href="/">
<Text>
Ħ {someVar} {vivinhoVar}
</Text>
</a>
</ThemeContextProvider>
);

expect(screen.getByRole('link', {name: 'Vivo something Vivo'})).toBeInTheDocument();
});

test('vivinho char is only replaced in vivo-new skin', () => {
render(
<ThemeContextProvider theme={makeTheme({skin: getMovistarSkin()})}>
Expand Down
20 changes: 11 additions & 9 deletions src/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ const vivinhoForScreenReaders = (
</>
);

const makeVivinhoCharReadableForScreenReaders = (text: string): React.ReactNode => {
if (text.includes(VIVINHO_CHAR)) {
const makeVivinhoCharReadableForScreenReaders = (children: React.ReactNode): React.ReactNode => {
return React.Children.map(children, (child) => {
if (typeof child !== 'string') {
return child;
}
if (!child.includes(VIVINHO_CHAR)) {
return child;
}
return (
<>
{text.split(VIVINHO_CHAR).map((segment, idx) => (
{child.split(VIVINHO_CHAR).map((segment, idx) => (
<React.Fragment key={idx}>
{idx > 0 && vivinhoForScreenReaders}
{segment}
</React.Fragment>
))}
</>
);
} else {
return text;
}
});
};

export interface TextPresetProps {
Expand Down Expand Up @@ -172,9 +176,7 @@ export const Text: React.FC<TextProps> = ({
textShadow,
},
},
typeof children === 'string' && skinName === VIVO_NEW_SKIN
? makeVivinhoCharReadableForScreenReaders(children)
: children
skinName === VIVO_NEW_SKIN ? makeVivinhoCharReadableForScreenReaders(children) : children
);
};

Expand Down

1 comment on commit e5c239c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-e0xclxmlg-flows-projects-65bb050e.vercel.app

Built with commit e5c239c.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.