-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathStandaloneSearchBox.jsx
More file actions
120 lines (102 loc) · 2.69 KB
/
StandaloneSearchBox.jsx
File metadata and controls
120 lines (102 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* -----------------------------------------------------------------------------
* This file is auto-generated from the corresponding file at `src/macros/`.
* Please **DO NOT** edit this file directly when creating PRs.
* -----------------------------------------------------------------------------
*/
/* global google */
import invariant from "invariant"
import React from "react"
import ReactDOM from "react-dom"
import PropTypes from "prop-types"
import {
construct,
componentDidMount,
componentDidUpdate,
componentWillUnmount,
} from "../../utils/MapChildHelper"
import { SEARCH_BOX } from "../../constants"
/**
* A wrapper around `google.maps.places.SearchBox` without the map
*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
*/
class SearchBox extends React.PureComponent {
static displayName = "StandaloneSearchBox"
static propTypes = {
/**
* @type LatLngBounds|LatLngBoundsLiteral
*/
defaultBounds: PropTypes.any,
/**
* @type LatLngBounds|LatLngBoundsLiteral
*/
bounds: PropTypes.any,
/**
* function
*/
onPlacesChanged: PropTypes.func,
}
state = {
[SEARCH_BOX]: null,
}
componentDidMount() {
invariant(
google.maps.places,
`Did you include "libraries=places" in the URL?`
)
const element = ReactDOM.findDOMNode(this)
/*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
*/
const searchBox = new google.maps.places.SearchBox(
element.querySelector("input") || element
)
construct(SearchBox.propTypes, updaterMap, this.props, searchBox)
componentDidMount(this, searchBox, eventMap)
this.setState({
[SEARCH_BOX]: searchBox,
})
}
componentDidUpdate(prevProps) {
componentDidUpdate(
this,
this.state[SEARCH_BOX],
eventMap,
updaterMap,
prevProps
)
}
componentWillUnmount() {
componentWillUnmount(this)
}
render() {
return React.Children.only(this.props.children)
}
/**
* Returns the bounds to which query predictions are biased.
* @type LatLngBounds
* @public
*/
getBounds() {
return this.state[SEARCH_BOX].getBounds()
}
/**
* Returns the query selected by the user, or `null` if no places have been found yet, to be used with `places_changed` event.
* @type Array<PlaceResult>
* @public
*/
getPlaces() {
return this.state[SEARCH_BOX].getPlaces()
}
}
export const StandaloneSearchBox = SearchBox
export default StandaloneSearchBox
const eventMap = {
onPlacesChanged: "places_changed",
}
const updaterMap = {
bounds(instance, bounds) {
instance.setBounds(bounds)
},
}