import React, { useRef, useEffect, ComponentProps, useMemo } from 'react'; import { GooglePlacesAutocomplete, GooglePlacesAutocompleteRef, } from 'react-native-google-places-autocomplete'; const GOOGLE_PLACES_API_KEY = 'AIzaSyAdIwj4AXzzANYXBdz28jToC0KRKoY6_jk'; import { LogBox, TextInput, View } from 'react-native'; LogBox.ignoreLogs([ 'VirtualizedLists should never be nested', // TODO: Remove when fixed ]); export interface Place { address: string; lat: number; lng: number; } interface Props extends Omit, 'onChange' | 'theme'> { onChange: (place: Place | undefined) => void; defaultPlace: Place | string | undefined; } const GooglePlacesInput = (props: Props) => { const ref = useRef(null); const placeAddress = useMemo( () => props.defaultPlace && (typeof props.defaultPlace === 'string' ? props.defaultPlace : props.defaultPlace.address), [props.defaultPlace] ); useEffect(() => { if (ref && props.defaultPlace) { ref.current?.setAddressText(placeAddress!); } }, [props.defaultPlace, placeAddress]); return ( { if (data === null) return; props.onChange({ address: details!.formatted_address, lat: details!.geometry.location.lat, lng: details!.geometry.location.lng, }); }} textInputProps={{ InputComp: TextInput, onChangeText: (text) => { if (text === '' || text === placeAddress) return; props.onChange(undefined); }, ...props, }} filterReverseGeocodingByTypes={[ 'street_number', 'street_address', 'geocode', ]} /> ); }; export default GooglePlacesInput;