Skip to content

Commit

Permalink
add support for warning templates
Browse files Browse the repository at this point in the history
  • Loading branch information
wouter-willems committed Jun 10, 2024
1 parent ec918ca commit fa3b71f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
4 changes: 4 additions & 0 deletions projects/demo/src/app/demo/demo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,7 @@
some cool stuff here <br />
some cool stuff here <br />
some cool stuff here <br />

<ng-template #myFancyTemplate>
This can be <span style="color: red;">ANYTHING</span>. <span style="color: green; font-weight: bold; font-size: 1.2rem;">Look at me me, i am BOLD AND GREEN</span>
</ng-template>
8 changes: 5 additions & 3 deletions projects/demo/src/app/demo/demo.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ViewChild} from '@angular/core';
import {Component, TemplateRef, ViewChild} from '@angular/core';
import {AbstractControl, FormBuilder, UntypedFormArray, UntypedFormGroup, Validators} from '@angular/forms';
import {AppSelectOptions, SelectComponent} from '@klippa/ngx-enhancy-forms';

Expand All @@ -8,11 +8,13 @@ import {AppSelectOptions, SelectComponent} from '@klippa/ngx-enhancy-forms';
styleUrls: ['./demo.component.scss']
})
export class DemoComponent {
public formWarnings = new Map<AbstractControl, string>();
@ViewChild('myFancyTemplate') myFancyTemplate: TemplateRef<any>;
public formWarnings = new Map<AbstractControl, string | TemplateRef<any>>();
constructor(private fb: FormBuilder) {

setTimeout(() => {
this.formWarnings.set(this.myForm.get('name'), 'This is a warning about your name');
// this.formWarnings.set(this.myForm.get('name'), 'This is a warning about your name');
this.formWarnings.set(this.myForm.get('name'), this.myFancyTemplate);
// this.formWarnings.delete(this.myForm.get('name'));
}, 1500);

Expand Down
2 changes: 1 addition & 1 deletion projects/klippa/ngx-enhancy-forms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@klippa/ngx-enhancy-forms",
"version": "14.19.4",
"version": "14.20.0",
"publishConfig": {
"access": "public"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
<div class="errorTooltipInner">
<i class="closeBtn" (click)="closePopup();">×</i>
<ng-container *ngIf="getErrorToShow()" [ngTemplateOutlet]="errorRef"></ng-container>
<div *ngIf="!getErrorToShow() && shouldShowWarningPopup()">{{getWarningToShow()}}</div>
<div *ngIf="!getErrorToShow() && shouldShowWarningPopup()">
<ng-container *ngIf="getWarningToShowIsTemplateRef()" [ngTemplateOutlet]="getWarningToShowAsTemplateRef()"></ng-container>
<span *ngIf="!getWarningToShowIsTemplateRef()">{{getWarningToShow()}}</span>
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class FormElementComponent implements AfterViewInit {
this.popupState = 'onHover';
return;
}
if (stringIsSetAndFilled(this.getWarningToShow())) {
if (isValueSet(this.getWarningToShow())) {
this.popupState = 'lockedOpen';
return;
}
Expand Down Expand Up @@ -134,10 +134,21 @@ export class FormElementComponent implements AfterViewInit {
this.captionRef = templateRef;
}

getWarningToShow(): string {
public getWarningToShow(): string | TemplateRef<any> {
return this.parent?.getWarningToShow(this.attachedControl);
}

public getWarningToShowAsTemplateRef(): TemplateRef<any> {
if (this.parent?.getWarningToShow(this.attachedControl) instanceof TemplateRef) {
return this.parent?.getWarningToShow(this.attachedControl) as TemplateRef<any>;
}
throw new Error('Warning is not a TemplateRef');
}

public getWarningToShowIsTemplateRef(): boolean {
return this.getWarningToShow() instanceof TemplateRef;
}

getErrorToShow(): string {
const firstError = Object.keys(this.attachedControl?.errors ?? {})[0];
if (this.attachedControl?.touched !== true) {
Expand Down Expand Up @@ -203,7 +214,7 @@ export class FormElementComponent implements AfterViewInit {
if (stringIsSetAndFilled(this.getErrorToShow())) {
return !this.errorFullyVisible;
}
if (stringIsSetAndFilled(this.getWarningToShow())) {
if (isValueSet(this.getWarningToShow())) {
return true;
}
return false;
Expand All @@ -224,7 +235,7 @@ export class FormElementComponent implements AfterViewInit {
}

public shouldShowWarningPopup(): boolean {
return stringIsSetAndFilled(this.getWarningToShow());
return isValueSet(this.getWarningToShow());
}

public closePopup(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Optional,
Output,
SimpleChanges,
SkipSelf
SkipSelf, TemplateRef
} from '@angular/core';
import {AbstractControl, FormArray, FormControl, FormGroup, UntypedFormArray, UntypedFormControl, UntypedFormGroup} from '@angular/forms';
import {FormElementComponent} from './form-element/form-element.component';
Expand Down Expand Up @@ -36,7 +36,7 @@ export class FormComponent implements OnInit, OnDestroy, OnChanges {
@Input() public showErrorMessages = true;
@Input() public errorMessageLocation: 'belowCaption' | 'rightOfCaption' = 'belowCaption';
@Input() public formGroup: UntypedFormGroup;
@Input() public warnings: Map<AbstractControl, string> = new Map<AbstractControl, string>();
@Input() public warnings: Map<AbstractControl, string | TemplateRef<any>> = new Map<AbstractControl, string | TemplateRef<any>>();
@Input() public patchValueInterceptor: (values: any) => Promise<any>;
@Output() public onInjected = new EventEmitter<Record<string, any>>();

Expand Down Expand Up @@ -217,7 +217,7 @@ export class FormComponent implements OnInit, OnDestroy, OnChanges {
return this.activeControls.find((e) => e.formControl === control)?.formElement;
}

public getWarningToShow(control: AbstractControl): string {
public getWarningToShow(control: AbstractControl): string | TemplateRef<any> {
return this.warnings.get(control);
}

Expand Down

0 comments on commit fa3b71f

Please sign in to comment.