PromucFlow_constructor/app/client/src/components/propertyControls/LocationSearchControl.tsx

79 lines
2.1 KiB
TypeScript
Raw Normal View History

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";
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;
}
const { google } = getAppsmithConfigs();
2020-04-15 11:42:11 +00:00
class LocationSearchControl extends BaseControl<ControlProps> {
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;
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();
const value = { lat, long, title };
2020-04-15 11:42:11 +00:00
this.updateProperty(this.props.propertyName, value);
this.setState({ title: title });
};
onSearchBoxMounted = (ref: SearchBox) => {
this.searchBox = ref;
2020-04-15 11:42:11 +00:00
};
2020-04-15 11:42:11 +00:00
render() {
// Todo: figure out why there's a race here.
if (!window.google) return null;
2020-04-15 11:42:11 +00:00
return (
<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;