@@ -5,6 +5,8 @@ import {parseGermanAddress} from '../../helpers/parser';
5
5
import { GermanAddress } from '../../interfaces' ;
6
6
import { Appearance } from '../mat-google-maps-autocomplete.component' ;
7
7
import { InputAnimations } from '../../animations' ;
8
+ import { debounceTime , distinctUntilChanged , takeUntil } from 'rxjs/operators' ;
9
+ import { Subject } from 'rxjs/internal/Subject' ;
8
10
9
11
@Component ( {
10
12
selector : 'mat-search-google-maps-autocomplete' ,
@@ -21,6 +23,11 @@ import {InputAnimations} from '../../animations';
21
23
} )
22
24
export class MatSearchGoogleMapsAutocompleteComponent implements OnInit , ControlValueAccessor {
23
25
26
+ constructor ( private formBuilder : FormBuilder ) {
27
+ // Set the private defaults
28
+ this . _unsubscribeAll = new Subject ( ) ;
29
+ }
30
+
24
31
@Input ( )
25
32
appearance : string | Appearance = Appearance . STANDARD ;
26
33
@@ -67,8 +74,7 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
67
74
@Input ( )
68
75
disableSearch : boolean ;
69
76
70
- @Input ( )
71
- value : GermanAddress ;
77
+ @Input ( ) private _value : GermanAddress ;
72
78
73
79
@Output ( )
74
80
onGermanAddressMapped : EventEmitter < GermanAddress > = new EventEmitter < GermanAddress > ( ) ;
@@ -78,14 +84,26 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
78
84
79
85
firstInit = true ;
80
86
87
+ // Private
88
+ private _unsubscribeAll : Subject < any > ;
89
+
81
90
propagateChange = ( _ : any ) => {
82
91
} ;
83
92
84
- constructor ( private formBuilder : FormBuilder ) {
93
+
94
+ get value ( ) : GermanAddress {
95
+ return this . _value ;
96
+ }
97
+
98
+ @Input ( )
99
+ set value ( value : GermanAddress ) {
100
+ this . _value = value ;
101
+ this . propagateChange ( this . value ) ;
85
102
}
86
103
87
104
ngOnInit ( ) {
88
105
this . createAddressFormGroup ( ) ;
106
+ this . enableCustomInput ( ) ;
89
107
}
90
108
91
109
createAddressFormGroup ( ) : void {
@@ -100,30 +118,82 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
100
118
} ) ;
101
119
}
102
120
121
+ enableCustomInput ( ) {
122
+ this . addressFormGroup
123
+ . get ( 'streetName' )
124
+ . valueChanges
125
+ . pipe ( distinctUntilChanged ( ) , debounceTime ( 400 ) , takeUntil ( this . _unsubscribeAll ) )
126
+ . subscribe ( streetName => {
127
+ console . log ( 'custom input for street Name' , streetName ) ;
128
+ console . log ( 'custom input - new german address' , this . value ) ;
129
+ ! this . value ? this . value = { streetName} : this . value . streetName = streetName ;
130
+ this . value . displayAddress = this . parseDisplayAddress ( ) ;
131
+ } ) ;
132
+ this . addressFormGroup
133
+ . get ( 'streetNumber' )
134
+ . valueChanges
135
+ . pipe ( distinctUntilChanged ( ) , debounceTime ( 400 ) , takeUntil ( this . _unsubscribeAll ) )
136
+ . subscribe ( streetNumber => {
137
+ ! this . value ? this . value = { streetNumber} : this . value . streetNumber = streetNumber ;
138
+ console . log ( 'custom input - new german address' , this . value ) ;
139
+ this . value . displayAddress = this . parseDisplayAddress ( ) ;
140
+ } ) ;
141
+ this . addressFormGroup
142
+ . get ( 'postalCode' )
143
+ . valueChanges
144
+ . pipe ( distinctUntilChanged ( ) , debounceTime ( 400 ) , takeUntil ( this . _unsubscribeAll ) )
145
+ . subscribe ( postalCode => {
146
+ ! this . value ? this . value = { postalCode} : this . value . postalCode = postalCode ;
147
+ console . log ( 'custom input - new german address' , this . value ) ;
148
+ this . value . displayAddress = this . parseDisplayAddress ( ) ;
149
+ } ) ;
150
+ this . addressFormGroup
151
+ . get ( 'vicinity' )
152
+ . valueChanges
153
+ . pipe ( distinctUntilChanged ( ) , debounceTime ( 400 ) , takeUntil ( this . _unsubscribeAll ) )
154
+ . subscribe ( vicinity => {
155
+ ! this . value ? this . value = { vicinity} : this . value . vicinity = vicinity ;
156
+ console . log ( 'custom input - new german address' , this . value ) ;
157
+ this . value . displayAddress = this . parseDisplayAddress ( ) ;
158
+ } ) ;
159
+ this . addressFormGroup
160
+ . get ( 'locality' )
161
+ . valueChanges
162
+ . pipe ( distinctUntilChanged ( ) , debounceTime ( 400 ) , takeUntil ( this . _unsubscribeAll ) )
163
+ . subscribe ( locality => {
164
+ ! this . value ? this . value = { locality} : this . value . locality = locality ;
165
+ console . log ( 'custom input - new german address' , this . value ) ;
166
+ this . value . displayAddress = this . parseDisplayAddress ( ) ;
167
+ } ) ;
168
+ }
169
+
170
+ parseDisplayAddress ( ) {
171
+ return `${ this . value ?. streetName } ${ this . value ?. streetNumber } , ${ this . value ?. postalCode } ${ this . value ?. locality ?. long } `
172
+ }
173
+
103
174
syncAutoComplete ( $event : google . maps . places . PlaceResult ) {
104
175
if ( this . germanAddress ) {
105
176
this . addressFormGroup . reset ( ) ;
106
177
}
107
178
const germanAddress : GermanAddress = parseGermanAddress ( $event ) ;
108
179
this . germanAddress = germanAddress ;
109
180
if ( germanAddress . vicinity ) {
110
- this . addressFormGroup . get ( 'vicinity' ) . patchValue ( germanAddress . vicinity ) ;
181
+ this . addressFormGroup . get ( 'vicinity' ) . patchValue ( germanAddress . vicinity , { emitEvent : false , onlySelf : true } ) ;
111
182
}
112
183
if ( germanAddress . streetName ) {
113
- this . addressFormGroup . get ( 'streetName' ) . patchValue ( germanAddress . streetName ) ;
184
+ this . addressFormGroup . get ( 'streetName' ) . patchValue ( germanAddress . streetName , { emitEvent : false , onlySelf : true } ) ;
114
185
}
115
186
if ( germanAddress . streetNumber ) {
116
- this . addressFormGroup . get ( 'streetNumber' ) . patchValue ( germanAddress . streetNumber . toString ( ) ) ;
187
+ this . addressFormGroup . get ( 'streetNumber' ) . patchValue ( germanAddress . streetNumber . toString ( ) , { emitEvent : false , onlySelf : true } ) ;
117
188
}
118
189
if ( germanAddress . postalCode ) {
119
- this . addressFormGroup . get ( 'postalCode' ) . patchValue ( germanAddress . postalCode ) ;
190
+ this . addressFormGroup . get ( 'postalCode' ) . patchValue ( germanAddress . postalCode , { emitEvent : false , onlySelf : true } ) ;
120
191
}
121
192
if ( germanAddress . locality && germanAddress . locality . long ) {
122
- this . addressFormGroup . get ( 'locality.long' ) . patchValue ( germanAddress . locality . long ) ;
193
+ this . addressFormGroup . get ( 'locality.long' ) . patchValue ( germanAddress . locality . long , { emitEvent : false , onlySelf : true } ) ;
123
194
}
124
195
125
196
this . value = germanAddress ;
126
- this . propagateChange ( this . value ) ;
127
197
this . onGermanAddressMapped . emit ( germanAddress ) ;
128
198
}
129
199
@@ -134,7 +204,6 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
134
204
shouldRecreateFG = true ;
135
205
}
136
206
this . value = obj ;
137
- this . propagateChange ( this . value ) ;
138
207
if ( shouldRecreateFG ) {
139
208
this . createAddressFormGroup ( ) ;
140
209
this . firstInit = false ;
0 commit comments