Skip to content

Commit b5d6518

Browse files
committed
Added support for label tags. Fix #103
1 parent 0b38bf4 commit b5d6518

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

public/jquery.selectric.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@
550550
_this.state.opened ? _this.close() : _this.open(e);
551551
});
552552

553+
// Translate original element focus event to dummy input
554+
_this.$element.on('focus' + bindSufix, function() {
555+
_this.elements.input.focus();
556+
});
557+
553558
_this.elements.input
554559
.prop({ tabindex: _this.originalTabindex, disabled: false })
555560
.on('keydown' + bindSufix, $.proxy(_this.handleKeys, _this))
@@ -788,9 +793,13 @@
788793
_this.elements.input.focus();
789794
}
790795

791-
$doc
792-
.on('click' + bindSufix, $.proxy(_this.close, _this))
793-
.on('scroll' + bindSufix, $.proxy(_this.isInViewport, _this));
796+
// Delayed binds events on Document to make label clicks work
797+
setTimeout(function() {
798+
$doc
799+
.on('click' + bindSufix, $.proxy(_this.close, _this))
800+
.on('scroll' + bindSufix, $.proxy(_this.isInViewport, _this));
801+
}, 1);
802+
794803
_this.isInViewport();
795804

796805
// Prevent window scroll when using mouse wheel inside items box

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.

public/selectric.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
.selectric-hide-select select {
102102
position: absolute;
103103
left: -100%;
104-
display: none;
105104
}
106105

107106
.selectric-input {

src/jquery.selectric.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@
533533
_this.state.opened ? _this.close() : _this.open(e);
534534
});
535535

536+
// Translate original element focus event to dummy input
537+
_this.$element.on('focus' + bindSufix, function() {
538+
_this.elements.input.focus();
539+
});
540+
536541
_this.elements.input
537542
.prop({ tabindex: _this.originalTabindex, disabled: false })
538543
.on('keydown' + bindSufix, $.proxy(_this.handleKeys, _this))
@@ -771,9 +776,13 @@
771776
_this.elements.input.focus();
772777
}
773778

774-
$doc
775-
.on('click' + bindSufix, $.proxy(_this.close, _this))
776-
.on('scroll' + bindSufix, $.proxy(_this.isInViewport, _this));
779+
// Delayed binds events on Document to make label clicks work
780+
setTimeout(function() {
781+
$doc
782+
.on('click' + bindSufix, $.proxy(_this.close, _this))
783+
.on('scroll' + bindSufix, $.proxy(_this.isInViewport, _this));
784+
}, 1);
785+
777786
_this.isInViewport();
778787

779788
// Prevent window scroll when using mouse wheel inside items box

src/selectric.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ $selectric-inner-height: $selectric-height - ($selectric-border-width * 2) !d
107107
select {
108108
position: absolute;
109109
left: -100%;
110-
display: none;
111110
}
112111
}
113112

test/basic.spec.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures */
3+
14
'use strict';
25

36
describe('basic suite', function() {
4-
var select;
7+
var select = false;
58

69
beforeEach(function() {
710
jasmine.getFixtures().fixturesPath = 'base/test/fixtures';
@@ -28,12 +31,12 @@ describe('basic suite', function() {
2831
});
2932

3033
it('should hide original <select>', function() {
31-
expect(select.is(':visible')).toBe(false);
34+
expect(select.parent().is(':visible')).toBe(false);
3235
});
3336

3437
it('should add disabled class', function() {
3538
select.prop('disabled', true);
36-
select.selectric('refresh')
39+
select.selectric('refresh');
3740
expect($('.selectric-wrapper').hasClass('selectric-disabled')).toBe(true);
3841
});
3942

@@ -74,7 +77,7 @@ describe('basic suite', function() {
7477
});
7578

7679
it('should be able to change button markup', function() {
77-
var char = String.fromCharCode(0x30A0 + Math.random() * (0x30FF-0x30A0+1));
80+
var char = String.fromCharCode(0x30A0 + Math.random() * (0x30FF - 0x30A0 + 1));
7881

7982
select.selectric({
8083
arrowButtonMarkup: '<b class="button">' + char + '</b>',
@@ -97,14 +100,14 @@ describe('basic suite', function() {
97100
var len = $('.selectric-items').find('li').length;
98101
select.append('<option>New</option>');
99102
select.selectric('refresh');
100-
expect($('.selectric-items').find('li').length).toBe(len+1);
103+
expect($('.selectric-items').find('li').length).toBe(len + 1);
101104
});
102105

103106
it('should remove items', function() {
104107
var len = $('.selectric-items').find('li').length;
105108
select.find('option:eq(1)').remove();
106109
select.selectric('refresh');
107-
expect($('.selectric-items').find('li').length).toBe(len-1);
110+
expect($('.selectric-items').find('li').length).toBe(len - 1);
108111
});
109112

110113
it('should disable items', function() {
@@ -136,7 +139,7 @@ describe('basic suite', function() {
136139

137140
it('should have custom option item text', function() {
138141
select.selectric({
139-
optionsItemBuilder: function(itemData, element, index) {
142+
optionsItemBuilder: function(itemData) {
140143
return '<span>' + itemData.text + '</span>';
141144
}
142145
});
@@ -184,9 +187,14 @@ describe('basic suite', function() {
184187
});
185188

186189
it('should prevent default event action on mousedown', function() {
187-
var event = $.Event('mousedown');
190+
var event = new $.Event('mousedown');
188191
$('.selectric').click();
189192
$('.selectric-wrapper').find('li').eq(2).trigger(event);
190193
expect(event.isDefaultPrevented()).toBeTruthy();
191194
});
195+
196+
it('should open on label click', function() {
197+
$('label').click();
198+
expect($('.selectric-wrapper').hasClass('selectric-open')).toBe(true);
199+
});
192200
});

test/fixtures/basic.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<label for="basic">Basic</label>
12
<select id="basic">
23
<option value=""></option>
34
<option value="loooooooooooooong-option">Loooooooooooooong option</option>

0 commit comments

Comments
 (0)