Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#2173] Add background configuration to …
Browse files Browse the repository at this point in the history
…map edit
  • Loading branch information
robinmolen committed Dec 3, 2024
1 parent e130091 commit e231899
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/ComponentConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ComponentConfiguration: React.FC<ComponentConfigurationProps> = ({
uniquifyKey,
supportedLanguageCodes = ['nl', 'en'],
richTextColors,
leafletMapBackgrounds,
theme,
getFormComponents,
getValidatorPlugins,
Expand Down Expand Up @@ -60,6 +61,7 @@ const ComponentConfiguration: React.FC<ComponentConfigurationProps> = ({
uniquifyKey,
supportedLanguageCodes,
richTextColors,
leafletMapBackgrounds,
theme,
getFormComponents,
getValidatorPlugins,
Expand Down
13 changes: 13 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export interface DocumentTypeOption {
description: string;
}

/*
Leaflet map background options
This datastructure is created by the Open Forms backend.
*/
export interface LeafletMapOption {
identifier: string;
url: string;
label: string;
}

/*
Builder
*/
Expand All @@ -48,6 +59,7 @@ export interface BuilderContextType {
getDocumentTypes: () => Promise<Array<DocumentTypeOption>>;
getConfidentialityLevels: () => Promise<SelectOption[]>;
getAuthPlugins: () => Promise<AuthPluginOption[]>;
leafletMapBackgrounds: LeafletMapOption[];
}

const BuilderContext = React.createContext<BuilderContextType>({
Expand All @@ -65,6 +77,7 @@ const BuilderContext = React.createContext<BuilderContextType>({
getDocumentTypes: async () => [],
getConfidentialityLevels: async () => [],
getAuthPlugins: async () => [],
leafletMapBackgrounds: [],
});

BuilderContext.displayName = 'BuilderContext';
Expand Down
4 changes: 3 additions & 1 deletion src/registry/map/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const EditForm: EditFormDefinition<MapComponentSchema> = () => {
'isSensitiveData',
'useConfigDefaultMapSettings',
'defaultZoom',
'initialCenter'
'initialCenter',
'backgroundIdentifier'
)}
/>
<BuilderTabs.Advanced hasErrors={hasAnyError('conditional')} />
Expand Down Expand Up @@ -134,6 +135,7 @@ EditForm.defaultValues = {
lat: undefined,
lng: undefined,
},
backgroundIdentifier: undefined,
defaultValue: null,
// Advanced tab
conditional: {
Expand Down
32 changes: 31 additions & 1 deletion src/registry/map/map-configuration.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {TILE_LAYER_RD} from '@open-formulieren/leaflet-tools';
import {useContext} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';

import {NumberField, Panel} from '@/components/formio';
import {NumberField, Panel, Select} from '@/components/formio';
import {BuilderContext} from '@/context';

const DefaultZoom: React.FC = () => {
const intl = useIntl();
Expand Down Expand Up @@ -80,6 +82,33 @@ const Longitude: React.FC = () => {
);
};

const Background: React.FC = () => {
const {leafletMapBackgrounds} = useContext(BuilderContext);

const intl = useIntl();
const tooltip = intl.formatMessage({
description: "Tooltip for 'backgroundIdentifier' builder field",
defaultMessage: 'The background to use as the map.',
});
return (
<Select
name="backgroundIdentifier"
label={
<FormattedMessage
description="Label for 'backgroundIdentifier' builder field"
defaultMessage="Background"
/>
}
isClearable
tooltip={tooltip}
options={leafletMapBackgrounds?.map(background => ({
label: background.label,
value: background.identifier,
}))}
/>
);
};

const MapConfiguration: React.FC = () => (
<Panel
title={
Expand All @@ -94,6 +123,7 @@ const MapConfiguration: React.FC = () => (
<DefaultZoom />
<Latitude />
<Longitude />
<Background />
</Panel>
);

Expand Down
18 changes: 16 additions & 2 deletions src/registry/map/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {CRS_RD, TILE_LAYER_RD} from '@open-formulieren/leaflet-tools';
import {MapComponentSchema} from '@open-formulieren/types';
import {useLayoutEffect} from 'react';
import {useContext, useLayoutEffect} from 'react';
import {MapContainer, TileLayer, useMap} from 'react-leaflet';
import useAsync from 'react-use/esm/useAsync';

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

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

Expand Down Expand Up @@ -37,10 +39,22 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
validate = {},
defaultZoom,
initialCenter = {},
backgroundIdentifier,
} = component;
const {leafletMapBackgrounds} = useContext(BuilderContext);
const {required = false} = validate;
const {lat = 52.1326332, lng = 5.291266} = initialCenter;
const zoom = defaultZoom ?? 8;

const url = (): string => {
if (!leafletMapBackgrounds?.length || !backgroundIdentifier) {
return TILE_LAYER_RD.url;
}
return (
leafletMapBackgrounds?.find(background => background.identifier === backgroundIdentifier)
?.url ?? TILE_LAYER_RD.url
);
};
return (
<Component
type={component.type}
Expand All @@ -61,7 +75,7 @@ const Preview: React.FC<ComponentPreviewProps<MapComponentSchema>> = ({component
minBlockSize: '400px',
}}
>
<TileLayer {...TILE_LAYER_RD} />
<TileLayer {...TILE_LAYER_RD} url={url()} />
<MapView lat={lat} lng={lng} zoom={zoom} />
</MapContainer>
{description && <Description text={description} />}
Expand Down

0 comments on commit e231899

Please sign in to comment.