Skip to content

Commit b1e3f5e

Browse files
author
Ari
committed
Added Loading container
1 parent deb1dea commit b1e3f5e

File tree

4 files changed

+140
-111
lines changed

4 files changed

+140
-111
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ export default GoogleApiWrapper({
3232
})(MapContainer)
3333
```
3434

35+
If you want to add a loading container _other than the default_ loading container, simply pass it in the HOC, like so:
36+
37+
```javascript
38+
const LoadingContainer = (props) => (
39+
<div>Fancy loading container!</div>
40+
)
41+
export default GoogleApiWrapper({
42+
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE),
43+
LoadingContainer: LoadingContainer
44+
})(MapContainer)
45+
```
46+
3547
## Sample Usage With Lazy-loading Google API:
3648

3749
```javascript
@@ -90,7 +102,7 @@ initalCenter: Takes an object containing latitude and longitude coordinates. Set
90102
onClick={this.onMapClicked}
91103
>
92104
```
93-
center: Takes an object containing latitude and longitude coordinates. Use this if you want to re-render the map after the initial render.
105+
center: Takes an object containing latitude and longitude coordinates. Use this if you want to re-render the map after the initial render.
94106

95107
```javascript
96108
<Map
@@ -104,7 +116,7 @@ center: Takes an object containing latitude and longitude coordinates. Use this
104116
onClick={this.onMapClicked}
105117
>
106118
```
107-
It also takes event handlers described below:
119+
It also takes event handlers described below:
108120

109121
### Events
110122

examples/Container.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import React from 'react'
2-
import PropTypes from 'prop-types'
3-
import ReactDOM from 'react-dom'
4-
import {Link} from 'react-router'
5-
import GitHubForkRibbon from 'react-github-fork-ribbon'
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import ReactDOM from 'react-dom';
4+
import {Link} from 'react-router';
5+
import GitHubForkRibbon from 'react-github-fork-ribbon';
66

77
let GoogleApiWrapper;
88
if (__IS_DEV__) {
9-
GoogleApiWrapper = require('../src/index').GoogleApiWrapper
9+
GoogleApiWrapper = require('../src/index').GoogleApiWrapper;
1010
} else {
11-
GoogleApiWrapper = require('../dist').GoogleApiWrapper
11+
GoogleApiWrapper = require('../dist').GoogleApiWrapper;
1212
}
1313

14-
import styles from './styles.module.css'
14+
import styles from './styles.module.css';
1515

1616
export const Container = React.createClass({
17-
1817
propTypes: {
1918
children: PropTypes.element.isRequired
2019
},
@@ -30,12 +29,10 @@ export const Container = React.createClass({
3029
const sharedProps = {
3130
google: this.props.google,
3231
loaded: this.props.loaded
33-
}
32+
};
3433
return React.Children.map(children, c => {
35-
return React.cloneElement(c, sharedProps, {
36-
37-
});
38-
})
34+
return React.cloneElement(c, sharedProps, {});
35+
});
3936
},
4037

4138
render: function() {
@@ -45,39 +42,45 @@ export const Container = React.createClass({
4542
const c = this.renderChildren();
4643
return (
4744
<div className={styles.container}>
48-
<GitHubForkRibbon href="//github.com/fullstackreact/google-maps-react"
49-
target="_blank"
50-
position="right">
45+
<GitHubForkRibbon
46+
href="//github.com/fullstackreact/google-maps-react"
47+
target="_blank"
48+
position="right"
49+
>
5150
Fork me on GitHub
5251
</GitHubForkRibbon>
5352
<div className={styles.wrapper}>
5453
<div className={styles.list}>
5554
<ul>
5655
{Object.keys(routeMap).map(key => {
5756
return (
58-
<Link to={key}
59-
activeClassName={styles.active}
60-
key={key}>
57+
<Link to={key} activeClassName={styles.active} key={key}>
6158
<li>{routeMap[key].name}</li>
6259
</Link>
63-
)
60+
);
6461
})}
6562
</ul>
6663
</div>
6764
<div className={styles.content}>
6865
<div className={styles.header}>
6966
<h1>{routeDef && routeDef.name} Example</h1>
70-
<h2><a href="https://github.com/fullstackreact/google-maps-react/blob/master/README.md">Readme</a></h2>
67+
<h2>
68+
<a href="https://github.com/fullstackreact/google-maps-react/blob/master/README.md">
69+
Readme
70+
</a>
71+
</h2>
7172
</div>
7273
{c}
7374
</div>
7475
</div>
7576
</div>
76-
)
77+
);
7778
}
78-
})
79+
});
7980

81+
const Loading = props => <div>Fancy loading container</div>;
8082
export default GoogleApiWrapper({
8183
apiKey: __GAPI_KEY__,
82-
libraries: ['places','visualization']
83-
})(Container)
84+
libraries: ['places', 'visualization'],
85+
LoadingContainer: Loading
86+
})(Container);

examples/index.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,87 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
3-
import {Router, hashHistory, Redirect, Route, IndexRoute, Link} from 'react-router'
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import {
4+
Router,
5+
hashHistory,
6+
Redirect,
7+
Route,
8+
IndexRoute,
9+
Link
10+
} from 'react-router';
411

512
import styles from './global.styles.css';
613

7-
import Container from './Container'
14+
import Container from './Container';
815

916
const routeMap = {
10-
'basic': {
17+
basic: {
1118
name: 'Simple',
1219
component: require('./components/basic').default
1320
},
14-
'markers': {
21+
markers: {
1522
name: 'Marker',
1623
component: require('./components/withMarkers').default
1724
},
18-
'clickable_markers': {
25+
clickable_markers: {
1926
name: 'Clickable markers',
2027
component: require('./components/clickableMarkers').default
2128
},
22-
'places': {
29+
places: {
2330
name: 'Google places',
2431
component: require('./components/places').default
2532
},
26-
'autocomplete': {
33+
autocomplete: {
2734
name: 'Autocomplete',
2835
component: require('./components/autocomplete').default
2936
},
30-
'heatMap': {
37+
heatMap: {
3138
name: 'Heat Map',
3239
component: require('./components/withHeatMap').default
3340
},
34-
'polygons': {
41+
polygons: {
3542
name: 'Polygon',
3643
component: require('./components/withPolygons').default
3744
},
38-
'polyline': {
45+
polyline: {
3946
name: 'Polyline',
4047
component: require('./components/withPolylines').default
4148
}
42-
}
49+
};
4350

4451
const createElement = (Component, props) => {
45-
const pathname = props.location.pathname.replace('/', '')
52+
const pathname = props.location.pathname.replace('/', '');
4653
const routeDef = routeMap[pathname];
4754
const newProps = {
48-
routeMap, pathname, routeDef
49-
}
50-
return <Component {...newProps} {...props} />
51-
}
55+
routeMap,
56+
pathname,
57+
routeDef
58+
};
59+
return <Component {...newProps} {...props} />;
60+
};
5261

5362
const routes = (
54-
<Router createElement={createElement}
55-
history={hashHistory}>
56-
<Route component={Container}
57-
path='/'>
63+
<Router createElement={createElement} history={hashHistory}>
64+
<Route component={Container} path="/">
5865
{Object.keys(routeMap).map(key => {
59-
const r = routeMap[key]
60-
return (<Route
61-
key={key}
62-
path={key}
63-
name={r.name}
64-
component={r.component} />)
66+
const r = routeMap[key];
67+
return (
68+
<Route key={key} path={key} name={r.name} component={r.component} />
69+
);
6570
})}
6671
<IndexRoute component={routeMap['basic'].component} />
6772
</Route>
6873
</Router>
69-
)
74+
);
7075

71-
const mountNode = document.querySelector('#root')
76+
const mountNode = document.querySelector('#root');
7277
if (mountNode) {
7378
ReactDOM.render(routes, mountNode);
7479
} else {
7580
const hljs = require('highlight.js');
7681

7782
const codes = document.querySelectorAll('pre code');
7883
for (let i = 0; i < codes.length; i++) {
79-
const block = codes[i]
84+
const block = codes[i];
8085
hljs.highlightBlock(block);
8186
}
8287
}

src/GoogleApiComponent.js

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,77 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
33

4-
import {ScriptCache} from './lib/ScriptCache'
5-
import GoogleApi from './lib/GoogleApi'
4+
import {ScriptCache} from './lib/ScriptCache';
5+
import GoogleApi from './lib/GoogleApi';
66

7-
const defaultMapConfig = {}
8-
const defaultCreateCache = (options) => {
9-
options = options || {};
10-
const apiKey = options.apiKey;
11-
const libraries = options.libraries || ['places'];
12-
const version = options.version || '3.29';
13-
const language = options.language || 'en';
14-
const url = options.url;
7+
const defaultMapConfig = {};
8+
const defaultCreateCache = options => {
9+
options = options || {};
10+
const apiKey = options.apiKey;
11+
const libraries = options.libraries || ['places'];
12+
const version = options.version || '3';
13+
const language = options.language || 'en';
14+
const url = options.url;
1515

16-
return ScriptCache({
17-
google: GoogleApi({
18-
apiKey: apiKey,
19-
language: language,
20-
libraries: libraries,
21-
version: version,
22-
url: url
23-
})
24-
});
16+
return ScriptCache({
17+
google: GoogleApi({
18+
apiKey: apiKey,
19+
language: language,
20+
libraries: libraries,
21+
version: version,
22+
url: url
23+
})
24+
});
2525
};
2626

27-
export const wrapper = (options) => (WrappedComponent) => {
28-
const createCache = options.createCache || defaultCreateCache;
27+
const DefaultLoadingContainer = props => <div>Loading...</div>;
2928

30-
class Wrapper extends React.Component {
31-
constructor(props, context) {
32-
super(props, context);
29+
export const wrapper = options => WrappedComponent => {
30+
const createCache = options.createCache || defaultCreateCache;
3331

34-
this.scriptCache = createCache(options);
35-
this.scriptCache.google.onLoad(this.onLoad.bind(this))
32+
class Wrapper extends React.Component {
33+
constructor(props, context) {
34+
super(props, context);
3635

37-
this.state = {
38-
loaded: false,
39-
map: null,
40-
google: null
41-
}
42-
}
36+
this.scriptCache = createCache(options);
37+
this.scriptCache.google.onLoad(this.onLoad.bind(this));
38+
this.LoadingContainer =
39+
options.LoadingContainer || DefaultLoadingContainer;
4340

44-
onLoad(err, tag) {
45-
this._gapi = window.google;
41+
this.state = {
42+
loaded: false,
43+
map: null,
44+
google: null
45+
};
46+
}
47+
48+
onLoad(err, tag) {
49+
this._gapi = window.google;
4650

47-
this.setState({loaded: true, google: this._gapi})
48-
}
51+
this.setState({loaded: true, google: this._gapi});
52+
}
4953

50-
render() {
51-
const props = Object.assign({}, this.props, {
52-
loaded: this.state.loaded,
53-
google: window.google
54-
});
54+
render() {
55+
const {LoadingContainer} = this;
56+
if (!this.state.loaded) {
57+
return <LoadingContainer />;
58+
}
5559

56-
return (
57-
<div>
58-
<WrappedComponent {...props}/>
59-
<div ref='map'/>
60-
</div>
61-
)
62-
}
60+
const props = Object.assign({}, this.props, {
61+
loaded: this.state.loaded,
62+
google: window.google
63+
});
64+
65+
return (
66+
<div>
67+
<WrappedComponent {...props} />
68+
<div ref="map" />
69+
</div>
70+
);
6371
}
72+
}
6473

65-
return Wrapper;
66-
}
74+
return Wrapper;
75+
};
6776

6877
export default wrapper;

0 commit comments

Comments
 (0)