Skip to content

Commit 2f3d8f7

Browse files
committed
inject __ele__ & __map__ to every child of Map component
1 parent c90d030 commit 2f3d8f7

File tree

6 files changed

+116
-29
lines changed

6 files changed

+116
-29
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# 更新日志
22

3+
## [2017-06-14] 1.0.0
4+
5+
### 重大更新
6+
7+
* 现在 react-amap 拥有了扩展能力,你可以自己写一个地图组件并且在 Map 中嵌入;相关文档请参考[自定义地图组件](https://elemefe.github.io/react-amap/articles/extend)
8+
9+
---
310

411
## [2017-05-11] 0.2.7
512

_theme/static/main.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ body{
77
/* background: linear-gradient(to right bottom, #ffdc79, #c4e4a2, #0090ff); */
88
}
99

10-
1110
::-webkit-scrollbar {
1211
width: 8px;
1312
border-left: 1px solid #aaa;

articles/changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ title: 更新日志
44
order: 11
55
---
66

7+
## [2017-06-14] 1.0.0
8+
9+
### 重大更新
10+
11+
* 现在 react-amap 拥有了扩展能力,你可以自己写一个地图组件并且在 Map 中嵌入;相关文档请参考[自定义地图组件](/articles/extend)
12+
13+
---
714

815
## [2017-05-11] 0.2.7
916

articles/extend.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: 自定义地图组件
3+
order: 2
4+
---
5+
6+
7+
### 如何写一个自定义的地图组件
8+
9+
react-amap 组件库目前包含的组件是有限的,所以最常碰到的疑问是:
10+
11+
> 如果我想要的地图组件在 react-amap 中没有怎么办?
12+
13+
现在,react-amap 拥有了扩展能力,你现在可以自己写一个地图组件了。然后,你就可以像这样使用你的组件,比如:
14+
15+
```jsx
16+
const MyMapComponent = (props) => {
17+
// props.__ele__;
18+
// props.__map__;
19+
// your code here
20+
};
21+
22+
// render here
23+
<Map>
24+
<MyMapComponent />
25+
</Map>
26+
```
27+
28+
我们会给 Map 组件的所有子组件注入两个属性:地图实例,和地图容器;在你的组件内部,你可以:
29+
30+
1. 通过 props 的 `__ele__` 属性访问到地图创建时的 div 容器;
31+
2. 通过 props 的 `__map__` 属性访问创建好的高德地图实例;
32+
33+
拥有访问这两个属性的能力后,你可以根据高德原生 API 做高德允许你做的一切事情。实际上,react-amap 中的其他组件就是这么做的。
34+
35+
下面的例子中,我们写了一个自定义的 `ZoomCtrl` 组件,来定义一个自己的地图 zoom 控制器。
36+
37+
```jsx
38+
import { Map } from 'react-amap';
39+
40+
const ZoomCtrl = (props) => {
41+
const map = props.__map__;
42+
if (!map) {
43+
console.log('组件必须作为 Map 的子组件使用');
44+
return;
45+
}
46+
const style = {
47+
position: 'absolute',
48+
top: '10px',
49+
left: '10px',
50+
background: '#fff',
51+
padding: '10px'
52+
}
53+
const zoomIn = () => map.zoomIn();
54+
const zoomOut = () => map.zoomOut();
55+
56+
return (<div style={style}>
57+
<button onClick={zoomIn}>zoom in</button>
58+
<button onClick={zoomOut}>zoom out</button>
59+
</div>);
60+
};
61+
62+
class App extends React.Component {
63+
render() {
64+
return <div style={{width: '100%', height: '400px'}}>
65+
<Map>
66+
<ZoomCtrl />
67+
</Map>
68+
</div>
69+
}
70+
}
71+
72+
ReactDOM.render(
73+
<App/>,
74+
mountNode
75+
)
76+
```
77+
78+
79+
80+
81+
默认情况下, react-amap 会给所有 Map 的子组件注入 `__ele__`, `__map__` 这两个属性,如果你明确不需要注入,可以设置组件的 `preventAmap` 属性为 `true`
82+
83+
```jsx
84+
const MyNormalComponent = (props) => {
85+
// props.__ele__ 和 props.__map__ 都是 undefined
86+
// your code here
87+
};
88+
89+
MyNormalComponent.preventAmap = true;
90+
91+
// render here
92+
<Map>
93+
<MyNormalComponent />
94+
</Map>
95+
```

articles/high.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
category: Article
33
title: 更复杂的需求
4-
order: 2
4+
order: 3
55
---
66

77
`react-amap` 是基于 React 对高德地图的进一步封装,有些时候你可能有更复杂的需求需要实现。

components/map/index.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ import MouseTool from '../mousetool';
1818

1919
const Component = React.Component;
2020
const Children = React.Children;
21-
const ComponentList = [
22-
Circle,
23-
GroundImage,
24-
InfoWindow,
25-
Markers,
26-
Marker,
27-
Polyline,
28-
Polygon,
29-
MouseTool
30-
];
3121

3222
const configurableProps = [
3323
'layers',
@@ -146,7 +136,12 @@ class Map extends Component {
146136
renderChildren() {
147137
return Children.map(this.props.children, (child) => {
148138
if (child) {
149-
if (ComponentList.indexOf(child.type) === -1) {
139+
const cType = child.type;
140+
/* 针对下面两种组件不注入地图相关属性
141+
* 1. 明确声明不需要注入的
142+
* 2. DOM 元素
143+
*/
144+
if (cType.preventAmap || (typeof cType === 'string')) {
150145
return child;
151146
}
152147
return React.cloneElement(child, {
@@ -160,22 +155,6 @@ class Map extends Component {
160155

161156
initMapInstance() {
162157
if (!this.map) {
163-
// let opts = {};
164-
// if ('createOptions' in this.props) {
165-
// opts = this.props.createOptions;
166-
// } else {
167-
// if ('zoom' in this.props) {
168-
// opts.zoom = this.props.zoom;
169-
// this.prevZoom = opts.zoom;
170-
// }
171-
// if ('center' in this.props) {
172-
// opts.center = new window.AMap.LngLat(
173-
// this.props.center.longitude,
174-
// this.props.center.latitude
175-
// );
176-
// this.prevCenter = opts.center;
177-
// }
178-
// }
179158
const options = this.buildCreateOptions();
180159
this.map = new window.AMap.Map(this.mapWrapper, options);
181160
const events = this.exposeMapInstance();

0 commit comments

Comments
 (0)