diff --git a/src/components/MarkerCluster/MarkerCluster.tsx b/src/components/MarkerCluster/MarkerCluster.tsx index aa7b1ad..46d4a03 100644 --- a/src/components/MarkerCluster/MarkerCluster.tsx +++ b/src/components/MarkerCluster/MarkerCluster.tsx @@ -15,7 +15,16 @@ const InternalMarkerCluster = ( props: MarkerClusterProps, ref: React.Ref, ) => { - const { cluster, render, renderCluster, data = [], zoomOnClick, zoomOnClickPadding = 20 } = props; + const { + cluster, + render, + renderCluster, + data = [], + zoomOnClick, + zoomOnClickPadding = 20, + onClick, + onClusterClick, + } = props; const { map } = useMap(); const [list, setList] = useState([]); @@ -43,7 +52,7 @@ const InternalMarkerCluster = ( }, [map]); useEffect(() => { - if (data && data.length) { + if (data) { supercluster.load(data); handleChangeBoundary(); } @@ -104,18 +113,25 @@ const InternalMarkerCluster = ( key={item.id} lngLat={geometry.coordinates} onClick={() => { + onClusterClick?.(point_count, cluster_id); if (zoomOnClick) { - handleClusterMarkerClick(JSON.parse(JSON.stringify(item))); + handleClusterMarkerClick(item); } }} > - {isFunction(renderCluster) ? renderCluster(point_count, cluster_id) : renderCluster} + {isFunction(renderCluster) ? renderCluster(point_count) : renderCluster} ); } return ( - + { + onClick?.(properties); + }} + > {isFunction(render) ? render(item) : render} ); diff --git a/src/components/MarkerCluster/types.ts b/src/components/MarkerCluster/types.ts index 6862310..2b67330 100644 --- a/src/components/MarkerCluster/types.ts +++ b/src/components/MarkerCluster/types.ts @@ -4,7 +4,7 @@ import type { PaddingOptions } from 'mapbox-gl'; export type AnyObject = Record; export type RenderMarkerFun = (data: any) => React.ReactNode; -export type RenderClusterMarkerFun = (count: number, clusterId: string) => React.ReactNode; +export type RenderClusterMarkerFun = (count: number) => React.ReactNode; export type { Supercluster }; @@ -49,8 +49,8 @@ export interface MarkerClusterProps { * @default 20 */ zoomOnClickPadding?: number | PaddingOptions; - onClick?: any; - onClusterClick?: any; + onClick?: (data: D) => void; + onClusterClick?: (count: number, clusterId: number) => void; } export type RefMarkerCluster = ( diff --git a/stories/examples/ClusterPro.stories.tsx b/stories/examples/ClusterPro.stories.tsx index 2241301..c51472a 100644 --- a/stories/examples/ClusterPro.stories.tsx +++ b/stories/examples/ClusterPro.stories.tsx @@ -28,7 +28,6 @@ const meta = { > ); }} + onClick={(data) => { + console.log(data); + }} + onClusterClick={(count, clusterId) => { + console.log(count); + + if (clusterRef.current) { + console.log(clusterRef.current.getLeaves(clusterId, Infinity)); + } + }} />