Sub topic 3
Designing the Search Field
Assume you understand basic forms, lists, and web or mobile interfaces. The examples use HTML and TypeScript. You should also know how a client sends a request and updates a result list.
A search field is the control where a person states a finding task. Its design should make three things clear:
- What can the user search?
- When does the search run?
- Where will the results appear?
Start with the user's intent
Use search when users have a specific term, name, or question in mind. Use filters when they want to narrow a known collection. Use browsing when they want to explore without knowing the exact term.
The placeholder can provide an example, but it should not replace a visible label. A label remains available after the user enters text and helps screen-reader users identify the field.
Choose wording that matches the data. “Search products” is clearer than “Search.” If the field searches several types of content, say so: “Search articles, authors, and topics.”
Decide what an empty submission means. It might show all items, do nothing, or display a validation message. Keep that behavior consistent with the surrounding page.
Build a complete search landmark
A search form groups the input and its action. The search landmark helps assistive-technology users find the search area quickly. A submit button supports keyboard, touch, and users who do not expect results to update while they type.
<form role="search" action="/search" method="get">
<label for="q">Search products</label>
<input
id="q"
name="q"
type="search"
autocomplete="off"
aria-describedby="search-help"
>
<button type="submit">Search</button>
</form>
<p id="search-help">Try a product name, brand, or model.</p>
<p role="status" aria-live="polite">24 results for “boots”</p>The name attribute allows the query to be submitted. The type="search" value gives browsers a search-appropriate control, including a clear button in some environments. The status message communicates result changes without forcing a user to inspect the whole page.
Use autocomplete="off" only when browser suggestions would conflict with your own suggestions or expose sensitive information. For ordinary product or document search, browser history may be useful.
Connect the field to the result state
A useful interaction has a visible state transition:
Keep the query visible after submission. Users need to know what produced the current results and should be able to edit it without starting over. Preserve the query when they navigate to a result and return with the back button.
If results update as the user types, show that behavior clearly and protect performance. Requesting data for every keystroke can overload the server and create out-of-order responses.
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 requestSuggestions = debounce((query: string) => {
console.log("fetch suggestions for", query);
}, 200);Debouncing waits until the user pauses before sending a request. Cancel or ignore stale responses as well, because a slower request for an older query can otherwise replace newer results.
Exercise
A catalog search field has only a magnifying-glass icon, sends a request on every keystroke, and clears the query when users return from a product page. What should you change first?
Show answer
Add a persistent label and preserve the query. Then debounce typeahead requests and define clear loading, error, and no-result feedback.