Skip to content

Commit

Permalink
refactor(Polygon): rewrite with enhanceElement and cleaner interfaces
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Naming convention for event handlers has tweaked to follow React's convention.

Before:

```js
<Polygon
  onClick={_.noop}
  onRightclick={_.noop}
  onDragstart={_.noop}
/>
```

After:

```js
<Polygon
  onClick={_.noop}
  onRightClick={_.noop}
  onDragStart={_.noop}
/>
```
  • Loading branch information
tomchentw committed Oct 4, 2016
1 parent 5603319 commit 1e20d70
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 146 deletions.
165 changes: 111 additions & 54 deletions src/lib/Polygon.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,137 @@
/* global google */
import _ from "lodash";

import {
default as React,
Component,
PropTypes,
} from "react";

import {
default as canUseDOM,
} from "can-use-dom";
MAP,
POLYGON,
} from "./constants";

import {
default as PolygonCreator,
polygonDefaultPropTypes,
polygonControlledPropTypes,
polygonEventPropTypes,
} from "./creators/PolygonCreator";

import { default as GoogleMapHolder } from "./creators/GoogleMapHolder";

export default class Polygon extends Component {
static propTypes = {
// Uncontrolled default[props] - used only in componentDidMount
...polygonDefaultPropTypes,
// Controlled [props] - used in componentDidMount/componentDidUpdate
...polygonControlledPropTypes,
// Event [onEventName]
...polygonEventPropTypes,
}
static contextTypes = {
mapHolderRef: PropTypes.instanceOf(GoogleMapHolder),
}
addDefaultPrefixToPropTypes,
collectUncontrolledAndControlledProps,
default as enhanceElement,
} from "./enhanceElement";

const controlledPropTypes = {
// NOTICE!!!!!!
//
// Only expose those with getters & setters in the table as controlled props.
//
// [].map.call($0.querySelectorAll("tr>td>code", function(it){ return it.textContent; })
// .filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); })
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polygon
draggable: PropTypes.bool,
editable: PropTypes.bool,
options: PropTypes.object,
path: PropTypes.any,
paths: PropTypes.any,
visible: PropTypes.bool,
};

const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes);

const eventMap = {
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polygon
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
onClick: `click`,

onDblClick: `dblclick`,

onDrag: `drag`,

onDragEnd: `dragend`,

onDragStart: `dragstart`,

onMouseDown: `mousedown`,

onMouseMove: `mousemove`,

onMouseOut: `mouseout`,

onMouseOver: `mouseover`,

onMouseUp: `mouseup`,

onRightClick: `rightclick`,
};

const publicMethodMap = {
// Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polygon
//
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
// .filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); })
getDraggable() { return this.state.polygon.getDraggable(); }
// .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); })
getDraggable(polygon) { return polygon.getDraggable(); },

getEditable() { return this.state.polygon.getEditable(); }
getEditable(polygon) { return polygon.getEditable(); },

getPath() { return this.state.polygon.getPath(); }
getPath(polygon) { return polygon.getPath(); },

getPaths() { return this.state.polygon.getPaths(); }
getPaths(polygon) { return polygon.getPaths(); },

getVisible() { return this.state.polygon.getVisible(); }
getVisible(polygon) { return polygon.getVisible(); },
// END - Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polygon
};

state = {
}
const controlledPropUpdaterMap = {
draggable(polygon, draggable) { polygon.setDraggable(draggable); },
editable(polygon, editable) { polygon.setEditable(editable); },
options(polygon, options) { polygon.setOptions(options); },
path(polygon, path) { polygon.setPath(path); },
paths(polygon, paths) { polygon.setPaths(paths); },
visible(polygon, visible) { polygon.setVisible(visible); },
};

componentWillMount() {
const { mapHolderRef } = this.context;
function getInstanceFromComponent(component) {
return component.state[POLYGON];
}

if (!canUseDOM) {
return;
}
const polygon = PolygonCreator._createPolygon({
...this.props,
mapHolderRef,
export default _.flowRight(
React.createClass,
enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap),
)({
displayName: `Polygon`,

propTypes: {
...controlledPropTypes,
...defaultUncontrolledPropTypes,
},

contextTypes: {
[MAP]: PropTypes.object,
},

getInitialState() {
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polygon
const polygon = new google.maps.Polygon({
map: this.context[MAP],
...collectUncontrolledAndControlledProps(
defaultUncontrolledPropTypes,
controlledPropTypes,
this.props
),
});
return {
[POLYGON]: polygon,
};
},

this.setState({ polygon });
}
componentWillUnmount() {
const polygon = getInstanceFromComponent(this);
if (polygon) {
polygon.setMap(null);
}
},

render() {
if (this.state.polygon) {
return (
<PolygonCreator polygon={this.state.polygon} {...this.props}>
{this.props.children}
</PolygonCreator>
);
} else {
return (<noscript />);
}
}
}
return false;

This comment has been minimized.

Copy link
@kylebakerio

kylebakerio Dec 5, 2016

wait, what? Does this work?

},
});
2 changes: 2 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export const MARKER = `__SECRET_MARKER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
export const RECTANGLE = `__SECRET_RECTANGLE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const POLYLINE = `__SECRET_POLYLINE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;

export const POLYGON = `__SECRET_POLYGON_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`;
77 changes: 0 additions & 77 deletions src/lib/creators/PolygonCreator.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/lib/eventLists/PolygonEventList.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export {
export {
default as Polyline,
} from "./Polyline";

export {
default as Polygon,
} from "./Polygon";

0 comments on commit 1e20d70

Please sign in to comment.