UNPKG

react-google-maps

Version:
144 lines (128 loc) 3.81 kB
/* global google */ import React from "react" import PropTypes from "prop-types" import { construct, componentDidMount, componentDidUpdate, componentWillUnmount, } from "../utils/MapChildHelper" import { MAP, MARKER, ANCHOR, MARKER_CLUSTERER } from "../constants" export const __jscodeshiftPlaceholder__ = `{ "eventMapOverrides": { "onDblClick": "dblclick", "onDragEnd": "dragend", "onDragStart": "dragstart", "onMouseDown": "mousedown", "onMouseOut": "mouseout", "onMouseOver": "mouseover", "onMouseUp": "mouseup", "onRightClick": "rightclick" }, "getInstanceFromComponent": "this.state[MARKER]" }` /** * @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#Marker */ export class Marker extends React.PureComponent { static propTypes = { __jscodeshiftPlaceholder__: null, /** * For the 2nd argument of `MarkerCluster#addMarker` * @see https://github.com/mikesaidani/marker-clusterer-plus */ noRedraw: PropTypes.bool, /** * For `MarkerWithLabel` * @see https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js */ markerWithLabel: PropTypes.func, /** * For `MarkerWithLabel` * @see https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js */ labelClass: PropTypes.string, /** * For `MarkerWithLabel` * @see https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js */ labelAnchor: PropTypes.object, /** * For `MarkerWithLabel` * @see https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js */ labelContent: PropTypes.string, /** * For `MarkerWithLabel` * @see https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerwithlabel/src/markerwithlabel.js */ labelStyle: PropTypes.object, } static contextTypes = { [MAP]: PropTypes.object, [MARKER_CLUSTERER]: PropTypes.object, } static childContextTypes = { [ANCHOR]: PropTypes.object, } /* * @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#Marker */ constructor(props, context) { super(props, context) const GMKlass = this.props.markerWithLabel || google.maps.Marker const marker = new GMKlass() construct(Marker.propTypes, updaterMap, this.props, marker) const markerClusterer = this.context[MARKER_CLUSTERER] if (markerClusterer) { markerClusterer.addMarker(marker, !!this.props.noRedraw) } else { marker.setMap(this.context[MAP]) } this.state = { [MARKER]: marker, } } getChildContext() { return { [ANCHOR]: this.context[ANCHOR] || this.state[MARKER], } } componentDidMount() { componentDidMount(this, this.state[MARKER], eventMap) } componentDidUpdate(prevProps) { componentDidUpdate( this, this.state[MARKER], eventMap, updaterMap, prevProps ) } componentWillUnmount() { componentWillUnmount(this) const marker = this.state[MARKER] if (marker) { const markerClusterer = this.context[MARKER_CLUSTERER] if (markerClusterer) { markerClusterer.removeMarker(marker, !!this.props.noRedraw) } marker.setMap(null) } } render() { const { children } = this.props return <div>{children}</div> } } export default Marker const eventMap = {} const updaterMap = { /* * @see https://github.com/printercu/google-maps-utility-library-v3-read-only/blob/master/markerwithlabel/src/markerwithlabel.js */ labelContent(instance, labelContent) { instance.set(`labelContent`, labelContent) }, }