Skip to content

Commit

Permalink
Have Carousel ignore keyboard events from <input>s or <textarea>s; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cvrebert committed Nov 5, 2014
1 parent 425a985 commit 469dc3a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
}

Carousel.prototype.keydown = function (e) {
if (/input|textarea/i.test(e.target.tagName)) return
switch (e.which) {
case 37: this.prev(); break
case 39: this.next(); break
Expand Down
42 changes: 42 additions & 0 deletions js/tests/unit/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,48 @@ $(function () {
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press')
})

test('should ignore keyboard events within <input>s and <textarea>s', function () {
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
+ '<div class="carousel-inner">'
+ '<div id="first" class="item active">'
+ '<img alt="">'
+ '<input type="text" id="in-put">'
+ '<textarea id="text-area"></textarea>'
+ '</div>'
+ '<div id="second" class="item">'
+ '<img alt="">'
+ '</div>'
+ '<div id="third" class="item">'
+ '<img alt="">'
+ '</div>'
+ '</div>'
+ '</div>'
var $template = $(templateHTML)
var $input = $template.find('#in-put')
var $textarea = $template.find('#text-area')

strictEqual($input.length, 1, 'found <input>')
strictEqual($textarea.length, 1, 'found <textarea>')

$template.bootstrapCarousel()

strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')


$input.trigger($.Event('keydown', { which: 39 }))
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <input>')

$input.trigger($.Event('keydown', { which: 37 }))
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>')


$textarea.trigger($.Event('keydown', { which: 39 }))
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <textarea>')

$textarea.trigger($.Event('keydown', { which: 37 }))
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>')
})

test('should only add mouseenter and mouseleave listeners when not on mobile', function () {
var isMobile = 'ontouchstart' in document.documentElement
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-pause="hover">'
Expand Down

0 comments on commit 469dc3a

Please sign in to comment.