Skip to content

Commit

Permalink
fix: Support identical wrappers with identical text content in rich t…
Browse files Browse the repository at this point in the history
…ext (#80)
  • Loading branch information
amannn authored Jan 19, 2022
1 parent 6b68f7e commit b35bb9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 2 additions & 3 deletions packages/use-intl/src/useTranslations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function prepareTranslationValues(values?: RichTranslationValues) {
// Workaround for https://github.com/formatjs/formatjs/issues/1467
const transformedValues: RichTranslationValues = {};
Object.keys(values).forEach((key) => {
let index = 0;
const value = values[key];

let transformed;
Expand All @@ -61,9 +62,7 @@ function prepareTranslationValues(values?: RichTranslationValues) {
const result = value(children);

return isValidElement(result)
? cloneElement(result, {
key: result.key || key + String(children)
})
? cloneElement(result, {key: key + index++})
: result;
};
} else {
Expand Down
21 changes: 18 additions & 3 deletions packages/use-intl/test/useTranslations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('t.rich', () => {
const {container} = renderRichTextMessage(
'This is <important>important</important> and <important>this as well</important>',
{
important: (children: ReactNode) => <b>{children}</b>
important: (children) => <b>{children}</b>
}
);
expect(container.innerHTML).toBe(
Expand All @@ -303,12 +303,27 @@ describe('t.rich', () => {
const {container} = renderRichTextMessage(
'This is <bold><italic>very</italic> important</bold>',
{
bold: (children: ReactNode) => <b>{children}</b>,
italic: (children: ReactNode) => <i>{children}</i>
bold: (children) => <b>{children}</b>,
italic: (children) => <i>{children}</i>
}
);
expect(container.innerHTML).toBe('This is <b><i>very</i> important</b>');
});

it('supports identical wrappers with identical text content', () => {
const consoleError = jest.spyOn(console, 'error');
const {container} = renderRichTextMessage(
'<b>foo</b> bar <b>foo</b> <i>foo</i> bar <i>foo</i> <b><i>foobar</i></b>',
{
b: (children) => <b>{children}</b>,
i: (children) => <i>{children}</i>
}
);
expect(container.innerHTML).toBe(
'<b>foo</b> bar <b>foo</b> <i>foo</i> bar <i>foo</i> <b><i>foobar</i></b>'
);
expect(consoleError).not.toHaveBeenCalled();
});
});

describe('t.raw', () => {
Expand Down

1 comment on commit b35bb9f

@vercel
Copy link

@vercel vercel bot commented on b35bb9f Jan 19, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.