Skip to content

Commit

Permalink
feat: marker绑定点击事件
Browse files Browse the repository at this point in the history
  • Loading branch information
leiyuanxiang committed Aug 31, 2022
1 parent 9f1283e commit 1cbd812
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/map/demos/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ export default () => {
<Map onClick={enable ? handleClick1 : handleClick2}>
<Marker
lngLat={[30.5, 50.5]}
onDragEnd={(e) => {
//@ts-ignore
console.log(e.target.getLngLat());
}}
onClick={(e) => {
//@ts-ignore
console.log(e.target.getLngLat());
}}
draggable={true}

// render={() => {
// return <div style={{ background: 'red' }}>123</div>
// }}
>
<div style={{ background: 'red' }}>123</div>
<div style={{ background: 'red', borderRadius: '50%', padding: '3px' }}>123</div>
</Marker>
</Map>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/marker/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const mapEventMap: EventMapping = {
};

/** 静态属性 */
export const StaticProps: PropKey[] = ['anchor', 'clickTolerance', 'color'];
export const StaticProps: PropKey[] = ['anchor', 'clickTolerance', 'color', 'onClick'];

/** 动态属性 */
export const NativeDynamicProps: PropKey[] = [
Expand Down
9 changes: 8 additions & 1 deletion src/marker/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ export const Marker = forwardRef<MapboxMarker, MarkerProps>((props, ref) => {
contentWrapper.current = document.createElement('div');

const marker = new MapboxMarker(contentWrapper.current, options);

marker.getElement().addEventListener('click', (e: MouseEvent) => {
//@ts-ignore
options.onClick?.({
type: 'click',
target: marker,
originalEvent: e,
});
});
return Promise.resolve(marker);
};

Expand Down
1 change: 1 addition & 0 deletions src/marker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface MarkerProps extends MarkerOptions, Partial<MarkerEvents> {
className?: string;
lngLat: LngLatLike;
children: any;
onClick?: (e: MapEventType['click']) => void;
}

export type PropKey = KeysOfUnion<MarkerProps>;

0 comments on commit 1cbd812

Please sign in to comment.