mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-09 23:01:00 +00:00
add an option to disable going to the next field when tabbing to select a suggestion
This commit is contained in:
parent
be517ccb33
commit
2c26b6c7e2
@ -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`.
|
||||
* `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.
|
||||
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
|
||||
|
||||
##Usage
|
||||
|
||||
|
@ -137,4 +137,54 @@ describe('Autocomplete', function () {
|
||||
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();
|
||||
});
|
||||
});
|
@ -75,7 +75,8 @@
|
||||
noCache: false,
|
||||
onSearchStart: noop,
|
||||
onSearchComplete: noop,
|
||||
containerClass: 'autocomplete-suggestions'
|
||||
containerClass: 'autocomplete-suggestions',
|
||||
tabDisabled: false
|
||||
};
|
||||
|
||||
// Shared variables:
|
||||
@ -257,7 +258,7 @@
|
||||
return;
|
||||
}
|
||||
this.select(this.selectedIndex);
|
||||
if (e.keyCode === keys.TAB) {
|
||||
if (e.keyCode === keys.TAB && this.options.tabDisabled === false) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user