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

allow triggering of 'search' event when clearing the input field #630

Closed
ggcaponetto opened this issue Sep 2, 2019 · 4 comments · Fixed by #992
Closed

allow triggering of 'search' event when clearing the input field #630

ggcaponetto opened this issue Sep 2, 2019 · 4 comments · Fixed by #992

Comments

@ggcaponetto
Copy link

ggcaponetto commented Sep 2, 2019

current behaviour

If the user initializes the library with the following options
const townsSelect= new Choices('#towns', { searchFloor: 0 })
and then clears the input field using the backspace, no search event is triggered.

expected behaviour

search event is triggered with empty string value

proposed fix

at line 933 of https://github.com/jshjohnson/Choices/blob/master/src/scripts/choices.js
change
... if (value && value.length >= searchFloor) {...} ...
to
... if (value !== null && typeof value !=="undefined" && value.length >= searchFloor) {...} ...

@RecuencoJones
Copy link

Logic in _onKeyUp handler should be tweaked as well to call _handleSearch with empty value.

https://github.com/jshjohnson/Choices/blob/master/src/scripts/choices.js#L1207

@bupy7
Copy link

bupy7 commented Oct 15, 2019

Fast and hard fix:

townsSelect.input.addEventListener('keyup', function () {
    console.log(this.value);
});

@alfonsoar
Copy link

@tinovyatkin @jshjohnson is this something you guys have on your radar? are you accepting PRs?

@PredragPeranovic
Copy link

I'm using this hard coded workaround for choices.js 9.0.1 for now.

townSelect.input.element.addEventListener('keyup', (event) => {
        if ((event.which === 8 || event.which === 46) && event.target.value === '' && townSelect.config.searchFloor === 0) {
          // Trigger search event
          var resultCount = townSelect.config.searchChoices ? townSelect._searchChoices(event.target.value) : 0 

          townSelect.passedElement.triggerEvent('search', {
            value: event.target.value,
            resultCount: resultCount
          })
        }
      })

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