Skip to content

Commit 294c5da

Browse files
Fix bug where query wasn't getting cleared (#57)
When require match is on, the query should get cleared whenever the model changes since an update to the model is the equivalent of an item getting chosen or unchosen. This only happens when requireMatch is on since when its off the implementor might allow free text searching.
1 parent 4a01146 commit 294c5da

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngc-omnibox",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "A modern, flexible, Angular 1.x autocomplete library with limited assumptions.",
55
"main": "dist/ngc-omnibox.js",
66
"scripts": {

spec/tests/angularComponent/ngcOmniboxControllerSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => {
4747
expect(newFieldEl.addEventListener).toHaveBeenCalled();
4848
});
4949

50+
it('should clear out the query when the ngModel changes when a match is required', () => {
51+
omniboxController.ngModel = ['one'];
52+
omniboxController.query = 'my query';
53+
omniboxController.ngModel.push('two');
54+
55+
expect(omniboxController.query).toBe('my query');
56+
57+
omniboxController.requireMatch = true;
58+
omniboxController.ngModel.push('three');
59+
expect(omniboxController.query).toBe('');
60+
});
61+
5062
describe('when populating suggestions via the source function', () => {
5163
it('it should empty out the suggestions when resolving to a falsy value', (done, fail) => {
5264
omniboxController.query = 'my query';

src/angularComponent/ngcOmniboxController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ export default class NgcOmniboxController {
670670
*/
671671
_onNgModelChange() {
672672
this.hasChoices = !!this.multiple && Array.isArray(this._ngModel) && !!this._ngModel.length;
673+
674+
if (this.requireMatch) {
675+
this.query = '';
676+
}
673677
}
674678

675679
_scrollSuggestionIntoView() {

0 commit comments

Comments
 (0)