2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-22 21:05:12 +00:00

Merge pull request #20 from jisaac01/optional_tabbing

add an option to disable going to the next field when tabbing to select ...
This commit is contained in:
Tomas Kirda 2013-01-04 17:34:55 -08:00
commit 714ccefadb
3 changed files with 54 additions and 2 deletions

View File

@ -31,6 +31,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `noCache`: Boolean value indicating whether to cache suggestion results. Default `true`. * `noCache`: Boolean value indicating whether to cache suggestion results. Default `true`.
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element. * `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element. * `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
##Usage ##Usage

View File

@ -137,4 +137,54 @@ describe('Autocomplete', function () {
expect(completeQuery).toBe('A'); expect(completeQuery).toBe('A');
}); });
}); });
it('Should should not preventDefault when tabDisabled is set to false', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: false});
input.value = 'Jam';
autocomplete.onValueChange();
var event = $.Event('keydown');
event.keyCode = 9; // the tab keycode
spyOn(event, 'stopImmediatePropagation');
spyOn(event, 'preventDefault');
spyOn(autocomplete, 'suggest');
expect(autocomplete.visible).toBe(true);
expect(autocomplete.disabled).toBe(undefined);
expect(autocomplete.selectedIndex).not.toBe(-1);
$(input).trigger(event);
expect(event.stopImmediatePropagation).not.toHaveBeenCalled();
expect(event.preventDefault).not.toHaveBeenCalled();
expect(autocomplete.suggest).not.toHaveBeenCalled();
});
it('Should should preventDefault when tabDisabled is set to true', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: true});
input.value = 'Jam';
autocomplete.onValueChange();
var event = $.Event('keydown');
event.keyCode = 9; // the tab keycode
spyOn(event, 'stopImmediatePropagation');
spyOn(event, 'preventDefault');
spyOn(autocomplete, 'suggest');
expect(autocomplete.visible).toBe(true);
expect(autocomplete.disabled).toBe(undefined);
expect(autocomplete.selectedIndex).not.toBe(-1);
$(input).trigger(event);
expect(event.stopImmediatePropagation).toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
expect(autocomplete.suggest).not.toHaveBeenCalled();
});
}); });

View File

@ -75,7 +75,8 @@
noCache: false, noCache: false,
onSearchStart: noop, onSearchStart: noop,
onSearchComplete: noop, onSearchComplete: noop,
containerClass: 'autocomplete-suggestions' containerClass: 'autocomplete-suggestions',
tabDisabled: false
}; };
// Shared variables: // Shared variables:
@ -257,7 +258,7 @@
return; return;
} }
this.select(this.selectedIndex); this.select(this.selectedIndex);
if (e.keyCode === keys.TAB) { if (e.keyCode === keys.TAB && this.options.tabDisabled === false) {
return; return;
} }
break; break;