react-native-google-places-autocomplete icon indicating copy to clipboard operation
react-native-google-places-autocomplete copied to clipboard

Feature Request: setaddressText using ref should trigger search

Open harshaliitr opened this issue 4 years ago • 14 comments

Describe the bug

I am saving recently searched terms in my state and when user clicks on one of the recent searches, I do this.autoCompleteRef.setAddressText(this.state.recentSearch). This updates the text input value but the search is not triggered. I have to press one more key to see the search results from place autocomplete api

Reproduction - (required - issue will be closed without this)

Steps to reproduce the behavior - a minimal reproducible code example, link to a snack or a repository.

Please provide a FULLY REPRODUCIBLE example.

Click to expand!
<GooglePlacesAutocomplete
            ref={comp => (this.autoCompleteRef = comp)}
            placeholder="Search"
            styles={{textInput: {borderBottomColor: "#4a4a4a", borderBottomWidth: 1, backgroundColor: 'transparent'}}}
            onPress={(data, details = null) => {
              // 'details' is provided when fetchDetails = true
              console.log(data);
              console.log(details);
            }}
            query={{
              key: 'key',
              language: 'en',
              components: 'country:in',
            }}
            currentLocation={true}
            currentLocationLabel="Use your current location"
            nearbyPlacesAPI="GoogleReverseGeocoding"
            textInputProps={{onBlur: () => {}}}
            fetchDetails //details.geometry.location - gives lat, lng
          />
         handleRecentLocationClick = (desc) => {
         this.autoCompleteRef.setAddressText(desc)
         this.autoCompleteRef.focus()
}

Please remember to remove you google API key from the code you provide here

Additional context

  • Library Version: [e.g. 1.4.2]

  • React Native Version: [e.g. 0.62.2]

  • [ ] iOS

  • [x] Android

  • [ ] Web

If you are using expo please indicate here:

  • [ ] I am using expo

Add any other context about the problem here, screenshots etc

harshaliitr avatar Nov 11 '20 09:11 harshaliitr

You need this.autoCompleteRef.triggerFocus() before version 2.

https://github.com/FaridSafi/react-native-google-places-autocomplete/releases/tag/v2.0.0

bell-steven avatar Nov 11 '20 15:11 bell-steven

Hello @bell-steven i am on version 2.0.3 "react-native-google-places-autocomplete": "^2.0.3"

harshaliitr avatar Nov 12 '20 11:11 harshaliitr

Ok. Can you try using version 2.1.1. There were some changes in v2.1.0 that may have addressed the issue.

bell-steven avatar Nov 12 '20 16:11 bell-steven

Ok. Can you try using version 2.1.1. There were some changes in v2.1.0 that may have addressed the issue.

v 2.1.1 here and got the same issue. The textinput is updated, but the search is not triggered.

triplumix avatar Nov 22 '20 19:11 triplumix

Ok. Can you try using version 2.1.1. There were some changes in v2.1.0 that may have addressed the issue.

i am having the same issue as well. Textinput triggered, but search not.

zhlee1997 avatar Dec 03 '20 08:12 zhlee1997

Same here using v2.1.2.

tention avatar Dec 30 '20 16:12 tention

Is there any solution for this bug? The same issue I am facing search is not triggered with a predefined text.

plabon avatar May 20 '21 10:05 plabon

Does anyone found a solution for this bug?

filippobarcellos avatar Jun 28 '21 20:06 filippobarcellos

For anyone still having trouble with this I managed to make it work. I'm on version 2.4.1 and you need to change a line in GooglePlacesAutocomplete.js inside react-native-google-places-autocomplete in node_modules. The problem seems to be that the useEffect at line 145 that is in charge of populating the results list runs before the local state variable stateText is updated from the setAddressText method. If you change line 161 from setStateText(address) to _handleChangeText(address) it should work.

icomad avatar Oct 04 '21 15:10 icomad

For anyone still having trouble with this I managed to make it work. I'm on version 2.4.1 and you need to change a line in GooglePlacesAutocomplete.js inside react-native-google-places-autocomplete in node_modules. The problem seems to be that the useEffect at line 145 that is in charge of populating the results list runs before the local state variable stateText is updated from the setAddressText method. If you change line 161 from setStateText(address) to _handleChangeText(address) it should work.

This works! Remember to this.autocompleteRef.focus() to display the results list.

joacoabrego avatar Oct 04 '21 19:10 joacoabrego

Does someone want to put in a PR?

bell-steven avatar Oct 04 '21 20:10 bell-steven

Does someone want to put in a PR?

Done!

joacoabrego avatar Oct 04 '21 22:10 joacoabrego

GooglePlacesAutocomplete.js inside react-native-google-places-autocomplete in node_modules. The problem seems to be that the useEffect at line 145 that is in charge of populating the results list runs before the local state variable stateText is updated from the setAddressText method. If you change line 161 from setStateText(address) to _handleChangeText(address) it should work.

This works! Remember to this.autocompleteRef.focus() to display the results list.

I tried this and works, is there any way to make it auto update after every character input?

this.autocompleteRef.focus()

where would you put this

ysefwakil avatar Apr 02 '23 07:04 ysefwakil

has this issue been resolved yet ? and if yes its not updated on the docs

Shamrockhub avatar Apr 09 '24 06:04 Shamrockhub