ng-dynamic-forms
ng-dynamic-forms copied to clipboard
feat(core): support transforming config before model creation
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 Report
Merging #1010 into development will decrease coverage by
0.01%
. The diff coverage is87.5%
.
@@ 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%> (ø) |
:arrow_up: |
...forms/core/src/lib/service/dynamic-form.service.ts | 92.25% <85.71%> (-0.36%) |
:arrow_down: |
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.
@ronnetzer Thank you very much for this PR!
Please give a bit of time to review this. Thanks again
@udos86 Tried really hard to improve the coverage but I have no idea why it doesn't work :(
@udos86 any news? would love to see this PR merged :)
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!