Sub topic 1
What People Are Trying to Find
Prerequisites
This chapter assumes you know basic HTML form controls such as input, label, and button. You should be comfortable reading TypeScript, and you should have used a list-based app screen, such as a shopping catalog or a file browser.
Search, filtering, and browsing: choosing the right tool
Users arrive with one of three goals. They may know the name of what they want, know its attributes but not its name, or want to explore. Each goal needs a different tool.
| User situation | Best tool | Example |
|---|---|---|
| Knows the name or a distinctive word | Search | "trail running shoes" |
| Knows the attributes but not the name | Filtering | Size 10, under $100, waterproof |
| Has no fixed target | Browsing | Category pages, "new arrivals" |
Offer search first when the input is cheap to type. Add filters once results return, so users can narrow them.
Designing the search field
A search field needs a visible label, a landmark role, and a status message that reports the result count. Placeholder text is not a label because it disappears when the user types.
<form role="search" action="/search" method="get">
<label for="q">Search products</label>
<input id="q" name="q" type="search" autocomplete="off">
<button type="submit">Search</button>
</form>
<p role="status">24 results for "boots"</p>Autocomplete and query suggestions
Suggestions should wait until the user pauses. A debounce function delays the request so the app does not fire one request per keystroke.
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) => {
console.log("fetch suggestions for", value);
}, 200);Forgiving queries: typos, synonyms, and normalization
Normalization makes equivalent queries match. It removes case differences, extra spaces, and accents before the app compares a query with its index.
function normalizeQuery(raw: string): string {
return raw
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.trim()
.replace(/\s+/g, " ");
}
console.log(normalizeQuery(" Café Boots ")); // "cafe boots"Typo tolerance and synonyms need a separate layer, such as a fuzzy matcher or a synonym table.
Exercise
Exercise
A user types "Sneekers" into a shoe store and gets zero results. Which two changes from this chapter would most likely help, and why?
Show answer
Add typo tolerance so "sneekers" matches "sneakers", and normalize the query so case and spacing do not block a match. Also show a "Showing results for sneakers" message so the user knows the correction happened.