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

Allow null override for default component #799

Merged
merged 4 commits into from
Nov 1, 2020
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
40 changes: 32 additions & 8 deletions packages/react/src/Trans.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ describe("Trans component", function () {
const originalConsole = console.error
console.error = jest.fn()

renderWithI18n(<Trans render="span" id="Just a text" />)
renderWithI18n(<Trans render="span" id="Some text" />)
expect(console.error).toHaveBeenCalled()

renderWithI18n(<Trans render="span" id="Just a text" />)
renderWithI18n(<Trans render="span" id="Some text" />)
expect(console.error).toHaveBeenCalledTimes(2)
console.error = originalConsole
})
Expand All @@ -56,7 +56,7 @@ describe("Trans component", function () {
const originalConsole = console.error
console.error = jest.fn()

renderWithI18n(<Trans render="div" component="span" id="Just a text" />)
renderWithI18n(<Trans render="div" component="span" id="Some text" />)
expect(console.error).toHaveBeenCalled()
console.error = originalConsole
})
Expand Down Expand Up @@ -137,9 +137,9 @@ describe("Trans component", function () {
})

describe("rendering", function () {
it("should render just a text without wrapping element", function () {
const txt = html(<Trans id="Just a text" />)
expect(txt).toEqual("Just a text")
it("should render a text node with no wrapper element", function () {
const txt = html(<Trans id="Some text" />)
expect(txt).toEqual("Some text")
})

it("should render custom element", function () {
Expand Down Expand Up @@ -173,10 +173,34 @@ describe("Trans component", function () {
}
const span = render(
<I18nProvider i18n={i18n} defaultComponent={ComponentFC}>
<Trans id="Just a text" />
<Trans id="Some text" />
</I18nProvider>
).container.innerHTML
expect(span).toEqual(`<div>Just a text</div>`)
expect(span).toEqual(`<div>Some text</div>`)
})

it("should ignore defaultComponent when render is null", function () {
const ComponentFC: React.FunctionComponent = (props: { children?: React.ReactNode }) => {
return (<div>{props.children}</div>)
}
const translation = render(
<I18nProvider i18n={i18n} defaultComponent={ComponentFC}>
<Trans id="Some text" render={null} />
</I18nProvider>
).container.innerHTML
expect(translation).toEqual("Some text")
})

it("should ignore defaultComponent when component is null", function () {
const ComponentFC: React.FunctionComponent = (props: { children?: React.ReactNode }) => {
return (<div>{props.children}</div>)
}
const translation = render(
<I18nProvider i18n={i18n} defaultComponent={ComponentFC}>
<Trans id="Some text" component={null} />
</I18nProvider>
).container.innerHTML
expect(translation).toEqual("Some text")
})
})

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/Trans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export function Trans(props: TransProps) {
? formatElements(_translation, components)
: null

if (render === null || component === null) {
return translation
}

const FallbackComponent = defaultComponent || React.Fragment

// Validation of `render` and `component` props
Expand Down