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

feat(Pagination): Avoir une valeur props pour la page courante. #206

Merged
merged 10 commits into from
Apr 12, 2021
1 change: 1 addition & 0 deletions packages/react/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'import/no-extraneous-dependencies': ['error',
{ devDependencies: ['src/**/*.test.{ts,tsx}', 'src/test-utils/**/*', 'test/**/*'] },
],
'linebreak-style': 'off',
},
settings: {
'import/resolver': {
Expand Down
16 changes: 14 additions & 2 deletions packages/react/src/components/pagination/pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, useState, VoidFunctionComponent } from 'react';
import React, { ReactElement, useState, VoidFunctionComponent, useEffect } from 'react';
import styled from 'styled-components';
import { useTranslation } from '../../i18n/use-translation';
import { focus } from '../../utils/css-state';
Expand Down Expand Up @@ -107,6 +107,11 @@ interface PaginationProps {
* Function callback when page is changed
*/
onPageChange?(pageNumber: number): void;

/**
* The current active page of the pagination
*/
activePage?: number;
}

export function Pagination({
Expand All @@ -116,16 +121,23 @@ export function Pagination({
defaultActivePage = 1,
pagesShown = 3,
onPageChange = () => undefined,
activePage,
Copy link
Contributor

Choose a reason for hiding this comment

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

Petit dernier détail, il faudrait changer la ligne 129 pour changer
const [currentPage, setCurrentPage] = useState(clamp(defaultActivePage, 1, totalPages));
pour
const [currentPage, setCurrentPage] = useState(clamp(activePage || defaultActivePage, 1, totalPages));
pour prendre en compte le activePage au premier render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

C'est fait

}: PaginationProps): ReactElement {
const { t } = useTranslation('pagination');
const { isMobile } = useDeviceContext();
const pagesDisplayed = Math.min(pagesShown, totalPages);
const [currentPage, setCurrentPage] = useState(clamp(defaultActivePage, 1, totalPages));
const [currentPage, setCurrentPage] = useState(clamp(activePage || defaultActivePage, 1, totalPages));
const canNavigatePrevious = currentPage > 1;
const canNavigateNext = currentPage < totalPages;
const firstLastNavActive = totalPages > 5;
const forwardBackwardNavActive = totalPages > 3 || pagesDisplayed < totalPages;

useEffect(() => {
if (activePage && currentPage !== activePage) {
setCurrentPage(activePage);
}
}, [activePage, currentPage]);

function changePage(page: number): void {
setCurrentPage(page);
maxime-gendron marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Sinon, peut-être juste appeler setCurrentPage ici quand activePage n'est pas défini

Copy link
Contributor Author

Choose a reason for hiding this comment

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

C'est fait !

onPageChange(page);
Expand Down
3 changes: 3 additions & 0 deletions packages/storybook/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
},
},
],
rules: {
'linebreak-style': 'off',
},
settings: {
'import/resolver': {
typescript: {
Expand Down
16 changes: 15 additions & 1 deletion packages/storybook/stories/pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pagination } from '@equisoft/design-elements-react';
import { Story } from '@storybook/react';
import React from 'react';
import React, { useState } from 'react';
import { DeviceContextDecorator } from './utils/device-context-decorator';

export default {
Expand All @@ -16,6 +16,20 @@ export const Normal: Story = () => (
<Pagination totalPages={50} numberOfResults={1530} />
</>
);

export const ControlledPagination: Story = () => {
const [currentPage, setCurrentPage] = useState(1);

return (
<Pagination
totalPages={5}
numberOfResults={100}
onPageChange={setCurrentPage}
activePage={currentPage}
/>
);
};

export const WithoutResults: Story = () => (
<>
<Pagination totalPages={11} />
Expand Down