-
Notifications
You must be signed in to change notification settings - Fork 928
Expand file tree
/
Copy pathSearchBox.jsx
More file actions
172 lines (152 loc) · 4.09 KB
/
SearchBox.jsx
File metadata and controls
172 lines (152 loc) · 4.09 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* global google */
import invariant from "invariant"
import canUseDOM from "can-use-dom"
import React from "react"
import ReactDOM from "react-dom"
import PropTypes from "prop-types"
import {
construct,
componentDidMount,
componentDidUpdate,
componentWillUnmount,
} from "../../utils/MapChildHelper"
import { MAP, SEARCH_BOX } from "../../constants"
export const __jscodeshiftPlaceholder__ = `{
"eventMapOverrides": {
},
"getInstanceFromComponent": "this.state[SEARCH_BOX]"
}`
/**
* A wrapper around `google.maps.places.SearchBox` on the map
*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
*/
export class SearchBox extends React.PureComponent {
static propTypes = {
__jscodeshiftPlaceholder__: null,
/**
* Where to put `<SearchBox>` inside a `<GoogleMap>`
*
* @example google.maps.ControlPosition.TOP_LEFT
* @type number
*/
controlPosition: PropTypes.number,
}
static contextTypes = {
[MAP]: PropTypes.object,
}
state = {
[SEARCH_BOX]: null,
}
componentWillMount() {
if (!canUseDOM || this.containerElement) {
return
}
invariant(
google.maps.places,
`Did you include "libraries=places" in the URL?`
)
this.containerElement = document.createElement(`div`)
this.handleRenderChildToContainerElement()
if (React.version.match(/^16/)) {
return
}
this.handleInitializeSearchBox()
}
componentDidMount() {
let searchBox = this.state[SEARCH_BOX]
if (React.version.match(/^16/)) {
searchBox = this.handleInitializeSearchBox()
}
componentDidMount(this, searchBox, eventMap)
this.handleMountAtControlPosition()
}
componentWillUpdate(nextProp) {
if (this.props.controlPosition !== nextProp.controlPosition) {
this.handleUnmountAtControlPosition()
}
}
componentDidUpdate(prevProps) {
componentDidUpdate(
this,
this.state[SEARCH_BOX],
eventMap,
updaterMap,
prevProps
)
if (this.props.children !== prevProps.children) {
this.handleRenderChildToContainerElement()
}
if (this.props.controlPosition !== prevProps.controlPosition) {
this.handleMountAtControlPosition()
}
}
componentWillUnmount() {
componentWillUnmount(this)
this.handleUnmountAtControlPosition()
if (React.version.match(/^16/)) {
return
}
if (this.containerElement) {
ReactDOM.unmountComponentAtNode(this.containerElement)
this.containerElement = null
}
}
handleInitializeSearchBox() {
/*
* @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
*/
const searchBox = new google.maps.places.SearchBox(
this.containerElement.querySelector("input")
)
construct(SearchBox.propTypes, updaterMap, this.props, searchBox)
this.setState({
[SEARCH_BOX]: searchBox,
})
return searchBox
}
handleRenderChildToContainerElement() {
if (React.version.match(/^16/)) {
return
}
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
React.Children.only(this.props.children),
this.containerElement
)
}
handleMountAtControlPosition() {
if (isValidControlPosition(this.props.controlPosition)) {
this.mountControlIndex =
-1 +
this.context[MAP].controls[this.props.controlPosition].push(
this.containerElement.firstChild
)
}
}
handleUnmountAtControlPosition() {
if (isValidControlPosition(this.props.controlPosition)) {
const child = this.context[MAP].controls[
this.props.controlPosition
].removeAt(this.mountControlIndex)
if (child !== undefined) {
this.containerElement.appendChild(child)
}
}
}
render() {
if (React.version.match(/^16/)) {
return ReactDOM.createPortal(
React.Children.only(this.props.children),
this.containerElement
)
}
return false
}
}
export default SearchBox
const isValidControlPosition = function(value) {
return typeof value === "number"
}
const eventMap = {}
const updaterMap = {}