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

Render all children regardless of type when using i18nIsDynamicList prop #1661

Merged
merged 1 commit into from
Aug 10, 2023
Merged
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
7 changes: 6 additions & 1 deletion src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
function renderInner(child, node, rootReactNode) {
const childs = getChildren(child);
const mappedChildren = mapAST(childs, node.children, rootReactNode);
return hasValidReactChildren(childs) && mappedChildren.length === 0 ? childs : mappedChildren;
// `mappedChildren` will always be empty if using the `i18nIsDynamicList` prop,
// but the children might not necessarily be react components
return (hasValidReactChildren(childs) && mappedChildren.length === 0) ||
child.props?.i18nIsDynamicList
? childs
: mappedChildren;
}

function pushTranslatedJSX(child, inner, mem, i, isVoid) {
Expand Down
21 changes: 20 additions & 1 deletion test/trans.render.dynamic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Trans should render nested components', () => {
`);
});

it('should render dynamic content correctly', () => {
it('should render dynamic Elements correctly', () => {
const dynamicContent = <div>testing</div>;

function TestComponent() {
Expand All @@ -90,4 +90,23 @@ describe('Trans should render nested components', () => {
</div>
`);
});

it('should render dynamic strings correctly', () => {
const dynamicContent = 'testing';

function TestComponent() {
return (
<Trans>
My dynamic content: <React.Fragment i18nIsDynamicList>{dynamicContent}</React.Fragment>
</Trans>
);
}
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
My dynamic content:
testing
</div>
`);
});
});