|
1 | | -import { |
2 | | - AfterContentInit, |
3 | | - AfterViewInit, |
4 | | - Component, |
5 | | - ComponentFactoryResolver, |
6 | | - ComponentRef, |
7 | | - ContentChild, |
8 | | - ElementRef, |
9 | | - HostBinding, |
10 | | - Inject, |
11 | | - Input, |
12 | | - Renderer2, |
13 | | - ViewChild, |
14 | | - ViewContainerRef |
15 | | -} from '@angular/core'; |
16 | | -import {FormControlName} from '@angular/forms'; |
17 | | -import {TranslateService} from '@ngx-translate/core'; |
18 | | -import {VALIDATION_ERROR_CONFIG, ValidationErrorsConfig} from './error-validation-config'; |
19 | | - |
20 | | -function toScreamingSnakeCase(input: string): string { |
21 | | - return input.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase(); |
22 | | -} |
| 1 | +import {AfterViewInit, Component, ContentChild, ElementRef} from '@angular/core'; |
| 2 | +import {ControlContainer, FormControlName} from '@angular/forms'; |
| 3 | +import {FormValidationContainer} from './form-validation-container'; |
23 | 4 |
|
24 | 5 | @Component({ |
25 | 6 | // tslint:disable:component-selector |
26 | 7 | selector: '[formFieldContainer], form-field-container', |
27 | 8 | template: ` |
28 | | - <ng-content></ng-content> |
29 | | - <ng-container #errorsContainer></ng-container> |
| 9 | + <ng-content></ng-content> |
| 10 | + <ng-container #errorsContainer></ng-container> |
30 | 11 | ` |
31 | 12 | }) |
32 | | -// <input-errors [innerValidationError]="innerValidationError" [messages]="messages" [params]="messageParams"></input-errors> |
33 | | -export class FormFieldContainerComponent implements AfterViewInit { |
34 | | - |
35 | | - @ContentChild(FormControlName) formControl: FormControlName; |
36 | | - |
37 | | - @ContentChild(FormControlName, {read: ElementRef}) input: ElementRef; |
38 | | - |
39 | | - @Input() customErrorMessages: {} = {}; |
40 | | - @Input() messageParams: {} = {}; |
41 | | - @Input() validationDisabled = false; |
42 | | - @Input() innerValidationError: boolean; |
43 | | - |
44 | | - @ViewChild('errorsContainer', {read: ViewContainerRef}) errorsContainer: ViewContainerRef; |
| 13 | +export class FormFieldContainerComponent extends FormValidationContainer implements AfterViewInit { |
45 | 14 |
|
46 | | - public messages: string[]; |
47 | | - private validationContext; |
48 | | - private componentRef: ComponentRef<any>; |
49 | | - |
50 | | - constructor( |
51 | | - private elRef: ElementRef, |
52 | | - private renderer: Renderer2, |
53 | | - private translateService: TranslateService, |
54 | | - private componentFactoryResolver: ComponentFactoryResolver, |
55 | | - @Inject(VALIDATION_ERROR_CONFIG) private validationErrorsConfig: ValidationErrorsConfig) { |
56 | | - this.validationContext = validationErrorsConfig.defaultContext; |
57 | | - } |
58 | | - |
59 | | - ngAfterViewInit(): void { |
60 | | - this.addErrorComponent(); |
61 | | - this.updateErrorComponent(); |
62 | | - } |
63 | | - |
64 | | - addErrorComponent() { |
65 | | - if (this.errorsContainer && !this.componentRef) { |
66 | | - const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.validationErrorsConfig.errorComponent as any); |
67 | | - this.componentRef = this.errorsContainer.createComponent(componentFactory); |
68 | | - } |
69 | | - } |
| 15 | + // tslint:disable-next-line:variable-name |
| 16 | + @ContentChild(FormControlName) _formControl: FormControlName; |
70 | 17 |
|
71 | | - updateErrorComponent() { |
72 | | - this.addErrorComponent(); |
73 | | - |
74 | | - if (this.componentRef) { |
75 | | - this.componentRef.instance.innerValidationError = this.innerValidationError; |
76 | | - this.componentRef.instance.messages = this.messages; |
77 | | - this.componentRef.instance.params = this.messageParams; |
78 | | - this.componentRef.changeDetectorRef.detectChanges(); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - @HostBinding('class.has-error') |
83 | | - get hasErrors(): boolean { |
84 | | - const hasError = (!this.formControl.valid && this.formControl.dirty && this.formControl.touched) && !this.validationDisabled; |
85 | | - |
86 | | - if (hasError && this.input && this.input.nativeElement) { |
87 | | - this.messages = Object.keys(this.formControl.errors).map(error => { |
88 | | - const fieldName = this.formControl.name; |
89 | | - const errorKey = `${toScreamingSnakeCase(fieldName)}.ERRORS.${toScreamingSnakeCase(error)}`; |
90 | | - if (this.translateService.instant(`${this.validationContext}.${errorKey}`) === `${this.validationContext}.${errorKey}`) { |
91 | | - return `${this.validationErrorsConfig.defaultContext}.ERRORS.${toScreamingSnakeCase(error)}`; |
92 | | - } else { |
93 | | - return `${this.validationContext}.${errorKey}`; |
94 | | - } |
95 | | - }); |
96 | | - const params = Object.values(this.formControl.errors).reduce((a, b) => { |
97 | | - a = {...a, ...b}; |
98 | | - return a; |
99 | | - }, {}); |
100 | | - this.messageParams = this.messageParams ? {...this.messageParams, ...params} : params; |
101 | | - if (this.messages && this.messages.length > 0) { |
102 | | - this.messages = [this.messages[0]]; |
103 | | - } |
104 | | - try { |
105 | | - this.renderer.removeClass(this.input.nativeElement, 'is-valid'); |
106 | | - |
107 | | - } catch (e) { |
108 | | - } |
109 | | - this.renderer.addClass(this.input.nativeElement, 'is-invalid'); |
110 | | - |
111 | | - } |
112 | | - this.updateErrorComponent(); |
113 | | - |
114 | | - return hasError; |
115 | | - } |
116 | | - |
117 | | - @HostBinding('class.has-success') |
118 | | - get hasSuccess(): boolean { |
119 | | - const hasSuccess = ( |
120 | | - this.formControl.valid && |
121 | | - this.formControl.dirty && this.formControl.touched) && |
122 | | - !this.validationDisabled; |
123 | | - if (hasSuccess && this.input && this.input.nativeElement) { |
124 | | - this.messages = []; |
125 | | - try { |
126 | | - this.renderer.removeClass(this.input.nativeElement, 'is-invalid'); |
127 | | - |
128 | | - } catch (e) { |
129 | | - } |
130 | | - } |
131 | | - return; |
132 | | - } |
| 18 | + // tslint:disable-next-line:variable-name |
| 19 | + @ContentChild(FormControlName, {read: ElementRef}) _input: ElementRef; |
133 | 20 |
|
134 | | - public setValidationContext(context: string): void { |
135 | | - this.validationContext = context; |
| 21 | + get formControl(): ControlContainer { |
| 22 | + return this._formControl; |
136 | 23 | } |
137 | 24 |
|
138 | | - setInnerValidation(innerValidation: boolean): void { |
139 | | - this.innerValidationError = innerValidation; |
| 25 | + get el(): ElementRef<any> { |
| 26 | + return this._input; |
140 | 27 | } |
141 | 28 | } |
0 commit comments