import React, { useEffect } from "react"; import { Button, ButtonGroup, Dropdown, OverlayTrigger, Tooltip, } from "react-bootstrap"; import Mousetrap from "mousetrap"; import { FormattedMessage, useIntl } from "react-intl"; import { IconDefinition } from "@fortawesome/fontawesome-svg-core"; import { Icon } from "../Shared/Icon"; import { faEllipsisH, faPencilAlt, faTrash, } from "@fortawesome/free-solid-svg-icons"; interface IListFilterOperation { text: string; onClick: () => void; isDisplayed?: () => boolean; icon?: IconDefinition; buttonVariant?: string; } interface IListOperationButtonsProps { onSelectAll?: () => void; onSelectNone?: () => void; onEdit?: () => void; onDelete?: () => void; itemsSelected?: boolean; otherOperations?: IListFilterOperation[]; } export const ListOperationButtons: React.FC = ({ onSelectAll, onSelectNone, onEdit, onDelete, itemsSelected, otherOperations, }) => { const intl = useIntl(); useEffect(() => { Mousetrap.bind("s a", () => onSelectAll?.()); Mousetrap.bind("s n", () => onSelectNone?.()); Mousetrap.bind("e", () => { if (itemsSelected) { onEdit?.(); } }); Mousetrap.bind("d d", () => { if (itemsSelected) { onDelete?.(); } }); return () => { Mousetrap.unbind("s a"); Mousetrap.unbind("s n"); Mousetrap.unbind("e"); Mousetrap.unbind("d d"); }; }); function maybeRenderButtons() { const buttons = (otherOperations ?? []).filter((o) => { if (!o.icon) { return false; } if (!o.isDisplayed) { return true; } return o.isDisplayed(); }); if (itemsSelected) { if (onEdit) { buttons.push({ icon: faPencilAlt, text: intl.formatMessage({ id: "actions.edit" }), onClick: onEdit, }); } if (onDelete) { buttons.push({ icon: faTrash, text: intl.formatMessage({ id: "actions.delete" }), onClick: onDelete, buttonVariant: "danger", }); } } if (buttons.length > 0) { return ( {buttons.map((button) => { return ( {button.text}} key={button.text} > ); })} ); } } function renderSelectAll() { if (onSelectAll) { return ( onSelectAll?.()} > ); } } function renderSelectNone() { if (onSelectNone) { return ( onSelectNone?.()} > ); } } function renderMore() { const options = [renderSelectAll(), renderSelectNone()].filter((o) => o); if (otherOperations) { otherOperations .filter((o) => { if (!o.isDisplayed) { return true; } return o.isDisplayed(); }) .forEach((o) => { options.push( {o.text} ); }); } if (options.length > 0) { return ( {options} ); } } return ( <> {maybeRenderButtons()}
{renderMore()}
); };