Skip to content

Commit

Permalink
Merge pull request #177 from MrWook/master
Browse files Browse the repository at this point in the history
Clear Live-Search
  • Loading branch information
lordfriend authored Dec 5, 2017
2 parents 8c0a2a9 + d7863d1 commit ac1ff6c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/nya-bs-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', '$compi
};
$document.on('click', outClick);

function reset_search() {
searchBox.children().eq(0)[0].value = "";
var options = dropdownMenu.children(),
length = options.length,
index,
option,
nyaBsOptionNode;
for(index = 0; index < length; index++) {
option = options.eq(index);
if(option.hasClass('nya-bs-option')) {
option.removeClass('not-match');
}
}
noSearchResult.removeClass('show');
nyaBsOptionNode = findFocus(true);

if(nyaBsOptionNode) {
options.removeClass('active');
jqLite(nyaBsOptionNode).addClass('active');
}
}

console.log(dropdownToggle[0]==$element.find('button').eq(0)[0]);

dropdownToggle.on('blur', function() {
Expand All @@ -356,6 +378,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', '$compi
calcMenuSize();
}
if($attrs.liveSearch === 'true' && $element.hasClass('open')) {
reset_search();
searchBox.children().eq(0)[0].focus();
nyaBsOptionNode = findFocus(true);
if(nyaBsOptionNode) {
Expand Down
63 changes: 63 additions & 0 deletions test/spec/live-search-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,67 @@ describe('features about live search related, etc;', function() {
check();
});

it('should clear live-search', function() {
// get amount of available li
function get_amount() {
var list = selectElement.find('ul').children();
var i, k, length = list.length,
liElement,
classList,
check,
counter = 0;
for(i = 0; i < length; i++) {
liElement = list.eq(i);
if(liElement.hasClass('nya-bs-option')) {
classList = liElement[0].classList;
check = false;
for(k = 0; k < classList.length; k++){
if(classList[k] === 'not-match'){
check = true;
}
}
if(check === false){
counter++;
}
}
}
return counter;
}

// string array
$scope.options = options;
$scope.model = [];
$scope.$digest();
var selectElement = angular.element('<ol class="nya-bs-select" ng-model="model" live-search="true">' +
'<li nya-bs-option="option in options">' +
'<a>{{option}}</a>' +
'</li>' +
'</ol>');
$compile(selectElement)($scope);

$scope.$digest();
//open dropdown
selectElement.find('button')[0].click();
//get amount of options
var before_change = get_amount(selectElement, 'amount');
//set live-search
selectElement.find('input').val('o');
//trigger live-search
selectElement.find('input').triggerHandler('input');
//trigger digest cycle
$scope.$digest();
//check if value is set
expect(selectElement.find('input').val()).toBe('o');
//check amount of options vs new amount
expect(get_amount(selectElement, 'amount')).not.toBe(before_change);
//close dropdown
selectElement.find('button')[0].click();
//open dropdown
selectElement.find('button')[0].click();
//check if value is empty
expect(selectElement.find('input').val()).toBe('');
//check amount of options vs new amount
expect(get_amount(selectElement, 'amount')).toBe(before_change);
});

});

0 comments on commit ac1ff6c

Please sign in to comment.