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

feat(core): support transforming config before model creation #1010

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ronnetzer
Copy link

This feature adds the ability to add an array of model JSON transform functions through an injectionToken.

export type ModelJSONTransformFn<T> = (modelJSON: T) => T;

export type DynamicFormControlJSONTransformFnArray<T = any> = ModelJSONTransformFn<T>[];
export const DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY = new InjectionToken<DynamicFormControlJSONTransformFnArray>(
  'DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY'
);

usage:

export const DynamicFormControlJSONTransformFnArrayProvider: Provider = {
	provide: DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY,
	useValue: [
		/* transform function here */
	]
};

@NgModule({
	...,
	imports: [
		...,
		ReactiveFormsModule,
		DynamicFormsCoreModule
	],
	providers: [
		...,
		DynamicFormControlJSONTransformFnArrayProvider
	]
})
export class AppModule {}

Motivation behind this feature:

Imagine that the form config contains all the labels and text as a path to a translation key.
With this feature, I'm able to Inject an array of transform functions, the first translate all the keys and the second one add an asterisk to the label if the model has required validator.

export const DynamicFormControlJSONTransformFnArrayProvider: Provider = {
	provide: DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY,
	useFactory: translateService => ([
		translateKeys(translateService),
		addRequiredAsteriskToLabel()
	]),
	deps: [TranslateService]
};

@codecov-io
Copy link

codecov-io commented Aug 17, 2019

Codecov Report

Merging #1010 into development will decrease coverage by 0.01%.
The diff coverage is 87.5%.

Impacted file tree graph

@@              Coverage Diff               @@
##           development   #1010      +/-   ##
==============================================
- Coverage        91.62%   91.6%   -0.02%     
==============================================
  Files              154     154              
  Lines             2293    2300       +7     
  Branches           236     237       +1     
==============================================
+ Hits              2101    2107       +6     
  Misses             135     135              
- Partials            57      58       +1
Impacted Files Coverage Δ
.../ng-dynamic-forms/core/src/lib/utils/json.utils.ts 100% <100%> (ø) ⬆️
...forms/core/src/lib/service/dynamic-form.service.ts 92.25% <85.71%> (-0.36%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dfc275f...80b4300. Read the comment docs.

@udos86
Copy link
Owner

udos86 commented Aug 19, 2019

@ronnetzer Thank you very much for this PR!

Please give a bit of time to review this. Thanks again

@ronnetzer
Copy link
Author

@udos86 Tried really hard to improve the coverage but I have no idea why it doesn't work :(

@ronnetzer
Copy link
Author

@udos86 any news? would love to see this PR merged :)

@matheo
Copy link

matheo commented Apr 16, 2021

Hi @ronnetzer
Do you mind to share your experience with dynamic forms
in the discussions of a new library to implement advanced features?
https://dev.to/myndpm/a-new-approach-to-have-dynamic-forms-in-angular-5d11
Thanks in advance!

@udos86 udos86 changed the base branch from development to master December 3, 2022 11:24
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