Skip to content
This repository has been archived by the owner on May 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request Josh-ES#19 from jlabeit/feature/rerender-marker-on…
Browse files Browse the repository at this point in the history
…-child-props-change

Rerender marker on child props change
  • Loading branch information
antoineraoulimpargo authored Mar 17, 2019
2 parents 9aac50a + 24cba30 commit 39bf59b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 39 additions & 23 deletions src/Marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -14,6 +15,7 @@ export interface MarkerProps extends H.map.Marker.Options, H.geo.IPoint {
bitmap?: string;
data?: any;
draggable?: boolean;
children?: React.ReactElement<any>;
}

// declare an interface containing the potential context parameters
Expand All @@ -38,6 +40,17 @@ export class Marker extends React.Component<MarkerProps, object> {
}
// 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,
Expand All @@ -47,7 +60,7 @@ export class Marker extends React.Component<MarkerProps, object> {
}
// 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);
Expand All @@ -63,11 +76,31 @@ export class Marker extends React.Component<MarkerProps, object> {
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((
<div className="dom-marker">
{children}
</div>
));

// 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,
Expand All @@ -78,24 +111,7 @@ export class Marker extends React.Component<MarkerProps, object> {

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((
<div className="dom-marker">
{children}
</div>
));

// 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
Expand Down

0 comments on commit 39bf59b

Please sign in to comment.