Query Builder
Build nested AND and OR conditions over your fields, as the advanced mode of Filter Bar.
Overview
QueryBuilder edits a tree of conditions. Each condition picks a field, an operator, and a value, and each group joins its conditions with "and" or "or". Groups can hold groups, so people can say "active or pending, and either over $1,000 or design work", which a row of filter chips cannot.
It is the advanced mode of Filter Bar, not a replacement for it. Most filtering is a few fields joined by "and", and Filter Bar handles that with less effort. Offer QueryBuilder behind an "Advanced" control for the cases that need "or" or nesting. Both read the same field definitions, and filtersToQuery and queryToFilters move between them without losing conditions.
The builder owns no data. It reports a JSON-serializable tree through onValueChange. Send the tree to your server, or filter rows in the browser with matchesQuery.
Anatomy
- Root groupLabelled group holding the top-level conditions
- JoinerWhere, then the and/or select, then the chosen word repeated
- ConditionOne rule in a labelled group
- Field selectThe field the condition tests
- Operator selectThe comparison, chosen from the field's operators
- Value editorText, number, range, date, choice, or yes/no input
- RemoveIcon button that removes the condition
- Nested groupInset panel with its own joiner, conditions, and Remove group
- Add buttonsAdd condition, and Add group while nesting is allowed
Installing query-builder brings Button, Input, Select, and the filter-bar types and utilities along as registry dependencies.
Behavior
Conditions and operators
Fields are Filter Bar's FilterDefinition objects. A field's type chooses the value editor and its default operators, and operators narrows them the same way it does in Filter Bar. Text takes a text input, number a number input or a from–to pair for "Between", select and multi-select their options, date a date input, date-range a pair of dates, and boolean a yes/no select. "Is empty" and "Is not empty" hide the value editor.
Changing the field resets the operator and value, because the old value may not fit the new field. Changing the operator keeps the value unless it switches between one value and a range.
Groups
The second row of every group holds the select that switches the group between "and" and "or". Later rows repeat the word, so the whole group reads as a sentence. A new group starts with the opposite word from its parent, which is almost always the reason for adding it.
maxDepth, which defaults to 2, is how many levels of groups may sit inside the root. Add group is hidden at the deepest level, and 0 turns grouping off.
Values in the tree
Values follow Filter Bar's shapes, except that dates are YYYY-MM-DD strings, so the tree survives JSON.stringify and a round trip through a URL. filtersToQuery converts Filter Bar's Date values on the way in, and queryToFilters converts them back.
queryToFilters returns null when the query uses "or", nesting, or two conditions on one field, since Filter Bar cannot show those. Keep the advanced view open in that case rather than dropping conditions.
Incomplete conditions
A condition without a value is kept in the tree while people edit, but matchesQuery, formatQuery, and pruneQuery skip it, so a half-written condition never empties a table. Call pruneQuery before sending a tree to a server.
Accessibility
Every group is a labelled group that states how it joins, such as "Group 2, match any", and every condition is a group named by its position, such as "Condition 2.1". Each control is named for the condition it belongs to, so "Operator for condition 2.1" makes sense without seeing the layout.
Adding a condition or group moves focus to the new field select. Removing one moves focus to its neighbor, or to the group's Add condition button when it was the last. A visually hidden status line announces how many conditions and groups remain.
Installation
Usage
import {
QueryBuilder,
matchesQuery,
type QueryField,
type QueryGroup,
} from "@/components/ui/query-builder/query-builder"
const fields: QueryField[] = [
{ key: "status", label: "Status", type: "multi-select", options: statusOptions },
{ key: "amount", label: "Amount", type: "number", meta: { prefix: "$" } },
{ key: "createdAt", label: "Created", type: "date" },
]
const [query, setQuery] = React.useState<QueryGroup>()
<QueryBuilder fields={fields} value={query} onValueChange={setQuery} />
const visible = rows.filter((row) => matchesQuery(row, query, fields))As Filter Bar's advanced mode
function openAdvanced() {
setQuery(filtersToQuery(filters, fields))
}
function closeAdvanced() {
const simple = queryToFilters(query, fields)
if (simple) {
setFilters(simple)
setQuery(null)
}
}Reading the query aloud
formatQuery(query, fields) returns one line, such as Status is any of Active, Pending and (Amount greater than $1000 or Category is any of Design). Use it for a summary, a saved view's description, or a confirmation.
Examples
Advanced mode for Filter Bar
The same fields drive both views. Turning on Advanced carries the filters into the builder. Turning it off is offered only while the query still fits Filter Bar.
API reference
QueryBuilder
Accepts native div props beyond those listed.