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
10 changes: 7 additions & 3 deletions dist/jquery.autocomplete.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ var _Autocomplete = class _Autocomplete {
const params = options.ignoreParams ? null : options.params;
if (typeof options.lookup === "function") {
options.lookup(q, (data) => {
this.suggestions = data.suggestions;
options.onSearchComplete.call(this.element, q, data.suggestions);
const suggestions = this.verifySuggestionsFormat(data.suggestions);
this.suggestions = suggestions;
options.onSearchComplete.call(this.element, q, suggestions);
this.suggest();
});
return;
Expand Down Expand Up @@ -430,6 +431,7 @@ var _Autocomplete = class _Autocomplete {
this.currentRequest = $.ajax(ajaxSettings).done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
result.suggestions = this.verifySuggestionsFormat(result.suggestions);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey);
}).fail((jqXHR, textStatus, errorThrown) => {
Expand Down Expand Up @@ -560,7 +562,9 @@ var _Autocomplete = class _Autocomplete {
if (suggestions.length && typeof suggestions[0] === "string") {
return suggestions.map((value) => ({ value, data: null }));
}
return suggestions;
return suggestions.map(
(s) => typeof s.value === "string" ? s : { ...s, value: String(s.value) }
);
}
validateOrientation(orientation, fallback) {
const normalized = (orientation || "").trim().toLowerCase();
Expand Down
10 changes: 7 additions & 3 deletions dist/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,9 @@
const params = options.ignoreParams ? null : options.params;
if (typeof options.lookup === "function") {
options.lookup(q, (data) => {
this.suggestions = data.suggestions;
options.onSearchComplete.call(this.element, q, data.suggestions);
const suggestions = this.verifySuggestionsFormat(data.suggestions);
this.suggestions = suggestions;
options.onSearchComplete.call(this.element, q, suggestions);
this.suggest();
});
return;
Expand Down Expand Up @@ -438,6 +439,7 @@
this.currentRequest = $2.ajax(ajaxSettings).done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
result.suggestions = this.verifySuggestionsFormat(result.suggestions);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey);
}).fail((jqXHR, textStatus, errorThrown) => {
Expand Down Expand Up @@ -568,7 +570,9 @@
if (suggestions.length && typeof suggestions[0] === "string") {
return suggestions.map((value) => ({ value, data: null }));
}
return suggestions;
return suggestions.map(
(s) => typeof s.value === "string" ? s : { ...s, value: String(s.value) }
);
}
validateOrientation(orientation, fallback) {
const normalized = (orientation || "").trim().toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.autocomplete.min.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/Autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,11 @@ export class Autocomplete {

if (typeof options.lookup === "function") {
(options.lookup as LookupCallback)(q, (data) => {
this.suggestions = data.suggestions;
const suggestions = this.verifySuggestionsFormat(data.suggestions);
this.suggestions = suggestions;
// Fire onSearchComplete before suggest() so consumers see
// "search complete" before any auto-select fires onSelect.
options.onSearchComplete.call(this.element, q, data.suggestions);
options.onSearchComplete.call(this.element, q, suggestions);
this.suggest();
});
return;
Expand Down Expand Up @@ -453,6 +454,7 @@ export class Autocomplete {
.done((data) => {
this.currentRequest = null;
const result = options.transformResult(data, q);
result.suggestions = this.verifySuggestionsFormat(result.suggestions);
options.onSearchComplete.call(this.element, q, result.suggestions);
this.processResponse(result, q, cacheKey!);
})
Expand Down Expand Up @@ -622,7 +624,11 @@ export class Autocomplete {
if (suggestions.length && typeof suggestions[0] === "string") {
return (suggestions as string[]).map((value) => ({ value, data: null }));
}
return suggestions as Suggestion[];
// Coerce non-string `value` so downstream string methods (toLowerCase,
// replace, substr, indexOf) don't throw on numeric or other types.
return (suggestions as Suggestion[]).map((s) =>
typeof s.value === "string" ? s : { ...s, value: String(s.value) }
);
}

validateOrientation(orientation: string | undefined, fallback: Orientation): Orientation {
Expand Down
41 changes: 41 additions & 0 deletions test/autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,47 @@ describe("Autocomplete", () => {
});
});

describe("Autocomplete non-string suggestion values", () => {
afterEach(() => {
$(".autocomplete-suggestions").remove();
});

it("coerces numeric value from a local lookup so render does not throw", () => {
const input = document.createElement("input");
const autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 12345, data: "n" }],
triggerSelectOnValidInput: false,
});

input.value = "1";
expect(() => autocomplete.onValueChange()).not.toThrow();

expect(typeof autocomplete.suggestions[0].value).toBe("string");
expect(autocomplete.suggestions[0].value).toBe("12345");
});

it("coerces numeric value from a function lookup callback", () => {
const input = document.createElement("input");
let completedValueType;
let selectedValueType;
const autocomplete = new $.Autocomplete(input, {
lookup: (_q, done) => done({ suggestions: [{ value: 42, data: "n" }] }),
onSearchComplete: (_q, suggestions) => {
completedValueType = typeof suggestions[0].value;
},
onSelect: (suggestion) => {
selectedValueType = typeof suggestion.value;
},
});

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

expect(completedValueType).toBe("string");
expect(selectedValueType).toBe("string");
});
});

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