Skip to content

Commit 1f51b80

Browse files
committed
Avoid duplicating inferrence logic
1 parent 580836f commit 1f51b80

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

packages/ra-core/src/util/getFieldLabelTranslationArgs.spec.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,13 @@ describe('getFieldLabelTranslationArgs', () => {
5252
).toEqual([`resources.posts.fields.title`, { _: 'Title' }]);
5353
});
5454

55-
it('should accept custom label args when no label is provided', () => {
55+
it('should accept use the parentSource to build the translation key if provided', () => {
5656
expect(
5757
getFieldLabelTranslationArgs({
5858
resource: 'posts',
59-
source: 'title',
60-
labelArgs: { _: 'Special Title' },
61-
})
62-
).toEqual([`resources.posts.fields.title`, { _: 'Special Title' }]);
63-
});
64-
65-
it('should accept custom label args when a label is provided', () => {
66-
expect(
67-
getFieldLabelTranslationArgs({
68-
resource: 'posts',
69-
source: 'title',
70-
label: 'my.own.key',
71-
labelArgs: { _: 'Special Title' },
59+
source: 'url',
60+
parentSource: 'backlinks',
7261
})
73-
).toEqual([`my.own.key`, { _: 'Special Title' }]);
62+
).toEqual([`resources.posts.fields.backlinks.url`, { _: 'Url' }]);
7463
});
7564
});

packages/ra-core/src/util/getFieldLabelTranslationArgs.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import inflection from 'inflection';
22

33
interface Args {
44
label?: string;
5-
labelArgs?: Record<string, any>;
5+
parentSource?: string;
66
resource?: string;
77
source?: string;
88
}
@@ -23,15 +23,16 @@ export default (options?: Args): TranslationArguments => {
2323
return [''];
2424
}
2525

26-
const { label, labelArgs, resource, source } = options;
26+
const { label, parentSource, resource, source } = options;
2727
return typeof label !== 'undefined'
28-
? [label, { _: label, ...labelArgs }]
28+
? [label, { _: label }]
2929
: typeof source !== 'undefined'
3030
? [
31-
`resources.${resource}.fields.${source}`,
31+
`resources.${resource}.fields.${
32+
parentSource ? `${parentSource}.${source}` : source
33+
}`,
3234
{
3335
_: inflection.transform(source, ['underscore', 'humanize']),
34-
...labelArgs,
3536
},
3637
]
3738
: [''];

packages/ra-ui-materialui/src/input/ArrayInput/SimpleFormIteratorItem.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,7 @@ export const SimpleFormIteratorItem = React.forwardRef(
127127
translate(
128128
...getFieldLabelTranslationArgs(
129129
{
130-
label:
131-
input.props
132-
.label ??
133-
// We prefix the source with the ArrayInput source to avoid conflicts with other inputs
134-
`resources.${resource}.fields.${parentSource}.${source}`,
135-
labelArgs: {
136-
// We use the input source before it is transformed into `arraySource.0.inputSource
137-
_: inflection.transform(
138-
source,
139-
[
140-
'underscore',
141-
'humanize',
142-
]
143-
),
144-
},
130+
parentSource,
145131
resource,
146132
source,
147133
}

0 commit comments

Comments
 (0)