Skip to content
synccn

Filter Builder

New

Build conditions like Status is Active AND Revenue > 1000, with nested groups and operators per field type.

Where

Status is Active AND Revenue > 1000

2 of 6 companies match

  • Northwind
  • Umbrella

Installation

$ pnpm dlx shadcn@latest add @synccn/filter-builder

First time in this project? Connect synccn with one command.

Usage

import { FilterBuilder, matchesFilter, filterToString } from "@/components/ui/filter-builder";<FilterBuilder fields={fields} value={filter} onValueChange={setFilter} />rows.filter((r) => matchesFilter(r, filter, fields))

API reference

Generated from the component's TypeScript types, so it always matches the code.

FilterBuilder

Visual query builder: conditions joined by AND or OR, nested groups, operators that fit each field type. Pair `matchesFilter` with the value to filter rows, and `filterToString` to show or log it.

  • fieldsrequired
    FilterField[]

    Fields people can filter on.

  • value
    FilterGroup

    Controlled filter.

  • defaultValue
    FilterGroup

    Initial filter when uncontrolled.

  • onValueChange
    (value: FilterGroup) => void

    Called with the new filter on every change.

  • maxDepth
    number

    Default 2

    How deep groups can nest. 1 means no nested groups.

  • className
    string

    Extra classes for the root element.

matchesFilter

True when `row` passes `group`. Empty groups match everything.

Takes no props.

filterToString

Readable form, e.g. `Status is Active AND (Revenue > 1000 OR Plan is Pro)`.

  • idrequired
    string

    —

  • combinatorrequired
    "and" | "or"

    —

  • rulesrequired
    (FilterRule | FilterGroup)[]

    —

Dependencies

Read from the component's imports when the site builds. The CLI installs all of these for you.

Components

Helpers

  • lib/hooksuseControllable (works controlled or uncontrolled)
  • lib/utilscn (merges class names)

npm packages

Keyboard

  • TabMove between field, operator and value controls
  • EnterorSpaceAdd, remove or switch AND/OR

Accessibility

  • Each rule and group is a labelled group, so screen readers announce where you are.
  • The AND/OR toggle names what it will switch to.

From the source

Read from this component's code when the site builds, so it can't drift.

  • ARIA roles: group
  • ARIA attributes: aria-label
  • Shows a visible focus ring for keyboard users