1
1
import { FocusMonitor } from '@angular/cdk/a11y' ;
2
2
import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
3
- import { Component , ElementRef , Input , OnDestroy } from '@angular/core' ;
4
- import { FormBuilder , FormGroup } from '@angular/forms' ;
3
+ import { Component , ElementRef , Input , OnDestroy , Optional , Self } from '@angular/core' ;
4
+ import { FormBuilder , FormGroup , ControlValueAccessor , NgControl } from '@angular/forms' ;
5
5
import { MatFormFieldControl } from '@angular/material' ;
6
6
import { Subject } from 'rxjs' ;
7
7
@@ -30,17 +30,18 @@ export class MyTel {
30
30
'[attr.aria-describedby]' : 'describedBy' ,
31
31
}
32
32
} )
33
- export class MyTelInput implements MatFormFieldControl < MyTel > , OnDestroy {
33
+ export class MyTelInput implements ControlValueAccessor , MatFormFieldControl < MyTel > , OnDestroy {
34
34
static nextId = 0 ;
35
35
36
36
parts : FormGroup ;
37
37
stateChanges = new Subject < void > ( ) ;
38
38
focused = false ;
39
- ngControl = null ;
40
39
errorState = false ;
41
40
controlType = 'example-tel-input' ;
42
41
id = `example-tel-input-${ MyTelInput . nextId ++ } ` ;
43
42
describedBy = '' ;
43
+ onChange = ( _ : any ) => { } ;
44
+ onTouched = ( ) => { } ;
44
45
45
46
get empty ( ) {
46
47
const { value : { area, exchange, subscriber} } = this . parts ;
@@ -89,7 +90,12 @@ export class MyTelInput implements MatFormFieldControl<MyTel>, OnDestroy {
89
90
this . stateChanges . next ( ) ;
90
91
}
91
92
92
- constructor ( fb : FormBuilder , private fm : FocusMonitor , private elRef : ElementRef < HTMLElement > ) {
93
+ constructor (
94
+ fb : FormBuilder ,
95
+ private fm : FocusMonitor ,
96
+ private elRef : ElementRef < HTMLElement > ,
97
+ @Optional ( ) @Self ( ) public ngControl : NgControl ) {
98
+
93
99
this . parts = fb . group ( {
94
100
area : '' ,
95
101
exchange : '' ,
@@ -100,6 +106,10 @@ export class MyTelInput implements MatFormFieldControl<MyTel>, OnDestroy {
100
106
this . focused = ! ! origin ;
101
107
this . stateChanges . next ( ) ;
102
108
} ) ;
109
+
110
+ if ( this . ngControl != null ) {
111
+ this . ngControl . valueAccessor = this ;
112
+ }
103
113
}
104
114
105
115
ngOnDestroy ( ) {
@@ -116,4 +126,32 @@ export class MyTelInput implements MatFormFieldControl<MyTel>, OnDestroy {
116
126
this . elRef . nativeElement . querySelector ( 'input' ) ! . focus ( ) ;
117
127
}
118
128
}
129
+
130
+ writeValue ( tel : MyTel | null ) : void {
131
+ this . value = tel ;
132
+ }
133
+
134
+ registerOnChange ( fn : any ) : void {
135
+ this . onChange = fn ;
136
+ }
137
+
138
+ registerOnTouched ( fn : any ) : void {
139
+ this . onTouched = fn ;
140
+ }
141
+
142
+ setDisabledState ( isDisabled : boolean ) : void {
143
+ this . disabled = isDisabled ;
144
+ }
145
+
146
+ _handleInput ( ) : void {
147
+ this . onChange ( this . parts . value ) ;
148
+ }
149
+
150
+ _handleBlur ( event : FocusEvent ) : void {
151
+ const inputs = Array . from ( this . elRef . nativeElement . querySelectorAll ( 'input' ) ) ;
152
+
153
+ if ( ! inputs . some ( input => event . relatedTarget === input ) ) {
154
+ this . onTouched ( ) ;
155
+ }
156
+ }
119
157
}
0 commit comments