Data Grid

Filter Builder

A grid with a condition builder: field, operator and value on every line, one and/or joiner for the whole set and a live count of matching rows.

  • datagrid
  • filters
  • query-builder
  • table

Preview

1440pxHost theme

Use it with AI

  1. 1. Copy the link.
  2. 2. Write to your agent in your own words and drop the link into the sentence.
  3. 3. The agent opens the link and installs the component from the registry.

put this in the header: https://vibeui.ru/c/datagrid-019?lang=en

Условия отбора

и

Подошло строк: 1 из 6

Rows are filtered by the set of conditions in the builder
КонтрагентРегионВыручка, тыс. ₽Просрочка, дн.
Дельта-СтройУрал26 50027
Show the full instructions

What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.

# Install and place "datagrid-019" (Filter Builder) from VibeUI

## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/datagrid-019.json

Registry item: https://vibeui.ru/r/datagrid-019.json
Installs to: components/vibeui/datagrid-019.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.

Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.

## 2. What it is
A grid with a condition builder: field, operator and value on every line, one and/or joiner for the whole set and a live count of matching rows.

A grid with an and/or condition builder: field, operator and value on every line. Zero dependencies, one file.

## 3. How to use it
import { Datagrid019 } from "@/components/vibeui/datagrid-019"

<Datagrid019 caption="Filtered by a set of conditions" joiner="or" />

Read the installed file for the full prop list.

## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.

Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)

## 5. Keep exactly as installed
- the local --vibeui-datagrid-019-* palette — do not swap it for your theme tokens
- the builder inside a fieldset with a legend: a scatter of fields never reads as one filter group
- clear:both on the condition rows — a legend with float:left would otherwise make them wrap around it
- the joiner on its own line between conditions rather than inside them: otherwise «and» looks like part of a neighbour
- resetting the operator when the field changes: numbers and text take different operator sets
- an aria-label with the condition number on every field, or three identical selects are indistinguishable

## 6. You may change
- the line under the toolbar through caption
- the initial joiner through joiner: and or or
- the counterparty list through rows
- the accent color through accent

## 7. Rules
- One joiner covers the whole set: nested brackets need a condition tree rather than a flat array.
- An empty value lets the condition pass — that guards against every row vanishing mid-typing.
- The condition count is capped at four: beyond that the builder stops fitting in one column.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.

## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.

For developers

npx shadcn@latest add https://vibeui.ru/r/datagrid-019.json
https://vibeui.ru/r/datagrid-019.json

Компонент самодостаточен: один файл, без зависимостей, палитра в --vibeui-datagrid-019-*. Клиентский: useState хранит массив условий и связку; смена поля сбрасывает оператор и значение, потому что для числа и текста наборы операторов разные. Конструктор собран в fieldset с legend — для скринридера это одна группа, а не россыпь полей; у legend стоит float:left с width:100%, поэтому строки условий получают clear:both. Связка показана отдельной строкой между условиями и не встроена в них: иначе «и» читается как часть соседнего условия. У каждого поля свой aria-label с номером условия, счётчик найденного объявлен role=status.

Component source

The same file your agent installs. Here in case you would rather copy it by hand.

"use client"

import { useState } from "react"
import type { ComponentPropsWithoutRef, CSSProperties } from "react"

export type Datagrid019Row = {
  id: string
  name: string
  region: string
  revenue: number
  overdue: number
}

export type Datagrid019Props = Omit<
  ComponentPropsWithoutRef<"section">,
  "children"
> & {
  rows?: Datagrid019Row[]
  caption?: string
  joiner?: "and" | "or"
  accent?: string
}

