-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement server pagination mode for TablePagination
Signed-off-by: Mason Hu <[email protected]>
- Loading branch information
Showing
6 changed files
with
557 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { | ||
ChangeEvent, | ||
HTMLAttributes, | ||
PropsWithChildren, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import classnames from "classnames"; | ||
import { usePagination } from "hooks"; | ||
import Select from "components/Select"; | ||
import TablePaginationControls from "./TablePaginationControls"; | ||
import "./TablePagination.scss"; | ||
import { BasePaginationProps } from "./TablePagination"; | ||
import { | ||
DEFAULT_PAGE_LIMITS, | ||
generatePagingOptions, | ||
getDescription, | ||
renderChildren, | ||
useFigureSmallScreen, | ||
} from "./utils"; | ||
|
||
export type ClientPaginationProps = BasePaginationProps & { | ||
/** | ||
* set pagination to be server based making the component purely presentational or | ||
* set pagination to be client based which will delegate control to this component | ||
*/ | ||
type?: "client-pagination"; | ||
}; | ||
|
||
export type Props = PropsWithChildren<ClientPaginationProps> & | ||
HTMLAttributes<HTMLDivElement>; | ||
|
||
const ClientPagination = ({ | ||
data, | ||
className, | ||
itemName = "item", | ||
description, | ||
position = "above", | ||
dataForwardProp = "rows", | ||
pageLimits = DEFAULT_PAGE_LIMITS, | ||
children, | ||
...divProps | ||
}: Props) => { | ||
const descriptionRef = useRef<HTMLDivElement>(null); | ||
const isSmallScreen = useFigureSmallScreen({ descriptionRef }); | ||
const [pageSize, setPageSize] = useState(() => { | ||
return generatePagingOptions(pageLimits)[0].value; | ||
}); | ||
const { paginate, currentPage, pageData, totalItems } = usePagination(data, { | ||
itemsPerPage: pageSize, | ||
autoResetPage: true, | ||
}); | ||
|
||
const handlePageSizeChange = (e: ChangeEvent<HTMLSelectElement>) => { | ||
paginate(1); | ||
setPageSize(parseInt(e.target.value)); | ||
}; | ||
|
||
const totalPages = Math.ceil(data.length / pageSize); | ||
const clonedChildren = renderChildren(children, dataForwardProp, pageData); | ||
const descriptionDisplay = getDescription({ | ||
description, | ||
data: pageData, | ||
isSmallScreen, | ||
totalItems, | ||
itemName, | ||
}); | ||
|
||
return ( | ||
<> | ||
{position === "below" && clonedChildren} | ||
<div | ||
className={classnames("pagination", className)} | ||
{...divProps} | ||
role="navigation" | ||
> | ||
<div className="description" ref={descriptionRef}> | ||
{descriptionDisplay} | ||
</div> | ||
<TablePaginationControls | ||
onPageChange={paginate} | ||
currentPage={currentPage} | ||
totalPages={totalPages} | ||
/> | ||
<Select | ||
className="items-per-page" | ||
label="Items per page" | ||
labelClassName="u-off-screen" | ||
id="itemsPerPage" | ||
options={generatePagingOptions(pageLimits)} | ||
onChange={handlePageSizeChange} | ||
value={pageSize} | ||
/> | ||
</div> | ||
{position === "above" && clonedChildren} | ||
</> | ||
); | ||
}; | ||
|
||
export default ClientPagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import React, { | ||
ChangeEvent, | ||
HTMLAttributes, | ||
PropsWithChildren, | ||
useRef, | ||
} from "react"; | ||
import classnames from "classnames"; | ||
import Select from "components/Select"; | ||
import TablePaginationControls from "./TablePaginationControls"; | ||
import "./TablePagination.scss"; | ||
import { BasePaginationProps } from "./TablePagination"; | ||
import { | ||
DEFAULT_PAGE_LIMITS, | ||
generatePagingOptions, | ||
getDescription, | ||
renderChildren, | ||
useFigureSmallScreen, | ||
} from "./utils"; | ||
|
||
export type ServerPaginationProps = BasePaginationProps & { | ||
/** | ||
* set pagination to be server based making the component purely presentational or | ||
* set pagination to be client based which will delegate control to this component | ||
*/ | ||
type?: "server-pagination"; | ||
/** | ||
* the total number of items available within the data. This prop is only relevant if | ||
* `type` is set to `server-pagination`. | ||
*/ | ||
totalItems: number; | ||
/** | ||
* the current page that's showing. This prop is only relevant if | ||
* `type` is set to `server-pagination`. | ||
*/ | ||
currentPage: number; | ||
/** | ||
* size per page. This prop is only relevant if `type` is set to `server-pagination`. | ||
*/ | ||
pageSize: number; | ||
/** | ||
* callback indicating a page change event to the parent component. | ||
* This prop is only relevant if `type` is set to `server-pagination`. | ||
*/ | ||
onPageChange: (page: number) => void; | ||
/** | ||
* callback indicating a page size change event to the parent component. | ||
* This prop is only relevant if `type` is set to `server-pagination`. | ||
*/ | ||
onPageSizeChange: (page: number) => void; | ||
}; | ||
|
||
export type Props = PropsWithChildren<ServerPaginationProps> & | ||
HTMLAttributes<HTMLDivElement>; | ||
|
||
const ServerPagination = ({ | ||
data, | ||
className, | ||
itemName = "item", | ||
description, | ||
position = "above", | ||
dataForwardProp = "rows", | ||
pageLimits = DEFAULT_PAGE_LIMITS, | ||
totalItems, | ||
currentPage, | ||
pageSize, | ||
onPageChange, | ||
onPageSizeChange, | ||
children, | ||
...divProps | ||
}: Props) => { | ||
const descriptionRef = useRef<HTMLDivElement>(null); | ||
const isSmallScreen = useFigureSmallScreen({ descriptionRef }); | ||
|
||
const handlePageSizeChange = (e: ChangeEvent<HTMLSelectElement>) => { | ||
onPageSizeChange(parseInt(e.target.value)); | ||
}; | ||
|
||
const totalPages = Math.ceil(totalItems / pageSize); | ||
const clonedChildren = renderChildren(children, dataForwardProp, data); | ||
const descriptionDisplay = getDescription({ | ||
description, | ||
data, | ||
isSmallScreen, | ||
totalItems, | ||
itemName, | ||
}); | ||
|
||
return ( | ||
<> | ||
{position === "below" && clonedChildren} | ||
<div | ||
className={classnames("pagination", className)} | ||
{...divProps} | ||
role="navigation" | ||
> | ||
<div className="description" ref={descriptionRef}> | ||
{descriptionDisplay} | ||
</div> | ||
<TablePaginationControls | ||
onPageChange={onPageChange} | ||
currentPage={currentPage} | ||
totalPages={totalPages} | ||
/> | ||
<Select | ||
className="items-per-page" | ||
label="Items per page" | ||
labelClassName="u-off-screen" | ||
id="itemsPerPage" | ||
options={generatePagingOptions(pageLimits)} | ||
onChange={handlePageSizeChange} | ||
value={pageSize} | ||
/> | ||
</div> | ||
{position === "above" && clonedChildren} | ||
</> | ||
); | ||
}; | ||
|
||
export default ServerPagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.