Skip to content

Commit 4632937

Browse files
author
wgj
committed
feature: add onCreate, onAdd, onBeforeRemove, onRemove lifecycle hooks to components extends Layer, for plugin development.
Signed-off-by: wgj <wgj>
1 parent ad157c9 commit 4632937

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

build/rollup.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ export default {
1818
dir: dist,
1919
format: 'umd',
2020
name: 'RCLeaflet',
21-
exports: 'named'
21+
exports: 'named',
22+
globals: {
23+
react: 'React',
24+
'react-dom': 'ReactDOM',
25+
'prop-types': 'PropTypes',
26+
classnames: 'classNames',
27+
'airbnb-prop-types': 'airbnbPropTypes',
28+
leaflet: 'L'
29+
}
2230
},
2331
external: ['react', 'react-dom', 'prop-types', 'airbnb-prop-types', 'classnames', 'leaflet', 'proj4', 'proj4leaflet', 'leaflet.markercluster'],
2432
plugins: [

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "rc-leaflet",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "React components of Leaflet",
55
"keywords": [
6-
"leaflet",
7-
"react",
86
"typescript",
97
"ts",
8+
"react",
9+
"leaflet",
1010
"map",
11+
"react-leaflet",
1112
"react-component",
1213
"component"
1314
],
@@ -85,7 +86,7 @@
8586
},
8687
"peerDependencies": {
8788
"typescript": ">=3.5.1",
88-
"react": ">=16.0.0",
89-
"react-dom": ">=16.0.0"
89+
"react": ">=16.2.0",
90+
"react-dom": ">=16.2.0"
9091
}
9192
}

src/components/ClusterPoints/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ export default class ClusterPoints extends Layer<L.MarkerClusterGroup, Props> {
7676
}
7777
}
7878

79-
public componentWillUnmount (): void {
80-
this.instance.remove()
81-
}
82-
8379
protected createInstance (props: Props): L.MarkerClusterGroup {
8480
const { points, icon, ...options } = props
8581

src/components/Layer/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import Context, { ContextType } from '../RCMap/Context'
55
import Evented from '../Evented'
66

77
interface PartialProps {
8+
onCreate (layer: L.Layer): void
9+
onAdd (e: L.LeafletEvent, layer: L.Layer): void
10+
onBeforeRemove (layer: L.Layer): void
11+
onRemove (e: L.LeafletEvent, layer: L.Layer): void
812
children: React.ReactNode
913
}
1014

@@ -15,6 +19,10 @@ export default abstract class Layer<T extends L.Layer, P extends L.LayerOptions,
1519
...Evented.propTypes,
1620
pane: PropTypes.string,
1721
attribution: PropTypes.string,
22+
onCreate: PropTypes.func,
23+
onAdd: PropTypes.func,
24+
onBeforeRemove: PropTypes.func,
25+
onRemove: PropTypes.func,
1826
children: PropTypes.node
1927
}
2028

@@ -24,15 +32,16 @@ export default abstract class Layer<T extends L.Layer, P extends L.LayerOptions,
2432

2533
protected constructor (props: Props & P, context: ContextType) {
2634
super(props, context)
27-
const { children, ...restProps } = props
35+
const { onCreate, onAdd, onRemove, children, ...restProps } = props
2836

2937
this.instance = this.createInstance({ ...this.getTheme(), ...restProps } as P)
30-
if (context.map) {
31-
this.instance.addTo(context.map)
32-
}
38+
onCreate && onCreate(this.instance)
39+
this.instance.on({ add: this.onAdd, remove: this.onRemove })
40+
this.instance.addTo(context.map)
3341
}
3442

3543
public componentWillUnmount (): void {
44+
this.props.onBeforeRemove && this.props.onBeforeRemove(this.instance)
3645
this.instance.remove()
3746
}
3847

@@ -42,6 +51,10 @@ export default abstract class Layer<T extends L.Layer, P extends L.LayerOptions,
4251
return {}
4352
}
4453

54+
private onAdd = (e: L.LeafletEvent) => this.props.onAdd && this.props.onAdd(e, this.instance)
55+
56+
private onRemove = (e: L.LeafletEvent) => this.props.onRemove && this.props.onRemove(e, this.instance)
57+
4558
public render (): React.ReactNode {
4659
const { children } = this.props
4760
const layer = this.instance

src/components/TileLayer/Component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ export default class TileLayer extends Component<Props> {
2727
const { url, ...options } = props
2828

2929
this.instance = L.tileLayer(url, options)
30-
if (context.map) {
31-
context.map.addLayer(this.instance)
32-
}
30+
context.map.addLayer(this.instance)
3331
}
3432

3533
public shouldComponentUpdate (nextProps: Props): boolean {

0 commit comments

Comments
 (0)