// Идея компонента: фильтр собирается из условий, а не выбирается из
// готового списка. Каждое условие — поле, оператор и значение; связка
// «и/или» одна на весь набор и стоит между строками условий, чтобы её
// нельзя было прочитать как часть соседнего условия. Набор живёт внутри
// fieldset с legend: для скринридера это одна группа, а не россыпь полей.
const STYLES = `
:where([data-vibeui-block="datagrid-019"]){
--vibeui-datagrid-019-bg:oklch(1 0 0);
--vibeui-datagrid-019-fg:oklch(0.23 0.014 285);
--vibeui-datagrid-019-muted:oklch(0.55 0.014 285);
--vibeui-datagrid-019-border:oklch(0.92 0.006 285);
--vibeui-datagrid-019-head:oklch(0.975 0.003 285);
--vibeui-datagrid-019-accent:oklch(0.5 0.14 230);
--vibeui-datagrid-019-panel:oklch(0.985 0.004 285);
--vibeui-datagrid-019-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}
[data-vibeui-block="datagrid-019"]{
box-sizing:border-box;width:100%;max-width:52rem;margin:0 auto;
background:var(--vibeui-datagrid-019-bg);color:var(--vibeui-datagrid-019-fg);
border:1px solid var(--vibeui-datagrid-019-border);border-radius:0.875rem;
font-family:var(--vibeui-datagrid-019-font);overflow:hidden;
}
[data-vibeui-block="datagrid-019"] *{box-sizing:border-box}
[data-vibeui-block="datagrid-019"] [data-part="builder"]{
border:0;margin:0;padding:0.75rem 0.875rem;background:var(--vibeui-datagrid-019-panel);
border-bottom:1px solid var(--vibeui-datagrid-019-border);
}
[data-vibeui-block="datagrid-019"] [data-part="builder"] legend{
padding:0;font-size:0.6875rem;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;
color:var(--vibeui-datagrid-019-muted);float:left;width:100%;
}
/* legend с float:left увёл бы следующие flex-строки в обтекание. */
[data-vibeui-block="datagrid-019"] [data-part="rule"]{clear:both;display:flex;flex-wrap:wrap;align-items:center;gap:0.375rem;margin-top:0.5rem}
[data-vibeui-block="datagrid-019"] [data-part="joiner"]{
clear:both;display:flex;align-items:center;gap:0.375rem;margin-top:0.5rem;
font-size:0.6875rem;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:var(--vibeui-datagrid-019-accent);
}
[data-vibeui-block="datagrid-019"] [data-part="joiner"]::after{content:"";flex:1;height:1px;background:var(--vibeui-datagrid-019-border)}
[data-vibeui-block="datagrid-019"] select,
[data-vibeui-block="datagrid-019"] [data-part="value"]{
font:inherit;font-size:0.75rem;color:inherit;padding:0.3125rem 0.5rem;
border:1px solid var(--vibeui-datagrid-019-border);border-radius:0.4375rem;
background:var(--vibeui-datagrid-019-bg);
}
[data-vibeui-block="datagrid-019"] [data-part="value"]{width:8rem;min-width:0}
[data-vibeui-block="datagrid-019"] select:focus-visible,
[data-vibeui-block="datagrid-019"] [data-part="value"]:focus-visible{outline:2px solid var(--vibeui-datagrid-019-accent);outline-offset:1px}
[data-vibeui-block="datagrid-019"] [data-part="drop"]{
appearance:none;cursor:pointer;font:inherit;font-size:0.875rem;line-height:1;
width:1.75rem;height:1.75rem;border-radius:0.4375rem;
border:1px solid var(--vibeui-datagrid-019-border);
background:var(--vibeui-datagrid-019-bg);color:var(--vibeui-datagrid-019-muted);
}
[data-vibeui-block="datagrid-019"] [data-part="drop"]:hover{color:oklch(0.55 0.17 28);border-color:oklch(0.55 0.17 28)}
[data-vibeui-block="datagrid-019"] [data-part="drop"]:focus-visible{outline:2px solid var(--vibeui-datagrid-019-accent);outline-offset:2px}
[data-vibeui-block="datagrid-019"] [data-part="add"]{
clear:both;appearance:none;cursor:pointer;font:inherit;font-size:0.75rem;font-weight:600;
margin-top:0.625rem;padding:0.3125rem 0.625rem;border-radius:0.4375rem;
border:1px dashed var(--vibeui-datagrid-019-accent);background:transparent;color:var(--vibeui-datagrid-019-accent);
}
[data-vibeui-block="datagrid-019"] [data-part="add"]:disabled{opacity:.45;cursor:not-allowed}
[data-vibeui-block="datagrid-019"] [data-part="add"]:focus-visible{outline:2px solid var(--vibeui-datagrid-019-accent);outline-offset:2px}
[data-vibeui-block="datagrid-019"] [data-part="summary"]{
clear:both;margin:0.625rem 0 0;font-size:0.75rem;color:var(--vibeui-datagrid-019-muted);
}
[data-vibeui-block="datagrid-019"] [data-part="scroll"]{overflow-x:auto}
[data-vibeui-block="datagrid-019"] [data-part="scroll"]:focus-visible{outline:2px solid var(--vibeui-datagrid-019-accent);outline-offset:-2px}
[data-vibeui-block="datagrid-019"] table{width:100%;border-collapse:collapse;font-size:0.8125rem}
[data-vibeui-block="datagrid-019"] caption{
padding:0.625rem 0.875rem;text-align:left;font-size:0.75rem;color:var(--vibeui-datagrid-019-muted);caption-side:top;
}
[data-vibeui-block="datagrid-019"] th,
[data-vibeui-block="datagrid-019"] td{
padding:0.5rem 0.875rem;text-align:left;white-space:nowrap;
border-top:1px solid var(--vibeui-datagrid-019-border);
}
[data-vibeui-block="datagrid-019"] thead th{background:var(--vibeui-datagrid-019-head);font-weight:600}
[data-vibeui-block="datagrid-019"] [data-align="end"]{text-align:right;font-variant-numeric:tabular-nums}
[data-vibeui-block="datagrid-019"] [data-late="true"]{color:oklch(0.55 0.17 28);font-weight:600}
[data-vibeui-block="datagrid-019"] [data-part="none"]{padding:1.5rem 0.875rem;text-align:center;color:var(--vibeui-datagrid-019-muted)}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="datagrid-019"] *{animation:none!important;transition:none!important}}
`

