Skip to content

Commit

Permalink
fix: table pagination description adjustments for all screen sizes [W…
Browse files Browse the repository at this point in the history
…D-8561]

Signed-off-by: Mason Hu <mason.hu@canonical.com>
  • Loading branch information
mas-who authored and lorumic committed Feb 7, 2024
1 parent 8fc31de commit f2f84f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Button from "components/Button";
import Icon from "components/Icon";
import Input from "components/Input";
import Select from "components/Select";
import React, { ChangeEvent, HTMLAttributes, useRef } from "react";
import React, { ChangeEvent, HTMLAttributes } from "react";
import classnames from "classnames";
import {
generatePagingOptions,
Expand Down Expand Up @@ -38,8 +38,7 @@ const TablePaginationControls = ({
onPageSizeChange,
...divProps
}: Props): JSX.Element => {
const descriptionRef = useRef<HTMLDivElement>(null);
const isSmallScreen = useFigureSmallScreen({ descriptionRef });
const isSmallScreen = useFigureSmallScreen();

const totalPages = Math.ceil(totalItems / pageSize);
const descriptionDisplay = getDescription({
Expand Down Expand Up @@ -77,7 +76,7 @@ const TablePaginationControls = ({
{...divProps}
role="navigation"
>
<div className="description" ref={descriptionRef}>
<div className="description" id="pagination-description">
{descriptionDisplay}
</div>
<Button
Expand Down Expand Up @@ -111,7 +110,7 @@ const TablePaginationControls = ({
<Icon name="chevron-down" />
</Button>
<Select
className="items-per-page"
className="u-no-margin--bottom"
label="Items per page"
labelClassName="u-off-screen"
id="itemsPerPage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`<TablePagination /> renders table pagination and matches the snapshot 1
>
<div
class="description"
id="pagination-description"
>
Showing all 5 items
</div>
Expand Down Expand Up @@ -72,7 +73,7 @@ exports[`<TablePagination /> renders table pagination and matches the snapshot 1
<select
aria-describedby=""
aria-invalid="false"
class="p-form-validation__input items-per-page"
class="p-form-validation__input u-no-margin--bottom"
id="itemsPerPage"
>
<option
Expand Down
24 changes: 3 additions & 21 deletions src/components/TablePagination/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@ import {
Children,
ReactElement,
ReactNode,
RefObject,
cloneElement,
useEffect,
useState,
} from "react";

/**
* Determine if we are working with a small screen.
* 'small screen' in this case is relative to the width of the description div
*/
export const figureSmallScreen = (
descriptionRef: RefObject<HTMLDivElement>
) => {
const descriptionElement = descriptionRef.current;
if (!descriptionElement) {
return true;
}
return descriptionElement.getBoundingClientRect().width < 230;
};

/**
* Iterate direct react child components and override the value of the prop specified by @param dataForwardProp
* for those child components.
Expand Down Expand Up @@ -78,20 +63,17 @@ export const getDescription = ({
}`;
};

export const useFigureSmallScreen = (args: {
descriptionRef: RefObject<HTMLDivElement>;
}) => {
const { descriptionRef } = args;
export const useFigureSmallScreen = () => {
const [isSmallScreen, setSmallScreen] = useState(false);
useEffect(() => {
const handleResize = () => {
setSmallScreen(figureSmallScreen(descriptionRef));
setSmallScreen(window.innerWidth < 620);
};
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
};
}, [isSmallScreen, descriptionRef]);
}, []);

return isSmallScreen;
};

0 comments on commit f2f84f8

Please sign in to comment.