Skip to content

Commit c5de2a7

Browse files
author
Ryan O'Boril
committed
rerender circle on props change, update README
1 parent 84f5400 commit c5de2a7

File tree

3 files changed

+98
-48
lines changed

3 files changed

+98
-48
lines changed

Diff for: README.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ render() {
472472

473473
return (
474474
<Map
475-
center={coords}
475+
initialCenter={coords}
476476
google={this.props.google}
477477
style={{width: 500, height: 500, position: 'relative'}}
478478
zoom={14}
@@ -483,12 +483,11 @@ render() {
483483
onMouseover={() => console.log('mouseover')}
484484
onClick={() => console.log('click')}
485485
onMouseout={() => console.log('mouseout')}
486-
options={{
487-
strokeColor: 'transparent',
488-
strokeOpacity: 0,
489-
fillColor: '#FF0000',
490-
fillOpacity: 0.2,
491-
}}
486+
strokeColor='transparent'
487+
strokeOpacity={0}
488+
strokeWeight={5}
489+
fillColor='#FF0000'
490+
fillOpacity={0.2}
492491
/>
493492
</Map>
494493
);

Diff for: dist/components/Circle.js

+59-30
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,33 @@
121121
_inherits(Circle, _React$Component);
122122

123123
function Circle() {
124+
var _ref;
125+
126+
var _temp, _this, _ret;
127+
124128
_classCallCheck(this, Circle);
125129

126-
return _possibleConstructorReturn(this, (Circle.__proto__ || Object.getPrototypeOf(Circle)).apply(this, arguments));
130+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
131+
args[_key] = arguments[_key];
132+
}
133+
134+
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Circle.__proto__ || Object.getPrototypeOf(Circle)).call.apply(_ref, [this].concat(args))), _this), _this.centerChanged = function (newCenter) {
135+
var _this$props$center = _this.props.center,
136+
lat = _this$props$center.lat,
137+
lng = _this$props$center.lng;
138+
139+
return lat !== newCenter.lat || lng !== newCenter.lng;
140+
}, _this.propsChanged = function (newProps) {
141+
if (_this.centerChanged(newProps.center)) return true;
142+
143+
return Object.keys(Circle.propTypes).some(function (key) {
144+
return _this.props[key] !== newProps[key];
145+
});
146+
}, _this.destroyCircle = function () {
147+
if (_this.circle) {
148+
_this.circle.setMap(null);
149+
}
150+
}, _temp), _possibleConstructorReturn(_this, _ret);
127151
}
128152

