Skip to content

Commit

Permalink
🏷️ [#2173] Add MapComponent type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Dec 17, 2024
1 parent 9f90a8f commit b58cddf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/openforms/formio/components/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
)
from ..formatters.formio import DefaultFormatter, TextFieldFormatter
from ..registry import BasePlugin, register
from ..typing import AddressNLComponent, Component, DateComponent, DatetimeComponent
from ..typing import (
AddressNLComponent,
Component,
DateComponent,
DatetimeComponent,
MapComponent,
)
from ..utils import conform_to_mask
from .np_family_members.constants import FamilyMembersDataAPIChoices
from .np_family_members.haal_centraal import get_np_family_members_haal_centraal
Expand Down Expand Up @@ -186,11 +192,11 @@ def build_serializer_field(


@register("map")
class Map(BasePlugin[Component]):
class Map(BasePlugin[MapComponent]):
formatter = MapFormatter

def mutate_config_dynamically(
self, component, submission: Submission, data: DataMapping
self, component: MapComponent, submission: Submission, data: DataMapping
) -> None:
if (identifier := component.get("tileLayerIdentifier")) is not None:
tile_layer = MapTileLayer.objects.filter(identifier=identifier).first()
Expand All @@ -199,15 +205,15 @@ def mutate_config_dynamically(
component["tileLayerUrl"] = tile_layer.url

@staticmethod
def rewrite_for_request(component, request: Request):
def rewrite_for_request(component: MapComponent, request: Request):
if component.get("useConfigDefaultMapSettings", False):
config = GlobalConfiguration.get_solo()
component["defaultZoom"] = config.form_map_default_zoom_level
component.setdefault("initialCenter", {})
component["initialCenter"]["lat"] = config.form_map_default_latitude
component["initialCenter"]["lng"] = config.form_map_default_longitude

def build_serializer_field(self, component: Component) -> serializers.ListField:
def build_serializer_field(self, component: MapComponent) -> serializers.ListField:
validate = component.get("validate", {})
required = validate.get("required", False)
base = serializers.FloatField(
Expand Down
4 changes: 2 additions & 2 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from ..typing import AddressNLComponent, Component
from ..typing import AddressNLComponent, Component, MapComponent
from .base import FormatterBase


Expand All @@ -22,7 +22,7 @@ def format(self, component: Component, value: str) -> str:


class MapFormatter(FormatterBase):
def format(self, component: Component, value: list[float]) -> str:
def format(self, component: MapComponent, value: list[float]) -> str:
# use a comma here since its a single data element
return ", ".join((str(x) for x in value))

Expand Down
3 changes: 2 additions & 1 deletion src/openforms/formio/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from .base import Component, FormioConfiguration, OptionDict
from .custom import AddressNLComponent, DateComponent
from .custom import AddressNLComponent, DateComponent, MapComponent
from .vanilla import (
Column,
ColumnsComponent,
Expand Down Expand Up @@ -43,5 +43,6 @@
# special
"EditGridComponent",
"AddressNLComponent",
"MapComponent",
# deprecated
]
10 changes: 10 additions & 0 deletions src/openforms/formio/typing/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .base import Component
from .dates import DatePickerConfig, DatePickerCustomOptions
from .map import MapInitialCenter


class DateComponent(Component):
Expand All @@ -11,3 +12,12 @@ class DateComponent(Component):

class AddressNLComponent(Component):
deriveAddress: bool


class MapComponent(Component):
useConfigDefaultMapSettings: bool
defaultZoom: NotRequired[int]
initialCenter: NotRequired[MapInitialCenter]
tileLayerIdentifier: NotRequired[str]
# The tileLayerUrl will be dynamically generated from the tileLayerIdentifier
tileLayerUrl: NotRequired[str]
6 changes: 6 additions & 0 deletions src/openforms/formio/typing/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import NotRequired, TypedDict


class MapInitialCenter(TypedDict):
lat: NotRequired[float]
lng: NotRequired[float]

0 comments on commit b58cddf

Please sign in to comment.