Sub topic 10
Applied Filters, Reset, and Shareable URLs
Assume you know basic web forms, lists, and TypeScript. This chapter focuses on the state that connects filter controls, displayed results, and the browser URL.
A filter is applied when it changes the result set. Users need to see which filters are active, remove one filter without losing the others, and reset all filters predictably. The URL should represent the same state so users can bookmark, share, and restore a search.
Keep one source of truth
A reliable design has one filter state. The controls, result query, applied-filter summary, and URL all read from that state.
flowchart LR
A[Filter controls] --> B[Filter state]
B --> C[Results]
B --> D[Applied filter summary]
B --> E[URL search parameters]
E --> BThe browser URL is useful as a serialized copy of the state. It should not become a second, conflicting source of truth.
For example, these parameters can describe a product search:
| UI value | URL parameter | Example |
|---|---|---|
| Search text | q | boots |
| Categories | category | hiking |
| Minimum price | min_price | 50 |
Use stable names and values. Avoid putting temporary interface details, such as whether a filter drawer is open, into the shareable search state.
Show applied filters clearly
Place an applied-filter summary near the result count. Each active filter can appear as a removable chip. The chip should name the value, such as “Category: Hiking,” rather than exposing an internal identifier.
Also provide a “Clear all” action when at least one filter is active. Removing one chip should change only that filter. “Clear all” should restore the default state and update the results, summary, and URL together.
If a user changes several controls before submitting, use an explicit Apply button. If results update immediately, show the change quickly and preserve the current focus. Do not make users wonder whether a checkbox took effect.
Serialize and restore state
URLSearchParams encodes and decodes query parameters. Repeated parameters work well for multi-select filters.
type Filters = {
query: string;
categories: string[];
minPrice?: number;
};
function toSearchParams(filters: Filters): URLSearchParams {
const params = new URLSearchParams();
if (filters.query.trim()) {
params.set("q", filters.query.trim());
}
for (const category of filters.categories) {
params.append("category", category);
}
if (filters.minPrice !== undefined && filters.minPrice >= 0) {
params.set("min_price", String(filters.minPrice));
}
return params;
}
function fromSearchParams(params: URLSearchParams): Filters {
const minPriceText = params.get("min_price");
const parsedMinPrice =
minPriceText === null ? undefined : Number(minPriceText);
return {
query: params.get("q") ?? "",
categories: params.getAll("category"),
minPrice:
parsedMinPrice !== undefined && Number.isFinite(parsedMinPrice)
? parsedMinPrice
: undefined,
};
}Validate values when reading the URL. A URL can be edited manually or copied from an older version of the application. Ignore unknown categories and invalid numbers instead of producing broken results.
Use history.replaceState when URL changes should not create a browser-history entry for every checkbox click. Use history.pushState when each submitted search should be a restorable step in the Back button history. Whichever choice you make, listen for the popstate event and restore the filters from the URL.
Reset without surprises
Reset should return every filter to its documented default. If the default sort order or page size affects results, define whether reset changes those values too. After reset, remove empty parameters from the URL rather than leaving confusing entries such as q=.
Exercise
A user removes the “Hiking” category chip while “Boots” remains in the search field. Which state should change?
Show answer
Only the categories value should change. Keep the query, other filters, results, applied-filter summary, and URL synchronized with that updated state.