-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ScriptjsGoogleMap): add "scriptjs" support
* Ref #92
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
default as React, | ||
Component, | ||
PropTypes, | ||
} from "react"; | ||
|
||
import { | ||
default as canUseDOM, | ||
} from "can-use-dom"; | ||
|
||
import { | ||
default as warning, | ||
} from "warning"; | ||
|
||
import { | ||
GoogleMap, | ||
} from "../index"; | ||
|
||
import { | ||
default as makeUrl, | ||
} from "../utils/makeUrl"; | ||
|
||
export default class ScriptjsGoogleMap extends Component { | ||
static propTypes = { | ||
// PropTypes for URL generation | ||
// https://nodejs.org/api/url.html#url_url_format_urlobj | ||
protocol: PropTypes.string, | ||
hostname: PropTypes.string.isRequired, | ||
port: PropTypes.number, | ||
pathname: PropTypes.string.isRequired, | ||
query: PropTypes.object.isRequired, | ||
// PropTypes for ScriptjsGoogleMap | ||
loadingElement: PropTypes.node, | ||
} | ||
|
||
state = { | ||
isLoaded: false, | ||
} | ||
|
||
componentWillMount () { | ||
if (!canUseDOM) { | ||
return; | ||
} | ||
const scriptjs = require("scriptjs"); | ||
const {protocol, hostname, port, pathname, query, ...restProps} = this.props; | ||
const urlObj = {protocol, hostname, port, pathname, query}; | ||
const url = makeUrl(urlObj); | ||
scriptjs(url, () => this.setState({ isLoaded: true })); | ||
} | ||
|
||
componentWillReceiveProps (nextProps) { | ||
const changedKeys = Object.keys(ScriptjsGoogleMap.propTypes) | ||
.filter(key => this.props[key] !== nextProps[key]); | ||
|
||
warning(0 === changedKeys.length, `ScriptjsGoogleMap doesn't support mutating props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`); | ||
} | ||
|
||
render () { | ||
if (this.state.isLoaded) { | ||
const {protocol, hostname, port, pathname, query, ...restProps} = this.props; | ||
return ( | ||
<GoogleMap {...restProps} /> | ||
); | ||
} else { | ||
return this.props.loadingElement; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { | ||
format as formatUrlObj, | ||
} from "url"; | ||
|
||
export default function makeUrl (urlObj) { | ||
return formatUrlObj({ | ||
protocol: urlObj.protocol, | ||
hostname: urlObj.hostname, | ||
port: urlObj.port, | ||
pathname: urlObj.pathname, | ||
query: urlObj.query, | ||
}); | ||
} |