2020-04-29 10:29:02 +00:00
|
|
|
import React from "react";
|
2020-04-15 11:42:11 +00:00
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import SearchBox from "react-google-maps/lib/components/places/SearchBox";
|
|
|
|
|
import StandaloneSearchBox from "react-google-maps/lib/components/places/StandaloneSearchBox";
|
2020-08-07 06:22:36 +00:00
|
|
|
import { getAppsmithConfigs } from "configs";
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
const StyledInput = styled.input`
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
border: 1px solid transparent;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 32px;
|
|
|
|
|
padding: 0 5px;
|
|
|
|
|
border-radius: 3px;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
outline: none;
|
|
|
|
|
text-overflow: ellipses;
|
|
|
|
|
background: #272821;
|
|
|
|
|
color: ${props => props.theme.colors.textOnDarkBG};
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
interface StandaloneSearchBoxProps {
|
2020-04-29 10:29:02 +00:00
|
|
|
onSearchBoxMounted: (ref: any) => void;
|
2020-04-15 11:42:11 +00:00
|
|
|
onPlacesChanged: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 06:22:36 +00:00
|
|
|
const { google } = getAppsmithConfigs();
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
class LocationSearchControl extends BaseControl<ControlProps> {
|
2020-11-24 08:43:45 +00:00
|
|
|
searchBox: any = null;
|
|
|
|
|
state: any = { title: "" };
|
|
|
|
|
|
|
|
|
|
handleChange = (ev: any) => {
|
|
|
|
|
const val = ev.target.value;
|
|
|
|
|
this.setState({ title: val });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onLocationSelection = () => {
|
|
|
|
|
const places = this.searchBox.getPlaces();
|
2020-04-15 11:42:11 +00:00
|
|
|
const location = places[0].geometry.location;
|
2020-11-24 08:43:45 +00:00
|
|
|
const title = places[0].formatted_address;
|
2020-04-15 11:42:11 +00:00
|
|
|
const lat = location.lat();
|
2020-05-07 10:51:37 +00:00
|
|
|
const long = location.lng();
|
2020-11-24 08:43:45 +00:00
|
|
|
const value = { lat, long, title };
|
2020-04-15 11:42:11 +00:00
|
|
|
this.updateProperty(this.props.propertyName, value);
|
2020-11-24 08:43:45 +00:00
|
|
|
this.setState({ title: title });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onSearchBoxMounted = (ref: SearchBox) => {
|
|
|
|
|
this.searchBox = ref;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
2020-11-24 08:43:45 +00:00
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
render() {
|
2020-11-24 08:43:45 +00:00
|
|
|
// Todo: figure out why there's a race here.
|
|
|
|
|
if (!window.google) return null;
|
2020-04-15 11:42:11 +00:00
|
|
|
return (
|
2020-11-24 08:43:45 +00:00
|
|
|
<div data-standalone-searchbox="">
|
|
|
|
|
<StandaloneSearchBox
|
|
|
|
|
ref={this.onSearchBoxMounted}
|
|
|
|
|
onPlacesChanged={this.onLocationSelection}
|
|
|
|
|
>
|
|
|
|
|
<StyledInput
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder="Enter location"
|
|
|
|
|
value={this.state.title || this.props.propertyValue.title}
|
|
|
|
|
onChange={this.handleChange}
|
|
|
|
|
/>
|
|
|
|
|
</StandaloneSearchBox>
|
|
|
|
|
</div>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "LOCATION_SEARCH";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default LocationSearchControl;
|