From b92a570be58c6505fd4ccd4356c64020cdc888b6 Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Wed, 13 Sep 2017 22:21:40 +0800 Subject: [PATCH] feat(visualization/HeatmapLayer): revamp with jscodeshift --- src/lib/visualization/HeatmapLayer.js | 99 ---------------------- src/lib/visualization/heatmapLayer.spec.js | 9 -- src/macros/visualization/HeatmapLayer.jsx | 76 +++++++++++++++++ src/visualization/HeatmapLayer.js | 8 ++ 4 files changed, 84 insertions(+), 108 deletions(-) delete mode 100644 src/lib/visualization/HeatmapLayer.js delete mode 100644 src/lib/visualization/heatmapLayer.spec.js create mode 100644 src/macros/visualization/HeatmapLayer.jsx create mode 100644 src/visualization/HeatmapLayer.js diff --git a/src/lib/visualization/HeatmapLayer.js b/src/lib/visualization/HeatmapLayer.js deleted file mode 100644 index aa1b6182..00000000 --- a/src/lib/visualization/HeatmapLayer.js +++ /dev/null @@ -1,99 +0,0 @@ -/* global google */ -import _ from "lodash"; - -import PropTypes from "prop-types"; - -import createReactClass from "create-react-class"; - -import { - MAP, - HEATMAP_LAYER, -} from "../constants"; - -import { - 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#HeatmapLayer - data: PropTypes.any, - options: PropTypes.object, -}; - -const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes); - -const eventMap = { - // https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer - // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) - onZoomChanged: `zoom_changed`, -}; - -const publicMethodMap = { - // Public APIs - // - // https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer - // - // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) - // .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); }) - // END - Public APIs -}; - -const controlledPropUpdaterMap = { - data(heatmapLayer, data) { heatmapLayer.setData(data); }, - options(heatmapLayer, options) { heatmapLayer.setOptions(options); }, -}; - -function getInstanceFromComponent(component) { - return component.state[HEATMAP_LAYER]; -} - -export default _.flowRight( - createReactClass, - enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap), -)({ - displayName: `HeatmapLayer`, - - propTypes: { - ...controlledPropTypes, - ...defaultUncontrolledPropTypes, - }, - - contextTypes: { - [MAP]: PropTypes.object, - }, - - getInitialState() { - // https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer - const heatmapLayer = new google.maps.visualization.HeatmapLayer({ - map: this.context[MAP], - ...collectUncontrolledAndControlledProps( - defaultUncontrolledPropTypes, - controlledPropTypes, - this.props - ), - }); - return { - [HEATMAP_LAYER]: heatmapLayer, - }; - }, - - componentWillUnmount() { - const heatmapLayer = getInstanceFromComponent(this); - if (heatmapLayer) { - heatmapLayer.setMap(null); - } - }, - - render() { - return false; - }, -}); diff --git a/src/lib/visualization/heatmapLayer.spec.js b/src/lib/visualization/heatmapLayer.spec.js deleted file mode 100644 index 0f7d7a97..00000000 --- a/src/lib/visualization/heatmapLayer.spec.js +++ /dev/null @@ -1,9 +0,0 @@ -import HeatmapLayer from "./HeatmapLayer"; - -describe(`HeatmapLayer`, () => { - - it(`should be exported`, () => { - expect(HeatmapLayer).toBeDefined(); - }); - -}); diff --git a/src/macros/visualization/HeatmapLayer.jsx b/src/macros/visualization/HeatmapLayer.jsx new file mode 100644 index 00000000..46f3bf82 --- /dev/null +++ b/src/macros/visualization/HeatmapLayer.jsx @@ -0,0 +1,76 @@ +/* global google */ +import React from "react" +import PropTypes from "prop-types" + +import { + construct, + componentDidMount, + componentDidUpdate, + componentWillUnmount, +} from "../../utils/MapChildHelper" + +import { MAP, HEATMAP_LAYER } from "../../constants" + +export const __jscodeshiftPlaceholder__ = `{ + "eventMapOverrides": { + }, + "getInstanceFromComponent": "this.state[HEATMAP_LAYER]" +}` + +/** + * @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer + */ +export class HeatmapLayer extends React.PureComponent { + static propTypes = { + __jscodeshiftPlaceholder__: null, + } + + static contextTypes = { + [MAP]: PropTypes.object, + } + + /* + * @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer + */ + constructor(props, context) { + super(props, context) + const heatmapLayer = new google.maps.visualization.HeatmapLayer() + construct(HeatmapLayer.propTypes, updaterMap, this.props, heatmapLayer) + heatmapLayer.setMap(this.context[MAP]) + this.state = { + [HEATMAP_LAYER]: heatmapLayer, + } + } + + componentDidMount() { + componentDidMount(this, this.state[HEATMAP_LAYER], eventMap) + } + + componentDidUpdate(prevProps) { + componentDidUpdate( + this, + this.state[HEATMAP_LAYER], + eventMap, + updaterMap, + prevProps + ) + } + + componentWillUnmount() { + componentWillUnmount(this) + const heatmapLayer = this.state[HEATMAP_LAYER] + if (heatmapLayer) { + heatmapLayer.setMap(null) + } + } + + render() { + return false + } +} + +export default HeatmapLayer + +const eventMap = {} + +const updaterMap = {} diff --git a/src/visualization/HeatmapLayer.js b/src/visualization/HeatmapLayer.js new file mode 100644 index 00000000..e231e9f9 --- /dev/null +++ b/src/visualization/HeatmapLayer.js @@ -0,0 +1,8 @@ +import warning from "warning" + +warning( + false, + `[DEPRECATED] "react-google-maps/lib/visualization/HeatmapLayer" has been moved to "react-google-maps/lib/components/visualization/HeatmapLayer".` +) + +export { default } from "../components/visualization/HeatmapLayer"