256 lines
10 KiB
TypeScript
256 lines
10 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, PopoverClose, PopoverContent, PopoverTrigger } from "../ui/popover"
|
||
import { ScrollArea } from "../ui/scroll-area"
|
||
import {
|
||
Sheet,
|
||
SheetClose,
|
||
SheetContent,
|
||
SheetDescription,
|
||
SheetFooter,
|
||
SheetHeader,
|
||
SheetTitle,
|
||
SheetTrigger,
|
||
} from "../ui/sheet"
|
||
import { Switch } from "../ui/switch"
|
||
|
||
/**
|
||
* Панель фильтров списка: основные фильтры в строку, расширенные (`more`) — в панели справа,
|
||
* которую открывает кнопка «Ещё»; «Ещё» и кнопка сброса — всегда справа.
|
||
* Поиску внутри дают `className="min-w-40 flex-1"` — он забирает свободное место.
|
||
* Фильтры в панели применяются сразу, как и в строке; поля внутри — `FormField`, переключатели — `FilterSwitch`.
|
||
* Сама панель — как форма в разделе: заголовок на мягкой подложке, поля на белой карточке.
|
||
*/
|
||
export function FilterBar({
|
||
more,
|
||
moreCount = 0,
|
||
moreTitle = "Фильтры",
|
||
moreVariant = "sheet",
|
||
activeCount = 0,
|
||
onReset,
|
||
className,
|
||
children,
|
||
}: {
|
||
/** Расширенные фильтры — показываются в панели справа по кнопке «Ещё». */
|
||
more?: React.ReactNode
|
||
/** Сколько из `more` включено: точка на «Ещё», число — в подсказке. */
|
||
moreCount?: number
|
||
/** Заголовок панели. */
|
||
moreTitle?: string
|
||
/** Как открыть расширенные фильтры: в боковой панели или всплывающим слоем над контентом. */
|
||
moreVariant?: "sheet" | "popover"
|
||
/** Сколько фильтров отличается от умолчания: точка на «Сбросить», число — в подсказке. */
|
||
activeCount?: number
|
||
/** Без него кнопки «Сбросить» нет. */
|
||
onReset?: () => void
|
||
className?: string
|
||
children: React.ReactNode
|
||
}) {
|
||
return (
|
||
<div data-slot="filter-bar" className={cn("flex flex-wrap items-start gap-2.5", className)}>
|
||
{/* Фильтры переносятся внутри своего блока, «Ещё» и «Сбросить» стоят справа;
|
||
на узкой панели (фильтрам меньше 16rem) кнопки уходят строкой ниже, но остаются справа. */}
|
||
<div className="flex min-w-[min(100%,16rem)] flex-1 flex-wrap items-center gap-2.5">{children}</div>
|
||
{(more != null || onReset) && (
|
||
<div
|
||
role="group"
|
||
aria-label="Действия с фильтрами"
|
||
className="ml-auto flex h-9 shrink-0 items-center gap-1 rounded-xl bg-muted/70 p-0.5"
|
||
>
|
||
{onReset && (
|
||
<Button
|
||
variant="ghost"
|
||
size="icon-sm"
|
||
disabled={!activeCount}
|
||
onClick={onReset}
|
||
aria-label="Сбросить фильтры"
|
||
title={activeCount ? `Сбросить фильтры: ${activeCount}` : "Нет активных фильтров"}
|
||
className="rounded-lg text-muted-foreground"
|
||
>
|
||
<RotateCwIcon />
|
||
</Button>
|
||
)}
|
||
{more != null && (
|
||
moreVariant === "popover" ? (
|
||
<Popover>
|
||
<PopoverTrigger asChild>
|
||
<MoreButton moreCount={moreCount} />
|
||
</PopoverTrigger>
|
||
<PopoverContent
|
||
align="end"
|
||
sideOffset={8}
|
||
collisionPadding={8}
|
||
className="flex max-h-[min(36rem,calc(100vh-1rem))] w-[min(24rem,calc(100vw-1rem))] flex-col overflow-hidden rounded-xl p-0 shadow-lg"
|
||
>
|
||
{/* Непрозрачная основа: bg-muted/50 сам по себе просвечивает страницу под слоем. */}
|
||
<div className="flex min-h-0 flex-1 flex-col bg-muted/50">
|
||
<MorePanel
|
||
title={moreTitle}
|
||
activeCount={activeCount}
|
||
onReset={onReset}
|
||
done={
|
||
<PopoverClose asChild>
|
||
<Button>Готово</Button>
|
||
</PopoverClose>
|
||
}
|
||
>
|
||
{more}
|
||
</MorePanel>
|
||
</div>
|
||
</PopoverContent>
|
||
</Popover>
|
||
) : (
|
||
<Sheet>
|
||
<SheetTrigger asChild>
|
||
<MoreButton moreCount={moreCount} />
|
||
</SheetTrigger>
|
||
{/* Как форма в разделе: мягкая подложка, заголовок, поля на белой карточке. */}
|
||
<SheetContent
|
||
side="right"
|
||
className="inset-y-2 right-2 h-auto w-[calc(100%-1rem)] gap-0 overflow-hidden rounded-xl border bg-card sm:max-w-sm"
|
||
>
|
||
<div className="flex min-h-0 flex-1 flex-col bg-muted/50">
|
||
<SheetHeader className="gap-0 px-4 pt-3.5 pb-1 pr-10 text-left">
|
||
<SheetTitle className="flex items-center gap-2 text-sm">
|
||
<SlidersHorizontalIcon className="size-4 text-primary" />
|
||
{moreTitle}
|
||
</SheetTitle>
|
||
{/* Описание нужно Radix для доступности; видно только скринридерам. */}
|
||
<SheetDescription className="sr-only">Фильтры применяются сразу</SheetDescription>
|
||
</SheetHeader>
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
<MoreFields>{more}</MoreFields>
|
||
</ScrollArea>
|
||
<SheetFooter className="mt-0 flex-row items-center justify-between gap-2 px-2 py-2">
|
||
<ResetAll activeCount={activeCount} onReset={onReset} />
|
||
<SheetClose asChild>
|
||
<Button>Готово</Button>
|
||
</SheetClose>
|
||
</SheetFooter>
|
||
</div>
|
||
</SheetContent>
|
||
</Sheet>
|
||
)
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** Заголовок панели — как у InfoSection: иконка и название на мягкой подложке, без разделителя. */
|
||
function MoreTitle({ title }: { title: string }) {
|
||
return (
|
||
<div className="flex items-center gap-2 px-4 pt-3.5 pb-1 text-sm font-semibold">
|
||
<SlidersHorizontalIcon className="size-4 text-primary" />
|
||
{title}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** Поля на белой карточке, как форма внутри InfoSection. */
|
||
function MoreFields({ children }: { children: React.ReactNode }) {
|
||
return (
|
||
<div className="px-2 pt-2">
|
||
<div className="flex flex-col gap-4 rounded-lg bg-card p-4 shadow-xs">{children}</div>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
function ResetAll({ activeCount, onReset }: { activeCount: number; onReset?: () => void }) {
|
||
if (!onReset) return <span />
|
||
return (
|
||
<Button variant="ghost" disabled={!activeCount} onClick={onReset} className="text-muted-foreground">
|
||
<RotateCwIcon /> Сбросить все
|
||
</Button>
|
||
)
|
||
}
|
||
|
||
function MorePanel({
|
||
title,
|
||
activeCount,
|
||
onReset,
|
||
done,
|
||
children,
|
||
}: {
|
||
title: string
|
||
activeCount: number
|
||
onReset?: () => void
|
||
done: React.ReactNode
|
||
children: React.ReactNode
|
||
}) {
|
||
return (
|
||
<>
|
||
<MoreTitle title={title} />
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
<MoreFields>{children}</MoreFields>
|
||
</ScrollArea>
|
||
<div className="flex items-center justify-between gap-2 px-2 py-2">
|
||
<ResetAll activeCount={activeCount} onReset={onReset} />
|
||
{done}
|
||
</div>
|
||
</>
|
||
)
|
||
}
|
||
|
||
function MoreButton({
|
||
moreCount,
|
||
className,
|
||
...props
|
||
}: { moreCount: number } & React.ComponentProps<typeof Button>) {
|
||
return (
|
||
<Button
|
||
{...props}
|
||
variant="ghost"
|
||
size="sm"
|
||
title={moreCount ? `Включено расширенных фильтров: ${moreCount}` : undefined}
|
||
className={cn("shrink-0 rounded-lg bg-card text-foreground shadow-xs hover:bg-card/80", className)}
|
||
>
|
||
<SlidersHorizontalIcon />
|
||
<Dotted on={moreCount > 0}>Ещё</Dotted>
|
||
</Button>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* Переключатель для панели расширенных фильтров — как тумблер в форме:
|
||
* тумблер слева, подпись и подсказка справа; нажимается вся строка.
|
||
*/
|
||
export function FilterSwitch({
|
||
id,
|
||
label,
|
||
hint,
|
||
checked,
|
||
onCheckedChange,
|
||
}: {
|
||
id: string
|
||
label: string
|
||
hint?: string
|
||
checked: boolean
|
||
onCheckedChange: (checked: boolean) => void
|
||
}) {
|
||
return (
|
||
<label htmlFor={id} className="flex cursor-pointer items-center gap-3">
|
||
<Switch id={id} checked={checked} onCheckedChange={onCheckedChange} />
|
||
<span className="min-w-0 space-y-0.5">
|
||
<span className="block text-sm leading-none font-medium">{label}</span>
|
||
{hint && <span className="block text-xs text-muted-foreground">{hint}</span>}
|
||
</span>
|
||
</label>
|
||
)
|
||
}
|
||
|
||
/** Подпись кнопки с точкой над концом, когда что-то включено; точка вне потока — ширина кнопки не меняется. */
|
||
function Dotted({ on, children }: { on: boolean; children: React.ReactNode }) {
|
||
return (
|
||
// mr-1 — запас под точку внутри кнопки, чтобы она не прилипала к рамке; есть всегда, ширина постоянная.
|
||
<span className="relative mr-1">
|
||
{children}
|
||
{on && <span aria-hidden className="absolute top-0.5 -right-2 size-1.5 rounded-full bg-primary" />}
|
||
</span>
|
||
)
|
||
}
|