Skip to content

Commit 85c78a9

Browse files
authored
Merge pull request #140 from erqk/v8
V8
2 parents 64d3560 + 6939f42 commit 85c78a9

10 files changed

+69
-84
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# 8.2.7 (2024-09-01)
2+
3+
[2818671]: https://github.com/erqk/ng-dynamic-json-form/commit/28186714b910aeda7e852e736e824b5efc69c620
4+
5+
| Commit | Type | Description |
6+
| --------- | ----- | -------------------------------------- |
7+
| [2818671] | chore | Support form rendering in server side. |
8+
9+
# 8.2.6 (2024-08-28)
10+
11+
[09faf63]: https://github.com/erqk/ng-dynamic-json-form/commit/09faf63326dcf3727f14cb431065adfbe3947e0e
12+
13+
| Commit | Type | Description |
14+
| --------- | ---- | --------------------------------------------------- |
15+
| [09faf63] | fix | Retain trigger's body value if no control is found. |
16+
117
# 8.2.5 (2024-08-20)
218

319
[802c219]: https://github.com/erqk/ng-dynamic-json-form/commit/802c2190f4ea5a09a15fdafd6c2c3e4df11eb3a8

lib/core/models/form-control-config.interface.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,16 @@ import { ValidatorConfig } from './validator-config.interface';
77

