|
1 | | -import React from 'react' |
2 | | -import ReactDOM from 'react-dom' |
| 1 | +import React, { Component } from 'react'; |
3 | 2 |
|
4 | | -import Map, {Marker, GoogleApiWrapper} from '../../src/index' |
5 | | -import styles from './autocomplete.module.css' |
| 3 | +import Map, { Marker } from '../../src/index'; |
6 | 4 |
|
7 | | -const Contents = React.createClass({ |
8 | | - getInitialState() { |
9 | | - return { |
10 | | - place: null, |
11 | | - position: null |
12 | | - } |
13 | | - }, |
| 5 | +import styles from './autocomplete.module.css'; |
14 | 6 |
|
15 | | - onSubmit: function(e) { |
16 | | - e.preventDefault(); |
17 | | - }, |
| 7 | +class Contents extends Component { |
| 8 | + state = { |
| 9 | + position: null |
| 10 | + }; |
18 | 11 |
|
19 | | - componentDidMount: function() { |
| 12 | + componentDidMount() { |
20 | 13 | this.renderAutoComplete(); |
21 | | - }, |
| 14 | + } |
22 | 15 |
|
23 | 16 | componentDidUpdate(prevProps) { |
24 | | - const {google, map} = this.props; |
25 | | - if (map !== prevProps.map) { |
26 | | - this.renderAutoComplete(); |
27 | | - } |
28 | | - }, |
| 17 | + if (this.props !== prevProps.map) this.renderAutoComplete(); |
| 18 | + } |
29 | 19 |
|
30 | | - renderAutoComplete: function() { |
31 | | - const {google, map} = this.props; |
| 20 | + onSubmit(e) { |
| 21 | + e.preventDefault(); |
| 22 | + } |
| 23 | + |
| 24 | + renderAutoComplete() { |
| 25 | + const { google, map } = this.props; |
32 | 26 |
|
33 | 27 | if (!google || !map) return; |
34 | 28 |
|
35 | | - const aref = this.refs.autocomplete; |
36 | | - const node = ReactDOM.findDOMNode(aref); |
37 | | - var autocomplete = new google.maps.places.Autocomplete(node); |
| 29 | + const autocomplete = new google.maps.places.Autocomplete(this.autocomplete); |
38 | 30 | autocomplete.bindTo('bounds', map); |
39 | 31 |
|
40 | 32 | autocomplete.addListener('place_changed', () => { |
41 | 33 | const place = autocomplete.getPlace(); |
42 | | - if (!place.geometry) { |
43 | | - return; |
44 | | - } |
45 | 34 |
|
46 | | - if (place.geometry.viewport) { |
47 | | - map.fitBounds(place.geometry.viewport); |
48 | | - } else { |
| 35 | + if (!place.geometry) return; |
| 36 | + |
| 37 | + if (place.geometry.viewport) map.fitBounds(place.geometry.viewport); |
| 38 | + else { |
49 | 39 | map.setCenter(place.geometry.location); |
50 | 40 | map.setZoom(17); |
51 | 41 | } |
52 | 42 |
|
53 | | - this.setState({ |
54 | | - place: place, |
55 | | - position: place.geometry.location |
56 | | - }) |
57 | | - }) |
58 | | - }, |
| 43 | + this.setState({ position: place.geometry.location }); |
| 44 | + }); |
| 45 | + } |
59 | 46 |
|
60 | | - render: function() { |
61 | | - const props = this.props; |
62 | | - const {position} = this.state; |
| 47 | + render() { |
| 48 | + const { position } = this.state; |
63 | 49 |
|
64 | 50 | return ( |
65 | 51 | <div className={styles.flexWrapper}> |
66 | 52 | <div className={styles.left}> |
67 | 53 | <form onSubmit={this.onSubmit}> |
68 | 54 | <input |
69 | | - ref='autocomplete' |
| 55 | + placeholder="Enter a location" |
| 56 | + ref={ref => (this.autocomplete = ref)} |
70 | 57 | type="text" |
71 | | - placeholder="Enter a location" /> |
72 | | - <input |
73 | | - className={styles.button} |
74 | | - type='submit' |
75 | | - value='Go' /> |
| 58 | + /> |
| 59 | + |
| 60 | + <input className={styles.button} type="submit" value="Go" /> |
76 | 61 | </form> |
| 62 | + |
77 | 63 | <div> |
78 | 64 | <div>Lat: {position && position.lat()}</div> |
79 | 65 | <div>Lng: {position && position.lng()}</div> |
80 | 66 | </div> |
81 | 67 | </div> |
| 68 | + |
82 | 69 | <div className={styles.right}> |
83 | | - <Map {...props} |
84 | | - containerStyle={{ |
85 | | - position: 'relative', |
86 | | - height: '100vh', |
87 | | - width: '100%' |
88 | | - }} |
89 | | - center={this.state.position} |
90 | | - centerAroundCurrentLocation={false}> |
91 | | - <Marker position={this.state.position} /> |
| 70 | + <Map |
| 71 | + {...this.props} |
| 72 | + center={position} |
| 73 | + centerAroundCurrentLocation={false} |
| 74 | + containerStyle={{ |
| 75 | + height: '100vh', |
| 76 | + position: 'relative', |
| 77 | + width: '100%' |
| 78 | + }}> |
| 79 | + <Marker position={position} /> |
92 | 80 | </Map> |
93 | 81 | </div> |
94 | 82 | </div> |
95 | | - ) |
96 | | - } |
97 | | -}) |
98 | | - |
99 | | -const MapWrapper = React.createClass({ |
100 | | - render: function() { |
101 | | - const props = this.props; |
102 | | - const {google} = this.props; |
103 | | - |
104 | | - return ( |
105 | | - <Map google={google} |
106 | | - className={'map'} |
107 | | - visible={false}> |
108 | | - <Contents {...props} /> |
109 | | - </Map> |
110 | 83 | ); |
111 | 84 | } |
112 | | -}) |
| 85 | +} |
| 86 | + |
| 87 | +const MapWrapper = props => ( |
| 88 | + <Map className="map" google={props.google} visible={false}> |
| 89 | + <Contents {...props} /> |
| 90 | + </Map> |
| 91 | +); |
113 | 92 |
|
114 | | -export default MapWrapper |
| 93 | +export default MapWrapper; |
0 commit comments