Skip to documentation content

Query Builder

Build nested AND and OR conditions over your fields, as the advanced mode of Filter Bar.

query-builder-demo

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

QueryBuilder
  • 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.

KeyAction
TabMove through joiners, fields, operators, values, and buttons in reading order
Enter, Space, or ↓Open the focused select
↑ ↓Move between options in an open select
EscapeClose the select without changing it

Installation

npx honestui@latest add query-builder

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.

query-builder-filter-bar

API reference

QueryBuilder

Accepts native div props beyond those listed.

PropTypeDefaultDescription
fieldsQueryField[]RequiredFilter Bar definitions the conditions can test.
valueQueryGroup—Controlled query tree.
defaultValueQueryGroupEmpty "and" groupInitial tree when uncontrolled.
onValueChange(value) => void—Called with the whole tree after each change.
maxDepthnumber2Levels of groups allowed inside the root.
disabledbooleanfalseKeeps every control visible but inactive.
labelsPartial<QueryBuilderLabels>EnglishReplacement text, including operator names.

Functions

ExportSignatureDescription
matchesQuery(record, query, fields) => booleanWhether a record passes. Incomplete conditions are skipped.
formatQuery(query, fields, labels?) => stringA one-line reading of the query.
filtersToQuery(values, fields) => QueryGroupFilter Bar values as one "and" group.
queryToFilters(query, fields) => FilterValue[] | nullFilter Bar values, or null when the query cannot be written that way.
pruneQuery(query, fields) => QueryGroupDrops incomplete conditions and empty groups.
createQueryGroup(combinator?, children?, id?) => QueryGroupA new group.
createQueryCondition(field, id?) => QueryConditionA new condition with the field's default operator.
updateQueryNode, insertQueryNode, removeQueryNodeTree, id, …Immutable edits by node id.
countQuery(query) => { conditions; groups }Totals below the root.

Types

ExportShape
QueryGroup{ id; kind: "group"; combinator: "and" | "or"; children: QueryNode[] }
QueryCondition{ id; kind: "condition"; field; operator; value }
QueryNodeQueryGroup | QueryCondition
QueryFieldFilter Bar's FilterDefinition
QueryBuilderLabelsEvery string the component writes