Query Input
A search bar that reads key:value filters as you type, suggests keys and values, and returns Filter objects plus the leftover text.
- Login page loops after SSONoor Haddad
Installation
The CLI also installs the npm packages and registry items it builds on, and reads the style in your components.json to pick the Base UI or Radix build.
Usage
"use client"
import type { FilterField } from "@/lib/filter"
import { QueryInput } from "@/components/ui/query-input"
const fields: FilterField[] = [
{ id: "is", label: "State", type: "select", options: [{ value: "open", label: "Open" }, { value: "closed", label: "Closed" }] },
{ id: "author", label: "Author", type: "text" },
{ id: "age", label: "Age in days", type: "number" },
]
export function IssueSearch({ onSearch }: { onSearch: (filters: unknown[], text: string) => void }) {
return <QueryInput aria-label="Search issues" fields={fields} defaultValue="is:open author:me" onFiltersChange={onSearch} />
}Examples
What it parses
On text and select fields, a comma list becomes is-any-of. On text fields, *text* becomes contains. On number and date fields, > and < compare, and a..b is a date range. Anything that is not a known key stays in the free text.
{
"filters": [
{
"id": "query-0",
"fieldId": "level",
"operator": "is-any-of",
"value": [
"error",
"warn"
]
},
{
"id": "query-1",
"fieldId": "service",
"operator": "contains",
"value": "api"
},
{
"id": "query-2",
"fieldId": "duration",
"operator": "gt",
"value": "500"
},
{
"id": "query-3",
"fieldId": "date",
"operator": "between",
"value": [
"2025-03-01",
"2025-03-07"
]
}
],
"text": "\"timed out\""
}Next to a filter builder
Both components use the Filter type, so they can share one filters array. Typing updates the chips, and editing a chip rewrites the query with formatQuery.
API reference
QueryInput
A combobox input that parses key:value terms into filters. Typing at the end of a term suggests keys, then values. Arrow keys move through the list, ArrowDown on an empty term opens it, Enter or Tab inserts the highlighted suggestion, and Escape closes. Key suggestions start with nothing highlighted, so Enter on a plain word still submits; value suggestions start on the first option.
| Prop | Type | Default |
|---|---|---|
fieldsRequiredFilterField[]The keys that can be filtered on. Field ids are the keys, and select options are suggested as values. | FilterField[] | |
valuestringControlled query. | string | |
defaultValuestringDefault""Initial value when uncontrolled. | string | "" |
onValueChange(value: string) => voidCalled when the query text changes. | (value: string) => void | |
onFiltersChange(filters: Filter[], text: string) => voidCalled on every edit with the parsed filters and the leftover free text. | (filters: Filter[], text: string) => void | |
classNamestringApplied to the bordered wrapper, which also sets the font for the text and its highlights. | string |
Other props go to the <input>.
parseQuery
parseQuery(query: string, fields: FilterField[]): { filters: Filter[]; text: string }Reads a query into filters and free text. Exported from the lib file.
formatQuery
formatQuery(filters: Filter[], text?: string): stringWrites filters and free text back as a query. parseQuery reads it back to the same filters, as long as the free text has no key:value terms for known fields.