Skip to content

Commit 88190ca

Browse files
committed
Define the new optional properties "centerToReset" and "centerToZoom"
1 parent eb66c26 commit 88190ca

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const Map = () => {
3535

3636
## Props
3737

38-
| Name | Type | Default | Description |
39-
|------------|----------------------------------------------------------------------|--------------|------------------------------------|
40-
| position? | [ControlOptions](https://leafletjs.com/reference.html#control-position) | "topleft" | The position of the control |
41-
| title? | string | "Reset map view" | The control title. |
42-
| icon? | string | "\u2610" | The control icon. Can be either a path for `url()` or a unicode character. |
43-
44-
38+
| Name | Type | Default | Description |
39+
|----------------|-------------------------------------------------------------------------|------------------|-----------------------------------------------------------------|
40+
| position? | [ControlOptions](https://leafletjs.com/reference.html#control-position) | "topleft" | The position of the control |
41+
| title? | string | "Reset map view" | The control title. |
42+
| icon? | string | "\u2610" | The control icon. Can be either a path for `url()` or a unicode |
43+
| centerToReset? | [LatLng](https://leafletjs.com/reference.html#latlng) | undefined | Define a different center than the one mounted by the map |
44+
| zoomToReset? | number | undefined | Define a different zoom than the one mounted by the map |

__tests__/ResetViewControl.spec.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ describe("ResetViewControl", () => {
1616
const defaultMapCenter = [-96.8716348, 32.8205866] as LatLngExpression;
1717
const defaultMapZoom = 5
1818

19-
const ControlWrapper = ({ title, icon }: ResetViewControlOptions) => {
19+
const ControlWrapper = ({ title, icon, centerToReset, zoomToReset }: ResetViewControlOptions) => {
2020
useMapEvents({
2121
viewreset: mockHandleViewReset,
2222
});
2323

2424
return (
25-
<>
26-
<ResetViewControl
27-
title={title ?? "Reset view"}
28-
icon={icon ?? "\u2612"}
29-
/>
30-
</>
25+
<ResetViewControl
26+
title={title ?? "Reset view"}
27+
icon={icon ?? "\u2612"}
28+
centerToReset={centerToReset}
29+
zoomToReset={zoomToReset}
30+
/>
3131
);
3232
};
33-
const Map = ({ title, icon }: ResetViewControlOptions) => {
33+
const Map = ({ title, icon, centerToReset, zoomToReset }: ResetViewControlOptions) => {
3434
return (
3535
<MapContainer zoom={defaultMapZoom} center={defaultMapCenter} ref={map => mapInstance = map}>
36-
<ControlWrapper title={title} icon={icon} />
36+
<ControlWrapper title={title} icon={icon} centerToReset={centerToReset} zoomToReset={zoomToReset} />
3737
</MapContainer>
3838
);
3939
};
@@ -59,6 +59,24 @@ describe("ResetViewControl", () => {
5959
expect(mockHandleViewReset).toHaveBeenCalledTimes(2);
6060
});
6161

62+
test("can reset the map view to a zoom and center different from those mounted by the map", () => {
63+
const centerToReset = [44.8, 6.3] as LatLngExpression;
64+
const zoomToReset = 17;
65+
render(<Map centerToReset={centerToReset} zoomToReset={zoomToReset} />);
66+
67+
expect(mapInstance?.getCenter().lat).toBeCloseTo(defaultMapCenter[0], 1);
68+
expect(mapInstance?.getCenter().lng).toBeCloseTo(defaultMapCenter[1], 1);
69+
expect(mapInstance?.getZoom()).toEqual(defaultMapZoom)
70+
71+
userEvent.click(screen.getByTitle("Reset view"));
72+
73+
expect(mapInstance?.getCenter().lat).toBeCloseTo(centerToReset[0], 1);
74+
expect(mapInstance?.getCenter().lng).toBeCloseTo(centerToReset[1], 1);
75+
expect(mapInstance?.getZoom()).toEqual(zoomToReset)
76+
77+
expect(mockHandleViewReset).toHaveBeenCalledTimes(3);
78+
});
79+
6280
test("can see icon", () => {
6381
render(<Map />);
6482

src/ResetViewControl.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createControlComponent } from "@react-leaflet/core";
22
import { Control, DomUtil, DomEvent, Util } from "leaflet";
33

4-
import type { Map, ControlOptions, LatLng } from "leaflet";
4+
import type { Map, ControlOptions, LatLng, LatLngExpression } from "leaflet";
55

66
export type ResetViewControlOptions = {
77
/**
@@ -14,15 +14,22 @@ export type ResetViewControlOptions = {
1414
* It renders a box by default.
1515
*/
1616
icon?: string;
17+
18+
/**
19+
* "zoom" and "center" properties are useful when the map is mounted in a particular position
20+
* and you need this button to reset the view to another position.
21+
*/
22+
zoomToReset?: number;
23+
centerToReset?: LatLngExpression;
1724
} & ControlOptions;
1825

1926
const _getControl = Control.extend({
20-
options: { position: "topleft", title: "Reset map view", icon: "\u2610" },
27+
options: { position: "topleft", title: "Reset map view", icon: "\u2610", zoomToReset: undefined, centerToReset: undefined },
2128

2229
onAdd: function (map: Map) {
2330
Util.setOptions(this, {
24-
zoom: map.getZoom(),
25-
center: map.getCenter(),
31+
zoom: this.options.zoomToReset ?? map.getZoom(),
32+
center: this.options.centerToReset ?? map.getCenter(),
2633
});
2734

2835
const { title, icon } = this.options;

0 commit comments

Comments
 (0)