Skip to content

Commit 4a3b6d1

Browse files
committed
update parser component
1 parent fbabce3 commit 4a3b6d1

File tree

9 files changed

+56
-46
lines changed

9 files changed

+56
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p align="center">
22
<a href="">
3-
<img alt="docsify" src="https://avatars.githubusercontent.com/u/114390448" style="width: 150px">
3+
<img alt="godzilla" src="https://user-images.githubusercontent.com/4659608/197927721-8e3a85ca-4c4b-4a26-88c1-2143839508f2.png" style="width: 350px">
44
</a>
55
</p>
66

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@godzilla-forms/angular-parser",
3-
"version": "0.0.11",
3+
"version": "0.0.14",
44
"description": "Godzilla Forms parser library for angular",
55
"private": false,
66
"publishConfig": {
@@ -30,7 +30,7 @@
3030
"@angular/common": "^14.0.0",
3131
"@angular/core": "^14.0.0",
3232
"@angular/forms": "^14.0.0",
33-
"@godzilla-forms/core": "^0.0.9",
33+
"@godzilla-forms/core": "^0.0.12",
3434
"rxjs": "~7.5.7"
3535
},
3636
"devDependencies": {

src/components/controller/error/error-controller.component.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<ng-container>
2-
<div class="invalid-feedback d-block" *ngIf="submitted && getFormController().invalid">
3-
<span *ngIf="getFormController().errors!!['pattern']" class="is-invalid">
2+
<div class="invalid-feedback d-block" *ngIf="submitted && form.invalid">
3+
<span *ngIf="errorList['pattern']" class="is-invalid">
44
{{ control.errors?.pattern ? control.errors?.pattern : "Field is not valid" }}
55
</span>
6-
<span *ngIf="getFormController().errors!!['required']" class="is-invalid">
6+
<span *ngIf="errorList['required']" class="is-invalid">
77
{{ control.errors?.required ? control.errors?.required : "Field is required" }}
88
</span>
9-
<span *ngIf="getFormController().errors!!['maxlength']" class="is-invalid">
9+
<span *ngIf="errorList['maxlength']" class="is-invalid">
1010
{{
1111
control.errors?.maxLength
1212
? control.errors?.maxLength
1313
: "Value cannot <" + control.validators?.maxLength
1414
}}
1515
</span>
16-
<span *ngIf="getFormController().errors!!['minlength']" class="is-invalid">{{
16+
<span *ngIf="errorList['minlength']" class="is-invalid">{{
1717
control.errors?.minLength
1818
? control.errors?.minLength
1919
: "Value cannot >" + control.validators?.minLength
2020
}}</span>
21-
<span *ngIf="getFormController().errors!!['max']" class="is-invalid">{{
21+
<span *ngIf="errorList['max']" class="is-invalid">{{
2222
control.errors?.max ? control.errors?.max : "Value cannot <" + control.validators?.max
2323
}}</span>
24-
<span *ngIf="getFormController().errors!!['min']" class="is-invalid">{{
24+
<span *ngIf="errorList['min']" class="is-invalid">{{
2525
control.errors?.min ? control.errors?.min : "Value cannot >" + control.validators?.min
2626
}}</span>
27-
<span *ngIf="getFormController().errors!!['maxSize']" class="is-invalid">maxSize</span>
28-
<span *ngIf="getFormController().errors!!['invalidMimeType']" class="is-invalid"
27+
<span *ngIf="errorList['maxSize']" class="is-invalid">maxSize</span>
28+
<span *ngIf="errorList['invalidMimeType']" class="is-invalid"
2929
>invalidMimeType</span
3030
>
3131
</div>

src/components/controller/error/error-controller.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export class ErrorControllerComponent implements OnInit {
2424
this.form = <FormGroup>this.controlContainer.control;
2525
}
2626

27-
getFormController() {
28-
return this.form.get(this.controlName)!!;
27+
get errorList() {
28+
const errors = this.form.get(this.controlName)?.errors;
29+
if (errors) {
30+
return errors;
31+
}
32+
return [];
2933
}
3034
}

src/components/form-parser.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div [formGroup]="form" class="row needs-validation">
2-
<div *ngFor="let page of jsonForm?.pages" class="row">
1+
<div [formGroup]="formGroup" class="row needs-validation">
2+
<div *ngFor="let page of form?.pages" class="row">
33
<div *ngFor="let control of page.controls" [class]="getCssClass(control)">
44
<div
55
*ngIf="isFlowValid(control)"
@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656
<godzilla-forms-button-controller
57-
*ngIf="jsonForm?.style?.showSubmitButton"
57+
*ngIf="form?.style?.showSubmitButton"
5858
(onClick)="validate()"
59-
[label]="jsonForm?.style?.submitLabel"></godzilla-forms-button-controller>
59+
[label]="form?.style?.submitLabel"></godzilla-forms-button-controller>
6060
</div>

src/components/form-parser.component.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,34 @@ import { controlCssClass, controlFlow, controlValidators } from '../utils/contro
1717
styleUrls: [],
1818
})
1919
export class GodzillaFormsParserComponent implements OnChanges {
20-
@Input() jsonForm: GodzillaForm | undefined;
20+
@Input() form: GodzillaForm | undefined;
21+
22+
@Input() jsonForm: string | undefined;
2123

2224
@Input() enableGridSystem: boolean = true;
2325

2426
@Output() readonly validData: EventEmitter<any> = new EventEmitter<any>();
2527

2628
submitted = false;
2729

28-
readonly form: FormGroup = this.formBuilder.group({});
30+
readonly formGroup: FormGroup = this.formBuilder.group({});
2931

3032
constructor(private formBuilder: FormBuilder, private godzillaLoader: GodzillaLoaderService) {}
3133

3234
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3335
ngOnChanges(changes: SimpleChanges) {
34-
if (this.jsonForm) {
35-
this.createPages(this.jsonForm.pages);
36+
if (this.form || this.jsonForm) {
37+
this.form = this.form || (JSON.parse(this.jsonForm || '') as GodzillaForm);
38+
this.createPages(this.form.pages);
3639
}
3740
}
3841

3942
/**
4043
* Public function to notify the parser component to reset the form
4144
*/
4245
public notifyFormChanged() {
43-
if (this.jsonForm) {
44-
this.createPages(this.jsonForm.pages);
46+
if (this.form) {
47+
this.createPages(this.form.pages);
4548
}
4649
}
4750

@@ -51,8 +54,8 @@ export class GodzillaFormsParserComponent implements OnChanges {
5154
*/
5255
public validate() {
5356
this.submitted = true;
54-
if (this.form.valid) {
55-
this.validData.emit(this.form.getRawValue());
57+
if (this.formGroup.valid) {
58+
this.validData.emit(this.formGroup.getRawValue());
5659
}
5760
}
5861

@@ -63,7 +66,7 @@ export class GodzillaFormsParserComponent implements OnChanges {
6366
* @private
6467
*/
6568
getFormControllerById(id: string) {
66-
return this.form.get(id);
69+
return this.formGroup.get(id);
6770
}
6871

6972
/**
@@ -75,11 +78,11 @@ export class GodzillaFormsParserComponent implements OnChanges {
7578
// eslint-disable-next-line no-restricted-syntax
7679
for (const page of pages) {
7780
// eslint-disable-next-line no-continue
78-
if (this.jsonForm?.style.type === GodzillaFormType.classic) {
79-
this.createForm(page.controls, this.form);
81+
if (this.form?.style.type === GodzillaFormType.classic) {
82+
this.createForm(page.controls, this.formGroup);
8083
} else {
8184
const group = this.formBuilder.group({});
82-
this.form.addControl(page.id, group);
85+
this.formGroup.addControl(page.id, group);
8386
}
8487
}
8588
}

src/parser.module.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ import { ButtonControllerComponent } from './components/controller/button/button
3535
})
3636
export class GodzillaParserModule {
3737
public static forRoot(config?: GodzillaOptions): ModuleWithProviders<GodzillaParserModule> {
38+
return this.getConfig(config);
39+
}
40+
41+
public static forChild(config?: GodzillaOptions): ModuleWithProviders<GodzillaParserModule> {
42+
return this.getConfig(config);
43+
}
44+
45+
private static getConfig(config?: GodzillaOptions) {
3846
return {
3947
ngModule: GodzillaParserModule,
4048
providers: [
@@ -45,11 +53,4 @@ export class GodzillaParserModule {
4553
],
4654
};
4755
}
48-
49-
public static forChild(): ModuleWithProviders<GodzillaParserModule> {
50-
return {
51-
ngModule: GodzillaParserModule,
52-
providers: [],
53-
};
54-
}
5556
}

src/utils/controller/flow.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ export function controlFlow(control: GodzillaFormControls, rootController: any)
6161
return true;
6262
}
6363
}
64+
65+

0 commit comments

Comments
 (0)