88
export interface FormControlConfig {
99
formControlName: string;
10-
11-
/**Action to do on this control when condition is met. e.g. Change visibility or toggle validators */
10+
1211
conditions?: Conditions;
1312

14-
/**Provide to make this control as a FormGroup, cannot use with `formArray` */
13+
/**Provide to make this control as a `FormGroup` */
1514
children?: FormControlConfig[];
1615

1716
description?: string;
1817

19-
/**The properties to bind to the target element or `Directive`. Example:
20-
* @example
21-
* <textarea
22-
* [rows]="extra.rows"
23-
* [cols]="extra.cols"
24-
* ...
25-
* ></textarea>
26-
*
27-
* @example
28-
* <p-calendar
29-
* [appendTo]="extra.appendTo"
30-
* ></p-calendar>
18+
/**
19+
* The properties to bind to the target element or `Directive`
3120
*/
3221
props?: any;
3322

@@ -40,15 +29,9 @@ export interface FormControlConfig {
4029

4130
inputMask?: FactoryArg;
4231

43-
/**A list of data, use with the following input type:
44-
* - `checkbox`
45-
* - `dropdown`
46-
* - `radio`
47-
* - ...custom component type
48-
*/
4932
options?: FormControlOptions;
5033

51-
/**Set this input to readonly, and will add a class `readonly` for styling */
34+
/**Set this input to readonly, and will add a class `readonly` to the host element of this control */
5235
readonly?: boolean;
5336

5437
type?: FormControlType;

lib/core/ng-dynamic-json-form.component.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommonModule, isPlatformServer } from '@angular/common';
1+
import { CommonModule } from '@angular/common';
22
import {
33
ChangeDetectorRef,
44
Component,
@@ -9,7 +9,6 @@ import {
99
Injector,
1010
Input,
1111
Output,
12-
PLATFORM_ID,
1312
SimpleChanges,
1413
TemplateRef,
1514
Type,
@@ -32,7 +31,6 @@ import {
3231
import {
3332
Observable,
3433
Subject,
35-
debounceTime,
3634
filter,
3735
fromEvent,
3836
merge,
@@ -117,7 +115,6 @@ export class NgDynamicJsonFormComponent
117115
optional: true,
118116
});
119117
private _cd = inject(ChangeDetectorRef);
120-
private _platformId = inject(PLATFORM_ID);
121118
private _el = inject(ElementRef);
122119
private _injector = inject(Injector);
123120
private _destroyRef = inject(DestroyRef);
@@ -238,10 +235,6 @@ export class NgDynamicJsonFormComponent
238235
}
239236

240237
ngOnChanges(simpleChanges: SimpleChanges): void {
241-
if (isPlatformServer(this._platformId)) {
242-
return;
243-
}
244-
245238
const { configs, hideErrorMessage } = simpleChanges;
246239

247240
if (hideErrorMessage) {
@@ -256,10 +249,6 @@ export class NgDynamicJsonFormComponent
256249
}
257250

258251
ngOnInit(): void {
259-
if (isPlatformServer(this._platformId)) {
260-
return;
261-
}
262-
263252
this._setupVariables();
264253
this._getControlDirective();
265254
this._buildForm();

lib/core/services/config-mapping.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { FormControlConfig } from '../models';
55
@Injectable()
66
export class ConfigMappingService {
77
getCorrectedConfig(input: FormControlConfig): FormControlConfig {
8-
const config = window.structuredClone(input) as FormControlConfig;
9-
const { formControlName, type, props, inputMask, children = [] } = config;
8+
const config = structuredClone(input) as FormControlConfig;
9+
const { formControlName, props, inputMask, children = [] } = config;
1010

1111
config.formControlName = this._getFormControlName(formControlName);
1212
config.value = config.value ?? this._getFallbackValue(config);

lib/core/services/form-conditions.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ export class FormConditionsService {
227227

228228
/**Get the target element by using `id`(full control path) on each `div` inside current NgDynamicJsonForm instance */
229229
private _getTargetEl$(controlPath: string): Observable<HTMLElement | null> {
230+
if (typeof window === 'undefined') {
231+
return of(null);
232+
}
233+
230234
return new Observable((subscriber) => {
231235
window.requestAnimationFrame(() => {
232236
// Use `CSS.escape()` to escape all the invalid characters.

lib/core/services/form-generator.service.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable, inject } from '@angular/core';
2-
import { AbstractControl, FormControl, UntypedFormGroup } from '@angular/forms';
2+
import { FormControl, UntypedFormGroup } from '@angular/forms';
33
import { ValidatorConfig } from '../models';
44
import { FormControlConfig } from '../models/form-control-config.interface';
55
import { FormValidationService } from './form-validation.service';
@@ -12,30 +12,13 @@ export class FormGeneratorService {
1212
const formGroup = new UntypedFormGroup({});
1313

1414
for (const item of data) {
15-
const isFormControl = !item.children?.length;
16-
const isFormGroup = !!item.children?.length;
15+
const control = !item.children?.length
16+
? new FormControl(item.value)
17+
: this.generateFormGroup(item.children);
1718

18-
let control: AbstractControl | null = null;
19-
20-
const validatorConfigs = (item.validators ?? []).reduce((acc, curr) => {
21-
if (!acc.find((x) => x.name === curr.name)) acc.push(curr);
22-
return acc;
23-
}, [] as ValidatorConfig[]);
24-
25-
const validators =
26-
this._formValidationService.getValidators(validatorConfigs);
27-
28-
if (isFormControl) {
29-
control = new FormControl(item.value);
30-
}
31-
32-
if (isFormGroup) {
33-
control = this.generateFormGroup(item.children!);
34-
}
35-
36-
if (!control) {
37-
throw 'failed to generate form control!';
38-
}
19+
const validators = this._formValidationService.getValidators(
20+
item.validators
21+
);
3922

4023
control.setValidators(validators);
4124
formGroup.addControl(item.formControlName, control);

lib/core/services/form-validation.service.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,21 @@ export class FormValidationService {
5757
);
5858
}
5959

60-
getValidators(input: ValidatorConfig[]): ValidatorFn[] {
61-
const customValidators = this._globalVariableService.customValidators;
62-
const getCustomValidator = (config: ValidatorConfig) => {
63-
const { name, value } = config;
64-
const validator = customValidators?.[name];
65-
66-
if (!validator) {
67-
return null;
68-
}
69-
70-
if (value === null || value === undefined) {
71-
return validator;
72-
}
60+
getValidators(input: ValidatorConfig[] | undefined): ValidatorFn[] | null {
61+
if (!input || !input.length) {
62+
return null;
63+
}
7364

74-
return validator(value) as ValidatorFn;
75-
};
65+
// Remove duplicates
66+
const filteredConfigs = [
67+
...new Map(input.map((v) => [v.name, v])).values(),
68+
];
7669

77-
return input.map((item) => {
70+
return filteredConfigs.map((item) => {
7871
const { name } = item;
7972
const value = this._getValidatorValue(item);
8073
const builtInValidator = builtInValidators(value)[name as ValidatorsEnum];
81-
const customValidator = getCustomValidator(item);
74+
const customValidator = this._getCustomValidator(item);
8275

8376
const result =
8477
builtInValidator ?? customValidator ?? Validators.nullValidator;
@@ -87,6 +80,22 @@ export class FormValidationService {
8780
});
8881
}
8982

83+
private _getCustomValidator(config: ValidatorConfig): ValidatorFn | null {
84+
const customValidators = this._globalVariableService.customValidators;
85+
const { name, value } = config;
86+
const validator = customValidators?.[name];
87+
88+
if (!validator) {
89+
return null;
90+
}
91+
92+
if (value === null || value === undefined || `${value}`.trim() === '') {
93+
return validator;
94+
}
95+
96+
return validator(value) as ValidatorFn;
97+
}
98+
9099
/**Get the error messages of the control
91100
*
92101
* @description

lib/core/services/options-data.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ export class OptionsDataService {
229229

230230
const form = this._globalVariableService.rootForm;
231231
const result = Object.keys(triggerBody).reduce((acc, key) => {
232-
const paths = getControlAndValuePath(triggerBody[key]);
233-
const value = getValueInObject(
234-
form?.get(paths.controlPath)?.value,
235-
paths.valuePath
232+
const { controlPath, valuePath } = getControlAndValuePath(
233+
triggerBody[key]
236234
);
237235

238-
acc[key] = value;
236+
const control = form?.get(controlPath);
237+
const value = getValueInObject(control?.value, valuePath);
238+
239+
acc[key] = !control ? triggerBody[key] : value;
239240
return acc;
240241
}, {} as any);
241242

lib/core/ui-basic/ui-basic-select/ui-basic-select.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class UiBasicSelectComponent extends CustomControlComponent {
2424

2525
override writeValue(obj: any): void {
2626
const value = this._controlValueService.getOptionsValue('stringified', obj);
27-
requestAnimationFrame(() => this.control.setValue(value));
27+
this.control.setValue(value);
2828
}
2929

3030
override registerOnChange(fn: any): void {

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-dynamic-json-form",
3-
"version": "8.2.5",
3+
"version": "8.2.7",
44
"author": {
55
"name": "erqk",
66
"url": "https://github.com/erqk"

0 commit comments

Comments
 (0)