129153
_createClass(Circle, [{
@@ -135,55 +159,58 @@
135159
}, {
136160
key: 'componentDidUpdate',
137161
value: function componentDidUpdate(prevProps) {
138-
if (this.props.map !== prevProps.map || !(0, _arePathsEqual.arePathsEqual)(this.props.path, prevProps.path)) {
139-
if (this.circle) {
140-
this.circle.setMap(null);
141-
}
162+
var _props = this.props,
163+
path = _props.path,
164+
map = _props.map;
165+
166+
167+
if (this.propsChanged(prevProps) || map !== prevProps.map || !(0, _arePathsEqual.arePathsEqual)(path, prevProps.path)) {
168+
this.destroyCircle();
142169
this.renderCircle();
143170
}
144171
}
145172
}, {
146173
key: 'componentWillUnmount',
147174
value: function componentWillUnmount() {
148-
if (this.circle) {
149-
this.circle.setMap(null);
150-
}
175+
this.destroyCircle();
151176
}
152177
}, {
153178
key: 'renderCircle',
154179
value: function renderCircle() {
155180
var _this2 = this;
156181

157-
var _props = this.props,
158-
map = _props.map,
159-
google = _props.google,
160-
center = _props.center,
161-
radius = _props.radius,
162-
strokeColor = _props.strokeColor,
163-
strokeOpacity = _props.strokeOpacity,
164-
strokeWeight = _props.strokeWeight,
165-
fillColor = _props.fillColor,
166-
fillOpacity = _props.fillOpacity,
167-
draggable = _props.draggable,
168-
visible = _props.visible,
169-
props = _objectWithoutProperties(_props, ['map', 'google', 'center', 'radius', 'strokeColor', 'strokeOpacity', 'strokeWeight', 'fillColor', 'fillOpacity', 'draggable', 'visible']);
182+
var _props2 = this.props,
183+
map = _props2.map,
184+
google = _props2.google,
185+
center = _props2.center,
186+
radius = _props2.radius,
187+
strokeColor = _props2.strokeColor,
188+
strokeOpacity = _props2.strokeOpacity,
189+
strokeWeight = _props2.strokeWeight,
190+
fillColor = _props2.fillColor,
191+
fillOpacity = _props2.fillOpacity,
192+
draggable = _props2.draggable,
193+
visible = _props2.visible,
194+
props = _objectWithoutProperties(_props2, ['map', 'google', 'center', 'radius', 'strokeColor', 'strokeOpacity', 'strokeWeight', 'fillColor', 'fillOpacity', 'draggable', 'visible']);
170195

171196
if (!google) {
172197
return null;
173198
}
174199

175-
var params = _extends({
200+
var params = _extends({}, props, {
176201
map: map,
177202
center: center,
178203
radius: radius,
179-
strokeColor: strokeColor,
180-
strokeOpacity: strokeOpacity,
181-
strokeWeight: strokeWeight,
182-
fillColor: fillColor,
183-
fillOpacity: fillOpacity,
184204
draggable: draggable,
185-
visible: visible
186-
}, props);
205+
visible: visible,
206+
options: {
207+
strokeColor: strokeColor,
208+
strokeOpacity: strokeOpacity,
209+
strokeWeight: strokeWeight,
210+
fillColor: fillColor,
211+
fillOpacity: fillOpacity
212+
}
213+
});
187214

188215
this.circle = new google.maps.Circle(params);
189216

@@ -227,7 +254,9 @@
227254
strokeOpacity: _propTypes2.default.number,
228255
strokeWeight: _propTypes2.default.number,
229256
fillColor: _propTypes2.default.string,
230-
fillOpacity: _propTypes2.default.number
257+
fillOpacity: _propTypes2.default.number,
258+
draggable: _propTypes2.default.bool,
259+
visible: _propTypes2.default.bool
231260
};
232261

233262
evtNames.forEach(function (e) {

Diff for: src/components/Circle.js

+33-11
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,36 @@ export class Circle extends React.Component {
2626
}
2727

2828
componentDidUpdate(prevProps) {
29+
const { path, map } = this.props;
30+
2931
if (
30-
this.props.map !== prevProps.map ||
31-
!arePathsEqual(this.props.path, prevProps.path)
32+
this.propsChanged(prevProps) ||
33+
map !== prevProps.map ||
34+
!arePathsEqual(path, prevProps.path)
3235
) {
33-
if (this.circle) {
34-
this.circle.setMap(null);
35-
}
36+
this.destroyCircle();
3637
this.renderCircle();
3738
}
3839
}
3940

41+
centerChanged = (newCenter) => {
42+
const { lat, lng } = this.props.center;
43+
return lat !== newCenter.lat || lng !== newCenter.lng;
44+
};
45+
46+
propsChanged = (newProps) => {
47+
if (this.centerChanged(newProps.center)) return true;
48+
49+
return Object.keys(Circle.propTypes).some(key => (
50+
this.props[key] !== newProps[key]
51+
));
52+
};
53+
4054
componentWillUnmount() {
55+
this.destroyCircle();
56+
}
57+
58+
destroyCircle = () => {
4159
if (this.circle) {
4260
this.circle.setMap(null);
4361
}
@@ -64,17 +82,19 @@ export class Circle extends React.Component {
6482
}
6583

6684
const params = {
85+
...props,
6786
map,
6887
center,
6988
radius,
70-
strokeColor,
71-
strokeOpacity,
72-
strokeWeight,
73-
fillColor,
74-
fillOpacity,
7589
draggable,
7690
visible,
77-
...props
91+
options: {
92+
strokeColor,
93+
strokeOpacity,
94+
strokeWeight,
95+
fillColor,
96+
fillOpacity,
97+
},
7898
};
7999

80100
this.circle = new google.maps.Circle(params);
@@ -112,6 +132,8 @@ Circle.propTypes = {
112132
strokeWeight: PropTypes.number,
113133
fillColor: PropTypes.string,
114134
fillOpacity: PropTypes.number,
135+
draggable: PropTypes.bool,
136+
visible: PropTypes.bool,
115137
}
116138

117139
evtNames.forEach(e => Circle.propTypes[e] = PropTypes.func)

0 commit comments

Comments
 (0)