|
| 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 = {} |
0 commit comments