Skip to content

Commit

Permalink
feat(showcase): add the ngx-form-errors demo to the showcase
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #1195
  • Loading branch information
christophercr committed Aug 23, 2020
1 parent 25927c6 commit 7e8e5b8
Show file tree
Hide file tree
Showing 28 changed files with 1,450 additions and 323 deletions.
730 changes: 418 additions & 312 deletions showcase/package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions showcase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@
"@angular/platform-server": "~7.2.2",
"@angular/router": "~7.2.2",
"@nationalbankbelgium/code-style": "^1.2.0",
"@nationalbankbelgium/stark-core": "file:../dist/packages-dist/stark-core/nationalbankbelgium-stark-core-10.0.0-e7b2cbb9.tgz",
"@nationalbankbelgium/stark-rbac": "file:../dist/packages-dist/stark-rbac/nationalbankbelgium-stark-rbac-10.0.0-e7b2cbb9.tgz",
"@nationalbankbelgium/stark-ui": "file:../dist/packages-dist/stark-ui/nationalbankbelgium-stark-ui-10.0.0-e7b2cbb9.tgz",
"@nationalbankbelgium/ngx-form-errors": "^1.0.0",
"@nationalbankbelgium/stark-core": "file:../dist/packages-dist/stark-core/nationalbankbelgium-stark-core-10.1.0-3442e87b.tgz",
"@nationalbankbelgium/stark-rbac": "file:../dist/packages-dist/stark-rbac/nationalbankbelgium-stark-rbac-10.1.0-3442e87b.tgz",
"@nationalbankbelgium/stark-ui": "file:../dist/packages-dist/stark-ui/nationalbankbelgium-stark-ui-10.1.0-3442e87b.tgz",
"@uirouter/visualizer": "~7.0.0",
"angular-in-memory-web-api": "~0.8.0",
"basscss": "~8.1.0",
Expand All @@ -139,8 +140,8 @@
},
"devDependencies": {
"@compodoc/compodoc": "~1.1.11",
"@nationalbankbelgium/stark-build": "file:../dist/packages-dist/stark-build/nationalbankbelgium-stark-build-10.0.0-e7b2cbb9.tgz",
"@nationalbankbelgium/stark-testing": "file:../dist/packages-dist/stark-testing/nationalbankbelgium-stark-testing-10.0.0-e7b2cbb9.tgz",
"@nationalbankbelgium/stark-build": "file:../dist/packages-dist/stark-build/nationalbankbelgium-stark-build-10.1.0-3442e87b.tgz",
"@nationalbankbelgium/stark-testing": "file:../dist/packages-dist/stark-testing/nationalbankbelgium-stark-testing-10.1.0-3442e87b.tgz",
"@types/core-js": "~2.5.3",
"@types/hammerjs": "~2.0.36",
"@types/node": "~10.17.27",
Expand Down
7 changes: 7 additions & 0 deletions showcase/src/app/app-menu.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export const APP_MENU_CONFIG: StarkMenuConfig = {
isVisible: true,
isEnabled: true,
targetState: "news"
},
{
id: "reactive-form-errors",
label: "SHOWCASE.NGX_FORM_ERRORS.TITLE",
isVisible: true,
isEnabled: true,
targetState: "reactive-form-errors"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions showcase/src/app/welcome/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./getting-started";
export * from "./home";
export * from "./news";
export * from "./no-content";
export * from "./reactive-form-errors";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<mat-card>
<ng-content></ng-content>
</mat-card>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:host mat-card {
box-sizing: border-box;
width: 100%;
min-height: 100%;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Component, HostBinding, Input } from "@angular/core";

type Colors = "primary" | "accent" | "warning" | "success";

@Component({
selector: "app-card",
templateUrl: "./card.component.html",
styleUrls: ["./card.component.scss"]
})
export class CardComponent {
@HostBinding("class.app-color-primary")
public primaryColor!: boolean;
@HostBinding("class.app-color-accent")
public accentColor!: boolean;
@HostBinding("class.app-color-warning")
public warningColor!: boolean;
@HostBinding("class.app-color-success")
public successColor!: boolean;

@Input()
public set color(color: Colors) {
this.primaryColor = false;
this.accentColor = false;
this.warningColor = false;
this.successColor = false;

switch (color) {
case "primary":
this.primaryColor = true;
break;
case "accent":
this.accentColor = true;
break;
case "warning":
this.warningColor = true;
break;
case "success":
this.successColor = true;
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
app-card.app-color-primary mat-card {
background-color: mat-color($primary-palette, 500);
color: mat-contrast($primary-palette, 500);
}

app-card.app-color-accent mat-card {
background-color: mat-color($primary-palette, 500);
color: mat-contrast($primary-palette, 500);
}

app-card.app-color-warning mat-card {
background-color: mat-color($warning-palette, 500);
color: mat-contrast($warning-palette, 500);
}

app-card.app-color-success mat-card {
/*Themes do not have a success map by default*/
background-color: mat-color($success-palette, 500);
color: mat-contrast($success-palette, 500);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./card.component";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./card";
export * from "./translated-form-error";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./translated-form-error.component";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div *ngFor="let error of errors; trackBy: trackError">{{ error.message | translate: error.params }}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Component, HostBinding, OnInit } from "@angular/core";
import { LangChangeEvent, TranslateService } from "@ngx-translate/core";
import { Observable } from "rxjs";
import { NgxFormErrorComponent, NgxFormFieldError } from "@nationalbankbelgium/ngx-form-errors";

@Component({
selector: "app-translated-form-error",
templateUrl: "./translated-form-error.component.html"
})
export class TranslatedFormErrorComponent implements NgxFormErrorComponent, OnInit {
@HostBinding("class")
public cssClass = "translated-form-error";

public errors: NgxFormFieldError[] = [];
public errors$!: Observable<NgxFormFieldError[]>;
public fieldName!: string;

public constructor(public translateService: TranslateService) {}

public ngOnInit(): void {
this.translateService.onLangChange.subscribe((_ev: LangChangeEvent) => {
this.updateTranslateFieldName(this.translateService.instant(this.fieldName));
});
}

public subscribeToErrors(): void {
this.errors$.subscribe((errors: NgxFormFieldError[]) => {
this.errors = errors;

if (errors.length) {
// the formField can be retrieved from the "fieldName" param of any of the errors
this.fieldName = errors[0].params.fieldName;
this.updateTranslateFieldName(this.translateService.instant(this.fieldName));
}
});
}

public updateTranslateFieldName(translatedFieldName: string): void {
for (const error of this.errors) {
error.params = { ...error.params, fieldName: translatedFieldName };
}
}

public trackError(index: number): number {
return index;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./reactive-form-errors-page.component";
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FormControl, FormGroup, ValidationErrors, ValidatorFn } from "@angular/forms";

export class PasswordValidator {
// Inspired on: http://plnkr.co/edit/Zcbg2T3tOxYmhxs7vaAm?p=preview
public static areEqual(formGroup: FormGroup): ValidationErrors | null {
let value: string | undefined;
let valid = true;

for (const key in formGroup.controls) {
if (formGroup.controls.hasOwnProperty(key)) {
const control: FormControl = <FormControl>formGroup.controls[key];

if (value === undefined) {
value = control.value;
} else if (value !== control.value) {
valid = false;
break;
}
}
}

/* tslint:disable-next-line:no-null-keyword */
return valid ? null : { areEqual: true };
}
}

export function getConfirmPasswordValidator(formGroup: FormGroup): ValidatorFn {
return (): ValidationErrors | null => {
return PasswordValidator.areEqual(formGroup);
};
}
Loading

0 comments on commit 7e8e5b8

Please sign in to comment.