Skip to content

Commit 8ae8868

Browse files
committed
Added forward refs
1 parent ba593ed commit 8ae8868

File tree

9 files changed

+65
-54
lines changed

9 files changed

+65
-54
lines changed

src/components/Annotation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { useContext } from "react"
2+
import React, { useContext, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapContext } from "./MapProvider"
@@ -14,13 +14,14 @@ const Annotation = ({
1414
curve = 0,
1515
className = "",
1616
...restProps
17-
}) => {
17+
}, ref) => {
1818
const { projection } = useContext(MapContext)
1919
const [x, y] = projection(subject)
2020
const connectorPath = createConnectorPath(dx, dy, curve)
2121

2222
return (
2323
<g
24+
ref={ref}
2425
transform={`translate(${x + dx}, ${y + dy})`}
2526
className={`rsm-annotation ${className}`}
2627
{...restProps}
@@ -44,4 +45,4 @@ Annotation.propTypes = {
4445
className: PropTypes.string,
4546
}
4647

47-
export default Annotation
48+
export default forwardRef(Annotation)

src/components/ComposableMap.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React from "react"
2+
import React, { forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapProvider } from "./MapProvider"
@@ -11,7 +11,7 @@ const ComposableMap = ({
1111
projectionConfig = {},
1212
className = "",
1313
...restProps
14-
}) => {
14+
}, ref) => {
1515
return (
1616
<MapProvider
1717
width={width}
@@ -20,6 +20,7 @@ const ComposableMap = ({
2020
projectionConfig={projectionConfig}
2121
>
2222
<svg
23+
ref={ref}
2324
viewBox={`0 0 ${width} ${height}`}
2425
className={`rsm-svg ${className}`}
2526
{...restProps}
@@ -39,4 +40,4 @@ ComposableMap.propTypes = {
3940
className: PropTypes.string,
4041
}
4142

42-
export default ComposableMap
43+
export default forwardRef(ComposableMap)

src/components/Geographies.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { useContext } from "react"
2+
import React, { useContext, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapContext } from "./MapProvider"
@@ -11,12 +11,12 @@ const Geographies = ({
1111
parseGeographies,
1212
className = "",
1313
...restProps
14-
}) => {
14+
}, ref) => {
1515
const { path, projection } = useContext(MapContext)
1616
const { geographies, outline, borders } = useGeographies({ geography, parseGeographies })
1717

1818
return (
19-
<g className={`rsm-geographies ${className}`} {...restProps}>
19+
<g ref={ref} className={`rsm-geographies ${className}`} {...restProps}>
2020
{
2121
geographies && geographies.length > 0 &&
2222
children({ geographies, outline, borders, path, projection })
@@ -36,4 +36,4 @@ Geographies.propTypes = {
3636
className: PropTypes.string,
3737
}
3838

39-
export default Geographies
39+
export default forwardRef(Geographies)

src/components/Geography.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { useState, memo } from "react"
2+
import React, { useState, memo, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
const Geography = ({
@@ -13,7 +13,7 @@ const Geography = ({
1313
style = {},
1414
className = "",
1515
...restProps
16-
}) => {
16+
}, ref) => {
1717
const [isPressed, setPressed] = useState(false)
1818
const [isFocused, setFocus] = useState(false)
1919

@@ -51,6 +51,7 @@ const Geography = ({
5151

5252
return (
5353
<path
54+
ref={ref}
5455
tabIndex="0"
5556
className={`rsm-geography ${className}`}
5657
d={geography.svgPath}
@@ -78,4 +79,4 @@ Geography.propTypes = {
7879
className: PropTypes.string,
7980
}
8081

81-
export default memo(Geography)
82+
export default memo(forwardRef(Geography))

src/components/Graticule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { memo, useContext } from "react"
2+
import React, { memo, useContext, forwardRef } from "react"
33
import PropTypes from "prop-types"
44
import { geoGraticule } from "d3-geo"
55

@@ -11,10 +11,11 @@ const Graticule = ({
1111
step = [10, 10],
1212
className = "",
1313
...restProps
14-
}) => {
14+
}, ref) => {
1515
const { path } = useContext(MapContext)
1616
return (
1717
<path
18+
ref={ref}
1819
d={path(geoGraticule().step(step)())}
1920
fill={fill}
2021
stroke={stroke}
@@ -31,4 +32,4 @@ Graticule.propTypes = {
3132
className: PropTypes.string,
3233
}
3334

34-
export default memo(Graticule)
35+
export default memo(forwardRef(Graticule))

src/components/Line.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { useContext } from "react"
2+
import React, { useContext, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapContext } from "./MapProvider"
@@ -13,7 +13,7 @@ const Line = ({
1313
fill = "transparent",
1414
className = "",
1515
...restProps
16-
}) => {
16+
}, ref) => {
1717
const { path } = useContext(MapContext)
1818

1919
const lineData = {
@@ -23,6 +23,7 @@ const Line = ({
2323

2424
return (
2525
<path
26+
ref={ref}
2627
d={path(lineData)}
2728
className={`rsm-line ${className}`}
2829
stroke={stroke}
@@ -43,4 +44,4 @@ Line.propTypes = {
4344
className: PropTypes.string,
4445
}
4546

46-
export default Line
47+
export default forwardRef(Line)

src/components/Marker.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { useContext, useState } from "react"
2+
import React, { useContext, useState, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapContext } from "./MapProvider"
@@ -16,7 +16,7 @@ const Marker = ({
1616
style = {},
1717
className = "",
1818
...restProps
19-
}) => {
19+
}, ref) => {
2020
const { projection } = useContext(MapContext)
2121
const [isPressed, setPressed] = useState(false)
2222
const [isFocused, setFocus] = useState(false)
@@ -57,6 +57,7 @@ const Marker = ({
5757

5858
return (
5959
<g
60+
ref={ref}
6061
transform={`translate(${x}, ${y})`}
6162
className={`rsm-marker ${className}`}
6263
onMouseEnter={handleMouseEnter}
@@ -89,4 +90,4 @@ Marker.propTypes = {
8990
className: PropTypes.string,
9091
}
9192

92-
export default Marker
93+
export default forwardRef(Marker)

src/components/Sphere.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React, { Fragment, memo, useMemo, useContext } from "react"
2+
import React, { Fragment, memo, useMemo, useContext, forwardRef } from "react"
33
import PropTypes from "prop-types"
44

55
import { MapContext } from "./MapProvider"
@@ -11,7 +11,7 @@ const Sphere = ({
1111
strokeWidth = 0.5,
1212
className = "",
1313
...restProps
14-
}) => {
14+
}, ref) => {
1515
const { path } = useContext(MapContext)
1616
const spherePath = useMemo(() => path({ type: "Sphere" }), [path])
1717
return (
@@ -22,6 +22,7 @@ const Sphere = ({
2222
</clipPath>
2323
</defs>
2424
<path
25+
ref={ref}
2526
d={spherePath}
2627
fill={fill}
2728
stroke={stroke}
@@ -42,4 +43,4 @@ Sphere.propTypes = {
4243
className: PropTypes.string,
4344
}
4445

45-
export default memo(Sphere)
46+
export default memo(forwardRef(Sphere))

src/components/ZoomableGroup.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1+
import React, { useContext, forwardRef } from "react";
2+
import PropTypes from "prop-types";
13

2-
import React, { useContext } from "react"
3-
import PropTypes from "prop-types"
4+
import { MapContext } from "./MapProvider";
5+
import useZoomPan from "./useZoomPan";
46

5-
import { MapContext } from "./MapProvider"
6-
import useZoomPan from "./useZoomPan"
7-
8-
const ZoomableGroup = ({
9-
center = [0, 0],
10-
zoom = 1,
11-
minZoom = 1,
12-
maxZoom = 8,
13-
translateExtent,
14-
filterZoomEvent,
15-
onMoveStart,
16-
onMove,
17-
onMoveEnd,
18-
className,
19-
...restProps
20-
}) => {
21-
const { width, height } = useContext(MapContext)
7+
const ZoomableGroup = (
8+
{
9+
center = [0, 0],
10+
zoom = 1,
11+
minZoom = 1,
12+
maxZoom = 8,
13+
translateExtent,
14+
filterZoomEvent,
15+
onMoveStart,
16+
onMove,
17+
onMoveEnd,
18+
className,
19+
...restProps
20+
},
21+
ref
22+
) => {
23+
const { width, height } = useContext(MapContext);
2224

23-
const {
24-
mapRef,
25-
transformString,
26-
} = useZoomPan({
25+
const { mapRef, transformString } = useZoomPan({
2726
center,
2827
filterZoomEvent,
2928
onMoveStart,
@@ -32,15 +31,20 @@ const ZoomableGroup = ({
3231
scaleExtent: [minZoom, maxZoom],
3332
translateExtent,
3433
zoom,
35-
})
34+
});
3635

3736
return (
3837
<g ref={mapRef}>
3938
<rect width={width} height={height} fill="transparent" />
40-
<g transform={transformString} className={`rsm-zoomable-group ${className}`} {...restProps} />
39+
<g
40+
ref={ref}
41+
transform={transformString}
42+
className={`rsm-zoomable-group ${className}`}
43+
{...restProps}
44+
/>
4145
</g>
42-
)
43-
}
46+
);
47+
};
4448

4549
ZoomableGroup.propTypes = {
4650
center: PropTypes.array,
@@ -52,6 +56,6 @@ ZoomableGroup.propTypes = {
5256
onMove: PropTypes.func,
5357
onMoveEnd: PropTypes.func,
5458
className: PropTypes.string,
55-
}
59+
};
5660

57-
export default ZoomableGroup
61+
export default forwardRef(ZoomableGroup);

0 commit comments

Comments
 (0)