2021-06-15 11:12:32 +00:00
|
|
|
import React, { FormEvent, ReactNode } from "react";
|
2020-10-07 15:47:03 +00:00
|
|
|
import {
|
|
|
|
Pagination,
|
|
|
|
ToggleTemplateProps,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { TableToolbar } from "./TableToolbar";
|
|
|
|
|
|
|
|
type TableToolbarProps = {
|
|
|
|
count: number;
|
|
|
|
first: number;
|
|
|
|
max: number;
|
|
|
|
onNextClick: (page: number) => void;
|
|
|
|
onPreviousClick: (page: number) => void;
|
|
|
|
onPerPageSelect: (max: number, first: number) => void;
|
2021-06-15 11:12:32 +00:00
|
|
|
searchTypeComponent?: ReactNode;
|
|
|
|
toolbarItem?: ReactNode;
|
|
|
|
subToolbar?: ReactNode;
|
|
|
|
children: ReactNode;
|
2020-10-07 15:47:03 +00:00
|
|
|
inputGroupName?: string;
|
|
|
|
inputGroupPlaceholder?: string;
|
|
|
|
inputGroupOnChange?: (
|
|
|
|
newInput: string,
|
2021-06-15 11:12:32 +00:00
|
|
|
event: FormEvent<HTMLInputElement>
|
2020-10-07 15:47:03 +00:00
|
|
|
) => void;
|
2021-03-22 08:14:24 +00:00
|
|
|
inputGroupOnEnter?: (value: string) => void;
|
2020-10-07 15:47:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const PaginatingTableToolbar = ({
|
|
|
|
count,
|
|
|
|
first,
|
|
|
|
max,
|
|
|
|
onNextClick,
|
|
|
|
onPreviousClick,
|
|
|
|
onPerPageSelect,
|
2021-02-23 13:36:37 +00:00
|
|
|
searchTypeComponent,
|
2020-10-07 15:47:03 +00:00
|
|
|
toolbarItem,
|
2021-06-15 11:12:32 +00:00
|
|
|
subToolbar,
|
2020-10-07 15:47:03 +00:00
|
|
|
children,
|
|
|
|
inputGroupName,
|
|
|
|
inputGroupPlaceholder,
|
|
|
|
inputGroupOnChange,
|
2021-03-22 08:14:24 +00:00
|
|
|
inputGroupOnEnter,
|
2020-10-07 15:47:03 +00:00
|
|
|
}: TableToolbarProps) => {
|
2021-02-16 08:39:26 +00:00
|
|
|
const page = Math.round(first / max);
|
2020-10-07 15:47:03 +00:00
|
|
|
const pagination = (variant: "top" | "bottom" = "top") => (
|
|
|
|
<Pagination
|
|
|
|
isCompact
|
|
|
|
toggleTemplate={({ firstIndex, lastIndex }: ToggleTemplateProps) => (
|
|
|
|
<b>
|
2021-06-16 11:35:03 +00:00
|
|
|
{firstIndex} - {lastIndex}
|
2020-10-07 15:47:03 +00:00
|
|
|
</b>
|
|
|
|
)}
|
2021-06-16 11:35:03 +00:00
|
|
|
itemCount={count + page * max}
|
2020-10-07 15:47:03 +00:00
|
|
|
page={page + 1}
|
|
|
|
perPage={max}
|
|
|
|
onNextClick={(_, p) => onNextClick((p - 1) * max)}
|
|
|
|
onPreviousClick={(_, p) => onPreviousClick((p - 1) * max)}
|
2021-02-16 08:39:26 +00:00
|
|
|
onPerPageSelect={(_, m, f) => onPerPageSelect(f - 1, m)}
|
2020-10-07 15:47:03 +00:00
|
|
|
variant={variant}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableToolbar
|
2021-02-23 13:36:37 +00:00
|
|
|
searchTypeComponent={searchTypeComponent}
|
2020-10-07 15:47:03 +00:00
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
{toolbarItem}
|
2021-03-31 08:48:14 +00:00
|
|
|
{count !== 0 && (
|
|
|
|
<ToolbarItem variant="pagination">{pagination()}</ToolbarItem>
|
|
|
|
)}
|
2020-10-07 15:47:03 +00:00
|
|
|
</>
|
|
|
|
}
|
2021-06-15 11:12:32 +00:00
|
|
|
subToolbar={subToolbar}
|
2021-03-31 08:48:14 +00:00
|
|
|
toolbarItemFooter={
|
|
|
|
count !== 0 ? <ToolbarItem>{pagination("bottom")}</ToolbarItem> : <></>
|
|
|
|
}
|
2020-10-07 15:47:03 +00:00
|
|
|
inputGroupName={inputGroupName}
|
|
|
|
inputGroupPlaceholder={inputGroupPlaceholder}
|
|
|
|
inputGroupOnChange={inputGroupOnChange}
|
2021-03-22 08:14:24 +00:00
|
|
|
inputGroupOnEnter={inputGroupOnEnter}
|
2020-10-07 15:47:03 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</TableToolbar>
|
|
|
|
);
|
|
|
|
};
|