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 <TextField> and <RichTextField> don't translate the emptyText #8924

Merged
merged 2 commits into from
May 17, 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
35 changes: 34 additions & 1 deletion packages/ra-ui-materialui/src/field/RichTextField.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import expect from 'expect';
import { render, screen, fireEvent } from '@testing-library/react';
import { RecordContextProvider } from 'ra-core';
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
import polyglotI18nProvider from 'ra-i18n-polyglot';

import { RichTextField, removeTags } from './RichTextField';
import { Secure } from './RichTextField.stories';
Expand Down Expand Up @@ -148,4 +149,36 @@ describe('<RichTextField />', () => {
(container.querySelector('#stolendata') as HTMLInputElement)?.value
).toEqual('none');
});

it('should translate emptyText', () => {
const i18nProvider = polyglotI18nProvider(
_locale =>
({
resources: {
books: {
name: 'Books',
fields: {
id: 'Id',
title: 'Title',
author: 'Author',
year: 'Year',
},
not_found: 'Not found',
},
},
} as any),
'en'
);
render(
<I18nContextProvider value={i18nProvider}>
<RichTextField
record={{ id: 123 }}
source="foo.bar"
emptyText="resources.books.not_found"
/>
</I18nContextProvider>
);

expect(screen.getByText('Not found')).not.toBeNull();
});
});
5 changes: 3 additions & 2 deletions packages/ra-ui-materialui/src/field/RichTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC, memo } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import Typography, { TypographyProps } from '@mui/material/Typography';
import { useRecordContext } from 'ra-core';
import { useRecordContext, useTranslate } from 'ra-core';
import purify from 'dompurify';

import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
Expand Down Expand Up @@ -35,6 +35,7 @@ export const RichTextField: FC<RichTextFieldProps> = memo<RichTextFieldProps>(
} = props;
const record = useRecordContext(props);
const value = get(record, source);
const translate = useTranslate();

return (
<Typography
Expand All @@ -44,7 +45,7 @@ export const RichTextField: FC<RichTextFieldProps> = memo<RichTextFieldProps>(
{...sanitizeFieldRestProps(rest)}
>
{value == null && emptyText ? (
emptyText
translate(emptyText, { _: emptyText })
) : stripTags ? (
removeTags(value)
) : (
Expand Down
37 changes: 35 additions & 2 deletions packages/ra-ui-materialui/src/field/TextField.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import expect from 'expect';
import { render, getNodeText } from '@testing-library/react';
import { RecordContextProvider } from 'ra-core';
import { screen, render, getNodeText } from '@testing-library/react';
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
import polyglotI18nProvider from 'ra-i18n-polyglot';

import { TextField } from './TextField';

Expand Down Expand Up @@ -79,4 +80,36 @@ describe('<TextField />', () => {
);
expect(queryByText('NA')).not.toBeNull();
});

it('should translate emptyText', () => {
const i18nProvider = polyglotI18nProvider(
_locale =>
({
resources: {
books: {
name: 'Books',
fields: {
id: 'Id',
title: 'Title',
author: 'Author',
year: 'Year',
},
not_found: 'Not found',
},
},
} as any),
'en'
);
render(
<I18nContextProvider value={i18nProvider}>
<TextField
record={{ id: 123 }}
source="foo.bar"
emptyText="resources.books.not_found"
/>
</I18nContextProvider>
);

expect(screen.getByText('Not found')).not.toBeNull();
});
});
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/field/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { memo, FC, ElementType } from 'react';
import get from 'lodash/get';
import Typography, { TypographyProps } from '@mui/material/Typography';
import { useRecordContext } from 'ra-core';
import { useRecordContext, useTranslate } from 'ra-core';

import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
Expand All @@ -11,6 +11,7 @@ export const TextField: FC<TextFieldProps> = memo(props => {
const { className, source, emptyText, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
const translate = useTranslate();

return (
<Typography
Expand All @@ -21,7 +22,8 @@ export const TextField: FC<TextFieldProps> = memo(props => {
>
{value != null && typeof value !== 'string'
? JSON.stringify(value)
: value || emptyText}
: value ||
(emptyText ? translate(emptyText, { _: emptyText }) : null)}
</Typography>
);
});
Expand Down