From 6aa1db25aac3ceac2f53f77544519b3cc60257f7 Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Mon, 26 Oct 2026 17:29:09 +1000 Subject: [PATCH] FilterTabs: multi-select pill tabs with counts and status highlight MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Like ContentTabs variant="pill" but several options can be on at once; an empty value means «Все». Options take count and tone; highlight colors selected options with their status tone. Co-Authored-By: Claude Opus 5.5 --- src/kit/filter-tabs.tsx | 141 ++++++++++++++++++++++++++++++++++++++++ src/kit/index.ts | 1 + 2 files changed, 142 insertions(+) create mode 100644 src/kit/filter-tabs.tsx diff --git a/src/kit/filter-tabs.tsx b/src/kit/filter-tabs.tsx new file mode 100644 index 0000000..0815ac5 --- /dev/null +++ b/src/kit/filter-tabs.tsx @@ -0,0 +1,141 @@ +import * as React from "react" +import { type LucideIcon } from "lucide-react" + +import { cn } from "../lib/utils" +import { StatusDot, type StatusTone } from "./status-badge" + +export interface FilterTabItem { + readonly id: T + readonly label: string + readonly icon?: LucideIcon + /** Счётчик справа от подписи; вариант с нулём приглушён. */ + readonly count?: number + /** Цветная точка статуса; с `highlight` — ещё и цвет выбранного варианта. */ + readonly tone?: StatusTone + readonly disabled?: boolean +} + +// Подсветка выбранного варианта цветом его статуса. +const highlightClass: Record = { + positive: "bg-success-soft text-success ring-success/25", + info: "bg-info-soft text-info ring-info/25", + warning: "bg-warning-soft text-warning ring-warning/30", + negative: "bg-coral-soft text-coral ring-coral/25", + neutral: "bg-card text-foreground ring-border", +} + +/** + * Вкладки-фильтры с множественным выбором — как `ContentTabs variant="pill"`, но можно + * включить несколько вариантов сразу (статусы, типы). Пустой `value` — «Все»: + * первая кнопка `allLabel` выбрана, когда ничего не выбрано, и сбрасывает выбор; + * выбрать все варианты по одному — то же, что «Все». + * `highlight` — выбранные варианты подсвечиваются цветом своего `tone`. + */ +export function FilterTabs({ + items, + value, + onValueChange, + allLabel = "Все", + allCount, + highlight = false, + className, +}: { + items: readonly FilterTabItem[] + value: readonly T[] + onValueChange: (value: T[]) => void + /** Подпись кнопки «Все»; `false` — без неё. */ + allLabel?: string | false + /** Счётчик на «Все»; по умолчанию — сумма счётчиков вариантов. */ + allCount?: number + /** Подсвечивать выбранные варианты цветом статуса. */ + highlight?: boolean + className?: string +}) { + const selected = new Set(value) + const total = allCount ?? (items.every((i) => i.count !== undefined) ? items.reduce((s, i) => s + (i.count ?? 0), 0) : undefined) + + const toggle = (id: T) => { + const next = selected.has(id) ? value.filter((v) => v !== id) : [...value, id] + // Порядок — как у кнопок, а не как нажимали: адрес и сравнение с умолчанием стабильны. + const ordered = items.map((i) => i.id).filter((i) => next.includes(i)) + // Выбраны все варианты — это и есть «Все». + onValueChange(allLabel !== false && ordered.length === items.length ? [] : ordered) + } + + return ( +
+ {allLabel !== false && ( + onValueChange([])}> + {allLabel} + + )} + {items.map((item) => ( + toggle(item.id)} + > + {item.icon && } + {item.tone && } + {item.label} + + ))} +
+ ) +} + +function FilterTabButton({ + active, + count, + tone, + disabled, + onClick, + children, +}: { + active: boolean + count?: number + /** Тон подсветки выбранной кнопки. */ + tone?: StatusTone + disabled?: boolean + onClick: () => void + children: React.ReactNode +}) { + return ( + + ) +} diff --git a/src/kit/index.ts b/src/kit/index.ts index 1ae3cc1..e63ba0b 100644 --- a/src/kit/index.ts +++ b/src/kit/index.ts @@ -17,6 +17,7 @@ export * from "./empty-state" export * from "./file-drop" export * from "./file-gallery" export * from "./filter-bar" +export * from "./filter-tabs" export * from "./form-field" export * from "./info-section" export * from "./loading-bar"