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 option for not selecting first list item by default, unselect with arrow keys #54

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
82 changes: 47 additions & 35 deletions dist/horsey.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/horsey.min.js

Large diffs are not rendered by default.

80 changes: 46 additions & 34 deletions horsey.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ var doc = document;
var docElement = doc.documentElement;

function horsey(el) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var setAppends = options.setAppends;
var _set = options.set;
var filter = options.filter;
var source = options.source;
var _options$cache = options.cache;
var cache = _options$cache === undefined ? {} : _options$cache;
var predictNextSearch = options.predictNextSearch;
var renderItem = options.renderItem;
var renderCategory = options.renderCategory;
var blankSearch = options.blankSearch;
var appendTo = options.appendTo;
var anchor = options.anchor;
var debounce = options.debounce;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var setAppends = options.setAppends,
_set = options.set,
filter = options.filter,
source = options.source,
_options$cache = options.cache,
cache = _options$cache === undefined ? {} : _options$cache,
predictNextSearch = options.predictNextSearch,
renderItem = options.renderItem,
renderCategory = options.renderCategory,
blankSearch = options.blankSearch,
appendTo = options.appendTo,
anchor = options.anchor,
debounce = options.debounce;

var caching = options.cache !== false;
if (!source) {
Expand Down Expand Up @@ -97,6 +97,7 @@ function horsey(el) {
noMatchesText: options.noMatches,
blankSearch: blankSearch,
debounce: debounce,
selectByDefault: options.selectByDefault !== false,
set: function set(s) {
if (setAppends !== true) {
el.value = '';
Expand All @@ -116,8 +117,8 @@ function horsey(el) {
return data.query.length;
}
function sourceFunction(data, done) {
var query = data.query;
var limit = data.limit;
var query = data.query,
limit = data.limit;

if (!options.blankSearch && query.length === 0) {
done(null, [], true);return;
Expand Down Expand Up @@ -167,25 +168,26 @@ function horsey(el) {
}

function autocomplete(el) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var o = options;
var parent = o.appendTo || doc.body;
var getText = o.getText;
var getValue = o.getValue;
var form = o.form;
var source = o.source;
var noMatches = o.noMatches;
var noMatchesText = o.noMatchesText;
var _o$highlighter = o.highlighter;
var highlighter = _o$highlighter === undefined ? true : _o$highlighter;
var _o$highlightCompleteW = o.highlightCompleteWords;
var highlightCompleteWords = _o$highlightCompleteW === undefined ? true : _o$highlightCompleteW;
var _o$renderItem = o.renderItem;
var renderItem = _o$renderItem === undefined ? defaultItemRenderer : _o$renderItem;
var _o$renderCategory = o.renderCategory;
var renderCategory = _o$renderCategory === undefined ? defaultCategoryRenderer : _o$renderCategory;
var setAppends = o.setAppends;
var getText = o.getText,
getValue = o.getValue,
form = o.form,
source = o.source,
noMatches = o.noMatches,
noMatchesText = o.noMatchesText,
_o$highlighter = o.highlighter,
highlighter = _o$highlighter === undefined ? true : _o$highlighter,
_o$highlightCompleteW = o.highlightCompleteWords,
highlightCompleteWords = _o$highlightCompleteW === undefined ? true : _o$highlightCompleteW,
_o$renderItem = o.renderItem,
renderItem = _o$renderItem === undefined ? defaultItemRenderer : _o$renderItem,
_o$renderCategory = o.renderCategory,
renderCategory = _o$renderCategory === undefined ? defaultCategoryRenderer : _o$renderCategory,
setAppends = o.setAppends,
selectByDefault = o.selectByDefault;

var limit = typeof o.limit === 'number' ? o.limit : Infinity;
var userFilter = o.filter || defaultFilter;
Expand Down Expand Up @@ -698,6 +700,10 @@ function autocomplete(el) {
var next = up ? 'previousSibling' : 'nextSibling';
var prev = up ? 'nextSibling' : 'previousSibling';
var li = findNext();
if (li === null && !selectByDefault) {
unselect();
return;
}
select(li);

if (hidden(li)) {
Expand All @@ -723,6 +729,12 @@ function autocomplete(el) {
return findList(cat[next])[first];
}
}
if (!selectByDefault) {
if (!up && selection === null) {
return findList(categories[first])[first];
}
return null;
}
return findList(categories[first])[first];
}
}
Expand Down Expand Up @@ -809,10 +821,10 @@ function autocomplete(el) {
} else {
hideNoResults();
}
if (!selection) {
if (!selection && selectByDefault) {
move();
}
if (!selection && !nomatch) {
if (!selection && !nomatch && selectByDefault) {
hide();
}
function walkCategories() {
Expand Down
18 changes: 15 additions & 3 deletions horsey.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function horsey (el, options = {}) {
noMatchesText: options.noMatches,
blankSearch,
debounce,
selectByDefault: options.selectByDefault !== false,
set (s) {
if (setAppends !== true) {
el.value = '';
Expand Down Expand Up @@ -148,7 +149,8 @@ function autocomplete (el, options = {}) {
highlightCompleteWords = true,
renderItem = defaultItemRenderer,
renderCategory = defaultCategoryRenderer,
setAppends
setAppends,
selectByDefault
} = o;
const limit = typeof o.limit === 'number' ? o.limit : Infinity;
const userFilter = o.filter || defaultFilter;
Expand Down Expand Up @@ -531,6 +533,10 @@ function autocomplete (el, options = {}) {
const next = up ? 'previousSibling' : 'nextSibling';
const prev = up ? 'nextSibling' : 'previousSibling';
const li = findNext();
if (li === null && !selectByDefault) {
unselect();
return;
}
select(li);

if (hidden(li)) {
Expand All @@ -556,6 +562,12 @@ function autocomplete (el, options = {}) {
return findList(cat[next])[first];
}
}
if (!selectByDefault) {
if (!up && selection === null) {
return findList(categories[first])[first];
}
return null;
}
return findList(categories[first])[first];
}
}
Expand Down Expand Up @@ -642,10 +654,10 @@ function autocomplete (el, options = {}) {
} else {
hideNoResults();
}
if (!selection) {
if (!selection && selectByDefault) {
move();
}
if (!selection && !nomatch) {
if (!selection && !nomatch && selectByDefault) {
hide();
}
function walkCategories () {
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ By default, items are rendered using the text for a `suggestion`. You can custom

By default, categories are rendered using just their `data.title`. You can customize this behavior by setting `autocomplete.renderCategory` to a function that receives `div, data` parameters. The `div` is a DOM element and the `data` is the full category data object, including the `list` of suggestions. After you customize the `div`, the list of suggestions for the category will be appended to `div`.

## `selectByDefault`

By default, the first displayed item in a list is selected automatically, pressing enter with any results displayed uses the selected value rather than user input, and moving through options with the arrow keys loops through items without unselecting. Setting `selectByDefault` to `false` doesn't select any items by default, and going outside the bounds of the list with the arrow keys will unselect all list items.

# API

Once you've instantiated a `horsey`, you can do a few more things with it.
Expand Down