Skip to content

Commit ea39062

Browse files
committed
refactor(Rectangle): rewrite with enhanceElement and cleaner interfaces
BREAKING CHANGE: Naming convention for event handlers has tweaked to follow React's convention. Before: ```js <Rectangle onClick={_.noop} onRightclick={_.noop} onDragstart={_.noop} /> ``` After: ```js <Rectangle onClick={_.noop} onRightClick={_.noop} onDragStart={_.noop} /> ```
1 parent c06aff2 commit ea39062

File tree

4 files changed

+112
-149
lines changed

4 files changed

+112
-149
lines changed

src/lib/Rectangle.js

Lines changed: 110 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,135 @@
1+
/* global google */
2+
import _ from "lodash";
3+
14
import {
25
default as React,
3-
Component,
46
PropTypes,
57
} from "react";
68

79
import {
8-
default as canUseDOM,
9-
} from "can-use-dom";
10+
MAP,
11+
RECTANGLE,
12+
} from "./constants";
1013

1114
import {
12-
default as RectangleCreator,
13-
rectangleDefaultPropTypes,
14-
rectangleControlledPropTypes,
15-
rectangleEventPropTypes,
16-
} from "./creators/RectangleCreator";
17-
18-
import { default as GoogleMapHolder } from "./creators/GoogleMapHolder";
19-
20-
/*
21-
* Original author: @alistairjcbrown
22-
* Original PR: https://github.com/tomchentw/react-google-maps/pull/80
23-
*/
24-
export default class Rectangle extends Component {
25-
static propTypes = {
26-
// Uncontrolled default[props] - used only in componentDidMount
27-
...rectangleDefaultPropTypes,
28-
// Controlled [props] - used in componentDidMount/componentDidUpdate
29-
...rectangleControlledPropTypes,
30-
// Event [onEventName]
31-
...rectangleEventPropTypes,
32-
}
33-
34-
static contextTypes = {
35-
mapHolderRef: PropTypes.instanceOf(GoogleMapHolder),
36-
}
15+
addDefaultPrefixToPropTypes,
16+
collectUncontrolledAndControlledProps,
17+
default as enhanceElement,
18+
} from "./enhanceElement";
19+
20+
const controlledPropTypes = {
21+
// NOTICE!!!!!!
22+
//
23+
// Only expose those with getters & setters in the table as controlled props.
24+
//
25+
// [].map.call($0.querySelectorAll("tr>td>code", function(it){ return it.textContent; })
26+
// .filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); })
27+
//
28+
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle
29+
bounds: PropTypes.any,
30+
draggable: PropTypes.bool,
31+
editable: PropTypes.bool,
32+
options: PropTypes.object,
33+
visible: PropTypes.bool,
34+
};
35+
36+
const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes);
37+
38+
const eventMap = {
39+
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle
40+
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
41+
onBoundsChanged: `bounds_changed`,
42+
43+
onClick: `click`,
44+
45+
onDblClick: `dblclick`,
46+
47+
onDrag: `drag`,
48+
49+
onDragEnd: `dragend`,
50+
51+
onDragStart: `dragstart`,
52+
53+
onMouseDown: `mousedown`,
3754

55+
onMouseMove: `mousemove`,
56+
57+
onMouseOut: `mouseout`,
58+
59+
onMouseOver: `mouseover`,
60+
61+
onMouseUp: `mouseup`,
62+
63+
onRightClick: `rightclick`,
64+
};
65+
66+
const publicMethodMap = {
3867
// Public APIs
3968
//
4069
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle
4170
//
4271
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
43-
// .filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); })
44-
getBounds() { return this.state.rectangle.getBounds(); }
72+
// .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); })
73+
getBounds(rectangle) { return rectangle.getBounds(); },
4574

46-
getDraggable() { return this.state.rectangle.getDraggable(); }
75+
getDraggable(rectangle) { return rectangle.getDraggable(); },
4776

48-
getEditable() { return this.state.rectangle.getEditable(); }
77+
getEditable(rectangle) { return rectangle.getEditable(); },
4978

50-
getVisible() { return this.state.rectangle.getVisible(); }
79+
getVisible(rectangle) { return rectangle.getVisible(); },
5180
// END - Public APIs
52-
//
53-
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle
81+
};
5482

55-
state = {
56-
}
83+
const controlledPropUpdaterMap = {
84+
bounds(rectangle, bounds) { rectangle.setBounds(bounds); },
85+
draggable(rectangle, draggable) { rectangle.setDraggable(draggable); },
86+
editable(rectangle, editable) { rectangle.setEditable(editable); },
87+
options(rectangle, options) { rectangle.setOptions(options); },
88+
visible(rectangle, visible) { rectangle.setVisible(visible); },
89+
};
5790

58-
componentWillMount() {
59-
const { mapHolderRef } = this.context;
91+
function getInstanceFromComponent(component) {
92+
return component.state[RECTANGLE];
93+
}
6094

61-
if (!canUseDOM) {
62-
return;
63-
}
64-
const rectangle = RectangleCreator._createRectangle({
65-
...this.props,
66-
mapHolderRef,
95+
export default _.flowRight(
96+
React.createClass,
97+
enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap),
98+
)({
99+
displayName: `Rectangle`,
100+
101+
propTypes: {
102+
...controlledPropTypes,
103+
...defaultUncontrolledPropTypes,
104+
},
105+
106+
contextTypes: {
107+
[MAP]: PropTypes.object,
108+
},
109+
110+
getInitialState() {
111+
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Rectangle
112+
const rectangle = new google.maps.Rectangle({
113+
map: this.context[MAP],
114+
...collectUncontrolledAndControlledProps(
115+
defaultUncontrolledPropTypes,
116+
controlledPropTypes,
117+
this.props
118+
),
67119
});
120+
return {
121+
[RECTANGLE]: rectangle,
122+
};
123+
},
68124

69-
this.setState({ rectangle });
70-
}
125+
componentWillUnmount() {
126+
const rectangle = getInstanceFromComponent(this);
127+
if (rectangle) {
128+
rectangle.setMap(null);
129+
}
130+
},
71131

72132
render() {
73-
if (this.state.rectangle) {
74-
return (
75-
<RectangleCreator rectangle={this.state.rectangle} {...this.props}>
76-
{this.props.children}
77-
</RectangleCreator>
78-
);
79-
} else {
80-
return (<noscript />);
81-
}
82-
}
83-
}
133+
return false;
134+
},
135+
});

src/lib/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export const MAP = `__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
33
// export const SKELETON = `__SECRET_SKELETON_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
44

55
export const MARKER = `__SECRET_MARKER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
6+
7+
export const RECTANGLE = `__SECRET_RECTANGLE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

src/lib/creators/RectangleCreator.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/lib/eventLists/RectangleEventList.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)