import React from 'react'; import { useTranslations } from '@shoptet/ui-core-web'; import { CheckboxField } from '../../Fields/CheckboxField/CheckboxField'; import { dictionary } from './dictionary'; import { MassActionDropdown } from './MassActionDropdown'; import type { MassActionsType } from './types'; export interface MassActionsOptions { /** * Define the mass actions available to the user. * * @default undefined */ massActions: MassActionsType; /** * Displays a checkbox to select all items on all pages. * * @default true */ massActionsApplicableToAll?: boolean; } export interface MassActionsProps extends MassActionsOptions { /** * Defines selected rows for actions to be applied on. * * @default undefined */ selectedRows: T[]; /** * True if all rows on this page are selected. * * @default undefined */ selectedPage: boolean; /** * True if all rows on all pages are selected. * * @default undefined */ selectedAll: boolean; /** * Function that handles selection/deselection of all rows on this page. * * @default undefined */ setSelectPage: (select: boolean) => void; /** * Function that handles selection/deselection of all rows on all pages. * * @default undefined */ toggleSelectAll: () => void; } export const MassActions = ({ massActions, selectedRows, selectedAll, setSelectPage, selectedPage, toggleSelectAll, massActionsApplicableToAll = true, ...rest }: MassActionsProps) => { const translations = useTranslations(dictionary); const nothingSelected = selectedRows.length === 0; const someButNotAllSelected = !nothingSelected && !selectedPage; return (
setSelectPage(nothingSelected), [setSelectPage, nothingSelected])} label={translations.selectAllOnPage} labelHidden />
{massActionsApplicableToAll && (
)}
); };