Sub topic 8
Choosing Filter Controls for Your Data
This chapter assumes you know the basic HTML form controls (checkboxes, radio buttons, selects, inputs) and can read TypeScript. It covers how to match a filter control to the kind of data it filters, and the common mistakes that follow from a bad match.
Start from the data, not the widget
A filter control is a way to express a constraint on one field. The right control depends on three properties of that field:
- Type: categorical (brand), ordered numeric (price), date, boolean (in stock), or free text.
- Cardinality: how many distinct values exist. Five categories and five thousand brands need different controls.
- Selection logic: can a result have one value or many, and should a user be able to pick one value, several values combined with OR, or several combined with AND?
Designers often pick a control by habit, for example a dropdown for everything. This causes trouble because each control hides or reveals a different amount of information, and it takes a different number of taps to use.
| Data | Typical control | Selection logic |
|---|---|---|
| Few categories, several may apply | Checkbox group | Multi-select, usually OR within the group |
| Few options, exactly one applies | Radio group or segmented control | Single-select |
| Many options (50+) | Searchable list or combobox with checkboxes | Multi-select |
| Continuous number (price, distance) | Range slider with number inputs | Min and max |
| Date | Date range picker with presets | Start and end |
| Yes/no property | Toggle or single checkbox | Boolean |
| Ordered rating | Radio group such as "4 stars & up" | Threshold |
| Free text within results | Text field | Substring or term match |
Checkboxes and radio buttons
Use a checkbox group when a user may want more than one value from a short list. Selecting "Boots" and "Sandals" should widen the results (OR logic). Within a single facet, OR is the convention users expect. Across different facets (category and brand), logic is AND: results must match the category group and the brand group.
Use radio buttons only when values are mutually exclusive, such as "Condition: New, Used, Refurbished" if an item can have only one condition and users rarely want two. If users might want two, use checkboxes.
A common error is using radio buttons for a filter that has no "any" option. Once the user selects one, they cannot return to the unfiltered state except through a global reset. Add an explicit "Any" or "All" option, or use checkboxes, which can be cleared individually.
<fieldset>
<legend>Category</legend>
<label><input type="checkbox" name="category" value="boots"> Boots</label>
<label><input type="checkbox" name="category" value="sandals"> Sandals</label>
<label><input type="checkbox" name="category" value="sneakers"> Sneakers</label>
</fieldset>The fieldset and legend give the group a name that screen readers announce along with each checkbox. Chapter 14 returns to accessibility in more detail.
Dropdowns and their cost
A native select is compact, but it hides its options until opened. That is a cost for filters, because the user cannot scan the options to see what is available. Use a dropdown when:
- The list is long and ordered in a familiar way (country, year).
- Space is tight and the filter is a secondary one.
- Exactly one value is selected.
Avoid a multi-select select element. Users must hold Ctrl or Cmd to pick several items, and touch devices handle it poorly. For multiple choices, use checkboxes inside a popover.
Long lists: search within the filter
When a facet has hundreds of values (brands, authors, cities), a full checkbox list overwhelms the page. Common approaches:
- Show the top 5 to 10 values by result count, then a "Show more" link.
- Add a small text field above the list that narrows the visible options.
- Move the full list into a modal or side panel with a search box.
Sorting the visible values matters. Sort by count when the user is exploring, and alphabetically when the user already knows the name they want. Do not switch ordering as selections change, because items that move under the pointer cause misclicks. A reasonable compromise is to keep selected values pinned at the top in a stable order.
function filterOptions(options: string[], term: string): string[] {
const needle = term.trim().toLowerCase();
if (needle === "") return options;
return options.filter((o) => o.toLowerCase().includes(needle));
}
const brands = ["Acme", "Alpine", "Birch", "Cobalt", "Summit"];
console.log(filterOptions(brands, "al")); // ["Alpine"]Note that the narrowing field filters the option list, not the results. Label it clearly (for example "Search brands"), or users will confuse it with the main search field.
Numeric ranges
Prices, distances, and sizes are continuous. Three controls cover most cases:
- Min and max number inputs. Precise, accessible, and easy to validate. Weak at helping a user discover a sensible range.
- Range slider. Good for exploration, poor for precision, and hard to operate on touch screens or with a keyboard if built carelessly.
- Predefined buckets such as "Under $25", "$25 to $50". Fast to use, but the buckets must suit the real distribution of your data. If 80% of items cost under $25, that bucket tells users nothing.
The strongest option is often a slider paired with two number inputs that stay in sync. The inputs give precision and keyboard access, and the slider gives a sense of the range. Showing a small histogram of the distribution above the slider helps users avoid ranges with no items.
Validate the ends. If the user enters a minimum larger than the maximum, either swap the values or show an inline message. Do not silently return no results.
type Range = { min?: number; max?: number };
function normalizeRange(r: Range): Range {
const { min, max } = r;
if (min !== undefined && max !== undefined && min > max) {
return { min: max, max: min };
}
return { min, max };
}
console.log(normalizeRange({ min: 50, max: 20 })); // { min: 20, max: 50 }Dates
For dates, a range picker is usually right, but typing a date is slow and a calendar is slow for dates far in the past. Offer presets ("Last 7 days", "Last 30 days", "This year") plus a custom range. Presets should be relative to the current date and computed on the server or with a clear time zone, since "today" differs between users.
Booleans, ratings, and toggles
A single yes/no property such as "In stock" or "Free shipping" fits a checkbox or toggle. Use wording that describes the filtered state ("In stock only") rather than a vague label. Avoid negative phrasing like "Hide sold out", which creates double negatives when turned off.
Ratings are thresholds, not categories. "4 stars & up" includes 5 stars. Present them as a radio group with an "Any" option rather than checkboxes for each star count.
Choosing under real constraints
The diagram shows a decision path that works for many catalogs.
The thresholds (6, 50) are starting points. Test them with your own content and screen sizes.
Common errors
- Wrong logic within a group. Using AND inside a checkbox group means selecting a second brand narrows the results, which surprises users.
- Options that lead to zero results. Offering values that cannot match the current selection invites dead ends. Facet counts (covered in the next chapter) address this, and disabled options with an explanation are better than silently missing ones.
- Controls that apply late or unpredictably. Decide whether changes apply immediately or after an "Apply" button, and keep it consistent. Immediate application suits fast results and few filters. An Apply button suits slow queries and mobile panels with many filters.
- Hidden state. A collapsed filter group that contains active selections should show a badge or summary, so the user knows it is doing something.
- Inconsistent labels. The control label, the applied-filter chip, and the URL parameter should describe the same thing in the same words.
Putting it together in code
Describing filters as data makes it easy to render the right control and keep state consistent. Each field declares its type, and one function chooses the control.
type FieldConfig =
| { kind: "boolean"; id: string; label: string }
| { kind: "number"; id: string; label: string; min: number; max: number }
| { kind: "date"; id: string; label: string }
| { kind: "category"; id: string; label: string; values: string[]; multi: boolean };
type Control =
| "toggle"
| "slider+inputs"
| "date-range"
| "radio"
| "checkboxes"
| "checkboxes+more"
| "searchable-list";
function chooseControl(f: FieldConfig): Control {
switch (f.kind) {
case "boolean":
return "toggle";
case "number":
return "slider+inputs";
case "date":
return "date-range";
case "category": {
const n = f.values.length;
if (n > 50) return "searchable-list";
if (n > 6) return "checkboxes+more";
return f.multi ? "checkboxes" : "radio";
}
}
}
const brand: FieldConfig = {
kind: "category",
id: "brand",
label: "Brand",
values: Array.from({ length: 120 }, (_, i) => `Brand ${i + 1}`),
multi: true,
};
console.log(chooseControl(brand)); // "searchable-list"This keeps the decision in one place, so a change in policy (for example, raising the threshold for "Show more") does not require touching every filter.
Exercise
A catalog has a "Color" field with 9 values where each product has exactly one color, and a "Price" field where 85% of products cost under $30. Which controls would you choose for each, and what would you change about the price control's design?
Show answer
Color: checkboxes with a "Show more" link (9 values, and shoppers often want several colors at once, so multi-select with OR logic within the group). Show the most common colors first and keep selected ones pinned. Price: a slider with two synced number inputs, not equal-width buckets, because most products sit under $30 and fixed buckets would put nearly everything in one. Add a histogram or use buckets weighted to the real distribution (for example Under $10, $10 to $20, $20 to $30, $30 and up), and handle min greater than max by swapping or showing an inline message.
Exercise
A "Brand" filter uses radio buttons with no "Any" option. Name two problems and give a fix.
Show answer
First, brands are often combined, so single-select forces users to run separate searches. Second, once a brand is chosen the user cannot return to the unfiltered state without a global reset. Fix: switch to checkboxes with OR logic inside the group, so each brand can be cleared individually, and add a searchable list if the number of brands is large.