Skip to content

Commit 1195376

Browse files
committed
fix(lib): added custom input functionality for the search component
1 parent a416d86 commit 1195376

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

projects/angular-material-extensions/google-maps-autocomplete/src/lib/component/mat-search-google-maps-autocomplete/mat-search-google-maps-autocomplete.component.ts

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {parseGermanAddress} from '../../helpers/parser';
55
import {GermanAddress} from '../../interfaces';
66
import {Appearance} from '../mat-google-maps-autocomplete.component';
77
import {InputAnimations} from '../../animations';
8+
import {debounceTime, distinctUntilChanged, takeUntil} from 'rxjs/operators';
9+
import {Subject} from 'rxjs/internal/Subject';
810

911
@Component({
1012
selector: 'mat-search-google-maps-autocomplete',
@@ -21,6 +23,11 @@ import {InputAnimations} from '../../animations';
2123
})
2224
export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, ControlValueAccessor {
2325

26+
constructor(private formBuilder: FormBuilder) {
27+
// Set the private defaults
28+
this._unsubscribeAll = new Subject();
29+
}
30+
2431
@Input()
2532
appearance: string | Appearance = Appearance.STANDARD;
2633

@@ -67,8 +74,7 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
6774
@Input()
6875
disableSearch: boolean;
6976

70-
@Input()
71-
value: GermanAddress;
77+
@Input() private _value: GermanAddress;
7278

7379
@Output()
7480
onGermanAddressMapped: EventEmitter<GermanAddress> = new EventEmitter<GermanAddress>();
@@ -78,14 +84,26 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
7884

7985
firstInit = true;
8086

87+
// Private
88+
private _unsubscribeAll: Subject<any>;
89+
8190
propagateChange = (_: any) => {
8291
};
8392

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);
85102
}
86103

87104
ngOnInit() {
88105
this.createAddressFormGroup();
106+
this.enableCustomInput();
89107
}
90108

91109
createAddressFormGroup(): void {
@@ -100,30 +118,82 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
100118
});
101119
}
102120

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+
103174
syncAutoComplete($event: google.maps.places.PlaceResult) {
104175
if (this.germanAddress) {
105176
this.addressFormGroup.reset();
106177
}
107178
const germanAddress: GermanAddress = parseGermanAddress($event);
108179
this.germanAddress = germanAddress;
109180
if (germanAddress.vicinity) {
110-
this.addressFormGroup.get('vicinity').patchValue(germanAddress.vicinity);
181+
this.addressFormGroup.get('vicinity').patchValue(germanAddress.vicinity, {emitEvent: false, onlySelf: true});
111182
}
112183
if (germanAddress.streetName) {
113-
this.addressFormGroup.get('streetName').patchValue(germanAddress.streetName);
184+
this.addressFormGroup.get('streetName').patchValue(germanAddress.streetName, {emitEvent: false, onlySelf: true});
114185
}
115186
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});
117188
}
118189
if (germanAddress.postalCode) {
119-
this.addressFormGroup.get('postalCode').patchValue(germanAddress.postalCode);
190+
this.addressFormGroup.get('postalCode').patchValue(germanAddress.postalCode, {emitEvent: false, onlySelf: true});
120191
}
121192
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});
123194
}
124195

125196
this.value = germanAddress;
126-
this.propagateChange(this.value);
127197
this.onGermanAddressMapped.emit(germanAddress);
128198
}
129199

@@ -134,7 +204,6 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
134204
shouldRecreateFG = true;
135205
}
136206
this.value = obj;
137-
this.propagateChange(this.value);
138207
if (shouldRecreateFG) {
139208
this.createAddressFormGroup();
140209
this.firstInit = false;

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ <h2>Usage</h2>
561561
formControlName="address"
562562
country="de"
563563
[showVicinity]="true"
564-
[readonly]="true"
564+
[readonly]="false"
565565
(onGermanAddressMapped)="onGermanAddressMapped($event)">
566566
</mat-search-google-maps-autocomplete>
567567
</form>

src/app/app.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export class AppComponent implements OnInit {
7171
address: new FormControl(),
7272
});
7373

74-
this.addressFormGroup.get('address').valueChanges.subscribe(value => console.log('value changed', value))
74+
this.addressFormGroup
75+
.get('address')
76+
.valueChanges
77+
.subscribe(value => console.log('value changed', value))
7578
}
7679
}

0 commit comments

Comments
 (0)