"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { focusAfterDismiss } from "@suphark/ui/lib/focus-after-remove"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronDownIcon, XIcon } from "lucide-react"; import type { ReactNode } from "react"; import { ActionMenu, type ActionMenuItem } from "./action-menu"; export interface SelectionAction { label: string; /** ไม่ต้องส่งเมื่อมี `items` */ onClick?: () => void; /** ปุ่มนี้เปิดเมนู (เช่น "ย้ายไป ▾" / "ตั้งสถานะ ▾") — รายการแบบเดียวกับ `ActionMenu` (เมนูย่อย/ยืนยันได้) */ items?: ReadonlyArray; icon?: ReactNode; destructive?: boolean; disabled?: boolean; } export interface SelectionBarProps { /** จำนวนที่เลือก — 0 = ไม่แสดงอะไรเลย */ count: number; /** ล้างการเลือก (เช่น `table.resetRowSelection`) */ onClear: () => void; /** ปุ่มคำสั่งหมู่ — ใส่เป็น array หรือจะส่ง `children` เป็น element เองก็ได้ */ actions?: ReadonlyArray; children?: ReactNode; /** ข้อความนับ ใช้ `{count}` เช่น "เลือก {count} รายการ" (ไม่ส่ง = `UiText.selection.selected`) */ label?: string; /** * floating = แถบลอยกลางล่างจอ (ค่าเริ่มต้น) · inline = แถบในหน้า เหนือตาราง · * pill = แคปซูลสีหลักลอยกลางล่างจอ (กลับสีเข้ม เด่นกว่า floating, ปุ่มโปร่งบนพื้นสีหลัก) */ variant?: "floating" | "inline" | "pill"; className?: string; } /** * แถบคำสั่งหมู่ที่โผล่เมื่อเลือกหลายแถว — "เลือก N รายการ" + ปุ่มคำสั่ง + ปุ่มล้าง * ใช้คู่ `DataTable` (`table.getFilteredSelectedRowModel().rows`) หรือ `SimpleTable` ที่แอปคุม state เอง * คำสั่งที่ย้อนกลับไม่ได้ให้ส่ง element ของ `ConfirmActionButton`/`DeleteManyButton` ผ่าน `children` * * table.resetRowSelection()} * actions={[{ label: "ส่งออก", onClick: exportRows }, { label: "ลบ", destructive: true, onClick: removeRows }]} /> */ export function SelectionBar({ count, onClear, actions, children, label, variant = "floating", className, }: SelectionBarProps) { const text = useUiText(); if (count <= 0) return null; const pill = variant === "pill"; const template = label ?? text.selection?.selected ?? "เลือก {count} รายการ"; const message = template.replace("{count}", String(count)); return (
{/* ประกาศเฉพาะจำนวน (เดิม aria-live ทั้งแถบ = อ่านปุ่มทุกปุ่มซ้ำทุกครั้งที่เลือกเพิ่ม) */} {message}
{actions?.map((action) => { const button = ( ); return action.items ? ( ) : ( button ); })} {children}
); }