Skip to content

Commit

Permalink
feat(ScriptjsLoader): new behavior will render GoogleMapLoader instead
Browse files Browse the repository at this point in the history
* Ref #92

BREAKING CHANGE: ScriptjsLoader will delegate to GoogleMapLoader when the script is loaded

Before:

```js
<ScriptjsLoader
  hostname={"maps.googleapis.com"}
  pathname={"/maps/api/js"}
  query={{v: `3.${ AsyncGettingStarted.version }`, libraries: "geometry,drawing,places"}}
  loadingElement={
    <div {...this.props} style={{ height: "100%" }}>
      <FaSpinner />
    </div>
  }
  googleMapElement={
    <GoogleMap
      containerProps={{
        ...this.props,
        style: {
          height: "100%",
        },
      }}
      ref={googleMap => {
        // Wait until GoogleMap is fully loaded. Related to #133
        setTimeout(() => {
          googleMap && console.log(`Zoom: ${ googleMap.getZoom() }`);
        }, 50);
      }}
      defaultZoom={3}
      defaultCenter={{lat: -25.363882, lng: 131.044922}}
      onClick={::this.handleMapClick}
    >
      <Marker
        {...this.state.marker}
        onRightclick={this.handleMarkerRightclick}
      />
    </GoogleMap>
  }
/>
```

After:

```js
<ScriptjsLoader
  hostname={"maps.googleapis.com"}
  pathname={"/maps/api/js"}
  query={{v: `3.${ AsyncGettingStarted.version }`, libraries: "geometry,drawing,places"}}
  loadingElement={
    <div {...this.props} style={{ height: "100%" }}>
      <FaSpinner />
    </div>
  }
  containerElement={
    <div {...this.props} style={{ height: "100%" }} />
  }
  googleMapElement={
    <GoogleMap
      ref={googleMap => {
        googleMap && console.log(`Zoom: ${ googleMap.getZoom() }`);
      }}
      defaultZoom={3}
      defaultCenter={{lat: -25.363882, lng: 131.044922}}
      onClick={::this.handleMapClick}
    >
      <Marker
        {...this.state.marker}
        onRightclick={this.handleMarkerRightclick}
      />
    </GoogleMap>
  }
/>
```
  • Loading branch information
tomchentw committed Nov 22, 2015
1 parent 6a34ff7 commit 0f100d8
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/async/ScriptjsLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "warning";

import {
GoogleMapLoader,
GoogleMap,
} from "../index";

Expand All @@ -31,14 +32,32 @@ export default class ScriptjsLoader extends Component {
...urlObjDefinition,
// PropTypes for ScriptjsLoader
loadingElement: PropTypes.node,
// ...GoogleMapLoader.propTypes,// Uncomment for 5.0.0
googleMapElement: propTypesElementOfType(GoogleMap).isRequired,
}
};

static defaultProps = {
};

state = {
isLoaded: false,
}

shouldUseNewBehavior () {
const {containerTagName, containerProps} = this.props.googleMapElement.props;
return (
null != this.props.containerElement &&
undefined === containerTagName &&
undefined === containerProps
);
}

componentWillMount () {
warning(this.shouldUseNewBehavior(),
`"async/ScriptjsLoader" is now rendering "GoogleMapLoader". Migrate to use "GoogleMapLoader" instead.
The old behavior will be removed in next major release (5.0.0).
See https://github.com/tomchentw/react-google-maps/pull/157 for more details.`
);
if (!canUseDOM) {
return;
}
Expand All @@ -65,7 +84,15 @@ export default class ScriptjsLoader extends Component {

render () {
if (this.state.isLoaded) {
return this.props.googleMapElement;
const {protocol, hostname, port, pathname, query, loadingElement, ...restProps} = this.props;

if (this.shouldUseNewBehavior()) {
return (
<GoogleMapLoader {...restProps} />
);
} else {//------------ Deprecated ------------
return this.props.googleMapElement;
}
} else {
return this.props.loadingElement;
}
Expand Down

0 comments on commit 0f100d8

Please sign in to comment.