Skip to content

Commit e9bd6a9

Browse files
sdirixlucas-koehler
authored andcommitted
fix: use translated description in Angular Material
The control description is already translated via JSON Forms core. The potentially translated description is now used instead of manually resolving the description from the schema. Adds testcases.
1 parent 0e863e8 commit e9bd6a9

File tree

2 files changed

+119
-4
lines changed

2 files changed

+119
-4
lines changed

packages/angular-material/test/number-control.spec.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,122 @@ describe('Number control custom', () => {
213213

214214
expect(numberNativeElement.value).toBe('1000000');
215215
});
216+
217+
it('should have description property set correctly', () => {
218+
const schema = {
219+
type: 'object',
220+
properties: {
221+
foo: {
222+
type: 'number',
223+
description: 'This is a number field',
224+
},
225+
},
226+
};
227+
const uischema = Object.assign({}, defaultUischema);
228+
component.uischema = uischema;
229+
const state: JsonFormsCore = {
230+
data: defaultData,
231+
schema: schema,
232+
uischema: uischema,
233+
};
234+
getJsonFormsService(component).init({
235+
core: state,
236+
i18n: {
237+
locale: 'en',
238+
},
239+
});
240+
getJsonFormsService(component).updateCore(
241+
Actions.init(state.data, state.schema)
242+
);
243+
component.ngOnInit();
244+
fixture.detectChanges();
245+
246+
// Verify the description property is set correctly on the component
247+
expect(component.description).toBe('This is a number field');
248+
});
249+
250+
it('should show description when showUnfocusedDescription is true', () => {
251+
const schema = {
252+
type: 'object',
253+
properties: {
254+
foo: {
255+
type: 'number',
256+
description: 'This is a number field',
257+
},
258+
},
259+
};
260+
const uischema = Object.assign({}, defaultUischema);
261+
uischema.options = {
262+
showUnfocusedDescription: true,
263+
};
264+
component.uischema = uischema;
265+
const state: JsonFormsCore = {
266+
data: defaultData,
267+
schema: schema,
268+
uischema: uischema,
269+
};
270+
getJsonFormsService(component).init({
271+
core: state,
272+
i18n: {
273+
locale: 'en',
274+
},
275+
});
276+
getJsonFormsService(component).updateCore(
277+
Actions.init(state.data, state.schema)
278+
);
279+
component.ngOnInit();
280+
fixture.detectChanges();
281+
282+
// Description should be visible even without focus
283+
const matHint = fixture.nativeElement.querySelector('mat-hint');
284+
expect(matHint).toBeTruthy();
285+
expect(matHint.textContent.trim()).toBe('This is a number field');
286+
});
287+
288+
it('should render translated description correctly', () => {
289+
const schema = {
290+
type: 'object',
291+
properties: {
292+
foo: {
293+
type: 'number',
294+
description: 'description.number',
295+
},
296+
},
297+
};
298+
const uischema = Object.assign({}, defaultUischema);
299+
uischema.options = {
300+
showUnfocusedDescription: true,
301+
};
302+
component.uischema = uischema;
303+
const state: JsonFormsCore = {
304+
data: defaultData,
305+
schema: schema,
306+
uischema: uischema,
307+
};
308+
getJsonFormsService(component).init({
309+
core: state,
310+
i18n: {
311+
locale: 'en',
312+
translate: (key: string, defaultMessage: string | undefined) => {
313+
const translations: { [key: string]: string } = {
314+
'foo.description': 'Translated number description',
315+
};
316+
return translations[key] ?? defaultMessage;
317+
},
318+
},
319+
});
320+
getJsonFormsService(component).updateCore(
321+
Actions.init(state.data, state.schema)
322+
);
323+
component.ngOnInit();
324+
fixture.detectChanges();
325+
326+
// Verify the description is translated and rendered correctly
327+
expect(component.description).toBe('Translated number description');
328+
329+
// Check that the translated description is displayed in the DOM
330+
const matHint = fixture.nativeElement.querySelector('mat-hint');
331+
expect(matHint).toBeTruthy();
332+
expect(matHint.textContent.trim()).toBe('Translated number description');
333+
});
216334
});

packages/angular/src/library/abstract-control.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ export abstract class JsonFormsAbstractControl<
126126
this.hidden = !visible;
127127
this.scopedSchema = schema;
128128
this.rootSchema = rootSchema;
129-
this.description =
130-
this.scopedSchema !== undefined
131-
? this.scopedSchema.description
132-
: '';
129+
this.description = props.description ?? '';
133130
this.id = props.id;
134131
this.form.setValue(data);
135132
this.propsPath = path;

0 commit comments

Comments
 (0)