Skip to content

Commit 02c24ec

Browse files
crisbetommalerba
authored andcommitted
fix(google-maps): support numbers for width/height of the map (#18105)
Similarly to what we support in Material and the CDK, these changes allow for numbers to be passed in directly as the `width` and `height` of a map. This is more convenient if the dimensions are computed.
1 parent efebc94 commit 02c24ec

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/google-maps/google-map/google-map.spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ describe('GoogleMap', () => {
9999
expect(container.nativeElement.style.width).toBe('350px');
100100
});
101101

102+
it('should be able to set a number value as the width/height', () => {
103+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
104+
mapConstructorSpy = createMapConstructorSpy(mapSpy);
105+
106+
const fixture = TestBed.createComponent(TestApp);
107+
const instance = fixture.componentInstance;
108+
instance.height = 750;
109+
instance.width = 400;
110+
fixture.detectChanges();
111+
112+
const container = fixture.debugElement.query(By.css('div'))!.nativeElement;
113+
expect(container.style.height).toBe('750px');
114+
expect(container.style.width).toBe('400px');
115+
116+
instance.height = '500';
117+
instance.width = '600';
118+
fixture.detectChanges();
119+
120+
expect(container.style.height).toBe('500px');
121+
expect(container.style.width).toBe('600px');
122+
});
123+
102124
it('sets center and zoom of the map', () => {
103125
const options = {center: {lat: 3, lng: 5}, zoom: 7};
104126
mapSpy = createMapSpy(options);
@@ -274,8 +296,8 @@ describe('GoogleMap', () => {
274296
})
275297
class TestApp {
276298
@ViewChild(GoogleMap) map: GoogleMap;
277-
height?: string;
278-
width?: string;
299+
height?: string | number;
300+
width?: string | number;
279301
center?: google.maps.LatLngLiteral;
280302
zoom?: number;
281303
options?: google.maps.MapOptions;

src/google-maps/google-map/google-map.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
7878
private _mapEl: HTMLElement;
7979
_googleMap: UpdatedGoogleMap;
8080

81-
@Input() height = DEFAULT_HEIGHT;
81+
@Input() height: string | number = DEFAULT_HEIGHT;
8282

83-
@Input() width = DEFAULT_WIDTH;
83+
@Input() width: string | number = DEFAULT_WIDTH;
8484

8585
@Input()
8686
set center(center: google.maps.LatLngLiteral|google.maps.LatLng) {
@@ -431,8 +431,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
431431

432432
private _setSize() {
433433
if (this._mapEl) {
434-
this._mapEl.style.height = this.height || DEFAULT_HEIGHT;
435-
this._mapEl.style.width = this.width || DEFAULT_WIDTH;
434+
const styles = this._mapEl.style;
435+
styles.height = coerceCssPixelValue(this.height) || DEFAULT_HEIGHT;
436+
styles.width = coerceCssPixelValue(this.width) || DEFAULT_WIDTH;
436437
}
437438
}
438439

@@ -493,3 +494,14 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
493494
}
494495
}
495496
}
497+
498+
const cssUnitsPattern = /([A-Za-z%]+)$/;
499+
500+
/** Coerces a value to a CSS pixel value. */
501+
function coerceCssPixelValue(value: any): string {
502+
if (value == null) {
503+
return '';
504+
}
505+
506+
return cssUnitsPattern.test(value) ? value : `${value}px`;
507+
}

tools/public_api_guard/google-maps/google-maps.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
66
readonly controls: Array<google.maps.MVCArray<Node>>;
77
readonly data: google.maps.Data;
88
headingChanged: Observable<void>;
9-
height: string;
9+
height: string | number;
1010
idle: Observable<void>;
1111
mapClick: Observable<google.maps.MouseEvent | google.maps.IconMouseEvent>;
1212
mapDblclick: Observable<google.maps.MouseEvent>;
@@ -24,7 +24,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
2424
projectionChanged: Observable<void>;
2525
tilesloaded: Observable<void>;
2626
tiltChanged: Observable<void>;
27-
width: string;
27+
width: string | number;
2828
zoom: number;
2929
zoomChanged: Observable<void>;
3030
constructor(_elementRef: ElementRef,

0 commit comments

Comments
 (0)