Skip to content

Commit 9123ead

Browse files
authored
Merge pull request #186 from nlopezcenteno/master
Alternative texts for search using the option attribute data-alt
2 parents 1d93433 + f712f9d commit 9123ead

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/jquery.selectric.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@
472472
className : $elm.prop('class'),
473473
text : $elm.html(),
474474
slug : $.trim(_this.utils.replaceDiacritics($elm.html())),
475+
alt : $elm.attr('data-alt'),
475476
selected : $elm.prop('selected'),
476477
disabled : isDisabled
477478
};
@@ -627,10 +628,26 @@
627628
if ( val.length ) {
628629
// Search in select options
629630
$.each(_this.items, function(i, elm) {
630-
if ( !elm.disabled && searchRegExp.test(elm.text) || searchRegExp.test(elm.slug) ) {
631+
if (elm.disabled) {
632+
return;
633+
}
634+
if (searchRegExp.test(elm.text) || searchRegExp.test(elm.slug)) {
631635
_this.highlight(i);
632636
return;
633637
}
638+
if (!elm.alt) {
639+
return;
640+
}
641+
var altItems = elm.alt.split('|');
642+
for (var ai = 0; ai < altItems.length; ai++) {
643+
if (!altItems[ai]) {
644+
break;
645+
}
646+
if (searchRegExp.test(altItems[ai].trim())) {
647+
_this.highlight(i);
648+
return;
649+
}
650+
}
634651
});
635652
}
636653
});

test/basic.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,52 @@ describe('basic suite', function() {
8080
expect(select.val()).toBe('banana');
8181
});
8282

83+
it('should not search a disabled option', function() {
84+
select.find('option:eq(4)').prop('disabled', 'disabled');
85+
select.selectric('refresh');
86+
$('.selectric-input').val('banana').trigger('input');
87+
$('.selectric-items').find('.highlighted').lenght;
88+
expect($('.selectric-items').find('.highlighted').length).toBe(0);
89+
});
90+
91+
it('should search alternative text', function () {
92+
select.find('option:eq(6)').attr('data-alt', 'alt blackberry');
93+
select.selectric('refresh');
94+
$('.selectric-input').val('alt blackberry').trigger('input');
95+
$('.selectric-items').find('.highlighted').click();
96+
expect(select.val()).toBe('blackberry');
97+
});
98+
99+
it('should search alternative text with separator', function () {
100+
select.find('option:eq(6)').attr('data-alt', 'alt blackberry | another berry');
101+
select.selectric('refresh');
102+
$('.selectric-input').val('alt blackberry').trigger('input');
103+
$('.selectric-items').find('.highlighted').click();
104+
expect(select.val()).toBe('blackberry');
105+
});
106+
107+
it('should search alternative text with separator 2', function () {
108+
select.find('option:eq(6)').attr('data-alt', 'alt blackberry | another berry');
109+
select.selectric('refresh');
110+
$('.selectric-input').val('another berry').trigger('input');
111+
$('.selectric-items').find('.highlighted').click();
112+
expect(select.val()).toBe('blackberry');
113+
});
114+
115+
it('should skip blank alternative text', function () {
116+
select.find('option:eq(6)').attr('data-alt', '');
117+
select.selectric('refresh');
118+
$('.selectric-input').val('a text that does not exist').trigger('input');
119+
expect($('.selectric-items').find('.highlighted').length).toBe(0);
120+
});
121+
122+
it('should skip blank alternative text with separator', function () {
123+
select.find('option:eq(6)').attr('data-alt', '|');
124+
select.selectric('refresh');
125+
$('.selectric-input').val('a text that does not exist').trigger('input');
126+
expect($('.selectric-items').find('.highlighted').length).toBe(0);
127+
});
128+
83129
it('highlight() should return undefined if index is undefined', function () {
84130
expect(select.data('selectric').highlight(undefined)).toBe(undefined);
85131
});

0 commit comments

Comments
 (0)