Skip to content

Commit ac1ff6c

Browse files
authored
Merge pull request #177 from MrWook/master
Clear Live-Search
2 parents 8c0a2a9 + d7863d1 commit ac1ff6c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/nya-bs-select.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,28 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', '$compi
342342
};
343343
$document.on('click', outClick);
344344

345+
function reset_search() {
346+
searchBox.children().eq(0)[0].value = "";
347+
var options = dropdownMenu.children(),
348+
length = options.length,
349+
index,
350+
option,
351+
nyaBsOptionNode;
352+
for(index = 0; index < length; index++) {
353+
option = options.eq(index);
354+
if(option.hasClass('nya-bs-option')) {
355+
option.removeClass('not-match');
356+
}
357+
}
358+
noSearchResult.removeClass('show');
359+
nyaBsOptionNode = findFocus(true);
360+
361+
if(nyaBsOptionNode) {
362+
options.removeClass('active');
363+
jqLite(nyaBsOptionNode).addClass('active');
364+
}
365+
}
366+
345367
console.log(dropdownToggle[0]==$element.find('button').eq(0)[0]);
346368

347369
dropdownToggle.on('blur', function() {
@@ -356,6 +378,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', '$compi
356378
calcMenuSize();
357379
}
358380
if($attrs.liveSearch === 'true' && $element.hasClass('open')) {
381+
reset_search();
359382
searchBox.children().eq(0)[0].focus();
360383
nyaBsOptionNode = findFocus(true);
361384
if(nyaBsOptionNode) {

test/spec/live-search-feature.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,67 @@ describe('features about live search related, etc;', function() {
7373
check();
7474
});
7575

76+
it('should clear live-search', function() {
77+
// get amount of available li
78+
function get_amount() {
79+
var list = selectElement.find('ul').children();
80+
var i, k, length = list.length,
81+
liElement,
82+
classList,
83+
check,
84+
counter = 0;
85+
for(i = 0; i < length; i++) {
86+
liElement = list.eq(i);
87+
if(liElement.hasClass('nya-bs-option')) {
88+
classList = liElement[0].classList;
89+
check = false;
90+
for(k = 0; k < classList.length; k++){
91+
if(classList[k] === 'not-match'){
92+
check = true;
93+
}
94+
}
95+
if(check === false){
96+
counter++;
97+
}
98+
}
99+
}
100+
return counter;
101+
}
102+
103+
// string array
104+
$scope.options = options;
105+
$scope.model = [];
106+
$scope.$digest();
107+
var selectElement = angular.element('<ol class="nya-bs-select" ng-model="model" live-search="true">' +
108+
'<li nya-bs-option="option in options">' +
109+
'<a>{{option}}</a>' +
110+
'</li>' +
111+
'</ol>');
112+
$compile(selectElement)($scope);
113+
114+
$scope.$digest();
115+
//open dropdown
116+
selectElement.find('button')[0].click();
117+
//get amount of options
118+
var before_change = get_amount(selectElement, 'amount');
119+
//set live-search
120+
selectElement.find('input').val('o');
121+
//trigger live-search
122+
selectElement.find('input').triggerHandler('input');
123+
//trigger digest cycle
124+
$scope.$digest();
125+
//check if value is set
126+
expect(selectElement.find('input').val()).toBe('o');
127+
//check amount of options vs new amount
128+
expect(get_amount(selectElement, 'amount')).not.toBe(before_change);
129+
//close dropdown
130+
selectElement.find('button')[0].click();
131+
//open dropdown
132+
selectElement.find('button')[0].click();
133+
//check if value is empty
134+
expect(selectElement.find('input').val()).toBe('');
135+
//check amount of options vs new amount
136+
expect(get_amount(selectElement, 'amount')).toBe(before_change);
137+
});
138+
76139
});

0 commit comments

Comments
 (0)