diff --git a/package-lock.json b/package-lock.json index 889f443..92688ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@impargo/react-here-maps", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3928,12 +3928,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3948,17 +3950,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -4075,7 +4080,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -4087,6 +4093,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -4101,6 +4108,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4212,7 +4220,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -4224,6 +4233,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -4345,6 +4355,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/src/Marker.tsx b/src/Marker.tsx index 99f305f..cfee758 100644 --- a/src/Marker.tsx +++ b/src/Marker.tsx @@ -5,6 +5,7 @@ import * as PropTypes from "prop-types"; import * as React from "react"; import * as ReactDOMServer from "react-dom/server"; +import * as isEqual from "react-fast-compare"; import getDomMarkerIcon from "./utils/get-dom-marker-icon"; import getMarkerIcon from "./utils/get-marker-icon"; @@ -14,6 +15,7 @@ export interface MarkerProps extends H.map.Marker.Options, H.geo.IPoint { bitmap?: string; data?: any; draggable?: boolean; + children?: React.ReactElement; } // declare an interface containing the potential context parameters @@ -38,6 +40,17 @@ export class Marker extends React.Component { } // change the position automatically if the props get changed public componentWillReceiveProps(nextProps: MarkerProps) { + // Rerender the marker if child props change + const nextChildProps = nextProps.children && nextProps.children.props + const childProps = this.props.children && this.props.children.props + if (nextChildProps && !isEqual(nextChildProps, childProps)) { + const { markersGroup } = this.context; + if (this.marker) { + markersGroup.removeObject(this.marker); + } + this.marker = this.renderChildren(nextProps.children, nextProps.lat, nextProps.lng) + return + } if (nextProps.lat !== this.props.lat || nextProps.lng !== this.props.lng) { this.setPosition({ lat: nextProps.lat, @@ -47,7 +60,7 @@ export class Marker extends React.Component { } // remove the marker on unmount of the component public componentWillUnmount() { - const { map, markersGroup } = this.context; + const { markersGroup } = this.context; if (this.marker) { markersGroup.removeObject(this.marker); @@ -63,11 +76,31 @@ export class Marker extends React.Component { return null; } + private renderChildren(children: any, lat: number, lng: number) { + const { markersGroup } = this.context; + + // if children are provided, we render the provided react + // code to an html string + const html = ReactDOMServer.renderToStaticMarkup(( +
+ {children} +
+ )); + + // we then get a dom icon object from the wrapper method + const icon = getDomMarkerIcon(html); + + // then create a dom marker instance and attach it to the map, + // provided via context + const marker = new H.map.DomMarker({lat, lng}, {icon}); + marker.draggable = this.props.draggable; + marker.setData(this.props.data); + markersGroup.addObject(marker); + return marker + } + private addMarkerToMap() { - const { - map, - markersGroup, - } = this.context; + const { markersGroup } = this.context; const { children, @@ -78,24 +111,7 @@ export class Marker extends React.Component { let marker: H.map.DomMarker | H.map.Marker; if (React.Children.count(children) > 0) { - // if children are provided, we render the provided react - // code to an html string - const html = ReactDOMServer.renderToStaticMarkup(( -
- {children} -
- )); - - // we then get a dom icon object from the wrapper method - const icon = getDomMarkerIcon(html); - - // then create a dom marker instance and attach it to the map, - // provided via context - marker = new H.map.DomMarker({lat, lng}, {icon}); - marker.draggable = this.props.draggable; - marker.setData(this.props.data); - markersGroup.addObject(marker); - + marker = this.renderChildren(children, lat, lng) } else if (bitmap) { // if we have an image url and no react children, create a // regular icon instance