Last active
December 14, 2015 16:58
-
-
Save schpet/5118516 to your computer and use it in GitHub Desktop.
A collection of bootstrap-typeahead.js hacks: don't autocomplete and have the tab key move your selection (coffeescript & js versions)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.typeahead.Constructor::render = (items) -> | |
items = $(items).map (i, item) => | |
i = $(@options.item).attr("data-value", item) | |
i.find("a").html @highlighter(item) | |
i[0] | |
@$menu.html items | |
this | |
$.fn.typeahead.Constructor::keyup = (e) -> | |
switch e.keyCode | |
when 9, 40, 38, 16, 17, 18 # tab, down, up, shift, ctrl, alt | |
break | |
when 13 # enter | |
return @select() if @shown | |
when 27 # escape | |
return @hide() if @shown | |
else | |
@lookup() | |
e.stopPropagation() | |
e.preventDefault() | |
return | |
$.fn.typeahead.Constructor::move = (e) -> | |
return unless @shown | |
switch e.keyCode | |
when 13, 27 # enter, escape | |
e.preventDefault() | |
when 38 # up arrow | |
e.preventDefault() | |
@prev() | |
when 9, 40 # tab, down arrow | |
e.preventDefault() | |
@next() | |
e.stopPropagation() | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.typeahead.Constructor.prototype.render = function(items) { | |
var _this = this; | |
items = $(items).map(function(i, item) { | |
i = $(_this.options.item).attr("data-value", item); | |
i.find("a").html(_this.highlighter(item)); | |
return i[0]; | |
}); | |
this.$menu.html(items); | |
return this; | |
}; | |
$.fn.typeahead.Constructor.prototype.keyup = function(e) { | |
switch (e.keyCode) { | |
case 9: | |
case 40: | |
case 38: | |
case 16: | |
case 17: | |
case 18: | |
break; | |
case 13: | |
if (this.shown) { | |
return this.select(); | |
} | |
break; | |
case 27: | |
if (this.shown) { | |
return this.hide(); | |
} | |
break; | |
default: | |
this.lookup(); | |
} | |
e.stopPropagation(); | |
e.preventDefault(); | |
}; | |
$.fn.typeahead.Constructor.prototype.move = function(e) { | |
if (!this.shown) { | |
return; | |
} | |
switch (e.keyCode) { | |
case 13: | |
case 27: | |
e.preventDefault(); | |
break; | |
case 38: | |
e.preventDefault(); | |
this.prev(); | |
break; | |
case 9: | |
case 40: | |
e.preventDefault(); | |
this.next(); | |
} | |
e.stopPropagation(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried using this but if you type something and the typeahead is shown, then press enter, everything you typed is cleared instead of submitting the form.
I think it's because of line 22, if the typeahead is shown, then it returns the selected value, but as nothing is selected it returns nothing.
I can't figure out how to submit the form if there is nothing selected and you press enter.