|
1 | 1 | import { AfterContentInit, Component, ContentChild, Input, OnDestroy } from '@angular/core'; |
| 2 | +import { NgControl } from '@angular/forms'; |
2 | 3 |
|
3 | | -import { removeSubscriptions } from '../../helpers'; |
| 4 | +import { Subscription } from 'rxjs'; |
4 | 5 |
|
| 6 | +import { removeSubscriptions } from '../../helpers'; |
5 | 7 | import { |
6 | 8 | InputGroupAddonLeftDirective, |
7 | 9 | InputGroupAddonRightDirective, |
8 | 10 | InputGroupContentDirective, |
9 | 11 | InputGroupLabelDirective |
10 | 12 | } from './input-group.directive'; |
11 | | - |
12 | 13 | import { InputTextDirective } from '../input-text/input-text.directive'; |
13 | | -import { NgControl } from '@angular/forms'; |
14 | | -import { Subscription } from 'rxjs'; |
15 | 14 |
|
16 | 15 |
|
17 | | -/* |
18 | | - * |
19 | | - */ |
20 | 16 | @Component({ |
21 | 17 | selector: 'mk-input-group', |
22 | 18 | templateUrl: './input-group.component.html' |
23 | 19 | }) |
24 | 20 | export class InputGroupComponent implements AfterContentInit, OnDestroy { |
25 | | - private subscriptions: Array<Subscription> = []; |
26 | | - |
27 | | - public currentColor: string; |
28 | | - public currentFontColor: string; |
29 | | - |
30 | | - @Input() addonLeft: string; |
31 | | - @Input() addonRight: string; |
| 21 | + @Input() addonLeft?: string; |
| 22 | + @Input() addonRight?: string; |
32 | 23 | @Input() inputColor = 'default'; |
33 | | - @Input() inputFontColor: string; |
| 24 | + @Input() inputFontColor?: string; |
34 | 25 | @Input() inputErrorColor = 'danger'; |
35 | | - @Input() inputErrorFontColor: string; |
| 26 | + @Input() inputErrorFontColor?: string; |
36 | 27 | @Input() inputValidColor = 'success'; |
37 | | - @Input() inputValidFontColor: string; |
38 | | - @Input() label: string; |
| 28 | + @Input() inputValidFontColor?: string; |
| 29 | + @Input() label?: string; |
39 | 30 | @Input() wrapperClasses = 'form-group'; |
40 | 31 |
|
41 | | - @ContentChild(InputGroupLabelDirective) public inputGroupLabelDirective: InputGroupLabelDirective; |
42 | | - @ContentChild(InputGroupAddonLeftDirective) public inputGroupAddonLeftDirective: InputGroupAddonLeftDirective; |
43 | | - @ContentChild(InputGroupAddonRightDirective) public inputGroupAddonRightDirective: InputGroupAddonRightDirective; |
44 | | - @ContentChild(InputGroupContentDirective) public inputGroupContentDirective: InputGroupContentDirective; |
45 | | - @ContentChild(InputTextDirective) public inputTextDirective: InputTextDirective; |
46 | | - |
47 | | - ngAfterContentInit() { |
48 | | - this.subscriptions.push(this.inputTextDirective.onKeyup.subscribe((value: NgControl) => { |
49 | | - if (value.invalid) { |
50 | | - this.currentColor = this.inputErrorColor; |
51 | | - this.currentFontColor = this.inputErrorFontColor; |
52 | | - } else if (!value.invalid) { |
53 | | - this.currentColor = this.inputValidColor; |
54 | | - this.currentFontColor = this.inputValidFontColor; |
55 | | - } else { |
56 | | - this.currentColor = this.inputColor; |
57 | | - this.currentFontColor = this.inputFontColor; |
58 | | - } |
59 | | - })); |
| 32 | + @ContentChild(InputGroupLabelDirective) public inputGroupLabelDirective?: InputGroupLabelDirective; |
| 33 | + @ContentChild(InputGroupAddonLeftDirective) public inputGroupAddonLeftDirective?: InputGroupAddonLeftDirective; |
| 34 | + @ContentChild(InputGroupAddonRightDirective) public inputGroupAddonRightDirective?: InputGroupAddonRightDirective; |
| 35 | + @ContentChild(InputGroupContentDirective) public inputGroupContentDirective?: InputGroupContentDirective; |
| 36 | + @ContentChild(InputTextDirective) public inputTextDirective?: InputTextDirective; |
| 37 | + |
| 38 | + private subscriptions: Subscription[] = []; |
| 39 | + |
| 40 | + public currentColor?: string; |
| 41 | + public currentFontColor?: string; |
| 42 | + |
| 43 | + ngAfterContentInit(): void { |
| 44 | + if (this.inputTextDirective) { |
| 45 | + this.subscriptions.push(this.inputTextDirective.onKeyup.subscribe((value: NgControl) => { |
| 46 | + if (value.invalid) { |
| 47 | + this.currentColor = this.inputErrorColor; |
| 48 | + this.currentFontColor = this.inputErrorFontColor; |
| 49 | + } else if (!value.invalid) { |
| 50 | + this.currentColor = this.inputValidColor; |
| 51 | + this.currentFontColor = this.inputValidFontColor; |
| 52 | + } else { |
| 53 | + this.currentColor = this.inputColor; |
| 54 | + this.currentFontColor = this.inputFontColor; |
| 55 | + } |
| 56 | + })); |
| 57 | + } |
60 | 58 | } |
61 | 59 |
|
62 | | - ngOnDestroy() { |
| 60 | + ngOnDestroy(): void { |
63 | 61 | removeSubscriptions(this.subscriptions); |
64 | 62 | } |
65 | 63 | } |
0 commit comments