diff --git a/GooglePlacesAutocomplete.d.ts b/GooglePlacesAutocomplete.d.ts index 15d5a8a0..6539eedb 100644 --- a/GooglePlacesAutocomplete.d.ts +++ b/GooglePlacesAutocomplete.d.ts @@ -431,6 +431,7 @@ interface GooglePlacesAutocompleteProps { } export type GooglePlacesAutocompleteRef = { + setAddressTextAndQuery(address: string): void; setAddressText(address: string): void; getAddressText(): string; getCurrentLocation(): void; diff --git a/GooglePlacesAutocomplete.js b/GooglePlacesAutocomplete.js index de3099a2..c365cf5f 100644 --- a/GooglePlacesAutocomplete.js +++ b/GooglePlacesAutocomplete.js @@ -158,6 +158,10 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => { }, [props.predefinedPlaces]); useImperativeHandle(ref, () => ({ + setAddressTextAndQuery:(address) => { + setStateText(address); + _handleAddressTextAndQuery(address); + }, setAddressText: (address) => { setStateText(address); }, @@ -563,6 +567,28 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => { } }; + const _handleAddressTextAndQuery = async (address) => { + + // Use Promise to ensure that the query is completed before logging addresses + await new Promise((resolve) => { + // Trigger the query to fetch autocomplete results based on the entered address + _request(address); + + // Wait for a short delay to ensure the query is completed and results are available + setTimeout(() => { + resolve(dataSource); + }, 500); // Adjust the delay as needed + }); + + if (_results.length > 0) { + // Get the first item from _results + const firstResult = _results[0]; + + // Trigger the onPress function with the first result + _onPress(firstResult); + } + }; + const _getRowLoader = () => { return ; };