Choices: allow triggering of 'search' event when clearing the input field

Created on 2 Sep 2019  路  4Comments  路  Source: Choices-js/Choices

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) {...} ...

All 4 comments

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

Fast and hard fix:

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

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

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
          })
        }
      })

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephendolan picture stephendolan  路  3Comments

mpreziuso picture mpreziuso  路  3Comments

Igloczek picture Igloczek  路  3Comments

notflip picture notflip  路  3Comments

alfonsoar picture alfonsoar  路  4Comments