Skip to content

Commit f13a1e6

Browse files
authored
Merge branch 'master' into add-container-style-to-readme
2 parents 428ca73 + 229c109 commit f13a1e6

File tree

10 files changed

+267
-20
lines changed

10 files changed

+267
-20
lines changed

.github/workflows/npmpublish.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Node.js Package
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: 12
15+
- run: npm ci
16+
- run: npm test
17+
18+
publish-npm:
19+
needs: build
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v2
23+
- uses: actions/setup-node@v1
24+
with:
25+
node-version: 12
26+
registry-url: https://registry.npmjs.org/
27+
- run: npm ci
28+
- run: npm publish
29+
env:
30+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
31+
32+
publish-gpr:
33+
needs: build
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v2
37+
- uses: actions/setup-node@v1
38+
with:
39+
node-version: 12
40+
registry-url: https://npm.pkg.github.com/
41+
scope: '@your-github-username'
42+
- run: npm ci
43+
- run: npm publish
44+
env:
45+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const style = {
103103
height: '100%'
104104
}
105105
```
106+
106107
Container Style: Takes CSS style object - optional, commonly when you want to change from the default of position "absolute".
107108

108109
```javascript
@@ -116,9 +117,9 @@ const containerStyle = {
116117
```javascript
117118
<Map
118119
containerStyle={containerStyle}
119-
```
120+
```
120121

121-
initalCenter: Takes an object containing latitude and longitude coordinates. Sets the maps center upon loading.
122+
initialCenter: Takes an object containing latitude and longitude coordinates. Sets the maps center upon loading.
122123

123124
```javascript
124125
<Map

dist/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@
356356
});
357357
});
358358
}
359+
}, {
360+
key: 'ref',
361+
value: {}
359362
}, {
360363
key: 'render',
361364
value: function render() {
@@ -370,7 +373,7 @@
370373
{ style: containerStyles, className: this.props.className },
371374
_react2.default.createElement(
372375
'div',
373-
{ style: style, ref: 'map' },
376+
{ style: style, ref: (element) => (this.ref.map = element) },
374377
'Loading map...'
375378
),
376379
this.renderChildren()

examples/components/withRectangle.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react';
2+
3+
import Map from '../../src/index';
4+
5+
import Rectangle from '../../src/components/Rectangle';
6+
7+
8+
const WithRectangles = props => {
9+
if (!props.loaded) return <div>Loading...</div>;
10+
11+
const bounds = {
12+
north: 37.789411,
13+
south: 37.731757,
14+
east: -122.410333,
15+
west: -122.489116,
16+
};
17+
18+
return (
19+
<Map
20+
google={props.google}
21+
className="map"
22+
style={{ height: '100%', position: 'relative', width: '100%' }}
23+
zoom={11}
24+
>
25+
<Rectangle
26+
fillColor="#0000FF"
27+
fillOpacity={0.35}
28+
bounds={bounds}
29+
strokeColor="#0000FF"
30+
strokeOpacity={0.8}
31+
strokeWeight={2}
32+
/>
33+
</Map>
34+
);
35+
};
36+
37+
export default WithRectangles;

examples/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Autocomplete from './components/autocomplete';
1919
import HeatMap from './components/withHeatMap';
2020
import Polygon from './components/withPolygons';
2121
import Polyline from './components/withPolylines';
22+
import Rectangle from './components/withRectangle';
2223
import CustomEvents from './components/resizeEvent';
2324

2425
const routes = [
@@ -62,6 +63,11 @@ const routes = [
6263
name: 'Polyline',
6364
component: Polyline
6465
},
66+
{
67+
path: '/rectangle',
68+
name: 'Rectangle',
69+
component: Rectangle
70+
},
6571
{
6672
path: '/onResizeEvent',
6773
name: 'Custom events',

src/GoogleApiComponent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export const wrapper = input => WrappedComponent => {
5151
google: null,
5252
options: options
5353
};
54+
55+
this.mapRef=React.createRef();
5456
}
5557

5658
componentWillReceiveProps(props) {
@@ -120,7 +122,7 @@ export const wrapper = input => WrappedComponent => {
120122
return (
121123
<div>
122124
<WrappedComponent {...props} />
123-
<div ref="map" />
125+
<div ref={this.mapRef} />
124126
</div>
125127
);
126128
}

src/components/Marker.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,7 @@ export class Marker extends React.Component {
104104
}
105105

106106
render() {
107-
return (
108-
<Fragment>
109-
{this.props.children && this.marker ?
110-
React.Children.only(
111-
React.cloneElement(
112-
this.props.children,
113-
{ marker: this.marker,
114-
google: this.props.google,
115-
map: this.props.map}
116-
)
117-
) : null
118-
}
119-
</Fragment>
120-
)
107+
return null;
121108
}
122109
}
123110

src/components/Rectangle.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import { areBoundsEqual } from '../lib/areBoundsEqual';
5+
import { camelize } from '../lib/String';
6+
const evtNames = ['click', 'mouseout', 'mouseover'];
7+
8+
const wrappedPromise = function() {
9+
var wrappedPromise = {},
10+
promise = new Promise(function (resolve, reject) {
11+
wrappedPromise.resolve = resolve;
12+
wrappedPromise.reject = reject;
13+
});
14+
wrappedPromise.then = promise.then.bind(promise);
15+
wrappedPromise.catch = promise.catch.bind(promise);
16+
wrappedPromise.promise = promise;
17+
18+
return wrappedPromise;
19+
}
20+
21+
export class Rectangle extends React.Component {
22+
componentDidMount() {
23+
this.rectanglePromise = wrappedPromise();
24+
this.renderRectangle();
25+
}
26+
27+
componentDidUpdate(prevProps) {
28+
if (
29+
this.props.map !== prevProps.map ||
30+
!areBoundsEqual(this.props.bounds, prevProps.bounds)
31+
) {
32+
if (this.rectangle) {
33+
this.rectangle.setMap(null);
34+
}
35+
this.renderRectangle();
36+
}
37+
}
38+
39+
componentWillUnmount() {
40+
if (this.rectangle) {
41+
this.rectangle.setMap(null);
42+
}
43+
}
44+
45+
renderRectangle() {
46+
const {
47+
map,
48+
google,
49+
bounds,
50+
strokeColor,
51+
strokeOpacity,
52+
strokeWeight,
53+
fillColor,
54+
fillOpacity,
55+
...props
56+
} = this.props;
57+
58+
if (!google) {
59+
return null;
60+
}
61+
62+
const params = {
63+
map,
64+
bounds,
65+
strokeColor,
66+
strokeOpacity,
67+
strokeWeight,
68+
fillColor,
69+
fillOpacity,
70+
...props
71+
};
72+
73+
this.rectangle = new google.maps.Rectangle(params);
74+
75+
evtNames.forEach(e => {
76+
this.rectangle.addListener(e, this.handleEvent(e));
77+
});
78+
79+
this.rectanglePromise.resolve(this.rectangle);
80+
}
81+
82+
getRectangle() {
83+
return this.rectanglePromise;
84+
}
85+
86+
handleEvent(evt) {
87+
return (e) => {
88+
const evtName = `on${camelize(evt)}`
89+
if (this.props[evtName]) {
90+
this.props[evtName](this.props, this.rectangle, e);
91+
}
92+
}
93+
}
94+
95+
render() {
96+
console.log('hii, ', this.props.bounds);
97+
return null;
98+
}
99+
}
100+
101+
Rectangle.propTypes = {
102+
bounds: PropTypes.object,
103+
strokeColor: PropTypes.string,
104+
strokeOpacity: PropTypes.number,
105+
strokeWeight: PropTypes.number,
106+
fillColor: PropTypes.string,
107+
fillOpacity: PropTypes.number
108+
}
109+
110+
evtNames.forEach(e => Rectangle.propTypes[e] = PropTypes.func)
111+
112+
Rectangle.defaultProps = {
113+
name: 'Rectangle'
114+
}
115+
116+
export default Rectangle

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {HeatMap} from './components/HeatMap';
4949
export {Polygon} from './components/Polygon';
5050
export {Polyline} from './components/Polyline';
5151
export {Circle} from './components/Circle';
52+
export {Rectangle} from './components/Rectangle';
5253

5354
export class Map extends React.Component {
5455
constructor(props) {
@@ -65,6 +66,8 @@ export class Map extends React.Component {
6566
lng: this.props.initialCenter.lng
6667
}
6768
};
69+
70+
this.mapRef=React.createRef();
6871
}
6972

7073
componentDidMount() {
@@ -130,7 +133,7 @@ export class Map extends React.Component {
130133
const {google} = this.props;
131134
const maps = google.maps;
132135

133-
const mapRef = this.refs.map;
136+
const mapRef = this.mapRef.current;
134137
const node = ReactDOM.findDOMNode(mapRef);
135138
const curr = this.state.currentLocation;
136139
const center = new maps.LatLng(curr.lat, curr.lng);
@@ -257,7 +260,7 @@ export class Map extends React.Component {
257260

258261
return (
259262
<div style={containerStyles} className={this.props.className}>
260-
<div style={style} ref="map">
263+
<div style={style} ref={this.mapRef}>
261264
Loading map...
262265
</div>
263266
{this.renderChildren()}

src/lib/areBoundsEqual.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Compares two bound objects.
3+
*/
4+
5+
export const areBoundsEqual = function(boundA, boundB) {
6+
if (boundA === boundB) {
7+
return true;
8+
}
9+
if (
10+
!(boundA instanceof Object) ||
11+
!(boundB instanceof Object)
12+
) {
13+
return false;
14+
}
15+
if (Object.keys(boundA).length !== Object.keys(boundB).length) {
16+
return false;
17+
}
18+
if (
19+
!areValidBounds(boundA) ||
20+
!areValidBounds(boundB)
21+
) {
22+
return false;
23+
}
24+
for (const key of Object.keys(boundA)) {
25+
if (boundA[key] !== boundB[key]) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
};
31+
32+
/**
33+
* Helper that checks whether an array consists of objects
34+
* with lat and lng properties
35+
* @param {object} elem the element to check
36+
* @returns {boolean} whether or not it's valid
37+
*/
38+
const areValidBounds = function(elem) {
39+
return (
40+
elem !== null &&
41+
typeof elem === 'object' &&
42+
elem.hasOwnProperty('north') &&
43+
elem.hasOwnProperty('south') &&
44+
elem.hasOwnProperty('east') &&
45+
elem.hasOwnProperty('west')
46+
);
47+
};

0 commit comments

Comments
 (0)