import clsx from 'clsx'; import classes from './MRT_ToolbarAlertBanner.module.css'; import { Fragment, useMemo } from 'react'; import { ActionIcon, Alert, type AlertProps, Badge, Button, Collapse, Flex, Stack, } from '@mantine/core'; 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'; interface Props extends Partial { stackAlertBanner?: boolean; table: MRT_TableInstance; } export const MRT_ToolbarAlertBanner = ({ stackAlertBanner, table, ...rest }: Props) => { 'use no memo'; const { getFilteredSelectedRowModel, getPrePaginationRowModel, getState, options: { enableRowSelection, enableSelectAll, icons: { IconX }, localization, mantineToolbarAlertBannerBadgeProps, mantineToolbarAlertBannerProps, manualPagination, positionToolbarAlertBanner, renderToolbarAlertBannerContent, rowCount, }, } = table; const { density, grouping, rowSelection, showAlertBanner } = getState(); const alertProps = { ...parseFromValuesOrFunc(mantineToolbarAlertBannerProps, { table, }), ...rest, }; const badgeProps = parseFromValuesOrFunc( mantineToolbarAlertBannerBadgeProps, { table }, ); const totalRowCount = rowCount ?? getPrePaginationRowModel().flatRows.length; const selectedRowCount = useMemo( () => manualPagination ? Object.values(rowSelection).filter(Boolean).length : getFilteredSelectedRowModel().rows.length, [rowSelection, totalRowCount, manualPagination], ); const selectedAlert = selectedRowCount ? ( {localization.selectedCountOfRowCountRowsSelected ?.replace('{selectedCount}', selectedRowCount.toString()) ?.replace('{rowCount}', totalRowCount.toString())} ) : null; const groupedAlert = grouping.length > 0 ? ( {localization.groupedBy}{' '} {grouping.map((columnId, index) => ( {index > 0 ? localization.thenBy : ''} table.getColumn(columnId).toggleGrouping()} size="xs" variant="subtle" > } variant="filled" {...badgeProps} > {table.getColumn(columnId).columnDef.header}{' '} ))} ) : null; return ( {renderToolbarAlertBannerContent?.({ groupedAlert, selectedAlert, table, }) ?? ( {enableRowSelection && enableSelectAll && positionToolbarAlertBanner === 'head-overlay' && ( )} {alertProps?.children} {selectedAlert} {groupedAlert} )} ); };