const DEFAULT_ROWS: Datagrid019Row[] = [
  {
    id: "a1",
    name: "Артель «Кама»",
    region: "Приволжье",
    revenue: 12400,
    overdue: 0,
  },
  {
    id: "a2",
    name: "Северный Порт",
    region: "Северо-Запад",
    revenue: 30100,
    overdue: 12,
  },
  {
    id: "a3",
    name: "Гранд-Сервис",
    region: "Центр",
    revenue: 4300,
    overdue: 41,
  },
  { id: "a4", name: "Мостовик", region: "Урал", revenue: 18700, overdue: 3 },
  {
    id: "a5",
    name: "Ювенко Логистика",
    region: "Центр",
    revenue: 8600,
    overdue: 0,
  },
  {
    id: "a6",
    name: "Дельта-Строй",
    region: "Урал",
    revenue: 26500,
    overdue: 27,
  },
]

const FIELDS = [
  { key: "name", label: "Название", numeric: false },
  { key: "region", label: "Регион", numeric: false },
  { key: "revenue", label: "Выручка, тыс.", numeric: true },
  { key: "overdue", label: "Просрочка, дн.", numeric: true },
] as const

const TEXT_OPS = [
  { key: "contains", label: "содержит" },
  { key: "equals", label: "равно" },
  { key: "starts", label: "начинается с" },
] as const

const NUMBER_OPS = [
  { key: "gt", label: "больше" },
  { key: "lt", label: "меньше" },
  { key: "eq", label: "равно" },
] as const

type Rule = {
  id: number
  field: (typeof FIELDS)[number]["key"]
  operator: string
  value: string
}

function test(row: Datagrid019Row, rule: Rule) {
  const field = FIELDS.find((item) => item.key === rule.field)

  if (!field) {
    return true
  }

  if (field.numeric) {
    const left = Number(row[rule.field as "revenue" | "overdue"])
    const right = Number(rule.value)

    if (Number.isNaN(right)) {
      return true
    }

    if (rule.operator === "gt") {
      return left > right
    }

    if (rule.operator === "lt") {
      return left < right
    }

    return left === right
  }

  const left = String(row[rule.field as "name" | "region"]).toLowerCase()
  const right = rule.value.trim().toLowerCase()

  if (right === "") {
    return true
  }

  if (rule.operator === "equals") {
    return left === right
  }

  if (rule.operator === "starts") {
    return left.startsWith(right)
  }

  return left.includes(right)
}

/**
 * Сетка с конструктором условий: поле, оператор и значение в каждой
 * строке, одна связка «и/или» на весь набор. Один файл, ноль зависимостей.
 */
