Skip to content

Commit

Permalink
Merge pull request Choices-js#992 from Choices-js/searchfloor0
Browse files Browse the repository at this point in the history
Trigger search when clearing the input field with search floor of 0
  • Loading branch information
mtriff authored Jan 1, 2022
2 parents 781c729 + 25a7ed0 commit 7758226
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/scripts/choices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,21 @@ describe('choices', () => {

instance._onKeyUp({ target: null, keyCode: null });
});

it('is fired with a searchFloor of 0', (done) => {
instance.config.searchFloor = 0;
instance.input.value = '';
instance.input.focus();
instance.passedElement.element.addEventListener('search', (event) => {
expect(event.detail).to.eql({
value: instance.input.value,
resultCount: 0,
});
done();
});

instance._onKeyUp({ target: null, keyCode: null });
});
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ class Choices implements Choices {
}

_handleSearch(value: string): void {
if (!value || !this.input.isFocussed) {
if (!this.input.isFocussed) {
return;
}

Expand All @@ -1239,7 +1239,11 @@ class Choices implements Choices {
const hasUnactiveChoices = choices.some((option) => !option.active);

// Check that we have a value to search and the input was an alphanumeric character
if (value && value.length >= searchFloor) {
if (
value !== null &&
typeof value !== 'undefined' &&
value.length >= searchFloor
) {
const resultCount = searchChoices ? this._searchChoices(value) : 0;
// Trigger search event
this.passedElement.triggerEvent(EVENTS.search, {
Expand Down

0 comments on commit 7758226

Please sign in to comment.