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

Add autoSelect top-level option #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Template.foo.settings = function() {
- `position` (= `top` or `bottom`) specifies if the autocomplete menu should render above or below the cursor. Select based on the placement of your `input`/`textarea` relative to other elements on the page.
- `limit`: Controls how big the autocomplete menu should get.
- `rules`: An array of matching rules for the autocomplete widget, which will be checked in order.
- `autoSelect`: (default `true`) Whether to automatically select the first matching result.

##### Rule Specific Options

Expand Down
17 changes: 13 additions & 4 deletions autocomplete-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class @AutoComplete
constructor: (settings) ->
@limit = settings.limit || 5
@position = settings.position || "bottom"
@autoSelect = if settings.autoSelect is false then false else true

@rules = settings.rules
validateRule(rule) for rule in @rules
Expand Down Expand Up @@ -213,7 +214,8 @@ class @AutoComplete

[ selector, options ] = getFindParams(rule, filter, @limit)

Meteor.defer => @ensureSelection()
if @autoSelect
Meteor.defer => @ensureSelection()

# if server collection, the server has already done the filtering work
return AutoCompleteRecords.find({}, options) if isServerSearch(rule)
Expand All @@ -230,7 +232,8 @@ class @AutoComplete
if showing
Meteor.defer =>
@positionContainer()
@ensureSelection()
if @autoSelect
@ensureSelection()

return showing

Expand Down Expand Up @@ -333,8 +336,11 @@ class @AutoComplete

# Select next item in list
next: ->
firstItem = @tmplInst.$(".-autocomplete-item:first-child")
return unless firstItem.length # Don't try to iterate an empty list
currentItem = @tmplInst.$(".-autocomplete-item.selected")
return unless currentItem.length # Don't try to iterate an empty list
return firstItem.addClass('selected') unless currentItem.length # No element was selected: select the first element

currentItem.removeClass("selected")

next = currentItem.next()
Expand All @@ -345,8 +351,11 @@ class @AutoComplete

# Select previous item in list
prev: ->
lastItem = @tmplInst.$(".-autocomplete-item:last-child")
return unless lastItem.length # Don't try to iterate an empty list
currentItem = @tmplInst.$(".-autocomplete-item.selected")
return unless currentItem.length # Don't try to iterate an empty list
return lastItem.addClass('selected') unless currentItem.length # No element was selected: select the last element

currentItem.removeClass("selected")

prev = currentItem.prev()
Expand Down