Skip to content

Commit

Permalink
feat(utils): add triggerEvent api
Browse files Browse the repository at this point in the history
* Closes #161
  • Loading branch information
tomchentw committed Dec 7, 2015
1 parent 9408309 commit 112a1e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/GoogleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Use "GoogleMapLoader" instead. See https://github.com/tomchentw/react-google-map

return (
<GoogleMapLoader
ref="loader"
containerElement={React.createElement(realContainerTagName, containerProps)}
googleMapElement={
<GoogleMap ref="delegate" containerTagName={USE_NEW_BEHAVIOR_TAG_NAME} {...mapProps}>
Expand Down
31 changes: 31 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
default as invariant,
} from "invariant";

import {
default as GoogleMap,
} from "../GoogleMap";

export function __getInstance (component) {
const {constructor} = component;
if (GoogleMap === constructor) {
if (component.props.map) {
return component.props.map;
} else {
// Deprecated
component = component.refs.loader;
}
}
// Otherwise, every other instance type exists in component state
const key = Object.keys(component.state)[0];
return component.state[key];
}

export function triggerEvent (component, ...args) {
const instance = __getInstance(component);
invariant(instance,
`The react-google-maps component %s is not mounted, hence we can't find an
associated Google Maps JavaScript API v3 instance with it.`, component);

return google.maps.event.trigger(instance, ...args);
}

0 comments on commit 112a1e6

Please sign in to comment.