umhan35 / react-native-search-bar

The high-quality iOS native search bar for react native.
MIT License
867 stars 209 forks source link

function onCancelButtonPress not being called #163

Closed marwan2 closed 5 years ago

marwan2 commented 5 years ago

First I'm using the search to call an API, not client side search. So the behaviour when I click X button to clear search is to go to function onCancelButtonPress

this is to call again the API after setState of searchKeyword to empty. This behviour not working.

SearchBar = () => {
    return (
      <SearchBar
        text={this.state.search}
        ref={bar => {
          this.searchBar = bar;
        }}
        placeholder="Search"
        barStyle="black"
        showsCancelButton={true}
        showsCancelButtonWhileEditing={false}
        onSearchButtonPress={this.onSearch}
        onCancelButtonPress={this.onCancelButtonPress}
      />
    );
  };

and the cancel handler:

onCancelButtonPress = (e) => {
    console.log(e.nativeEvent.text);
    this.setState(
      {
        search: '',
        page: 1,
        requests: [],
      },
      () => {
        this._getRequests();
      },
    );
  };
iRoachie commented 5 years ago

You're listening for an event that will never happen. onCancelButtonPress only fires when tapping the Cancel button, there's no special event for tapping X, just use onChange

marwan2 commented 5 years ago

Why don't you add event for pressing X button ?

iRoachie commented 5 years ago

UISearchBar doesn't have an event for this natively. The only way is to check if the string is empty in the onChange

marwan2 commented 5 years ago

OK I'll try it, thanks