Sub topic 2
Presenting search results
This chapter assumes you can read TypeScript, know how lists and form controls work in a web or mobile UI, and have seen a URL with query parameters.
Presenting search results
Show the query and the result count above the list so users can confirm what they searched for. Announce changes to the count in a live region so screen reader users hear the update. Keep each result scannable with a title, one line of context, and the fields that help users decide, such as price or rating.
Ranking and sorting results
Relevance ranking orders items by how well they match the query. Sorting orders them by one field, such as price or date. Choose a default that fits the task, label the active sort, and keep the query and filters unchanged when the user changes the sort.
Choosing filter controls for your data
Pick the control from the data type. Use checkboxes for several values in one category, radio buttons for exactly one value, min and max inputs for numeric ranges, and a toggle for yes or no. Avoid sliders when users need exact values.
Faceted navigation and result counts
A facet is a filter dimension, such as brand or size. Each facet value shows how many results it would return. Compute counts over the items that match the other active filters. If you count against all items, users can select a value that returns nothing.
type Item = { id: string; brand: string; size: string };
// Pass in only the items that match the other active filters.
function facetCounts(items: Item[], field: "brand" | "size"): Map<string, number> {
const counts = new Map<string, number>();
for (const item of items) {
counts.set(item[field], (counts.get(item[field]) ?? 0) + 1);
}
return counts;
}Applied filters, reset, and shareable URLs
Show applied filters as removable chips, and offer a "Clear all" action. Store the query and filters in the URL so users can share, bookmark, and restore results with the back button.
function toQueryString(q: string, brands: string[]): string {
const params = new URLSearchParams();
if (q) params.set("q", q);
for (const b of brands) params.append("brand", b);
return params.toString();
}
// toQueryString("boots", ["acme", "zenith"]) returns "q=boots&brand=acme&brand=zenith"> Note: the callout below uses the required fence syntax.
Exercise
A user selects brand "acme" and then sees the brand counts for other brands drop to zero. Which items should facetCounts receive to avoid zero-result choices, and why?
Show answer
It should receive only the items that match the other active filters, excluding the brand filter itself, so each brand count reflects what the user would get by selecting that brand alongside the current filters.