Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing override on blur after auto-match #476

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions angucomplete-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
}
else {
mousedownOn = event.target.className;
if(event.target.innerText === scope.textNoResults) {
scope.hideResults(event);
}
}
});

Expand Down Expand Up @@ -185,12 +188,14 @@
}

function setInputString(str) {
callOrAssign({originalObject: str});
if(!scope.exactMatch) {
callOrAssign({originalObject: str});

if (scope.clearSelected) {
scope.searchStr = null;
if (scope.clearSelected) {
scope.searchStr = null;
}
clearResults();
}
clearResults();
}

function extractTitle(data) {
Expand Down Expand Up @@ -398,7 +403,7 @@
});
}
} else if (which === KEY_TAB) {
if (scope.results && scope.results.length > 0 && scope.showDropdown) {
if (scope.results && scope.showDropdown) {
if (scope.currentIndex === -1 && scope.overrideSuggestions) {
// intentionally not sending event so that it does not
// prevent default tab behavior
Expand All @@ -416,7 +421,7 @@
// no results
// intentionally not sending event so that it does not
// prevent default tab behavior
if (scope.searchStr && scope.searchStr.length > 0) {
if (scope.searchStr) {
handleOverrideSuggestions();
}
}
Expand Down Expand Up @@ -542,10 +547,12 @@
if (!str) { return false; }
for(var key in obj){
if(obj[key].toLowerCase() === str.toLowerCase()){
scope.exactMatch = true;
scope.selectResult(result);
return true;
}
}
scope.exactMatch = false;
return false;
}

Expand Down Expand Up @@ -576,7 +583,7 @@
function processResults(responseData, str) {
var i, description, image, text, formattedText, formattedDesc;

if (responseData && responseData.length > 0) {
if (responseData) {
scope.results = [];

for (i = 0; i < responseData.length; i++) {
Expand Down Expand Up @@ -658,7 +665,7 @@
hideTimer = $timeout(function() {
clearResults();
scope.$apply(function() {
if (scope.searchStr && scope.searchStr.length > 0) {
if (scope.searchStr) {
inputField.val(scope.searchStr);
}
});
Expand All @@ -670,7 +677,7 @@
}

if (scope.overrideSuggestions) {
if (scope.searchStr && scope.searchStr.length > 0 && scope.currentIndex === -1) {
if (scope.searchStr && scope.currentIndex === -1) {
handleOverrideSuggestions();
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/angucomplete-alt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,34 @@ describe('angucomplete-alt', function() {

expect(element.isolateScope().searchStr).toBe(null);
});

it('should not override on tab keydown when a selection has auto-matched and input text has not changed', function() {
var element = angular.element('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" minlength="1" override-suggestions="true" auto-match="true"/>');
$scope.selectedCountry = undefined;
$scope.countries = [
{name: 'Afghanistan', code: 'AF'},
{name: 'Aland Islands', code: 'AX'},
{name: 'Albania', code: 'AL'}
];
$compile(element)($scope);
$scope.$digest();

var inputField = element.find('#ex1_value');
var e = $.Event('keyup');
e.which = 97; // letter: a

inputField.val('Albania');
inputField.trigger('input');
inputField.trigger(e);
$timeout.flush();
expect($scope.selectedCountry.originalObject).toEqual($scope.countries[2]);

var eKeydown = $.Event('keydown');
eKeydown.which = KEY_TAB;
inputField.trigger(eKeydown);
inputField.blur();
expect($scope.selectedCountry.originalObject).toEqual($scope.countries[2]);
});
});

describe('selectedObject callback', function() {
Expand Down