FilterBar: extended filters in a right-side drawer
The «Ещё» button now always opens a sheet on the right with the `more` filters (applied immediately, «Сбросить все» and «Готово» in the footer). «Ещё» and «Сбросить» sit at the right edge and wrap below on narrow bars. New FilterSwitch for toggles in the drawer. collapseAt is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
50a01fc34e
commit
c9be29e6fb
|
|
@ -3,42 +3,41 @@ import { RotateCwIcon, SlidersHorizontalIcon } from "lucide-react"
|
||||||
|
|
||||||
import { cn } from "../lib/utils"
|
import { cn } from "../lib/utils"
|
||||||
import { Button } from "../ui/button"
|
import { Button } from "../ui/button"
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"
|
import { Label } from "../ui/label"
|
||||||
|
import { ScrollArea } from "../ui/scroll-area"
|
||||||
/** Ширина элемента; меряется до отрисовки, чтобы панель не мигала при первом показе. */
|
import {
|
||||||
function useWidth(ref: React.RefObject<HTMLElement | null>) {
|
Sheet,
|
||||||
const [width, setWidth] = React.useState<number | null>(null)
|
SheetClose,
|
||||||
React.useLayoutEffect(() => {
|
SheetContent,
|
||||||
const el = ref.current
|
SheetDescription,
|
||||||
if (!el) return
|
SheetFooter,
|
||||||
setWidth(el.clientWidth)
|
SheetHeader,
|
||||||
const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.width))
|
SheetTitle,
|
||||||
observer.observe(el)
|
SheetTrigger,
|
||||||
return () => observer.disconnect()
|
} from "../ui/sheet"
|
||||||
}, [ref])
|
import { Switch } from "../ui/switch"
|
||||||
return width
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Панель фильтров списка: основные фильтры в строку, второстепенные (`more`) —
|
* Панель фильтров списка: основные фильтры в строку, расширенные (`more`) — в панели справа,
|
||||||
* в строку на широкой панели и в поповер «Ещё» на узкой, «Сбросить» — всегда последней.
|
* которую открывает кнопка «Ещё» со счётчиком включённых; «Ещё» и «Сбросить» — всегда справа.
|
||||||
* Поиску внутри дают `className="min-w-40 flex-1"` — он забирает свободное место.
|
* Поиску внутри дают `className="min-w-40 flex-1"` — он забирает свободное место.
|
||||||
|
* Фильтры в панели применяются сразу, как и в строке; поля внутри — `FormField`, переключатели — `FilterSwitch`.
|
||||||
*/
|
*/
|
||||||
export function FilterBar({
|
export function FilterBar({
|
||||||
more,
|
more,
|
||||||
moreCount = 0,
|
moreCount = 0,
|
||||||
collapseAt = 1100,
|
moreTitle = "Фильтры",
|
||||||
activeCount = 0,
|
activeCount = 0,
|
||||||
onReset,
|
onReset,
|
||||||
className,
|
className,
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
/** Второстепенные фильтры: переключатели «Онлайн», «Мои задачи»… */
|
/** Расширенные фильтры — показываются в панели справа по кнопке «Ещё». */
|
||||||
more?: React.ReactNode
|
more?: React.ReactNode
|
||||||
/** Сколько из `more` включено — счётчик на кнопке «Ещё». */
|
/** Сколько из `more` включено — счётчик на кнопке «Ещё». */
|
||||||
moreCount?: number
|
moreCount?: number
|
||||||
/** Ширина панели (px), ниже которой `more` уходит в поповер. */
|
/** Заголовок панели. */
|
||||||
collapseAt?: number
|
moreTitle?: string
|
||||||
/** Сколько фильтров отличается от умолчания — счётчик и доступность «Сбросить». */
|
/** Сколько фильтров отличается от умолчания — счётчик и доступность «Сбросить». */
|
||||||
activeCount?: number
|
activeCount?: number
|
||||||
/** Без него кнопки «Сбросить» нет. */
|
/** Без него кнопки «Сбросить» нет. */
|
||||||
|
|
@ -46,42 +45,84 @@ export function FilterBar({
|
||||||
className?: string
|
className?: string
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}) {
|
}) {
|
||||||
const ref = React.useRef<HTMLDivElement>(null)
|
|
||||||
const width = useWidth(ref)
|
|
||||||
const collapsed = more != null && width != null && width < collapseAt
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={ref} data-slot="filter-bar" className={cn("flex items-start gap-2.5", className)}>
|
<div data-slot="filter-bar" className={cn("flex flex-wrap items-start gap-2.5", className)}>
|
||||||
{/* Фильтры переносятся внутри своего блока — «Сбросить» не уезжает одна на вторую строку. */}
|
{/* Фильтры переносятся внутри своего блока, «Ещё» и «Сбросить» стоят справа;
|
||||||
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-2.5">
|
на узкой панели (фильтрам меньше 16rem) кнопки уходят строкой ниже, но остаются справа. */}
|
||||||
{children}
|
<div className="flex min-w-[min(100%,16rem)] flex-1 flex-wrap items-center gap-2.5">{children}</div>
|
||||||
{more != null && !collapsed && <div className="flex items-center gap-4 px-1">{more}</div>}
|
{(more != null || onReset) && (
|
||||||
{collapsed && (
|
<div className="ml-auto flex h-9 shrink-0 items-center gap-1">
|
||||||
<Popover>
|
{more != null && (
|
||||||
<PopoverTrigger asChild>
|
<Sheet>
|
||||||
<Button variant="outline" size="sm" className="shrink-0">
|
<SheetTrigger asChild>
|
||||||
<SlidersHorizontalIcon /> Ещё
|
<Button variant="outline" size="sm" className="shrink-0">
|
||||||
{moreCount > 0 && <Count value={moreCount} />}
|
<SlidersHorizontalIcon /> Ещё
|
||||||
</Button>
|
{moreCount > 0 && <Count value={moreCount} />}
|
||||||
</PopoverTrigger>
|
</Button>
|
||||||
<PopoverContent align="end" className="flex w-auto min-w-52 flex-col items-start gap-3 p-3">
|
</SheetTrigger>
|
||||||
{more}
|
<SheetContent side="right" className="gap-0 sm:max-w-sm">
|
||||||
</PopoverContent>
|
<SheetHeader className="border-b px-5 py-4 pr-12">
|
||||||
</Popover>
|
<SheetTitle>{moreTitle}</SheetTitle>
|
||||||
)}
|
{/* Описание нужно Radix для доступности; видно только скринридерам. */}
|
||||||
</div>
|
<SheetDescription className="sr-only">Фильтры применяются сразу</SheetDescription>
|
||||||
{onReset && (
|
</SheetHeader>
|
||||||
<div className="flex h-9 shrink-0 items-center">
|
<ScrollArea className="min-h-0 flex-1">
|
||||||
<Button variant="ghost" size="sm" disabled={!activeCount} onClick={onReset} className="text-muted-foreground">
|
<div className="flex flex-col gap-5 p-5">{more}</div>
|
||||||
<RotateCwIcon /> Сбросить
|
</ScrollArea>
|
||||||
{activeCount > 0 && <Count value={activeCount} />}
|
<SheetFooter className="flex-row justify-between border-t px-5 py-3">
|
||||||
</Button>
|
{onReset ? (
|
||||||
|
<Button variant="ghost" size="sm" disabled={!activeCount} onClick={onReset} className="text-muted-foreground">
|
||||||
|
<RotateCwIcon /> Сбросить все
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<span />
|
||||||
|
)}
|
||||||
|
<SheetClose asChild>
|
||||||
|
<Button size="sm">Готово</Button>
|
||||||
|
</SheetClose>
|
||||||
|
</SheetFooter>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
)}
|
||||||
|
{onReset && (
|
||||||
|
<Button variant="ghost" size="sm" disabled={!activeCount} onClick={onReset} className="text-muted-foreground">
|
||||||
|
<RotateCwIcon /> Сбросить
|
||||||
|
{activeCount > 0 && <Count value={activeCount} />}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Переключатель для панели расширенных фильтров: подпись слева, тумблер справа, подсказка под подписью. */
|
||||||
|
export function FilterSwitch({
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
hint,
|
||||||
|
checked,
|
||||||
|
onCheckedChange,
|
||||||
|
}: {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
hint?: string
|
||||||
|
checked: boolean
|
||||||
|
onCheckedChange: (checked: boolean) => void
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-start justify-between gap-4">
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
<Label htmlFor={id} className="font-normal">
|
||||||
|
{label}
|
||||||
|
</Label>
|
||||||
|
{hint && <p className="text-xs text-muted-foreground">{hint}</p>}
|
||||||
|
</div>
|
||||||
|
<Switch id={id} checked={checked} onCheckedChange={onCheckedChange} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function Count({ value }: { value: number }) {
|
function Count({ value }: { value: number }) {
|
||||||
return (
|
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">
|
<span className="grid min-w-4.5 place-items-center rounded-full bg-primary px-1 text-2xs font-semibold text-primary-foreground tabular">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue