Skip to content

Commit

Permalink
Typeahead now support multiple selections
Browse files Browse the repository at this point in the history
  • Loading branch information
codeimpossible committed Feb 16, 2012
1 parent 9143d8e commit e048303
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
14 changes: 12 additions & 2 deletions js/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
this.$menu = $(this.options.menu).appendTo('body')
this.source = this.options.source
this.shown = false
this.delimiter = this.options.delimiter || this.delimiter
this.listen()
this.mode = this.options.mode || this.mode
this.selections = []
}

Typeahead.prototype = {
Expand All @@ -39,7 +42,11 @@

, select: function () {
var val = this.$menu.find('.active').attr('data-value')
this.$element.val(val)
if( this.mode === 'multiple' ) {
this.selections.push(val)
val = this.selections.join(this.delimiter)
}
this.$element.val( val )
return this.hide()
}

Expand Down Expand Up @@ -68,8 +75,9 @@
var that = this
, items
, q
, input = this.mode === 'multiple' ? this.$element.val().split(this.delimiter) : [this.$element.val()]

this.query = this.$element.val()
this.query = $.trim(input[input.length - 1])

if (!this.query) {
return this.shown ? this.hide() : this
Expand Down Expand Up @@ -251,6 +259,8 @@
, items: 8
, menu: '<ul class="typeahead dropdown-menu"></ul>'
, item: '<li><a href="#"></a></li>'
, delimiter: ','
, mode: 'single'
}

$.fn.typeahead.Constructor = Typeahead
Expand Down
65 changes: 64 additions & 1 deletion js/tests/unit/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,67 @@ $(function () {

typeahead.$menu.remove()
})
})

test("should allow multiple selections", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
, mode: 'multiple'
})
, typeahead = $input.data('typeahead')

$input.val('a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

$input.val( $input.val() + ',a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

equals($input.val(), 'ac,ac', 'input value was correctly set')
ok(!typeahead.$menu.is(':visible'), 'the menu was hidden')

typeahead.$menu.remove()
})

test("should allow user to specify delimiter in options", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
, delimiter: ';'
, mode: 'multiple'
})
, typeahead = $input.data('typeahead')

$input.val('a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

$input.val( $input.val() + ';a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

equals($input.val(), 'ac;ac', 'input value was correctly set')
})

test("should not allow multiple selection if multiple selection mode is not enabled", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
})
, typeahead = $input.data('typeahead')

$input.val('a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

$input.val( $input.val() + ', a')
typeahead.lookup()

$(typeahead.$menu.find('li')[2]).mouseover().click()

equals($input.val(), 'ac', 'input value was correctly set')
})
})

0 comments on commit e048303

Please sign in to comment.