-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(map-and-label): remove individual features (#3625)
Co-authored-by: Dafydd Llŷr Pearson <[email protected]>
- Loading branch information
1 parent
579e56a
commit e1bcd7a
Showing
4 changed files
with
168 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Feature } from "geojson"; | ||
import { Dispatch, SetStateAction, useEffect, useState } from "react"; | ||
|
||
type Projection = "EPSG:3857" | "EPSG:27700"; | ||
|
||
export type GeoJSONChange = Record<Projection, { features: Feature[] }>; | ||
export type GeoJSONChangeEvent = CustomEvent<GeoJSONChange>; | ||
|
||
const isGeoJSONChangeEvent = (event: Event): event is GeoJSONChangeEvent => { | ||
return event instanceof CustomEvent; | ||
}; | ||
|
||
type UseGeoJSONChange = ( | ||
mapId: string, | ||
callback: (event: GeoJSONChangeEvent) => void, | ||
) => [Feature[] | undefined, Dispatch<SetStateAction<Feature[] | undefined>>]; | ||
|
||
/** | ||
* Hook for interacting with @opensystemslab/map | ||
* Assign a callback function to be triggered on the "geojsonChange" event | ||
*/ | ||
export const useGeoJSONChange: UseGeoJSONChange = (mapId, callback) => { | ||
const [features, setFeatures] = useState<Feature[] | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
const geojsonChangeHandler: EventListener = (event) => { | ||
if (!isGeoJSONChangeEvent(event)) return; | ||
|
||
callback(event); | ||
}; | ||
|
||
const map: HTMLElement | null = document.getElementById(mapId); | ||
map?.addEventListener("geojsonChange", geojsonChangeHandler); | ||
|
||
return function cleanup() { | ||
map?.removeEventListener("geojsonChange", geojsonChangeHandler); | ||
}; | ||
}, [callback, mapId]); | ||
|
||
return [features, setFeatures]; | ||
}; |