5
5
} from "react" ;
6
6
7
7
import {
8
- findDOMNode ,
9
- } from "react-dom " ;
8
+ default as warning ,
9
+ } from "warning " ;
10
10
11
11
import {
12
12
default as GoogleMapHolder ,
@@ -15,40 +15,47 @@ import {
15
15
mapEventPropTypes ,
16
16
} from "./creators/GoogleMapHolder" ;
17
17
18
+ import {
19
+ default as GoogleMapLoader ,
20
+ } from "./GoogleMapLoader" ;
21
+
22
+ const USE_NEW_BEHAVIOR_TAG_NAME = `__new_behavior__` ;
23
+
18
24
export default class GoogleMap extends Component {
19
25
static propTypes = {
20
- containerTagName : PropTypes . string . isRequired ,
21
- containerProps : PropTypes . object . isRequired ,
26
+ containerTagName : PropTypes . string ,
27
+ containerProps : PropTypes . object ,
28
+ map : PropTypes . object ,
22
29
// Uncontrolled default[props] - used only in componentDidMount
23
30
...mapDefaultPropTypes ,
24
31
// Controlled [props] - used in componentDidMount/componentDidUpdate
25
32
...mapControlledPropTypes ,
26
33
// Event [onEventName]
27
34
...mapEventPropTypes ,
28
- }
35
+ } ;
29
36
30
37
// Public APIs
31
38
//
32
39
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Map
33
40
//
34
41
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }).filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); })
35
- getBounds ( ) { return this . state . map . getBounds ( ) ; }
42
+ getBounds ( ) { return ( this . props . map || this . refs . delegate ) . getBounds ( ) ; }
36
43
37
- getCenter ( ) { return this . state . map . getCenter ( ) ; }
44
+ getCenter ( ) { return ( this . props . map || this . refs . delegate ) . getCenter ( ) ; }
38
45
39
- getDiv ( ) { return this . state . map . getDiv ( ) ; }
46
+ getDiv ( ) { return ( this . props . map || this . refs . delegate ) . getDiv ( ) ; }
40
47
41
- getHeading ( ) { return this . state . map . getHeading ( ) ; }
48
+ getHeading ( ) { return ( this . props . map || this . refs . delegate ) . getHeading ( ) ; }
42
49
43
- getMapTypeId ( ) { return this . state . map . getMapTypeId ( ) ; }
50
+ getMapTypeId ( ) { return ( this . props . map || this . refs . delegate ) . getMapTypeId ( ) ; }
44
51
45
- getProjection ( ) { return this . state . map . getProjection ( ) ; }
52
+ getProjection ( ) { return ( this . props . map || this . refs . delegate ) . getProjection ( ) ; }
46
53
47
- getStreetView ( ) { return this . state . map . getStreetView ( ) ; }
54
+ getStreetView ( ) { return ( this . props . map || this . refs . delegate ) . getStreetView ( ) ; }
48
55
49
- getTilt ( ) { return this . state . map . getTilt ( ) ; }
56
+ getTilt ( ) { return ( this . props . map || this . refs . delegate ) . getTilt ( ) ; }
50
57
51
- getZoom ( ) { return this . state . map . getZoom ( ) ; }
58
+ getZoom ( ) { return ( this . props . map || this . refs . delegate ) . getZoom ( ) ; }
52
59
// END - Public APIs
53
60
//
54
61
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Map
@@ -59,55 +66,50 @@ export default class GoogleMap extends Component {
59
66
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Map
60
67
//
61
68
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }).filter(function(it){ return !it.match(/^get/) && !it.match(/^set/) && !it.match(/Map$/); })
62
- fitBounds ( bounds ) { return this . state . map . fitBounds ( bounds ) ; }
69
+ fitBounds ( bounds ) { return ( this . props . map || this . refs . delegate ) . fitBounds ( bounds ) ; }
63
70
64
- panBy ( x , y ) { return this . state . map . panBy ( x , y ) ; }
71
+ panBy ( x , y ) { return ( this . props . map || this . refs . delegate ) . panBy ( x , y ) ; }
65
72
66
- panTo ( latLng ) { return this . state . map . panTo ( latLng ) ; }
73
+ panTo ( latLng ) { return ( this . props . map || this . refs . delegate ) . panTo ( latLng ) ; }
67
74
68
- panToBounds ( latLngBounds ) { return this . state . map . panToBounds ( latLngBounds ) ; }
75
+ panToBounds ( latLngBounds ) { return ( this . props . map || this . refs . delegate ) . panToBounds ( latLngBounds ) ; }
69
76
// END - Public APIs - Use this carefully
70
77
//
71
78
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Map
72
79
73
- static defaultProps = {
74
- containerTagName : "div" ,
75
- containerProps : { } ,
76
- }
77
-
78
- state = {
79
- }
80
+ componentWillMount ( ) {
81
+ const { containerTagName} = this . props ;
82
+ const isUsingNewBehavior = USE_NEW_BEHAVIOR_TAG_NAME === containerTagName ;
80
83
81
- componentDidMount ( ) {
82
- const domEl = findDOMNode ( this ) ;
83
- const { containerTagName, containerProps, children, ...mapProps } = this . props ;
84
- // TODO: support asynchronous load of google.maps API at this level.
85
- //
86
- // Create google.maps.Map instance so that dom is initialized before
87
- // React's children creators.
88
- //
89
- const map = GoogleMapHolder . _createMap ( domEl , mapProps ) ;
90
- this . setState ( { map } ) ;
84
+ warning ( isUsingNewBehavior ,
85
+ `"GoogleMap" with containerTagName is deprecated now and will be removed in next major release (5.0.0).
86
+ Use "GoogleMapLoader" instead. See https://github.com/tomchentw/react-google-maps/pull/157 for more details.`
87
+ ) ;
91
88
}
92
89
93
90
render ( ) {
94
- const { containerTagName, containerProps, children, ...mapProps } = this . props ;
95
- const child = this . state . map ? (
96
- // Notice: implementation details
97
- //
98
- // In this state, the DOM of google.maps.Map is already initialized in
99
- // my innerHTML. Adding extra React components will not clean it
100
- // in current version*. It will use prepend to add DOM of
101
- // GoogleMapHolder and become a sibling of the DOM of google.maps.Map
102
- // Not sure this is subject to change
103
- //
104
- // *current version: 0.13.3, 0.14.2
105
- //
106
- < GoogleMapHolder map = { this . state . map } { ...mapProps } >
107
- { children }
108
- </ GoogleMapHolder >
109
- ) : undefined ;
110
-
111
- return React . createElement ( containerTagName , containerProps , child ) ;
91
+ const { containerTagName, containerProps = { } , children, ...mapProps } = this . props ;
92
+ const isUsingNewBehavior = USE_NEW_BEHAVIOR_TAG_NAME === containerTagName ;
93
+
94
+ if ( isUsingNewBehavior ) {
95
+ return (
96
+ < GoogleMapHolder { ...mapProps } >
97
+ { children }
98
+ </ GoogleMapHolder >
99
+ ) ;
100
+ } else { //------------ Deprecated ------------
101
+ const realContainerTagName = null == containerTagName ? `div` : containerTagName ;
102
+
103
+ return (
104
+ < GoogleMapLoader
105
+ containerElement = { React . createElement ( realContainerTagName , containerProps ) }
106
+ googleMapElement = {
107
+ < GoogleMap ref = "delegate" containerTagName = { USE_NEW_BEHAVIOR_TAG_NAME } { ...mapProps } >
108
+ { children }
109
+ </ GoogleMap >
110
+ }
111
+ />
112
+ ) ;
113
+ }
112
114
}
113
115
}
0 commit comments