Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minLength option #3960

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,6 @@ <h2>Example</h2>

<hr class="bs-docs-separator">


<h2>Usage</h2>

<h3>Via data attributes</h3>
Expand Down Expand Up @@ -1529,6 +1528,12 @@ <h3>Options</h3>
<td>8</td>
<td>The max number of items to display in the dropdown.</td>
</tr>
<tr>
<td>minLength</td>
<td>number</td>
<td>1</td>
<td>The min number of characters entered to start querying.</td>
</tr>
<tr>
<td>matcher</td>
<td>function</td>
Expand Down
6 changes: 4 additions & 2 deletions js/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
this.sorter = this.options.sorter || this.sorter
this.highlighter = this.options.highlighter || this.highlighter
this.updater = this.options.updater || this.updater
this.minLength = this.options.minLength || this.minLength
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't necessary

this.$menu = $(this.options.menu).appendTo('body')
this.source = this.options.source
this.shown = false
Expand Down Expand Up @@ -78,10 +79,10 @@

, lookup: function (event) {
var items

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove the spaces here? thanks!

this.query = this.$element.val()

if (!this.query) {
if (!this.query || this.query.length < this.minLength) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this.options.minLength

return this.shown ? this.hide() : this
}

Expand Down Expand Up @@ -277,6 +278,7 @@
$.fn.typeahead.defaults = {
source: []
, items: 8
, minLength: 1
, menu: '<ul class="typeahead dropdown-menu"></ul>'
, item: '<li><a href="#"></a></li>'
}
Expand Down
20 changes: 20 additions & 0 deletions js/tests/unit/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,24 @@ $(function () {

typeahead.$menu.remove()
})

test("should start querying when minLength is met", function () {
var $input = $('<input />').typeahead({
source: ['aaaa', 'aaab', 'aaac'],
minLength: 3
})
, typeahead = $input.data('typeahead')

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

equals(typeahead.$menu.find('li').length, 0, 'has 0 items in menu')

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

equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')

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