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 8ef3a1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/jsonEditor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@import 'leaflet-draw/dist/leaflet.draw.css';
@import 'leaflet/dist/leaflet.css';

.json-editor {
min-block-size: 45vh;

Expand Down
35 changes: 31 additions & 4 deletions src/registry/map/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 {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 +42,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 +66,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 +93,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 8ef3a1f

Please sign in to comment.