Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#2177] Add map component interaction to…
Browse files Browse the repository at this point in the history
… preview
  • Loading branch information
robinmolen committed Dec 18, 2024
1 parent ad322ba commit a6fc4b6
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/registry/map/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {CRS_RD, TILE_LAYER_RD} from '@open-formulieren/leaflet-tools';
import {MapComponentSchema} from '@open-formulieren/types';
import {useContext, useLayoutEffect} from 'react';
import {MapContainer, TileLayer, useMap} from 'react-leaflet';
import type {FeatureGroup as LeafletFeatureGroup} from 'leaflet';
import 'leaflet-draw/dist/leaflet.draw.css';
import 'leaflet/dist/leaflet.css';
import {useContext, useLayoutEffect, useRef} from 'react';
import {FeatureGroup, MapContainer, TileLayer, useMap} from 'react-leaflet';
import {EditControl} from 'react-leaflet-draw';
import useAsync from 'react-use/esm/useAsync';

import Loader from '@/components/builder/loader';
import {Component, Description} from '@/components/formio';
import {BuilderContext} from '@/context';

import {ComponentPreviewProps} from '../types';
import {ComponentPreviewProps} from '@/registry/types';

interface MapViewProps {
lat: number;
Expand Down Expand Up @@ -41,8 +44,10 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
defaultZoom,
initialCenter = {},
tileLayerIdentifier,
interactions,
} = component;
const {getMapTileLayers} = useContext(BuilderContext);
const featureGroupRef = useRef<LeafletFeatureGroup>(null);
const {value: tileLayers, loading, error} = useAsync(async () => await getMapTileLayers(), []);
if (error) {
throw error;
Expand All @@ -63,6 +68,13 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
TILE_LAYER_RD.url
);
};

const onFeatureCreate = (event: any) => {
console.log(featureGroupRef);
featureGroupRef.current?.clearLayers();
featureGroupRef.current?.addLayer(event.layer);
};

return (
<Component
type={component.type}
Expand All @@ -84,6 +96,24 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
}}
>
<TileLayer {...TILE_LAYER_RD} url={tileLayerUrl()} />
<FeatureGroup ref={featureGroupRef}>
<EditControl
position="topright"
onCreated={onFeatureCreate}
edit={{
edit: false,
remove: false,
}}
draw={{
rectangle: false,
circle: !!interactions?.circle,
polyline: !!interactions?.polyline,
polygon: !!interactions?.polygon,
marker: !!interactions?.marker,
circlemarker: false,
}}
/>
</FeatureGroup>
<MapView lat={lat} lng={lng} zoom={zoom} />
</MapContainer>
{description && <Description text={description} />}
Expand Down

0 comments on commit a6fc4b6

Please sign in to comment.