2020-10-05 18:13:23 +00:00
|
|
|
import React, { MouseEventHandler } from "react";
|
2020-08-24 18:11:17 +00:00
|
|
|
import {
|
|
|
|
ToggleTemplateProps,
|
|
|
|
Toolbar,
|
|
|
|
ToolbarContent,
|
|
|
|
ToolbarItem,
|
|
|
|
InputGroup,
|
|
|
|
TextInput,
|
|
|
|
Button,
|
2020-09-28 15:58:03 +00:00
|
|
|
ButtonVariant,
|
2020-08-24 18:11:17 +00:00
|
|
|
Pagination,
|
2020-09-01 14:51:59 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { SearchIcon } from "@patternfly/react-icons";
|
2020-09-28 15:58:03 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-08-24 18:11:17 +00:00
|
|
|
|
|
|
|
type TableToolbarProps = {
|
|
|
|
count: number;
|
|
|
|
first: number;
|
|
|
|
max: number;
|
|
|
|
onNextClick: (page: number) => void;
|
|
|
|
onPreviousClick: (page: number) => void;
|
|
|
|
onPerPageSelect: (max: number, first: number) => void;
|
|
|
|
toolbarItem?: React.ReactNode;
|
|
|
|
children: React.ReactNode;
|
2020-09-28 15:58:03 +00:00
|
|
|
inputGroupName?: string;
|
|
|
|
inputGroupPlaceholder?: string;
|
|
|
|
inputGroupOnChange?: (
|
|
|
|
newInput: string,
|
|
|
|
event: React.FormEvent<HTMLInputElement>
|
|
|
|
) => void;
|
2020-10-05 18:13:23 +00:00
|
|
|
inputGroupOnClick?: MouseEventHandler;
|
2020-08-24 18:11:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const TableToolbar = ({
|
|
|
|
count,
|
|
|
|
first,
|
|
|
|
max,
|
|
|
|
onNextClick,
|
|
|
|
onPreviousClick,
|
|
|
|
onPerPageSelect,
|
|
|
|
toolbarItem,
|
|
|
|
children,
|
2020-09-28 15:58:03 +00:00
|
|
|
inputGroupName,
|
|
|
|
inputGroupPlaceholder,
|
|
|
|
inputGroupOnChange,
|
2020-10-05 18:13:23 +00:00
|
|
|
inputGroupOnClick,
|
2020-08-24 18:11:17 +00:00
|
|
|
}: TableToolbarProps) => {
|
2020-09-28 15:58:03 +00:00
|
|
|
const { t } = useTranslation("groups");
|
2020-08-24 18:11:17 +00:00
|
|
|
const page = first / max;
|
2020-09-01 14:51:59 +00:00
|
|
|
const pagination = (variant: "top" | "bottom" = "top") => (
|
2020-08-24 18:11:17 +00:00
|
|
|
<Pagination
|
|
|
|
isCompact
|
|
|
|
toggleTemplate={({ firstIndex, lastIndex }: ToggleTemplateProps) => (
|
|
|
|
<b>
|
|
|
|
{firstIndex} - {lastIndex}
|
|
|
|
</b>
|
|
|
|
)}
|
|
|
|
itemCount={count + page * max + (count <= max ? 1 : 0)}
|
|
|
|
page={page + 1}
|
|
|
|
perPage={max}
|
|
|
|
onNextClick={(_, p) => onNextClick((p - 1) * max)}
|
|
|
|
onPreviousClick={(_, p) => onPreviousClick((p - 1) * max)}
|
|
|
|
onPerPageSelect={(_, m, f) => onPerPageSelect(f, m)}
|
|
|
|
variant={variant}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Toolbar>
|
|
|
|
<ToolbarContent>
|
2020-09-28 15:58:03 +00:00
|
|
|
<React.Fragment>
|
|
|
|
{inputGroupName && (
|
|
|
|
<ToolbarItem>
|
|
|
|
<InputGroup>
|
|
|
|
<TextInput
|
|
|
|
name={inputGroupName}
|
|
|
|
id={inputGroupName}
|
|
|
|
type="search"
|
|
|
|
aria-label={t("Search")}
|
|
|
|
placeholder={inputGroupPlaceholder}
|
|
|
|
onChange={inputGroupOnChange}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.control}
|
|
|
|
aria-label={t("Search")}
|
2020-10-05 18:13:23 +00:00
|
|
|
onClick={inputGroupOnClick}
|
2020-09-28 15:58:03 +00:00
|
|
|
>
|
|
|
|
<SearchIcon />
|
|
|
|
</Button>
|
|
|
|
</InputGroup>
|
|
|
|
</ToolbarItem>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
{toolbarItem}
|
2020-08-24 18:11:17 +00:00
|
|
|
<ToolbarItem variant="pagination">{pagination()}</ToolbarItem>
|
|
|
|
</ToolbarContent>
|
|
|
|
</Toolbar>
|
|
|
|
{children}
|
|
|
|
<Toolbar>
|
2020-09-01 14:51:59 +00:00
|
|
|
<ToolbarItem>{pagination("bottom")}</ToolbarItem>
|
2020-08-24 18:11:17 +00:00
|
|
|
</Toolbar>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|