Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to have a callback if no autocomplete item is selected? #106

Closed
ericluwj opened this issue Aug 8, 2017 · 8 comments
Closed

Possible to have a callback if no autocomplete item is selected? #106

ericluwj opened this issue Aug 8, 2017 · 8 comments

Comments

@ericluwj
Copy link

ericluwj commented Aug 8, 2017

I am facing an issue whereby I would like to clear the value if no autocomplete item was selected.
The exact scenario is whereby I had typed in an address and then moved to the next field by pressing "Tab" without selecting any autocomplete item.
In order to invalidate the input field, I would want to clear the filled in address.

@hibiken
Copy link
Owner

hibiken commented Aug 8, 2017

Would this work for your use case?

// In `constructor`
this.state = {
  address: '',
  isAddressValid: false,
}
this.handleChange = (address) => this.setState({ address })
this.handleSelect = ({ placeId}) => this.setState({ isAddressValid: !!placeId })
this.handleBlur = () => {
  if (!this.state.isAddressValid) {
    this.setState({ address: '' })
  }
}


// And in `render`
const inputProps = {
  onChange: this.handleChange,
  value: this.state.address,
  onBlur: this.handleBlur,
}

<PlacesAutocomplete
  inputProps={inputProps}
  onSelect={this.handleSelect}
/>

@ericluwj
Copy link
Author

ericluwj commented Aug 8, 2017

Oh this seems to work. Thanks a lot.

@ahmadalfy
Copy link

This doesn't seem to work anymore. The blur event is never fired

@jjolley
Copy link

jjolley commented Jan 15, 2019

The blur event doesn't fire for me either.

@georgefeast
Copy link

Any update on this? Would be great to be able to handle blur for the input

@javidjamae
Copy link

javidjamae commented May 27, 2019

If you pass the onBlur into the getInputProps it works:

      <PlacesAutocomplete
        ...
      >
          { ( { getInputProps, suggestions, getSuggestionItemProps, loading } ) => (
            <div>
              <input
                { ...getInputProps( { onBlur: this.handleBlur  } ) }
            />
           ...

@hibiken
Copy link
Owner

hibiken commented Jun 3, 2019

Thanks @javidjamae for posting this 👍
We made a big change in API when we went from v6 to v7.
Please refer to @javidjamae 's snippet and ignore my snippet from 2017 (unless you're using v6 or below).

@ryanconnaughton
Copy link

If you pass the onBlur into the getInputProps it works:

      <PlacesAutocomplete
        ...
      >
          { ( { getInputProps, suggestions, getSuggestionItemProps, loading } ) => (
            <div>
              <input
                { ...getInputProps( { onBlur: this.handleBlur  } ) }
            />
           ...

Thanks! Just had to remove this. on the newer version and it worked. Assumed it was a React bug for far too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants