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 field label translation concatenates previous prefix #9648

Merged
merged 7 commits into from
Feb 7, 2024
Merged
5 changes: 4 additions & 1 deletion packages/ra-core/src/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export const Form = (props: FormProps) => {

return (
<OptionalRecordContextProvider value={record}>
<LabelPrefixContextProvider prefix={`resources.${resource}.fields`}>
<LabelPrefixContextProvider
prefix={`resources.${resource}.fields`}
concatenate={false}
>
slax57 marked this conversation as resolved.
Show resolved Hide resolved
<FormProvider {...form}>
<FormGroupsProvider>
<form
Expand Down
34 changes: 34 additions & 0 deletions packages/ra-core/src/util/LabelPrefixContextProvider.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { LabelPrefixContextProvider } from './LabelPrefixContextProvider';
import { useLabelPrefix } from './useLabelPrefix';

describe('LabelPrefixContextProvider', () => {
it('should return the prefix', () => {
const Label = () => {
return <>{useLabelPrefix()}</>;
};
render(
<LabelPrefixContextProvider prefix="resource.posts.fields.title">
<Label />
</LabelPrefixContextProvider>
);
screen.getByText('resource.posts.fields.title');
});
it('should not concatenate previous prefix', () => {
const Label = () => {
return <>{useLabelPrefix()}</>;
};
render(
<LabelPrefixContextProvider prefix="resource.posts.fields.title">
<LabelPrefixContextProvider
prefix="resource.comments.fields.body"
concatenate={false}
>
<Label />
</LabelPrefixContextProvider>
</LabelPrefixContextProvider>
);
screen.getByText('resource.comments.fields.body');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SimpleForm } from '../../form';
import { ArrayInput } from './ArrayInput';
import { TextInput } from '../TextInput';
import { SimpleFormIterator } from './SimpleFormIterator';
import { Basic } from './SimpleFormIterator.stories';

describe('<SimpleFormIterator />', () => {
// bypass confirm leave form with unsaved changes
Expand Down Expand Up @@ -978,4 +979,23 @@ describe('<SimpleFormIterator />', () => {
);
});
});

it('should have the correct translation keys', async () => {
render(<Basic />);
const authorsTranslationKey = screen.queryByLabelText(
'resources.books.fields.authors'
);

expect(authorsTranslationKey).toBeDefined();

const authorsNameTranslationKey = await screen.findAllByLabelText(
'resources.books.fields.authors.name'
);
expect(authorsNameTranslationKey).toHaveLength(2);

const authorsRoleTranslationKey = await screen.findAllByLabelText(
'resources.books.fields.authors.role'
);
expect(authorsRoleTranslationKey).toHaveLength(2);
});
});
5 changes: 4 additions & 1 deletion packages/ra-ui-materialui/src/list/filter/FilterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ export const FilterFormBase = (props: FilterFormBaseProps) => {
);

return (
<LabelPrefixContextProvider prefix={`resources.${resource}.fields`}>
<LabelPrefixContextProvider
prefix={`resources.${resource}.fields`}
concatenate={false}
>
<StyledForm
className={className}
{...sanitizeRestProps(rest)}
Expand Down
Loading