Skip to content

Commit

Permalink
feat: 新增组件 AMapMouseTool
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Jun 3, 2023
1 parent f3eeaa7 commit 0946959
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/components/AMapMouseTool/AMapMouseTool.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
forwardRef,
useEffect,
useState,
useImperativeHandle,
} from 'react';

import useAMap from '../../hooks/useAMap';
import useAMapEventBinder from '../../hooks/useAMapEventBinder';

import type { AMapMouseToolProps } from './interface';

/**
*
* Origin Docs See:
* https://lbs.amap.com/api/javascript-api/reference/plugin#AMap.MouseTool
*
*/
const AMapMouseTool = forwardRef<AMap.MouseTool, AMapMouseToolProps>(
({
type, options, onCompleted,
}, ref) => {
const { __AMAP__: AMap, map } = useAMap();
const [curInstance, setInstance] = useState<AMap.MouseTool | null>(null);

useEffect(() => {
if (!AMap || !map) return;

const init = () => {
const instance = new AMap.MouseTool(map);
setInstance(instance);
};

if (AMap.MouseTool) {
init();
} else {
AMap.plugin(['AMap.MouseTool'], init);
}
}, [AMap, map]);

useImperativeHandle(ref, () => curInstance!, [curInstance]);

useAMapEventBinder(curInstance, 'draw', onCompleted);

useEffect(() => {
let clearEffect;
if (!curInstance) return clearEffect;

clearEffect = () => curInstance.close(false);

curInstance[type]((options ?? {}) as { [k: string]: any });

return clearEffect;
}, [curInstance, options, type]);

return null;
},
);

AMapMouseTool.defaultProps = {
options: {},
onCompleted: undefined,
};

export default AMapMouseTool;
2 changes: 2 additions & 0 deletions src/components/AMapMouseTool/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './interface';
export { default } from './AMapMouseTool';
23 changes: 23 additions & 0 deletions src/components/AMapMouseTool/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type AMapMouseToolProps = {
type:
| 'marker'
| 'circle'
| 'rectangle'
| 'polyline'
| 'polygon'
| 'measureArea'
| 'rule'
| 'rectZoomIn'
| 'rectZoomOut';
options?:
| AMap.MarkerOptions
| AMap.PolylineOptions
| AMap.PolygonOptions
| AMap.RectangleOptions
| AMap.CircleOptions
| AMap.PolylineOptions
| AMap.PolygonOptions
| AMap.PolygonOptions
| AMap.PolygonOptions;
onCompleted?: (e: any) => void;
};
63 changes: 63 additions & 0 deletions src/components/AMapMouseTool/stories/AMapMouseTool.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { Meta, Story } from '@storybook/react';
import { actions } from '@storybook/addon-actions';

import { AMapMouseTool, AMapMouseToolProps } from 'index';
import { withAMapContainer } from '../../AMapMap/stories/AMapMap.stories';

const eventHandler = actions('onCompleted');

export default {
title: 'Components/Tools/AMapMouseTool',
decorators: [withAMapContainer],
args: {
type: 'polygon',
options: {
strokeColor: 'red',
strokeWeight: 4,
},
onCompleted: eventHandler.onCompleted,
},
argTypes: {
type: {
description: '工具类型',
type: { required: true, summary: 'string ' },
control: {
type: 'select',
options: [
'marker',
'circle',
'rectangle',
'polyline',
'polygon',
'measureArea',
'rule',
'rectZoomIn',
'rectZoomOut',
],
},
},
options: {
description: '额外参数',
type: { summary: 'object' },
},
onCompleted: {
description: '鼠标工具绘制覆盖物结束时触发此事件,obj对象为绘制出来的覆盖物对象',
type: { summary: '(event: AMap.Event<"draw">) => void' },
},
},
} as Meta;

const Template: Story<AMapMouseToolProps> = (args) => (
<AMapMouseTool {...args} />
);

export const CommonUse = Template.bind({});
CommonUse.args = {
type: 'polygon',
options: {
strokeColor: 'red',
strokeWeight: 4,
},
onCompleted: eventHandler.onCompleted,
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export {
AMapGeoJSONGetOverlayCallback,
AMapGeoJSONProps,
} from './components/AMapGeoJSON';

export { default as AMapMouseTool, AMapMouseToolProps } from './components/AMapMouseTool';

0 comments on commit 0946959

Please sign in to comment.