Skip to content

Commit d7bb99c

Browse files
authored
Adding removing layers dynamically (#42)
* Supporting adding and remoing google layers dynamically * Fixed naming
1 parent fbd0584 commit d7bb99c

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

src/index.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,38 @@ const createLeafletElement = (props: IProps, context: LeafletContextInterface) =
3636
return { instance, context };
3737
}
3838

39+
const updateLeafletElement = (
40+
instance: L.GridLayer,
41+
props: IProps,
42+
prevProps: IProps
43+
) => {
44+
let prevGoogleMapsAddLayers = prevProps.googleMapsAddLayers
45+
? prevProps.googleMapsAddLayers.map((addLayer: IGoogleMapsAddLayer) => addLayer.name)
46+
: [];
47+
let currentGoogleMapsAddLayers = props.googleMapsAddLayers
48+
? props.googleMapsAddLayers.map((addLayer: IGoogleMapsAddLayer) => addLayer.name)
49+
: [];
50+
if (props.googleMapsAddLayers) {
51+
props.googleMapsAddLayers.forEach((layer) => {
52+
if (prevGoogleMapsAddLayers.indexOf(layer.name) === -1) {
53+
(instance as L.gridLayer.GoogleMutant).addGoogleLayer(
54+
layer.name,
55+
layer.options
56+
);
57+
}
58+
});
59+
if (prevProps.googleMapsAddLayers)
60+
prevProps.googleMapsAddLayers.forEach((layer) => {
61+
if (currentGoogleMapsAddLayers.indexOf(layer.name) === -1) {
62+
(instance as L.gridLayer.GoogleMutant).removeGoogleLayer(layer.name);
63+
}
64+
});
65+
}
66+
67+
updateGridLayer(instance, props, prevProps);
68+
};
3969

40-
export default createLayerComponent<L.GridLayer, LayerProps & IProps>(createLeafletElement, updateGridLayer);
70+
export default createLayerComponent<L.GridLayer, LayerProps & IProps>(
71+
createLeafletElement,
72+
updateLeafletElement
73+
);

0 commit comments

Comments
 (0)