import React from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { BaseTableData } from '../types'; import { cn } from '../utils/cn'; export interface BatchActionsBarProps { /** * Selected row count */ selectedCount: number; /** * Selected row data */ selectedRows: T[]; /** * Handler for deleting selected rows */ onDelete?: (selectedRows: T[]) => Promise | boolean; /** * Handler for clearing selection */ onClearSelection: () => void; /** * Additional custom actions */ customActions?: React.ReactNode; /** * Class name for styling */ className?: string; /** * Custom text for selection count */ selectionText?: string; /** * Whether to animate the bar * @default true */ animate?: boolean; } /** * BatchActionsBar - A floating bar that appears when rows are selected */ export function BatchActionsBar({ selectedCount, selectedRows, onDelete, onClearSelection, customActions, className, selectionText, animate = true, }: BatchActionsBarProps) { const [isDeleting, setIsDeleting] = React.useState(false); // Handle batch delete action const handleDelete = async () => { if (!onDelete || isDeleting) return; try { setIsDeleting(true); await onDelete(selectedRows); onClearSelection(); } catch (error) { console.error('Error deleting selected rows:', error); } finally { setIsDeleting(false); } }; // If no rows are selected, don't render anything if (selectedCount === 0) { return null; } // The batch actions bar const content = (
{selectionText || `${selectedCount} hàng đã chọn`}
{customActions} {/* Always show delete button when onDelete is provided */} {onDelete && ( )} {/* Always show cancel button */}
); // If animation is enabled, wrap in AnimatePresence + motion.div return (
{animate ? ( {content} ) : ( content )}
); } export default BatchActionsBar;