Skip to content

Commit

Permalink
Merge pull request JustFly1984#879 from joshcoales/infowindow-childre…
Browse files Browse the repository at this point in the history
…n-of-marker

Anchoring InfoWindow children of Markers
  • Loading branch information
JustFly1984 authored May 29, 2020
2 parents 12589b4 + b9b319a commit 6228e74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/components/drawing/Marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import * as React from 'react'
import { unregisterEvents, applyUpdatersToPropsAndRegisterEvents } from '../../utils/helper'

import MapContext from '../../map-context'
import { HasMarkerAnchor } from '../../types'

import { Clusterer } from '@react-google-maps/marker-clusterer'
import { ReactNode } from 'react'

const eventMap = {
onAnimationChanged: 'animation_changed',
Expand Down Expand Up @@ -248,7 +250,18 @@ export class Marker extends React.PureComponent<MarkerProps, MarkerState> {
}

render(): React.ReactNode {
return this.props.children || null
let children: ReactNode | null = null
if(this.props.children) {
children = React.Children.map(this.props.children, child => {
if(!React.isValidElement<HasMarkerAnchor>(child)) {
return child;
}

let elementChild: React.ReactElement<HasMarkerAnchor> = child;
return React.cloneElement(elementChild, {anchor: this.state.marker});
})
}
return children || null
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface HasMarkerAnchor {
anchor?: google.maps.Marker | null
}

0 comments on commit 6228e74

Please sign in to comment.