Skip to content

Commit 61a9843

Browse files
committed
Fixed an issue where you could click on optgroup label and break the plugin
1 parent efe375d commit 61a9843

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

public/jquery.selectric.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -952,19 +952,16 @@
952952

953953
_this.utils.triggerCallback('BeforeHighlight', _this);
954954

955-
// Parameter index is required
956-
if ( index === undefined ) {
955+
// Parameter index is required and should not be a disabled item
956+
if ( index === undefined || index === -1 || _this.lookupItems[index].disabled ) {
957957
return;
958958
}
959959

960-
// If element is disabled, can't select it
961-
if ( index !== -1 && !_this.lookupItems[index].disabled ) {
962-
$filteredLi
963-
.eq(_this.state.highlightedIdx = index)
964-
.addClass('highlighted');
960+
$filteredLi
961+
.eq(_this.state.highlightedIdx = index)
962+
.addClass('highlighted');
965963

966-
_this.detectItemVisibility(index);
967-
}
964+
_this.detectItemVisibility(index);
968965

969966
_this.utils.triggerCallback('Highlight', _this);
970967
},
@@ -980,9 +977,9 @@
980977

981978
_this.utils.triggerCallback('BeforeSelect', _this, index);
982979

983-
// Don't select disabled items
984-
if (index !== -1 && _this.lookupItems[index].disabled) {
985-
return false;
980+
// Parameter index is required and should not be a disabled item
981+
if ( index === undefined || index === -1 || _this.lookupItems[index].disabled ) {
982+
return;
986983
}
987984

988985
if ( _this.state.multiple ) {

public/jquery.selectric.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.selectric.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -935,19 +935,16 @@
935935

936936
_this.utils.triggerCallback('BeforeHighlight', _this);
937937

938-
// Parameter index is required
939-
if ( index === undefined ) {
938+
// Parameter index is required and should not be a disabled item
939+
if ( index === undefined || index === -1 || _this.lookupItems[index].disabled ) {
940940
return;
941941
}
942942

943-
// If element is disabled, can't select it
944-
if ( index !== -1 && !_this.lookupItems[index].disabled ) {
945-
$filteredLi
946-
.eq(_this.state.highlightedIdx = index)
947-
.addClass('highlighted');
943+
$filteredLi
944+
.eq(_this.state.highlightedIdx = index)
945+
.addClass('highlighted');
948946

949-
_this.detectItemVisibility(index);
950-
}
947+
_this.detectItemVisibility(index);
951948

952949
_this.utils.triggerCallback('Highlight', _this);
953950
},
@@ -963,9 +960,9 @@
963960

964961
_this.utils.triggerCallback('BeforeSelect', _this, index);
965962

966-
// Don't select disabled items
967-
if (index !== -1 && _this.lookupItems[index].disabled) {
968-
return false;
963+
// Parameter index is required and should not be a disabled item
964+
if ( index === undefined || index === -1 || _this.lookupItems[index].disabled ) {
965+
return;
969966
}
970967

971968
if ( _this.state.multiple ) {

test/basic.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ describe('basic suite', function() {
5858
expect($('.selectric-wrapper').find('.selectric-group-label').length).toBe(3);
5959
});
6060

61+
it('should not select <optgroup> label', function() {
62+
loadFixtures('optgroup.html');
63+
64+
select = $('#optgroup');
65+
select.selectric();
66+
67+
$('.selectric').click();
68+
$('.selectric-wrapper').find('.selectric-group-label').click();
69+
expect(select.val()).toBe('0');
70+
});
71+
6172
it('should add class on hover', function() {
6273
$('.selectric').trigger('mouseenter');
6374
expect($('.selectric-wrapper').hasClass('selectric-hover')).toBe(true);
@@ -120,6 +131,14 @@ describe('basic suite', function() {
120131
expect($('.selectric-items').find('li:eq(1)').hasClass('disabled')).toBe(true);
121132
});
122133

134+
it('should not select disabled item', function() {
135+
select.find('option:eq(2)').prop('disabled', true);
136+
select.selectric('refresh');
137+
$('.selectric').click();
138+
$('.selectric-items').find('li:eq(2)').click();
139+
expect(select.val()).toBe('apricot');
140+
});
141+
123142
it('should update label', function() {
124143
$('.selectric').click();
125144
$('.selectric-items').find('li:eq(4)').click();

test/fixtures/optgroup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<select id="optgroup">
2-
<option>Select with optgroup</option>
2+
<option value="0">Select with optgroup</option>
33
<optgroup label="Group 1">
44
<option>Option 1.1</option>
55
</optgroup>

0 commit comments

Comments
 (0)