Skip to content

Commit 866ec69

Browse files
committed
update instance when prop changes
1 parent ebe157b commit 866ec69

5 files changed

Lines changed: 62 additions & 32 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,2 @@
11
## Prerequisites
2-
3-
[Node.js](http://nodejs.org/) >= 10 must be installed.
4-
5-
## Installation
6-
7-
- Running `npm install` in the component's root directory will install everything you need for development.
8-
9-
## Demo Development Server
10-
11-
- `npm start` will run a development server with the component's demo app at [http://localhost:3000](http://localhost:3000) with hot module reloading.
12-
13-
## Running Tests
14-
15-
- `npm test` will run the tests once.
16-
17-
- `npm run test:coverage` will run the tests and produce a coverage report in `coverage/`.
18-
19-
- `npm run test:watch` will run the tests on every change.
20-
21-
## Building
22-
23-
- `npm run build` will build the component for publishing to npm and also bundle the demo app.
24-
25-
- `npm run clean` will delete built resources.
2+
Build package locally and test it with example project with `npm link` command.

dist/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ function createMarkerClusterGroup(props, context) {
3232
);
3333
}
3434
var updateMarkerCluster = (instance, props, prevProps) => {
35+
const { clusterProps, clusterEvents } = getPropsAndEvents(props);
36+
const { clusterProps: prevClusterProps, clusterEvents: prevClusterEvents } = getPropsAndEvents(prevProps);
37+
Object.keys(clusterProps).forEach((key) => {
38+
if (clusterProps[key] !== prevClusterProps[key]) {
39+
instance.options[key] = clusterProps[key];
40+
}
41+
});
42+
Object.entries(prevClusterEvents).forEach(([eventAsProp, callback]) => {
43+
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
44+
instance.off(clusterEvent, callback);
45+
});
46+
Object.entries(clusterEvents).forEach(([eventAsProp, callback]) => {
47+
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
48+
instance.on(clusterEvent, callback);
49+
});
3550
};
3651
var MarkerClusterGroup = createPathComponent(
3752
createMarkerClusterGroup,

package-lock.json

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"build": "tsup src/index.tsx --format esm --dts --clean --external @react-leaflet/core && npm run copy:assets",
1313
"format": "prettier --write \"src/**/*.tsx\"",
14-
"lint": "eslint src/**/*.{ts,tsx}",
14+
"lint": "eslint 'src/**/*.{ts,tsx}'",
1515
"prepublishOnly": "npm run lint",
1616
"preversion": "npm run lint",
1717
"version": "npm run format && git add -A src",
@@ -22,11 +22,11 @@
2222
"leaflet.markercluster": "^1.5.3"
2323
},
2424
"peerDependencies": {
25+
"@react-leaflet/core": "^3.0.0",
2526
"leaflet": "^1.9.0",
2627
"react": "^19.0.0",
2728
"react-dom": "^19.0.0",
28-
"react-leaflet": "^5.0.0",
29-
"@react-leaflet/core": "^3.0.0"
29+
"react-leaflet": "^5.0.0"
3030
},
3131
"devDependencies": {
3232
"@types/leaflet": "^1.9.0",
@@ -38,6 +38,7 @@
3838
"@typescript-eslint/parser": "^4.33.0",
3939
"cpx": "^1.2.1",
4040
"eslint": "^7.32.0",
41+
"eslint-config-prettier": "^10.1.8",
4142
"eslint-plugin-prettier": "^3.4.1",
4243
"eslint-plugin-react": "^7.30.0",
4344
"eslint-plugin-react-hooks": "^4.5.0",

src/index.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,29 @@ const updateMarkerCluster = (
6969
props: MarkerClusterControl,
7070
prevProps: MarkerClusterControl,
7171
) => {
72-
//TODO when prop change update instance
73-
// if (props. !== prevProps.center || props.size !== prevProps.size) {
74-
// instance.setBounds(getBounds(props))
75-
// }
72+
const { clusterProps, clusterEvents } = getPropsAndEvents(props)
73+
const { clusterProps: prevClusterProps, clusterEvents: prevClusterEvents } =
74+
getPropsAndEvents(prevProps)
75+
76+
// Update Options
77+
Object.keys(clusterProps).forEach((key) => {
78+
if (clusterProps[key] !== prevClusterProps[key]) {
79+
// eslint-disable-next-line
80+
// @ts-ignore
81+
instance.options[key] = clusterProps[key]
82+
}
83+
})
84+
85+
// Update Events
86+
Object.entries(prevClusterEvents).forEach(([eventAsProp, callback]) => {
87+
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`
88+
instance.off(clusterEvent, callback)
89+
})
90+
91+
Object.entries(clusterEvents).forEach(([eventAsProp, callback]) => {
92+
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`
93+
instance.on(clusterEvent, callback)
94+
})
7695
}
7796

7897
const MarkerClusterGroup = createPathComponent<L.MarkerClusterGroup, MarkerClusterControl>(

0 commit comments

Comments
 (0)