export function Datagrid019({
  rows = DEFAULT_ROWS,
  caption = "Строки отбираются набором условий из конструктора",
  joiner = "and",
  accent,
  className,
  style,
  ...props
}: Datagrid019Props) {
  const [join, setJoin] = useState<"and" | "or">(joiner)
  const [rules, setRules] = useState<Rule[]>([
    { id: 1, field: "region", operator: "contains", value: "Урал" },
    { id: 2, field: "overdue", operator: "gt", value: "10" },
  ])

  const filtered = rows.filter((row) => {
    if (rules.length === 0) {
      return true
    }

    return join === "and"
      ? rules.every((rule) => test(row, rule))
      : rules.some((rule) => test(row, rule))
  })

  const palette = {
    ...(accent ? { "--vibeui-datagrid-019-accent": accent } : null),
    ...style,
  } as CSSProperties

  function patch(id: number, change: Partial<Rule>) {
    setRules((current) =>
      current.map((rule) => (rule.id === id ? { ...rule, ...change } : rule)),
    )
  }

  return (
    <>
      <style href="vibeui-datagrid-019" precedence="medium">
        {STYLES}
      </style>
      <section
        {...props}
        data-vibeui-block="datagrid-019"
        className={className}
        style={palette}
      >
        <fieldset data-part="builder">
          <legend>Условия отбора</legend>
          {rules.map((rule, index) => {
            const field = FIELDS.find((item) => item.key === rule.field)
            const operators = field?.numeric ? NUMBER_OPS : TEXT_OPS

            return (
              <div key={rule.id}>
                {index > 0 ? (
                  <p data-part="joiner">
                    {join === "and" ? "и" : "или"}
                    <span hidden>связка условий</span>
                  </p>
                ) : null}
                <div data-part="rule">
                  <select
                    value={rule.field}
                    aria-label={`Поле условия ${index + 1}`}
                    onChange={(event) => {
                      const next = FIELDS.find(
                        (item) => item.key === event.target.value,
                      )

                      patch(rule.id, {
                        field: event.target.value as Rule["field"],
                        operator: next?.numeric ? "gt" : "contains",
                        value: "",
                      })
                    }}
                  >
                    {FIELDS.map((item) => (
                      <option key={item.key} value={item.key}>
                        {item.label}
                      </option>
                    ))}
                  </select>
                  <select
                    value={rule.operator}
                    aria-label={`Оператор условия ${index + 1}`}
                    onChange={(event) =>
                      patch(rule.id, { operator: event.target.value })
                    }
                  >
                    {operators.map((item) => (
                      <option key={item.key} value={item.key}>
                        {item.label}
                      </option>
                    ))}
                  </select>
                  <input
                    data-part="value"
                    type={field?.numeric ? "number" : "text"}
                    value={rule.value}
                    aria-label={`Значение условия ${index + 1}`}
                    onChange={(event) =>
                      patch(rule.id, { value: event.target.value })
                    }
                  />
                  <button
                    type="button"
                    data-part="drop"
                    aria-label={`Удалить условие ${index + 1}`}
                    onClick={() =>
                      setRules((current) =>
                        current.filter((item) => item.id !== rule.id),
                      )
                    }
                  >
                    ×
                  </button>
                  {index === 0 ? (
                    <select
                      value={join}
                      aria-label="Связка между условиями"
                      onChange={(event) =>
                        setJoin(event.target.value as "and" | "or")
                      }
                    >
                      <option value="and">все условия (и)</option>
                      <option value="or">любое условие (или)</option>
                    </select>
                  ) : null}
                </div>
              </div>
            )
          })}
          <button
            type="button"
            data-part="add"
            disabled={rules.length >= 4}
            onClick={() =>
              setRules((current) => [
                ...current,
                {
                  id: Math.max(0, ...current.map((rule) => rule.id)) + 1,
                  field: "name",
                  operator: "contains",
                  value: "",
                },
              ])
            }
          >
            + Добавить условие
          </button>
          <p data-part="summary" role="status" aria-live="polite">
            Подошло строк: {filtered.length} из {rows.length}
          </p>
        </fieldset>
        <div
          data-part="scroll"
          role="region"
          aria-label="Таблица контрагентов, прокручивается вбок"
          tabIndex={0}
        >
          <table>
            <caption>{caption}</caption>
            <thead>
              <tr>
                <th scope="col">Контрагент</th>
                <th scope="col">Регион</th>
                <th scope="col" data-align="end">
                  Выручка, тыс. ₽
                </th>
                <th scope="col" data-align="end">
                  Просрочка, дн.
                </th>
              </tr>
            </thead>
            <tbody>
              {filtered.map((row) => (
                <tr key={row.id}>
                  <th scope="row">{row.name}</th>
                  <td>{row.region}</td>
                  <td data-align="end">
                    {row.revenue.toLocaleString("ru-RU")}
                  </td>
                  <td
                    data-align="end"
                    data-late={row.overdue > 0 ? "true" : undefined}
                  >
                    {row.overdue}
                  </td>
                </tr>
              ))}
              {filtered.length === 0 ? (
                <tr>
                  <td colSpan={4} data-part="none">
                    Набор условий не пропустил ни одной строки
                  </td>
                </tr>
              ) : null}
            </tbody>
          </table>
        </div>
      </section>
    </>
  )
}