92 lines
3.8 KiB
TypeScript
92 lines
3.8 KiB
TypeScript
import * as React from "react"
|
||
import { RotateCwIcon, SlidersHorizontalIcon } from "lucide-react"
|
||
|
||
import { cn } from "../lib/utils"
|
||
import { Button } from "../ui/button"
|
||
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"
|
||
|
||
/** Ширина элемента; меряется до отрисовки, чтобы панель не мигала при первом показе. */
|
||
function useWidth(ref: React.RefObject<HTMLElement | null>) {
|
||
const [width, setWidth] = React.useState<number | null>(null)
|
||
React.useLayoutEffect(() => {
|
||
const el = ref.current
|
||
if (!el) return
|
||
setWidth(el.clientWidth)
|
||
const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.width))
|
||
observer.observe(el)
|
||
return () => observer.disconnect()
|
||
}, [ref])
|
||
return width
|
||
}
|
||
|
||
/**
|
||
* Панель фильтров списка: основные фильтры в строку, второстепенные (`more`) —
|
||
* в строку на широкой панели и в поповер «Ещё» на узкой, «Сбросить» — всегда последней.
|
||
* Поиску внутри дают `className="min-w-40 flex-1"` — он забирает свободное место.
|
||
*/
|
||
export function FilterBar({
|
||
more,
|
||
moreCount = 0,
|
||
collapseAt = 1100,
|
||
activeCount = 0,
|
||
onReset,
|
||
className,
|
||
children,
|
||
}: {
|
||
/** Второстепенные фильтры: переключатели «Онлайн», «Мои задачи»… */
|
||
more?: React.ReactNode
|
||
/** Сколько из `more` включено — счётчик на кнопке «Ещё». */
|
||
moreCount?: number
|
||
/** Ширина панели (px), ниже которой `more` уходит в поповер. */
|
||
collapseAt?: number
|
||
/** Сколько фильтров отличается от умолчания — счётчик и доступность «Сбросить». */
|
||
activeCount?: number
|
||
/** Без него кнопки «Сбросить» нет. */
|
||
onReset?: () => void
|
||
className?: string
|
||
children: React.ReactNode
|
||
}) {
|
||
const ref = React.useRef<HTMLDivElement>(null)
|
||
const width = useWidth(ref)
|
||
const collapsed = more != null && width != null && width < collapseAt
|
||
|
||
return (
|
||
<div ref={ref} data-slot="filter-bar" className={cn("flex items-start gap-2.5", className)}>
|
||
{/* Фильтры переносятся внутри своего блока — «Сбросить» не уезжает одна на вторую строку. */}
|
||
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-2.5">
|
||
{children}
|
||
{more != null && !collapsed && <div className="flex items-center gap-4 px-1">{more}</div>}
|
||
{collapsed && (
|
||
<Popover>
|
||
<PopoverTrigger asChild>
|
||
<Button variant="outline" size="sm" className="shrink-0">
|
||
<SlidersHorizontalIcon /> Ещё
|
||
{moreCount > 0 && <Count value={moreCount} />}
|
||
</Button>
|
||
</PopoverTrigger>
|
||
<PopoverContent align="end" className="flex w-auto min-w-52 flex-col items-start gap-3 p-3">
|
||
{more}
|
||
</PopoverContent>
|
||
</Popover>
|
||
)}
|
||
</div>
|
||
{onReset && (
|
||
<div className="flex h-9 shrink-0 items-center">
|
||
<Button variant="ghost" size="sm" disabled={!activeCount} onClick={onReset} className="text-muted-foreground">
|
||
<RotateCwIcon /> Сбросить
|
||
{activeCount > 0 && <Count value={activeCount} />}
|
||
</Button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
function Count({ value }: { value: number }) {
|
||
return (
|
||
<span className="grid min-w-4.5 place-items-center rounded-full bg-primary px-1 text-2xs font-semibold text-primary-foreground tabular">
|
||
{value}
|
||
</span>
|
||
)
|
||
}
|