2020-11-30 07:23:05 +00:00
|
|
|
import React, { useState } 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-11-30 07:23:05 +00:00
|
|
|
import { useScript, ScriptStatus, AddScriptTo } from "utils/hooks/useScript";
|
2020-08-07 06:22:36 +00:00
|
|
|
|
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;
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) => props.theme.colors.textOnDarkBG};
|
2020-04-15 11:42:11 +00:00
|
|
|
`;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
clearLocation = () => {
|
|
|
|
|
this.updateProperty(this.props.propertyName, {
|
|
|
|
|
lat: -34.397,
|
|
|
|
|
long: 150.644,
|
|
|
|
|
title: "",
|
|
|
|
|
});
|
2020-11-24 08:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
return (
|
2020-11-30 07:23:05 +00:00
|
|
|
<MapScriptWrapper
|
|
|
|
|
onSearchBoxMounted={this.onSearchBoxMounted}
|
|
|
|
|
onPlacesChanged={this.onLocationSelection}
|
|
|
|
|
propertyValue={this.props.propertyValue}
|
|
|
|
|
clearLocation={this.clearLocation}
|
|
|
|
|
/>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "LOCATION_SEARCH";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
interface MapScriptWrapperProps {
|
|
|
|
|
onSearchBoxMounted: (ref: SearchBox) => void;
|
|
|
|
|
onPlacesChanged: () => void;
|
|
|
|
|
clearLocation: () => void;
|
|
|
|
|
propertyValue: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MapScriptWrapper = (props: MapScriptWrapperProps) => {
|
|
|
|
|
const status = useScript(
|
|
|
|
|
`https://maps.googleapis.com/maps/api/js?key=${google.apiKey}&v=3.exp&libraries=geometry,drawing,places`,
|
|
|
|
|
AddScriptTo.HEAD,
|
|
|
|
|
);
|
|
|
|
|
const [title, setTitle] = useState("");
|
2021-01-19 07:29:15 +00:00
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
return (
|
|
|
|
|
<div data-standalone-searchbox="">
|
|
|
|
|
{status === ScriptStatus.READY && (
|
|
|
|
|
<StandaloneSearchBox
|
|
|
|
|
ref={props.onSearchBoxMounted}
|
|
|
|
|
onPlacesChanged={() => {
|
|
|
|
|
props.onPlacesChanged();
|
|
|
|
|
setTitle("");
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<StyledInput
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder="Enter location"
|
2021-01-19 07:29:15 +00:00
|
|
|
value={title || props.propertyValue?.title}
|
2020-12-24 04:32:25 +00:00
|
|
|
onChange={(ev) => {
|
2020-11-30 07:23:05 +00:00
|
|
|
const val = ev.target.value;
|
|
|
|
|
if (val === "") {
|
|
|
|
|
props.clearLocation();
|
|
|
|
|
}
|
|
|
|
|
setTitle(val);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</StandaloneSearchBox>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-15 11:42:11 +00:00
|
|
|
export default LocationSearchControl;
|