Skip to content

Commit e6359cd

Browse files
authoredJan 13, 2022
feat(google-maps): allow for info window focus behavior to be customized (#23831)
Allows for the `shouldFocus` flag to be passed when opening the info window. Fixes #23829.
1 parent ffc1de4 commit e6359cd

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed
 

‎src/google-maps/map-info-window/map-info-window.spec.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ describe('MapInfoWindow', () => {
129129
expect(infoWindowSpy.close).toHaveBeenCalled();
130130

131131
infoWindowComponent.open(fakeMarkerComponent);
132-
expect(infoWindowSpy.open).toHaveBeenCalledWith(mapSpy, fakeMarker);
132+
expect(infoWindowSpy.open).toHaveBeenCalledWith(
133+
jasmine.objectContaining({
134+
map: mapSpy,
135+
anchor: fakeMarker,
136+
shouldFocus: undefined,
137+
}),
138+
);
133139
});
134140

135141
it('should not try to reopen info window multiple times for the same marker', () => {
@@ -224,6 +230,29 @@ describe('MapInfoWindow', () => {
224230
infoWindowComponent.open();
225231
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
226232
});
233+
234+
it('should allow for the focus behavior to be changed when opening the info window', () => {
235+
const fakeMarker = {} as unknown as google.maps.Marker;
236+
const fakeMarkerComponent = {
237+
marker: fakeMarker,
238+
getAnchor: () => fakeMarker,
239+
} as unknown as MapMarker;
240+
const infoWindowSpy = createInfoWindowSpy({});
241+
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
242+
243+
const fixture = TestBed.createComponent(TestApp);
244+
const infoWindowComponent = fixture.debugElement
245+
.query(By.directive(MapInfoWindow))!
246+
.injector.get<MapInfoWindow>(MapInfoWindow);
247+
fixture.detectChanges();
248+
249+
infoWindowComponent.open(fakeMarkerComponent, false);
250+
expect(infoWindowSpy.open).toHaveBeenCalledWith(
251+
jasmine.objectContaining({
252+
shouldFocus: false,
253+
}),
254+
);
255+
});
227256
});
228257

229258
@Component({

‎src/google-maps/map-info-window/map-info-window.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
168168
* Opens the MapInfoWindow using the provided anchor. If the anchor is not set,
169169
* then the position property of the options input is used instead.
170170
*/
171-
open(anchor?: MapAnchorPoint) {
171+
open(anchor?: MapAnchorPoint, shouldFocus?: boolean) {
172172
this._assertInitialized();
173173
const anchorObject = anchor ? anchor.getAnchor() : undefined;
174174

@@ -178,7 +178,13 @@ export class MapInfoWindow implements OnInit, OnDestroy {
178178
// case where the window doesn't have an anchor, but is placed at a particular position.
179179
if (this.infoWindow.get('anchor') !== anchorObject || !anchorObject) {
180180
this._elementRef.nativeElement.style.display = '';
181-
this.infoWindow.open(this._googleMap.googleMap, anchorObject);
181+
182+
// The config is cast to `any`, because the internal typings are out of date.
183+
this.infoWindow.open({
184+
map: this._googleMap.googleMap,
185+
anchor: anchorObject,
186+
shouldFocus,
187+
} as any);
182188
}
183189
}
184190

‎src/google-maps/testing/fake-google-map-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function createInfoWindowSpy(
148148
'get',
149149
]);
150150
infoWindowSpy.addListener.and.returnValue({remove: () => {}});
151-
infoWindowSpy.open.and.callFake((_map: any, target: any) => (anchor = target));
151+
infoWindowSpy.open.and.callFake((config: any) => (anchor = config.anchor));
152152
infoWindowSpy.close.and.callFake(() => (anchor = null));
153153
infoWindowSpy.get.and.callFake((key: string) => (key === 'anchor' ? anchor : null));
154154
return infoWindowSpy;

‎tools/public_api_guard/google-maps/google-maps.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
329329
ngOnDestroy(): void;
330330
// (undocumented)
331331
ngOnInit(): void;
332-
open(anchor?: MapAnchorPoint): void;
332+
open(anchor?: MapAnchorPoint, shouldFocus?: boolean): void;
333333
// (undocumented)
334334
set options(options: google.maps.InfoWindowOptions);
335335
// (undocumented)

0 commit comments

Comments
 (0)
Please sign in to comment.