Altinn / app-frontend-react

Altinn application React frontend
BSD 3-Clause "New" or "Revised" License
18 stars 31 forks source link

Geometry in map component not updating when changed #2727

Closed MartinRoberg closed 5 days ago

MartinRoberg commented 6 days ago

Description of the bug

The map-component renderes geometries given to it by the datamodel. The component expects an array of objects { data: string; label?: string; }. When objects are added to the array, the map component renderes them properly. On the other hand, if you change the data/polygon part of a geometry that has previously been rendered on the map, the map moves to the new geometry location, but the geometry itself remains unchanged. Since the map moves to the new placement, that means the geometry bounds are recalculated, showing that the datamodel has been updated correctly. It seems like this happens as long as the data/gemoetry object has the same index and label in the datamodel array as it had on previous renders/updates. Reversing the list in the app before storing it in the datamodel so the index of the object has been changed since last update makes the geometry behave correctly/as expected.

The problem is propably with the key of the geojson components in the map component.

{geometries?.map(({ data, label }, i) => (
        <GeoJSON
          key={`${i}-${label}`}

What happened

Geometry is placed in datamodel and rendered in map. When the geometry is changed in the datamodel, the geometry remains in place on the map, instead of moving where it should.

What should have happened

Map should have moved to the new placement of the geometry, and the geometry object should have moved and adapted to the changed geometry data.

Steps To Reproduce

  1. Make an app with a map and geometries that can be changed by the user.
  2. Place geometries in the datamodel/map
  3. Watch them render on the map
  4. Change/update the data prop/field of one of the geometries
  5. The map should move the view to the updated placement of the geometry, while the geometry stays in place.

Additional Information

Placed geometry on map, and rendered correctly Image

Changed the data of a geometry, but kept same label and placement in the array. The map moves but geometry does not. Image

Expected behavior is that the geometry updates to new place: Image