Skip to content

Commit ddc3d1d

Browse files
committed
feat(google-maps): allow for info window focus behavior to be customized
Allows for the `shouldFocus` flag to be passed when opening the info window. Fixes #23829.
1 parent a931de5 commit ddc3d1d

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-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

+6-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,11 @@ 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+
this.infoWindow.open({
182+
map: this._googleMap.googleMap,
183+
anchor: anchorObject,
184+
shouldFocus,
185+
});
182186
}
183187
}
184188

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
@@ -328,7 +328,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
328328
ngOnDestroy(): void;
329329
// (undocumented)
330330
ngOnInit(): void;
331-
open(anchor?: MapAnchorPoint): void;
331+
open(anchor?: MapAnchorPoint, shouldFocus?: boolean): void;
332332
// (undocumented)
333333
set options(options: google.maps.InfoWindowOptions);
334334
// (undocumented)

0 commit comments

Comments
 (0)