question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SearchBar onCancel not firing

See original GitHub issue

Explain what you did

Added a SearchBar component to my Expo app.

Expected behavior

Based on the documentation, I expected my callback to be fired when pressing the Cancel button that appears alongside the search when there’s text in the input.

Describe the bug

The function passed to onCancel doesn’t fire. Instead, tapping the Cancel button just appears to blur the input. This forces the keyboard to close and focus to be lost, leaving any text in the input.

I get that I can pass showCancel to the component and that this will always show the Cancel button (which can then be pressed to fire the callback) but this kind of defeats the object of having it there in the first place and doesn’t work in the same way a native iOS search bar works (try searching in Settings – you can tap cancel for it to clear and close whereas the small clear button in the input just clears but keeps focus). If it has to stay like this with your component, it’d be great if you could adjust the section on onCancel as right now it doesn’t mention a potential requirement of showCancel being true.

Basically, I would’ve liked to be able to press Cancel and have my callback clear the input and blur focus in one go as opposed to having to tap the input once to blur (and hide Cancel) to only then be able to tap the clear icon.

To Reproduce

const handleCancelSearch = () => {
  // Do something...
}
<SearchBar
  platform="ios"
  placeholder={"..."}
  onChangeText={handleSearch}
  onCancel={handleCancelSearch}
  lightTheme={true}
  value={searchQuery}
/>

Your Environment (please complete the following information):

software version
react-native-elements ^1.2.7
react-native Expo SDK 35.0.0

Thank you for all the hard work you and the team put into React Native Elements! It’s awesome.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
oxyiicommented, Nov 16, 2019

@darrylyoung Reproduced. This is not a SearchBar issue exactly. It’s ScrollView and ScrollView-based components (such a FlatList) behavior.

<ScrollView keyboardShouldPersistTaps="handled"> {/* or "always" */}
  <SearchBar ... />
</ScrollView>

You cannot tap Cancel button without keyboardShouldPersistTaps because SearchBar (TextInput) onBlur func make Cancel button hidden early

1reaction
darrylyoungcommented, Nov 16, 2019

Got it. Thank for you the explanation! I’m new to React Native so learning this as I go. Have a nice weekend.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Search Bar onCancel method doesn't fire
When I use Search Bar component from React Native Elements library, I am unable to capture 'onCancel' property. Code is provided below. < ......
Read more >
SearchBar - React Native Elements
SearchBars are used to search or filter items. ... onCancel ​. callback fired when pressing the cancel button (iOS) or the back icon...
Read more >
ion-searchbar - Ionic Framework
Search bars represent a text field that can be used to search through a collection. Learn to input Ion-Search Bar as an icon...
Read more >
What event is triggered when pressing the clear/cancel in a ...
As the title suggests, I am trying to find out what event is triggered when pressing the little cross in SearchBar.
Read more >
Xamarin.Forms SearchBar: which event does the Cancel ...
Hi! I have been trying to grab the event that the Cancel-button on the Xamarin.Forms.SearchBar fires, but with no luck. It doesnt ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found