|
| 1 | +/* global google */ |
| 2 | +import _ from "lodash" |
| 3 | +import warning from "warning" |
| 4 | +import invariant from "invariant" |
| 5 | +import { getDisplayName, createEagerFactory } from "recompose" |
| 6 | +import PropTypes from "prop-types" |
| 7 | +import React from "react" |
| 8 | +import { MAP } from "./constants" |
| 9 | + |
| 10 | +export default function withGoogleMap(BaseComponent) { |
| 11 | + const factory = createEagerFactory(BaseComponent) |
| 12 | + |
| 13 | + class Container extends React.PureComponent { |
| 14 | + static displayName = `withGoogleMap(${getDisplayName(BaseComponent)})` |
| 15 | + |
| 16 | + static propTypes = { |
| 17 | + containerElement: PropTypes.node.isRequired, |
| 18 | + mapElement: PropTypes.node.isRequired, |
| 19 | + } |
| 20 | + |
| 21 | + static childContextTypes = { |
| 22 | + [MAP]: PropTypes.object, |
| 23 | + } |
| 24 | + |
| 25 | + state = { |
| 26 | + map: null, |
| 27 | + } |
| 28 | + |
| 29 | + handleComponentMount = _.bind(this.handleComponentMount, this) |
| 30 | + |
| 31 | + getChildContext() { |
| 32 | + return { |
| 33 | + [MAP]: this.state.map, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + componentWillMount() { |
| 38 | + const { containerElement, mapElement } = this.props |
| 39 | + invariant( |
| 40 | + !!containerElement && !!mapElement, |
| 41 | + `Required props containerElement or mapElement is missing. You need to provide both of them. |
| 42 | + The \`google.maps.Map\` instance will be initialized on mapElement and it's wrapped by\ |
| 43 | + containerElement.\nYou need to provide both of them since Google Map requires the DOM to\ |
| 44 | + have height when initialized.` |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + handleComponentMount(node) { |
| 49 | + if (this.state.map || node === null) { |
| 50 | + return |
| 51 | + } |
| 52 | + warning( |
| 53 | + `undefined` !== typeof google, |
| 54 | + `Make sure you've put a <script> tag in your <head> element to load Google Maps JavaScript API v3. |
| 55 | + If you're looking for built-in support to load it for you, use the "async/ScriptjsLoader" instead. |
| 56 | + See https://github.com/tomchentw/react-google-maps/pull/168` |
| 57 | + ) |
| 58 | + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Map |
| 59 | + const map = new google.maps.Map(node) |
| 60 | + this.setState({ map }) |
| 61 | + } |
| 62 | + |
| 63 | + render() { |
| 64 | + const { containerElement, mapElement, ...restProps } = this.props |
| 65 | + |
| 66 | + const { map } = this.state |
| 67 | + |
| 68 | + if (map) { |
| 69 | + return React.cloneElement( |
| 70 | + containerElement, |
| 71 | + {}, |
| 72 | + React.cloneElement(mapElement, { |
| 73 | + ref: this.handleComponentMount, |
| 74 | + }), |
| 75 | + <div>{factory(restProps)}</div> |
| 76 | + ) |
| 77 | + } else { |
| 78 | + return React.cloneElement( |
| 79 | + containerElement, |
| 80 | + {}, |
| 81 | + React.cloneElement(mapElement, { |
| 82 | + ref: this.handleComponentMount, |
| 83 | + }), |
| 84 | + <div /> |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return Container |
| 91 | +} |
0 commit comments