Skip to content

Commit bc41752

Browse files
committed
feat(places/SearchBox): revamp with jscodeshift
BREAKING CHANGE: input element can now render directly as the only child Before: ```js <SearchBox inputPlaceholder="Customized your placeholder" inputStyle={{ padding: `0 12px`, fontSize: `14px`, outline: `none` }} /> ``` After: ```js <SearchBox> <input type="text" placeholder="Customized your placeholder" style={{ padding: `0 12px`, fontSize: `14px`, outline: `none`, }} /> </SearchBox> ```
1 parent 01bfb80 commit bc41752

File tree

5 files changed

+148
-180
lines changed

5 files changed

+148
-180
lines changed

src/lib/places/SearchBox.js

Lines changed: 0 additions & 137 deletions
This file was deleted.

src/lib/places/searchBox.spec.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/lib/utils/SearchBoxHelper.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/macros/places/SearchBox.jsx

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* global google */
2+
import _ from "lodash"
3+
import canUseDOM from "can-use-dom"
4+
import invariant from "invariant"
5+
import React from "react"
6+
import ReactDOM from "react-dom"
7+
import PropTypes from "prop-types"
8+
9+
import {
10+
construct,
11+
componentDidMount,
12+
componentDidUpdate,
13+
componentWillUnmount,
14+
} from "../../utils/MapChildHelper"
15+
16+
import { MAP, SEARCH_BOX } from "../../constants"
17+
18+
export const __jscodeshiftPlaceholder__ = `{
19+
"eventMapOverrides": {
20+
},
21+
"getInstanceFromComponent": "this.state[SEARCH_BOX]"
22+
}`
23+
24+
/**
25+
* @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
26+
*/
27+
export class SearchBox extends React.PureComponent {
28+
static propTypes = {
29+
__jscodeshiftPlaceholder__: null,
30+
controlPosition: PropTypes.number,
31+
}
32+
33+
static contextTypes = {
34+
[MAP]: PropTypes.object,
35+
}
36+
37+
state = {
38+
[SEARCH_BOX]: null,
39+
}
40+
41+
componentWillMount() {
42+
if (!canUseDOM || this.state[SEARCH_BOX]) {
43+
return
44+
}
45+
this.containerElement = document.createElement(`div`)
46+
this.handleRenderChildToContainerElement()
47+
/*
48+
* @url https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox
49+
*/
50+
const searchBox = new google.maps.places.SearchBox(
51+
this.containerElement.firstChild
52+
)
53+
construct(SearchBox.propTypes, updaterMap, this.props, searchBox)
54+
this.state = {
55+
[SEARCH_BOX]: searchBox,
56+
}
57+
}
58+
59+
componentDidMount() {
60+
componentDidMount(this, this.state[SEARCH_BOX], eventMap)
61+
this.handleMountAtControlPosition()
62+
}
63+
64+
componentWillUpdate(nextProp) {
65+
if (this.props.controlPosition !== nextProp.controlPosition) {
66+
this.handleUnmountAtControlPosition()
67+
}
68+
}
69+
70+
componentDidUpdate(prevProps) {
71+
componentDidUpdate(
72+
this,
73+
this.state[SEARCH_BOX],
74+
eventMap,
75+
updaterMap,
76+
prevProps
77+
)
78+
if (this.props.children !== prevProps.children) {
79+
this.handleRenderChildToContainerElement()
80+
}
81+
if (this.props.controlPosition !== prevProps.controlPosition) {
82+
this.handleMountAtControlPosition()
83+
}
84+
}
85+
86+
componentWillUnmount() {
87+
componentWillUnmount(this)
88+
this.handleUnmountAtControlPosition()
89+
if (this.containerElement) {
90+
ReactDOM.unmountComponentAtNode(this.containerElement)
91+
this.containerElement = null
92+
}
93+
}
94+
95+
handleRenderChildToContainerElement() {
96+
ReactDOM.unstable_renderSubtreeIntoContainer(
97+
this,
98+
React.Children.only(this.props.children),
99+
this.containerElement
100+
)
101+
}
102+
103+
handleMountAtControlPosition() {
104+
if (isValidControlPosition(this.props.controlPosition)) {
105+
invariant(
106+
this.context[MAP],
107+
`If you're using <SearchBox> with controlPosition, please put it as a child of a <GoogleMap> component.`
108+
)
109+
this.mountControlIndex =
110+
this.context[MAP].controls[this.props.controlPosition].push(
111+
this.containerElement.firstChild
112+
) - 1
113+
}
114+
}
115+
116+
handleUnmountAtControlPosition() {
117+
if (isValidControlPosition(this.props.controlPosition)) {
118+
invariant(
119+
this.context[MAP],
120+
`If you're using <SearchBox> with controlPosition, please put it as a child of a <GoogleMap> component.`
121+
)
122+
const child = this.context[MAP].controls[
123+
this.props.controlPosition
124+
].removeAt(this.mountControlIndex)
125+
this.containerElement.appendChild(child)
126+
}
127+
}
128+
129+
render() {
130+
return false
131+
}
132+
}
133+
134+
export default SearchBox
135+
136+
const isValidControlPosition = _.isNumber
137+
138+
const eventMap = {}
139+
140+
const updaterMap = {}

src/places/SearchBox.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import warning from "warning"
2+
3+
warning(
4+
false,
5+
`[DEPRECATED] "react-google-maps/lib/places/SearchBox" has been moved to "react-google-maps/lib/components/places/SearchBox".`
6+
)
7+
8+
export { default } from "../components/places/SearchBox"

0 commit comments

Comments
 (0)