Sub topic 4
Autocomplete and Query Suggestions
Assume you know basic web forms, lists, and TypeScript. This chapter focuses on suggestions shown while a person types into search. It does not cover result ranking or filter controls.
What suggestions should do
Autocomplete is a feature that proposes likely completions before the user submits a query. Suggestions reduce typing and help users discover the words your system understands.
A useful suggestion usually matches one of these intents:
- Complete a query: “wireless headphones”
- Navigate to a known item: “Nike Air Max 90”
- Open a category: “Men’s running shoes”
- Repeat a recent search: “black boots”
Label these types when the difference matters. A person may expect a product suggestion to open a product page, but a query suggestion to run a search. Mixing the behaviors creates surprises.
Show suggestions only when they add value. A short query such as one character may produce noisy results. You can wait until two or three meaningful characters are present, then remove suggestions when the field is empty.
Keep suggestions responsive
Suggestions normally come from a server. Requesting data after every keystroke can waste resources and produce unnecessary screen updates. Debouncing waits until the user pauses typing.
function debounce<A extends unknown[]>(
fn: (...args: A) => void,
waitMs: number,
) {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: A) => {
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(() => fn(...args), waitMs);
};
}
const onInput = debounce((value: string) => {
if (value.trim().length < 2) return;
console.log("fetch suggestions for", value);
}, 200);
onInput("bo");Debouncing limits request frequency, but it does not solve every asynchronous problem. If request A starts before request B and finishes afterward, stale results from A can replace newer results. Track a request number or use AbortController so only the latest request updates the list.
Support keyboard and pointer use
A suggestion list needs a clear active item. The usual keyboard behavior is:
- Down Arrow moves into the list or to the next item.
- Up Arrow moves to the previous item.
- Enter accepts the active item.
- Escape closes the list without changing the query.
- Tab leaves the control without forcing a selection.
Keep the typed value visible after selection when the user chose a query. If the user chose a known item, navigate to that item and make the action explicit.
For accessibility, expose the input and list as a combobox pattern. Give each suggestion an accessible name, announce when results arrive, and ensure the active suggestion is communicated to screen readers. Do not rely on color alone to show selection.
Model the interaction
Test intent, timing, and recovery
Test slow responses, no suggestions, failed requests, rapid typing, keyboard selection, touch selection, and reopening the list after Escape. Suggestions should never block a normal search submission. If the service fails, preserve the typed query and let the user submit it manually.
Exercise
A user types “cam” and quickly changes it to “camera”. An older request returns last. What should the interface do?
Show answer
Ignore the older response. Use a request identifier or AbortController so only results for the current query can update the suggestions.