import log from "loglevel"; import React, { useState } from "react"; import styled from "styled-components"; import { Wrapper, Status } from "@googlemaps/react-wrapper"; import { StyledInputGroup } from "./StyledControls"; import { isDynamicValue } from "utils/DynamicBindingUtils"; import type { ControlData, ControlProps } from "./BaseControl"; import BaseControl from "./BaseControl"; const MapStatusText = styled.span` font-size: 14px; `; const renderMapStatus = (status: Status) => { switch (status) { case Status.LOADING: return Loading...; case Status.FAILURE: return Error in the component; case Status.SUCCESS: return Component loaded....; } }; class LocationSearchControl extends BaseControl { clearLocation = () => { this.updateProperty(this.props.propertyName, { lat: -34.397, long: 150.644, title: "", }); }; onLocationSelection = (ref: any) => { try { // For some places, the length is zero const places = ref.getPlaces(); const location = places[0].geometry.location; const title = places[0].formatted_address; const lat = location.lat(); const long = location.lng(); const value = { lat, long, title }; this.updateProperty(this.props.propertyName, value, true); } catch (e) { if (ref && ref.getPlaces) log.debug("Error selecting location:", ref.getPlaces()); else { log.debug("Error selecting location - searchBox not found"); } } }; onSearchBoxMounted = (ref: any) => { if (window) { const searchBox = new window.google.maps.places.SearchBox(ref); searchBox.addListener("places_changed", () => { this.onLocationSelection(searchBox); }); } }; render() { return ( ); } static getControlType() { return "LOCATION_SEARCH"; } static canDisplayValueInUI(config: ControlData, value: any): boolean { return !isDynamicValue(value); } } interface MapScriptWrapperProps { onSearchBoxMounted: (ref: any) => void; clearLocation: () => void; propertyValue: any; } function MapScriptWrapper(props: MapScriptWrapperProps) { const [title, setTitle] = useState(""); return (
{ if (value === "") { props.clearLocation(); } setTitle(value); }} placeholder="Enter location" ref={props.onSearchBoxMounted} tabIndex={-1} />
); } export default LocationSearchControl;