Skip to content

Angular Material: Translate Autocomplete Control Renderer#2535

Open
daniel-shuy wants to merge 4 commits intoeclipsesource:masterfrom
daniel-shuy:angular-material/translate-autocomplete-control-renderer
Open

Angular Material: Translate Autocomplete Control Renderer#2535
daniel-shuy wants to merge 4 commits intoeclipsesource:masterfrom
daniel-shuy:angular-material/translate-autocomplete-control-renderer

Conversation

@daniel-shuy
Copy link
Contributor

@daniel-shuy daniel-shuy commented Jan 21, 2026

Background

The company I work for (SICPA Product Security) is considering adopting JSON Forms for a project.

Unfortunately the project is written in Angular, and I noticed that the Angular Material renderer set is missing quite a lot of supported features compared to the React/Vue renderer sets.

As such, I have been testing the Angular Material renderer set quite thoroughly, and will be contributing some missing features, especially to the Angular Material renderer set.

Description

The Angular Material renderer set renders enums using the AutocompleteControlRenderer.

Unlike the other renderer sets, AutocompleteControlRenderer does not translate the enum values.

This PR updates AutocompleteControlRenderer to use mapStateToEnumControlProps(JsonFormsState, OwnPropsOfControl & OwnPropsOfEnum) like the other renderer sets, which translates the enum values as labels, while keeping the original values as the values:

/**
* Default mapStateToCellProps for enum control. Options is used for populating dropdown list
* @param state
* @param ownProps
* @returns {StatePropsOfControl & OwnPropsOfEnum}
*/
export const mapStateToEnumControlProps = (
state: JsonFormsState,
ownProps: OwnPropsOfControl & OwnPropsOfEnum
): StatePropsOfControl & OwnPropsOfEnum => {
const props: StatePropsOfControl = mapStateToControlProps(state, ownProps);
const options: EnumOption[] =
ownProps.options ||
props.schema.enum?.map((e) =>
enumToEnumOptionMapper(
e,
getTranslator()(state),
getI18nKeyPrefix(props.schema, props.uischema, props.path)
)
) ||
(props.schema.const && [
enumToEnumOptionMapper(
props.schema.const,
getTranslator()(state),
getI18nKeyPrefix(props.schema, props.uischema, props.path)
),
]);
return {
...props,
options,
};
};

For backwards compatibility, I've allowed the options @Input to still take in string[] (by typing it as EnumOption[] | string[]), but it will not be translated if a string[] is passed.

@netlify
Copy link

netlify bot commented Jan 21, 2026

Deploy Preview for jsonforms-examples ready!

Name Link
🔨 Latest commit f6fe0f2
🔍 Latest deploy log https://app.netlify.com/projects/jsonforms-examples/deploys/699ef76bf703a70008f594ef
😎 Deploy Preview https://deploy-preview-2535--jsonforms-examples.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@CLAassistant
Copy link

CLAassistant commented Jan 21, 2026

CLA assistant check
All committers have signed the CLA.

@sdirix
Copy link
Member

sdirix commented Jan 22, 2026

Hi @daniel-shuy,

You are right, the Angular Material renderer set is the most inconsistent one with the most missing features. Happy to review your contribution(s) ❤️

@lucas-koehler lucas-koehler self-requested a review January 30, 2026 11:39
Copy link
Contributor

@lucas-koehler lucas-koehler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @daniel-shuy Thanks for the contribution and extending the tests ❤️ . Allowing translation of the values defintely makes sense!
I have two comments inline. Pease have a look.

@daniel-shuy daniel-shuy force-pushed the angular-material/translate-autocomplete-control-renderer branch 2 times, most recently from 21eaaba to a7d33df Compare February 3, 2026 08:16
@coveralls
Copy link

coveralls commented Feb 3, 2026

Coverage Status

coverage: 82.803% (+0.005%) from 82.798%
when pulling 5302153 on daniel-shuy:angular-material/translate-autocomplete-control-renderer
into 563015d on eclipsesource:master.

lucas-koehler

This comment was marked as outdated.

Copy link
Contributor

@lucas-koehler lucas-koehler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misclicked before, sorry:

