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

fix issue 1815 #1816

Merged
merged 1 commit into from
Nov 29, 2024
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
2 changes: 1 addition & 1 deletion react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
const comp = react.cloneElement(components[c], {
key: componentKey
});
if (typeof comp.type === 'function' || !comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
if (!comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
function Componentized() {
return react.createElement(react.Fragment, null, comp);
}
Expand Down
1 change: 0 additions & 1 deletion src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ export function Trans({
const componentKey = components[c].key || c;
const comp = cloneElement(components[c], { key: componentKey });
if (
typeof comp.type === 'function' ||
!comp.props ||
!comp.props.children ||
(translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0)
Expand Down
64 changes: 64 additions & 0 deletions test/trans.render.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,70 @@ describe('trans should work with selfclosing elements in components', () => {
});
});

describe('trans should work with self closing elements with react components', () => {
simkentys marked this conversation as resolved.
Show resolved Hide resolved
function Component({ children }) {
return <div>{children}</div>;
}
it('should render component children', () => {
const { container } = render(
<Trans
i18nKey="transTestWithSelfClosing"
components={{
component: <Component>These children will be preserved</Component>,
}}
/>,
);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
interpolated component:
<div>
These children will be preserved
</div>
</div>
`);
});
it('should render Link component children', () => {
const { container } = render(
<Trans
i18nKey="transTestWithSelfClosing"
components={{
component: <Link to="#">These children will be preserved</Link>,
}}
/>,
);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
interpolated component:
<a
href="#"
>
These children will be preserved
</a>
</div>
`);
});
it('should render anchor tag children', () => {
const { container } = render(
<Trans
i18nKey="transTestWithSelfClosing"
components={{
component: <a href="#">These children will be preserved</a>,
}}
/>,
);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
interpolated component:
<a
href="#"
>
These children will be preserved
</a>
</div>
`);
});
});

describe('trans with null child', () => {
function TestComponent() {
return (
Expand Down
Loading