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

96 lines
2.7 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 { compose, withProps, lifecycle } = require("recompose");
const { withScriptjs } = require("react-google-maps");
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
const PlacesWithStandaloneSearchBox = compose(
withProps({
googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${google.apiKey}&v=3.exp&libraries=geometry,drawing,places`,
2020-04-15 11:42:11 +00:00
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
}),
lifecycle({
componentWillMount() {
2020-04-29 10:29:02 +00:00
let searchBox: any = React.createRef<SearchBox>();
2020-04-15 11:42:11 +00:00
this.setState({
places: [],
2020-04-29 10:29:02 +00:00
onSearchBoxMounted: (ref: any) => {
2020-04-15 11:42:11 +00:00
searchBox = ref;
},
onPlacesChanged: () => {
if (searchBox === null) return;
2020-04-29 10:29:02 +00:00
if (searchBox.getPlaces === null) return;
const places = searchBox.getPlaces();
2020-04-15 11:42:11 +00:00
this.setState({
places,
});
this.props.onLocationSelection(places);
},
});
},
}),
withScriptjs,
)((props: any) => (
<div data-standalone-searchbox="">
<StandaloneSearchBox
ref={props.onSearchBoxMounted}
onPlacesChanged={props.onPlacesChanged}
>
<StyledInput type="text" placeholder="Enter location" />
</StandaloneSearchBox>
</div>
));
class LocationSearchControl extends BaseControl<ControlProps> {
onLocationSelection = (
places: Array<{
geometry: { location: { lat: () => number; lng: () => number } };
}>,
) => {
const location = places[0].geometry.location;
const lat = location.lat();
2020-05-07 10:51:37 +00:00
const long = location.lng();
const value = { lat, long };
2020-04-15 11:42:11 +00:00
this.updateProperty(this.props.propertyName, value);
};
render() {
return (
<PlacesWithStandaloneSearchBox
onLocationSelection={this.onLocationSelection}
/>
);
}
static getControlType() {
return "LOCATION_SEARCH";
}
}
export default LocationSearchControl;