Hi @daniel-shuy ,
thanks for the update!
Unfortunately, it seems that something is broken now:
Looking at the Enums example in the preview https://deploy-preview-2535--jsonforms-examples.netlify.app/angular-material/ selecting a value does not update the value for me. On the master preview this works fine for me.
Can you have a look?

@daniel-shuy daniel-shuy force-pushed the angular-material/translate-autocomplete-control-renderer branch from a7d33df to 83360e7 Compare February 3, 2026 08:58
@daniel-shuy
Copy link
Contributor Author

@lucas-koehler sorry, I had some leftover code that depended on displayWith. I've removed it, please try again

Copy link
Contributor

@lucas-koehler lucas-koehler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @daniel-shuy , thanks for the update! Generally, the enum controls work again. However, after selecting a value, the untranslated value is shown in the enum control while the dropdown (correctly) shows the translated values.
This should be aligned to also show the translated value in the input. This is for instance already the case in the react-material renderer set.

Find attached an enum i18n example to test this. You can also add this example to the PR.

enum-i18n.patch

@daniel-shuy
Copy link
Contributor Author

daniel-shuy commented Feb 4, 2026

@lucas-koehler Ah, now I remember why I needed displayWith. This is challenging as I'm trying to not break backwards compatibility by keeping the data and FormControl value type as string, but it looks like I will need to change it to EnumOption to translate the input label, which is a breaking change for those who use the internal JSON Forms API. Is that acceptable?

I'll also update the tests to assert that the input label is correctly translated.

@lucas-koehler
Copy link
Contributor

Hi @daniel-shuy ,
I'd say it's acceptable to break the API here as long as it only affects this renderer resp. renderer set. We do not want to change the core behavior of JSON Forms :)

@daniel-shuy daniel-shuy force-pushed the angular-material/translate-autocomplete-control-renderer branch 2 times, most recently from 68fd15d to 5302153 Compare February 10, 2026 03:16
@daniel-shuy
Copy link
Contributor Author

@lucas-koehler Great! I've made the changes and updated the tests, it should work now

Copy link
Contributor

@lucas-koehler lucas-koehler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates but something is still missing.
When I select an option in the enum i18n example at the preview https://deploy-preview-2535--jsonforms-examples.netlify.app/angular-material/ the control stays empty:

jf-angular-enums.webm

@daniel-shuy daniel-shuy force-pushed the angular-material/translate-autocomplete-control-renderer branch from 5302153 to 36c55fe Compare February 24, 2026 07:40
@daniel-shuy daniel-shuy force-pushed the angular-material/translate-autocomplete-control-renderer branch from 36c55fe to 995e0ff Compare February 25, 2026 09:02
@daniel-shuy
Copy link
Contributor Author

@lucas-koehler sorry for the delay, I was away on holiday last week.

I've finally managed to fix all the issues. Now I understand why this hasn't been implemented, the Angular Material mat-autocomplete API is so insanely complex 😅

Copy link
Contributor

@lucas-koehler lucas-koehler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @daniel-shuy , no worries about the delay - we're not in a rush :D
Thanks for the updates! The renderer now works for me 🚀 I pushed an update for the tests because they should test persisting the enum value - not the enumoption.

Besides that I have a few small suggestions inline. Please have a look :)


override onChange(ev: any) {
const eventValue = this.getEventValue(ev);
const option = Array.from(this.valuesToTranslatedOptions?.values()).find(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.from(undefined) throws an error, thus have a safe fallback:

Suggested change
const option = Array.from(this.valuesToTranslatedOptions?.values()).find(
const option = Array.from(this.valuesToTranslatedOptions?.values() ?? []).find(


override mapAdditionalProps(_props: StatePropsOfControl & OwnPropsOfEnum) {
this.valuesToTranslatedOptions = new Map(
_props.options.map((option) => [option.value, option])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options may be undefined.

Suggested change
_props.options.map((option) => [option.value, option])
(_props.options ?? []).map((option) => [option.value, option])


if (typeof option === 'string') {
if (!this.valuesToTranslatedOptions) {
return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rather show the raw, untranslated value than nothing as long as the translation is not available. Or do you see any issues with that?

Suggested change
return '';
return option; // show raw value until translations are ready

Comment on lines +216 to +218
trackByEnumOptionId(_index: number, option: EnumOption): string {
return option.value;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems to be unused now. Can it be removed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants