Skip to content
Merged
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
6 changes: 3 additions & 3 deletions dist/jquery.autocomplete.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ var _Autocomplete = class _Autocomplete {
if (typeof options.lookup === "function") {
options.lookup(q, (data) => {
this.suggestions = data.suggestions;
this.suggest();
options.onSearchComplete.call(this.element, q, data.suggestions);
this.suggest();
});
return;
}
Expand All @@ -416,8 +416,8 @@ var _Autocomplete = class _Autocomplete {
}
if (response && Array.isArray(response.suggestions)) {
this.suggestions = response.suggestions;
this.suggest();
options.onSearchComplete.call(this.element, q, response.suggestions);
this.suggest();
} else if (!this.isBadQuery(q)) {
this.abortAjax();
const ajaxSettings = {
Expand All @@ -430,8 +430,8 @@ var _Autocomplete = class _Autocomplete {
this.currentRequest = $.ajax(ajaxSettings).done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
this.processResponse(result, q, cacheKey);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey);
}).fail((jqXHR, textStatus, errorThrown) => {
options.onSearchError.call(this.element, q, jqXHR, textStatus, errorThrown);
});
Expand Down
6 changes: 3 additions & 3 deletions dist/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@
if (typeof options.lookup === "function") {
options.lookup(q, (data) => {
this.suggestions = data.suggestions;
this.suggest();
options.onSearchComplete.call(this.element, q, data.suggestions);
this.suggest();
});
return;
}
Expand All @@ -424,8 +424,8 @@
}
if (response && Array.isArray(response.suggestions)) {
this.suggestions = response.suggestions;
this.suggest();
options.onSearchComplete.call(this.element, q, response.suggestions);
this.suggest();
} else if (!this.isBadQuery(q)) {
this.abortAjax();
const ajaxSettings = {
Expand All @@ -438,8 +438,8 @@
this.currentRequest = $2.ajax(ajaxSettings).done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
this.processResponse(result, q, cacheKey);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey);
}).fail((jqXHR, textStatus, errorThrown) => {
options.onSearchError.call(this.element, q, jqXHR, textStatus, errorThrown);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.autocomplete.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/Autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,10 @@ export class Autocomplete {
if (typeof options.lookup === "function") {
(options.lookup as LookupCallback)(q, (data) => {
this.suggestions = data.suggestions;
this.suggest();
// Fire onSearchComplete before suggest() so consumers see
// "search complete" before any auto-select fires onSelect.
options.onSearchComplete.call(this.element, q, data.suggestions);
this.suggest();
});
return;
}
Expand All @@ -434,8 +436,8 @@ export class Autocomplete {

if (response && Array.isArray(response.suggestions)) {
this.suggestions = response.suggestions;
this.suggest();
options.onSearchComplete.call(this.element, q, response.suggestions);
this.suggest();
} else if (!this.isBadQuery(q)) {
this.abortAjax();

Expand All @@ -451,8 +453,8 @@ export class Autocomplete {
.done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
this.processResponse(result, q, cacheKey!);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey!);
})
.fail((jqXHR, textStatus, errorThrown) => {
options.onSearchError.call(this.element, q, jqXHR, textStatus, errorThrown);
Expand Down
21 changes: 21 additions & 0 deletions test/autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,27 @@ describe("Autocomplete", () => {
});
});

describe("Autocomplete event ordering", () => {
afterEach(() => {
$(".autocomplete-suggestions").remove();
});

it("fires onSearchComplete before onSelect when a single match auto-selects", () => {
const input = document.createElement("input");
const calls = [];
const autocomplete = new $.Autocomplete(input, {
lookup: [{ value: "Apple", data: 1 }],
onSearchComplete: () => calls.push("searchComplete"),
onSelect: () => calls.push("select"),
});

input.value = "Apple";
autocomplete.onValueChange();

expect(calls).toEqual(["searchComplete", "select"]);
});
});

describe("Autocomplete groupBy", () => {
afterEach(() => {
$(".autocomplete-suggestions").remove();
Expand Down