Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@react-ui] Add initialSelectedPage to Pagination Component #815

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const meta: Meta<
step: 1,
},
},
initialSelectedPage: {
control: {
type: 'number',
min: 1,
max: 20,
step: 1,
},
},
},
};

Expand All @@ -53,9 +61,14 @@ export const Controlled: Story = {
label: 'Label',
timoheddes marked this conversation as resolved.
Show resolved Hide resolved
totalPages: 10,
visiblePageLimit: 3,
initialSelectedPage: 2,
},
render: ({ totalPages, label, visiblePageLimit }) => {
const [page, setPage] = React.useState(1);
render: ({ totalPages, label, visiblePageLimit, initialSelectedPage }) => {
const validInitialSelectedPage =
initialSelectedPage && initialSelectedPage <= totalPages;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also be greater than 0

const [page, setPage] = React.useState(
validInitialSelectedPage ? initialSelectedPage : 1,
);

return (
<Stack direction="column" gap="$4">
Expand All @@ -65,6 +78,7 @@ export const Controlled: Story = {
currentPage={page}
label={label}
visiblePageLimit={visiblePageLimit}
initialSelectedPage={initialSelectedPage}
onPageChange={setPage}
/>
</Stack>
Expand All @@ -77,13 +91,15 @@ export const Uncontrolled: Story = {
label: 'Label',
totalPages: 10,
visiblePageLimit: 3,
initialSelectedPage: 2,
},
render: ({ totalPages, label, visiblePageLimit }) => {
render: ({ totalPages, label, visiblePageLimit, initialSelectedPage }) => {
return (
<Pagination
totalPages={totalPages}
label={label}
visiblePageLimit={visiblePageLimit}
initialSelectedPage={initialSelectedPage}
onPageChange={() => console.log('Updating Page')}
/>
);
Expand Down
25 changes: 17 additions & 8 deletions packages/libs/react-ui/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IPaginationProps {
currentPage?: number;
label: string;
visiblePageLimit?: number;
initialSelectedPage?: number;
onPageChange: (page: number) => void;
}

Expand All @@ -20,9 +21,15 @@ export const Pagination: FC<IPaginationProps> = ({
currentPage,
label,
visiblePageLimit = 3,
initialSelectedPage,
onPageChange,
}) => {
const [_page, setPage] = useState(1);
const validInitialSelectedPage =
initialSelectedPage && initialSelectedPage <= totalPages;

const [_page, setPage] = useState(
validInitialSelectedPage ? initialSelectedPage : 1,
);
const page = currentPage || _page;
const pages = paginate({
page,
Expand All @@ -32,7 +39,7 @@ export const Pagination: FC<IPaginationProps> = ({
const enablePrevious = page > 1;
const enableNext = page < totalPages;

const onClick = (page: number) => {
const onClick = (page: number): void => {
setPage(page);
onPageChange(page);
};
Expand All @@ -49,12 +56,14 @@ export const Pagination: FC<IPaginationProps> = ({
/>
</li>
{pages.map((pageNum) => (
<PageNum
key={pageNum}
number={pageNum}
current={pageNum === page}
onClick={() => onClick(pageNum)}
/>
<li key={pageNum}>
<PageNum
key={pageNum}
number={pageNum}
current={pageNum === page}
onClick={() => onClick(pageNum)}
/>
</li>
))}
<li>
<PageNav
Expand Down