-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generalise Pagination component for tables
Signed-off-by: Mason Hu <[email protected]>
- Loading branch information
Showing
18 changed files
with
522 additions
and
273 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
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
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
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,186 @@ | ||
import { | ||
Button, | ||
Icon, | ||
Input, | ||
Select, | ||
usePagination, | ||
} from "@canonical/react-components"; | ||
import React, { | ||
ChangeEvent, | ||
Children, | ||
FC, | ||
HTMLAttributes, | ||
PropsWithChildren, | ||
ReactElement, | ||
ReactNode, | ||
RefObject, | ||
cloneElement, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import classnames from "classnames"; | ||
|
||
const figureSmallScreen = (descriptionRef: RefObject<HTMLDivElement>) => { | ||
const descriptionElement = descriptionRef.current; | ||
if (!descriptionElement) { | ||
return true; | ||
} | ||
return descriptionElement.getBoundingClientRect().width < 230; | ||
}; | ||
|
||
const renderChildren = ( | ||
children: ReactNode, | ||
dataForwardProp: string, | ||
data: unknown[], | ||
) => { | ||
return Children.map(children, (child) => { | ||
return cloneElement(child as ReactElement, { | ||
[dataForwardProp]: data, | ||
}); | ||
}); | ||
}; | ||
|
||
const DEFAULT_PAGE_LIMITS = [50, 100, 200]; | ||
const generatePagingOptions = (pageLimits: number[]) => { | ||
return pageLimits.map((limit) => ({ value: limit, label: `${limit}/page` })); | ||
}; | ||
|
||
interface Props { | ||
data: unknown[]; | ||
itemName?: string; | ||
containerClass?: string; | ||
description?: ReactNode; | ||
position?: "below" | "above"; | ||
dataForwardProp?: string; | ||
pageLimits?: number[]; | ||
} | ||
|
||
const TablePagination: FC< | ||
PropsWithChildren<Props> & HTMLAttributes<HTMLDivElement> | ||
> = ({ | ||
data, | ||
containerClass, | ||
itemName = "item", | ||
description, | ||
position = "above", | ||
dataForwardProp = "rows", | ||
pageLimits = DEFAULT_PAGE_LIMITS, | ||
children, | ||
...divProps | ||
}) => { | ||
const descriptionRef = useRef<HTMLDivElement>(null); | ||
const [isSmallScreen, setSmallScreen] = useState(false); | ||
const [pageSize, setPageSize] = useState(() => { | ||
return generatePagingOptions(pageLimits)[0].value; | ||
}); | ||
const { paginate, currentPage, pageData, totalItems } = usePagination(data, { | ||
itemsPerPage: pageSize, | ||
autoResetPage: true, | ||
}); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
setSmallScreen(figureSmallScreen(descriptionRef)); | ||
}; | ||
window.addEventListener("resize", handleResize); | ||
return () => { | ||
window.removeEventListener("resize", handleResize); | ||
}; | ||
}, [isSmallScreen]); | ||
|
||
const handlePageSizeChange = (e: ChangeEvent<HTMLSelectElement>) => { | ||
paginate(1); | ||
setPageSize(parseInt(e.target.value)); | ||
}; | ||
|
||
const getDescription = () => { | ||
if (description) { | ||
return description; | ||
} | ||
|
||
const visibleCount = pageData.length; | ||
|
||
if (isSmallScreen) { | ||
return `${visibleCount} out of ${totalItems}`; | ||
} | ||
|
||
if (visibleCount === totalItems && visibleCount > 1) { | ||
return `Showing all ${totalItems} ${itemName}s`; | ||
} | ||
|
||
return `Showing ${visibleCount} out of ${totalItems} ${itemName}${ | ||
totalItems !== 1 ? "s" : "" | ||
}`; | ||
}; | ||
|
||
const totalPages = Math.ceil(data.length / pageSize); | ||
const clonedChildren = renderChildren(children, dataForwardProp, pageData); | ||
return ( | ||
<> | ||
{position === "below" && clonedChildren} | ||
<div className={classnames("pagination", containerClass)} {...divProps}> | ||
<div className="description" ref={descriptionRef}> | ||
{getDescription()} | ||
</div> | ||
<Button | ||
aria-label="Previous page" | ||
className="back" | ||
appearance="base" | ||
hasIcon | ||
disabled={currentPage === 1} | ||
onClick={() => { | ||
if (currentPage > 1) { | ||
paginate(currentPage - 1); | ||
} | ||
}} | ||
> | ||
<Icon name="chevron-down" /> | ||
</Button> | ||
<Input | ||
id="paginationPageInput" | ||
label="Page number" | ||
labelClassName="u-off-screen" | ||
className="u-no-margin--bottom" | ||
onChange={(e) => { | ||
const newPage = Math.min( | ||
totalPages, | ||
Math.max(1, parseInt(e.target.value)), | ||
); | ||
|
||
paginate(newPage); | ||
}} | ||
value={currentPage} | ||
type="number" | ||
/>{" "} | ||
of {totalPages} | ||
<Button | ||
aria-label="Next page" | ||
className="next" | ||
appearance="base" | ||
hasIcon | ||
disabled={currentPage === totalPages} | ||
onClick={() => { | ||
if (currentPage < totalPages) { | ||
paginate(currentPage + 1); | ||
} | ||
}} | ||
> | ||
<Icon name="chevron-down" /> | ||
</Button> | ||
<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 TablePagination; |
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
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.