Skip to content

Commit e345c4d

Browse files
committed
fix(lib): support default value in mat-search-google-maps-autocomplete
1 parent 84c16e2 commit e345c4d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,20 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
7676
germanAddress: GermanAddress;
7777
addressFormGroup: FormGroup;
7878

79+
firstInit = true;
80+
7981
propagateChange = (_: any) => {
8082
};
8183

8284
constructor(private formBuilder: FormBuilder) {
8385
}
8486

8587
ngOnInit() {
86-
this.addressFormGroup = this.createAddressFormGroup();
88+
this.createAddressFormGroup();
8789
}
8890

89-
createAddressFormGroup(): FormGroup {
90-
return this.formBuilder.group({
91+
createAddressFormGroup(): void {
92+
this.addressFormGroup = this.formBuilder.group({
9193
streetName: [this.value && this.value.streetName ? this.value.streetName : null, Validators.required],
9294
streetNumber: [this.value && this.value.streetNumber ? this.value.streetNumber : null, Validators.required],
9395
postalCode: [this.value && this.value.postalCode ? this.value.postalCode : null, Validators.required],
@@ -126,8 +128,17 @@ export class MatSearchGoogleMapsAutocompleteComponent implements OnInit, Control
126128
}
127129

128130
writeValue(obj: any): void {
131+
let shouldRecreateFG = false;
129132
if (obj) {
133+
if (!this.value && this.firstInit) {
134+
shouldRecreateFG = true;
135+
}
130136
this.value = obj;
137+
this.propagateChange(this.value);
138+
if (shouldRecreateFG) {
139+
this.createAddressFormGroup();
140+
this.firstInit = false;
141+
}
131142
}
132143
}
133144

src/app/app.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class AppComponent implements OnInit {
2121
public longitude: number;
2222
public showAsDirective = false;
2323
public showAsComponent = true;
24+
2425
addressValue: GermanAddress = {
2526
streetNumber: '100',
2627
streetName: 'Your StreetName',
@@ -66,6 +67,7 @@ export class AppComponent implements OnInit {
6667

6768
ngOnInit(): void {
6869
this.addressFormGroup = new FormGroup({
70+
// address: new FormControl(this.addressValue),
6971
address: new FormControl(),
7072
});
7173

0 commit comments

Comments
 (0)