Filter a list with chips that read like a sentence, added from a searchable menu.
npx shadcn@latest add @preskok/filter-barimport { useState } from "react"
import {
FilterBar,
FilterBarAdd,
FilterBarChips,
FilterBarClear,
matchesFilter,
type Filter,
type FilterField,
} from "@/components/ui/preskok-ui/filter-bar"
const fields: Array<FilterField> = [
{
id: "status",
label: "Status",
variant: "select",
options: [
{ value: "available", label: "Available" },
{ value: "sold", label: "Sold" },
],
},
{ id: "price", label: "Price", variant: "number", unit: "€" },
]
const vehicles = [
{ id: 1, name: "Volkswagen Golf", status: "available", price: 24900 },
{ id: 2, name: "Tesla Model 3", status: "sold", price: 39500 },
]
export function Vehicles() {
const [filters, setFilters] = useState<Array<Filter>>([])
const rows = vehicles.filter((vehicle) =>
filters.every((filter) =>
matchesFilter(
filter.field === "price" ? vehicle.price : vehicle.status,
filter
)
)
)
return (
<>
<FilterBar fields={fields} value={filters} onChange={setFilters}>
<FilterBarChips />
<FilterBarAdd />
<FilterBarClear />
</FilterBar>
<ul>
{rows.map((vehicle) => (
<li key={vehicle.id}>{vehicle.name}</li>
))}
</ul>
</>
)
}FilterBar holds the filters and lays its children out in a wrapping row, so a search field or a
sort button can sit beside the chips. A filter is plain data, { field, operator, value } with a
tagged value such as { type: "range", min: 10000, max: 25000 }, so it goes into a URL or a
request body as it is. "+ Filter" adds the filter as soon as a field is
picked and drops it if the popover closes without a value, so use isFilterActive to skip
filters that narrow nothing yet.
A field's variant sets its operators and value editor. The first operator is the default; pass
operators to restrict or reorder them.
| Variant | Operators | Value type |
|---|---|---|
text | contains, notContains, is, isNot, isEmpty, isNotEmpty | text (text) |
number | between, is, isNot, lt, lte, gt, gte, isEmpty, isNotEmpty | range (min, max) or number |
select | isAnyOf, isNoneOf, isEmpty, isNotEmpty | options (values) |
boolean | is | boolean (value) |
date | between, is, before, after, isEmpty, isNotEmpty | days (start, end) or day |
isEmpty and isNotEmpty carry { type: "none" }.
The bar keeps its own filters from defaultValue. A select with multiple: false closes on the
first pick.
FilterBarChips renders <FilterChip /> for each filter. Pass a function to build chips from
FilterChipField, FilterChipOperator, FilterChipValue and FilterChipRemove.
Pass messages for the strings; anything left out stays English. Numbers and dates follow the
I18nProvider locale.
Send the active filters with the request instead of filtering rows in the browser.
<FilterBarAdd shortcut="f" /> opens "+ Filter" with F when no text field is focused.
In a value editor, Enter applies the value and closes it.
Vehicle | Year | Price | Mileage | Status | Listed |
|---|---|---|---|---|---|
| Volkswagen Golf | 2023 | €24,900 | 12,400 km | available | Nov 18, 2026 |
| Tesla Model 3 | 2024 | €39,500 | 8,100 km | reserved | Nov 2, 2026 |
| BMW i4 | 2023 | €47,800 | 21,000 km | available | Oct 12, 2026 |
| Renault Zoe | 2022 | €15,900 | 31,700 km | available | Nov 15, 2026 |
| Audi A4 Avant | 2022 | €33,400 | 41,900 km | available | Sep 21, 2026 |
| Volkswagen ID.4 | 2024 | €41,200 | 2,300 km | reserved | Nov 19, 2026 |