|
1 | 1 | import React, { useRef, useEffect } from 'react'; |
| 2 | +import PropTypes from 'prop-types' |
2 | 3 |
|
3 | 4 | function createPopupClass() { |
4 | 5 | function Popup({ position, content, map }) { |
@@ -39,26 +40,35 @@ function createPopupClass() { |
39 | 40 | return Popup; |
40 | 41 | } |
41 | 42 |
|
42 | | -const CustomPopup = ({ map, position, children }) => { |
| 43 | +const CustomPopup = ({ map, position, children, visible, google }) => { |
43 | 44 | const containerEl = useRef(null); |
44 | 45 | useEffect(() => { |
| 46 | + if (map) { |
45 | 47 | const pos = new google.maps.LatLng(position.lat, position.lng); |
46 | 48 | const Popup = createPopupClass(); |
47 | | - const popup = new Popup({ |
| 49 | + new Popup({ |
48 | 50 | position: pos, |
49 | 51 | content: containerEl.current, |
50 | 52 | map, |
51 | 53 | }); |
52 | | - }); |
| 54 | + } |
| 55 | + }, [map]); |
53 | 56 | return ( |
54 | | - <div |
55 | | - style={{ position: 'absolute' }} |
56 | | - className="custom-popup" |
57 | | - ref={containerEl} |
58 | | - > |
59 | | - {children} |
| 57 | + <div style={{ position: 'absolute' }} ref={containerEl}> |
| 58 | + {visible && children} |
60 | 59 | </div> |
61 | 60 | ); |
62 | 61 | }; |
63 | 62 |
|
| 63 | +CustomPopup.propTypes = { |
| 64 | + children: PropTypes.element.isRequired, |
| 65 | + map: PropTypes.object, |
| 66 | + marker: PropTypes.object, |
| 67 | + position: PropTypes.object, |
| 68 | + visible: PropTypes.bool, |
| 69 | +} |
| 70 | + |
| 71 | +CustomPopup.defaultProps = { |
| 72 | + visible: true |
| 73 | +} |
64 | 74 | export default CustomPopup; |
0 commit comments