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 86da8d7
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 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,12 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
TILE_LAYER_RD.url
);
};

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

return (
<Component
type={component.type}
Expand All @@ -84,6 +95,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 86da8d7

Please sign in to comment.