diff --git a/tabtest.html b/tabtest.html new file mode 100644 index 0000000..665853d --- /dev/null +++ b/tabtest.html @@ -0,0 +1,28 @@ + + + + + Tabbing test + + + + +

With tabindex

+

+

+

+

Without tabindex

+

+

+

+ + + \ No newline at end of file diff --git a/test.html b/test.html index 366b856..5336aea 100644 --- a/test.html +++ b/test.html @@ -57,6 +57,14 @@ +
+

+

+

+
\ No newline at end of file diff --git a/test.js b/test.js index 54bcd3b..ebf2afd 100644 --- a/test.js +++ b/test.js @@ -472,6 +472,17 @@ describe('zelect', function() { }) }) + describe('Catching tab focus', function() { + beforeEach(function() { + setup('multiple-fields') + $('#select').find('.field2').zelect() + }) + it('focusing on catcher element opens search', function() { + $('#select').find('.focuz').focus() + defaultOpenState() + }) + }) + function ok(bool, msg) { assert.isTrue(bool, msg) } diff --git a/zelect.js b/zelect.js index 6b8af44..dc80504 100644 --- a/zelect.js +++ b/zelect.js @@ -32,6 +32,7 @@ var $zelect = $('
').addClass('zelect') var $selected = $('
').addClass('zelected') var $dropdown = $('
').addClass('dropdown').hide() + var $focusCatcher = $('').addClass('focuz').css({ height: 0, width: 0, border: 'none', display: 'block', padding: 0 }) var $noResults = $('
').addClass('no-results') var $search = $('').addClass('zearch') var $list = $('
    ') @@ -60,7 +61,7 @@ }) $search.keydown(function(e) { switch (e.which) { - case keys.tab: e.preventDefault(); hide(); return; + case keys.tab: hide(); return; case keys.up: e.preventDefault(); listNavigator.prev(); return; case keys.down: e.preventDefault(); listNavigator.next(); return; } @@ -69,12 +70,15 @@ $list.on('click', 'li', function() { selectItem($(this).data('zelect-item')) }) $zelect.mouseenter(function() { $zelect.addClass('hover') }) $zelect.mouseleave(function() { $zelect.removeClass('hover') }) - $zelect.attr("tabindex", $select.attr("tabindex")) + $search.attr("tabindex", $select.attr("tabindex")) + $focusCatcher.attr("tabindex", $select.attr("tabindex")) $zelect.blur(function() { if (!$zelect.hasClass('hover')) hide() }) $search.blur(function() { if (!$zelect.hasClass('hover')) hide() }) $selected.click(toggle) + $focusCatcher.focus(toggle) + $focusCatcher.insertBefore($select) $zelect.insertAfter($select) .append($selected) .append($dropdown.append($('
    ').addClass('zearch-container').append($search).append($noResults)).append($list)) @@ -115,6 +119,7 @@ function toggle() { $dropdown.toggle() + $focusCatcher.hide() $zelect.toggleClass('open') if ($dropdown.is(':visible')) { $search.focus().select() @@ -126,6 +131,7 @@ function hide() { $dropdown.hide() $zelect.removeClass('open') + setTimeout(function(){ $focusCatcher.show() }, 0) } function renderContent($obj, content) {