"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { focusAfterRemove } from "@suphark/ui/lib/focus-after-remove"; import { cn } from "@suphark/ui/lib/utils"; import { XIcon } from "lucide-react"; import type { ReactNode } from "react"; export interface ActiveFilterChip { key: string; /** ชื่อตัวกรอง เช่น "หมวด" */ label: string; /** ค่าที่เลือก เช่น "คอนกรีต, เหล็ก" / "≥ ฿50,000" */ value: ReactNode; onRemove: () => void; } export interface ActiveFilterChipsProps { items: ReadonlyArray; /** "ล้างทั้งหมด" — แสดงเมื่อมีชิปตั้งแต่ 2 อัน */ onClearAll?: () => void; /** ข้อความนำหน้า เช่น "พบ 37 จาก 248 รายการ" */ summary?: ReactNode; className?: string; } /** * ชิปสรุปตัวกรองที่ใช้อยู่ + ปุ่ม × ต่อชิป + "ล้างทั้งหมด" — สำหรับหน้าที่ไม่ใช้ DataTable (DataTable มีชิปในตัวแล้ว) * จอแคบ: แถวเดียวเลื่อนแนวนอน ปุ่มล้างค้างท้าย · ไม่มีชิป = ไม่แสดงอะไร * * setCats([]) }]} /> */ export function ActiveFilterChips({ items, onClearAll, summary, className, }: ActiveFilterChipsProps) { const t = useUiText().filterChips; if (items.length === 0) return null; // ล้างตัวสุดท้าย = แถบนี้หายทั้งแถบ → ไปตัวควบคุมแรกของกล่องที่ครอบ (ช่องค้นหา/ปุ่มตัวกรอง) const keepFocus = (button: HTMLElement) => { const root = button.closest('[data-slot="active-filter-chips"]'); const parent = root?.parentElement; focusAfterRemove(button, root, () => parent?.querySelector("input, button, a[href]"), ); }; return (
{summary && ( {summary} )}
{items.map((item) => ( {item.label} {item.value} ))}
{onClearAll && items.length > 1 && ( )}
); }