import { Fragment, useMemo } from 'react'; import Alert, { type AlertProps } from '@mui/material/Alert'; import AlertTitle from '@mui/material/AlertTitle'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Chip from '@mui/material/Chip'; import Collapse from '@mui/material/Collapse'; import Stack from '@mui/material/Stack'; import { type MRT_RowData, type MRT_TableInstance } from '../../types'; import { getMRT_SelectAllHandler } from '../../utils/row.utils'; import { parseFromValuesOrFunc } from '../../utils/utils'; import { MRT_SelectCheckbox } from '../inputs/MRT_SelectCheckbox'; export interface MRT_ToolbarAlertBannerProps extends AlertProps { stackAlertBanner?: boolean; table: MRT_TableInstance; } export const MRT_ToolbarAlertBanner = ({ stackAlertBanner, table, ...rest }: MRT_ToolbarAlertBannerProps) => { const { getFilteredSelectedRowModel, getCoreRowModel, getState, options: { enableRowSelection, enableSelectAll, localization, manualPagination, muiToolbarAlertBannerChipProps, muiToolbarAlertBannerProps, positionToolbarAlertBanner, renderToolbarAlertBannerContent, rowCount, }, refs: { tablePaperRef }, } = table; const { density, grouping, rowSelection, showAlertBanner } = getState(); const alertProps = { ...parseFromValuesOrFunc(muiToolbarAlertBannerProps, { table, }), ...rest, }; const chipProps = parseFromValuesOrFunc(muiToolbarAlertBannerChipProps, { table, }); const totalRowCount = rowCount ?? getCoreRowModel().rows.length; const filteredRowCount = getFilteredSelectedRowModel().rows.length; const selectedRowCount = useMemo( () => manualPagination ? Object.values(rowSelection).filter(Boolean).length : filteredRowCount, [rowSelection, totalRowCount, manualPagination, filteredRowCount], ); const selectedAlert = selectedRowCount > 0 ? ( {localization.selectedCountOfRowCountRowsSelected ?.replace('{selectedCount}', selectedRowCount.toLocaleString(localization.language)) ?.replace('{rowCount}', totalRowCount.toLocaleString(localization.language))} ) : null; const groupedAlert = grouping.length > 0 ? ( {localization.groupedBy}{' '} {grouping.map((columnId, index) => ( {index > 0 ? localization.thenBy : ''} table.getColumn(columnId).toggleGrouping()} {...chipProps} /> ))} ) : null; return ( ({ '& .MuiAlert-message': { maxWidth: `calc(${ tablePaperRef.current?.clientWidth ?? 360 }px - 1rem)`, width: '100%', }, borderRadius: 0, fontSize: '1rem', left: 0, mb: stackAlertBanner ? 0 : positionToolbarAlertBanner === 'bottom' ? '-1rem' : undefined, p: 0, position: 'relative', right: 0, top: 0, width: '100%', zIndex: 2, ...(parseFromValuesOrFunc(alertProps?.sx, theme) as any), })} > {renderToolbarAlertBannerContent?.({ groupedAlert, selectedAlert, table, }) ?? ( <> {alertProps?.title && {alertProps.title}} {alertProps?.children} {alertProps?.children && (selectedAlert || groupedAlert) && (
)} {enableRowSelection && enableSelectAll && positionToolbarAlertBanner === 'head-overlay' && ( )}{' '} {selectedAlert} {selectedAlert && groupedAlert &&
} {groupedAlert}
)}
); };