|
| 1 | +import { |
| 2 | + default as React, |
| 3 | + Component, |
| 4 | + PropTypes, |
| 5 | +} from "react"; |
| 6 | + |
| 7 | +import { |
| 8 | + default as canUseDOM, |
| 9 | +} from "can-use-dom"; |
| 10 | + |
| 11 | +import { |
| 12 | + default as warning, |
| 13 | +} from "warning"; |
| 14 | + |
| 15 | +import { |
| 16 | + GoogleMap, |
| 17 | +} from "../index"; |
| 18 | + |
| 19 | +import { |
| 20 | + default as makeUrl, |
| 21 | +} from "../utils/makeUrl"; |
| 22 | + |
| 23 | +export default class ScriptjsGoogleMap extends Component { |
| 24 | + static propTypes = { |
| 25 | + // PropTypes for URL generation |
| 26 | + // https://nodejs.org/api/url.html#url_url_format_urlobj |
| 27 | + protocol: PropTypes.string, |
| 28 | + hostname: PropTypes.string.isRequired, |
| 29 | + port: PropTypes.number, |
| 30 | + pathname: PropTypes.string.isRequired, |
| 31 | + query: PropTypes.object.isRequired, |
| 32 | + // PropTypes for ScriptjsGoogleMap |
| 33 | + loadingElement: PropTypes.node, |
| 34 | + } |
| 35 | + |
| 36 | + state = { |
| 37 | + isLoaded: false, |
| 38 | + } |
| 39 | + |
| 40 | + componentWillMount () { |
| 41 | + if (!canUseDOM) { |
| 42 | + return; |
| 43 | + } |
| 44 | + const scriptjs = require("scriptjs"); |
| 45 | + const {protocol, hostname, port, pathname, query, ...restProps} = this.props; |
| 46 | + const urlObj = {protocol, hostname, port, pathname, query}; |
| 47 | + const url = makeUrl(urlObj); |
| 48 | + scriptjs(url, () => this.setState({ isLoaded: true })); |
| 49 | + } |
| 50 | + |
| 51 | + componentWillReceiveProps (nextProps) { |
| 52 | + const changedKeys = Object.keys(ScriptjsGoogleMap.propTypes) |
| 53 | + .filter(key => this.props[key] !== nextProps[key]); |
| 54 | + |
| 55 | + warning(0 === changedKeys.length, `ScriptjsGoogleMap doesn't support mutating props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`); |
| 56 | + } |
| 57 | + |
| 58 | + render () { |
| 59 | + if (this.state.isLoaded) { |
| 60 | + const {protocol, hostname, port, pathname, query, ...restProps} = this.props; |
| 61 | + return ( |
| 62 | + <GoogleMap {...restProps} /> |
| 63 | + ); |
| 64 | + } else { |
| 65 | + return this.props.loadingElement; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments