Skip to content

Commit

Permalink
feat(pagination.tsx): next and previous buttons should disable when o…
Browse files Browse the repository at this point in the history
…n 1 and last page respectively (#731)

* feat(pagination.tsx): next and previous buttons should disable when on 1 and last page respectively

* feat(pagination.tsx): created separate button for Next/Previous pagination buttons

fix #726

* fix(paginationbutton.tsx): added default value for disabled prop

#726

---------

Co-authored-by: Lokesh Pathrabe <[email protected]>
  • Loading branch information
lokeshpathrabe and Lokesh Pathrabe authored May 25, 2023
1 parent 8d6d79e commit 8e8531b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
23 changes: 23 additions & 0 deletions src/lib/components/Pagination/Pagination.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ describe('Pagination', () => {
expect(pages()).toEqual([1, 2, 3, 4, 5]);
expect(currentPage()).toEqual(4);
});

it('should disable previous button when on 1st page', async () => {
render(<PaginationTest />);

const firstButton = buttons()[0];

expect(currentPage()).toEqual(1);
expect(firstButton).toBeDisabled();
});

it('should disable next button when on last page', async () => {
const user = userEvent.setup();
render(<PaginationTest />);

const lastButton = buttons()[buttons().length - 1];

for (let i = 0; i < 5; ++i) {
await user.click(nextButton());
}

expect(currentPage()).toEqual(5);
expect(lastButton).toBeDisabled();
});
});

describe('Props', () => {
Expand Down
38 changes: 17 additions & 21 deletions src/lib/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mergeDeep } from '../../helpers/mergeDeep';
import range from '../../helpers/range';
import { useTheme } from '../Flowbite/ThemeContext';
import type { FlowbitePaginationButtonTheme, PaginationButtonProps } from './PaginationButton';
import PaginationButton from './PaginationButton';
import { PaginationButton, PaginationNavigation } from './PaginationButton';

export interface FlowbitePaginationTheme {
base: string;
Expand Down Expand Up @@ -87,16 +87,14 @@ const PaginationComponent: FC<PaginationProps> = ({
)}
<ul className={theme.pages.base}>
<li>
{renderPaginationButton({
className: classNames(classNames(theme.pages.previous.base, showIcon && theme.pages.showIcon)),
onClick: goToPreviousPage,
children: (
<>
{showIcon && <HiChevronLeft aria-hidden className={theme.pages.previous.icon} />}
{previousLabel}
</>
),
})}
<PaginationNavigation
className={classNames(theme.pages.previous.base, showIcon && theme.pages.showIcon)}
onClick={goToPreviousPage}
disabled={currentPage === 1}
>
{showIcon && <HiChevronLeft aria-hidden className={theme.pages.previous.icon} />}
{previousLabel}
</PaginationNavigation>
</li>
{layout === 'pagination' &&
range(firstPage, lastPage).map((page: number) => (
Expand All @@ -112,16 +110,14 @@ const PaginationComponent: FC<PaginationProps> = ({
</li>
))}
<li>
{renderPaginationButton({
className: classNames(theme.pages.next.base, showIcon && theme.pages.showIcon),
onClick: goToNextPage,
children: (
<>
{nextLabel}
{showIcon && <HiChevronRight aria-hidden className={theme.pages.next.icon} />}
</>
),
})}
<PaginationNavigation
className={classNames(theme.pages.next.base, showIcon && theme.pages.showIcon)}
onClick={goToNextPage}
disabled={currentPage === totalPages}
>
{nextLabel}
{showIcon && <HiChevronRight aria-hidden className={theme.pages.next.icon} />}
</PaginationNavigation>
</li>
</ul>
</nav>
Expand Down
37 changes: 35 additions & 2 deletions src/lib/components/Pagination/PaginationButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useTheme } from '../Flowbite/ThemeContext';
export interface FlowbitePaginationButtonTheme {
base: string;
active: string;
disabled: string;
}

export interface PaginationButtonProps extends ComponentProps<'button'> {
Expand All @@ -17,7 +18,11 @@ export interface PaginationButtonProps extends ComponentProps<'button'> {
theme?: DeepPartial<FlowbitePaginationButtonTheme>;
}

const PaginationButton: FC<PaginationButtonProps> = ({
export interface PaginationPrevButtonProps extends Omit<PaginationButtonProps, 'active'> {
disabled?: boolean;
}

export const PaginationButton: FC<PaginationButtonProps> = ({
active,
children,
className,
Expand All @@ -44,4 +49,32 @@ const PaginationButton: FC<PaginationButtonProps> = ({
};

PaginationButton.displayName = 'Pagination.Button';
export default PaginationButton;

export const PaginationNavigation: FC<PaginationPrevButtonProps> = ({
children,
className,
onClick,
theme: customTheme = {},
disabled = false,
...props
}) => {
const theme = mergeDeep(useTheme().theme.pagination, customTheme);

return (
<button
className={classNames(
{
[theme.pages.selector.disabled]: disabled,
},
className,
)}
disabled={disabled}
onClick={onClick}
{...props}
>
{children}
</button>
);
};

PaginationNavigation.displayName = 'Pagination.Navigation';
1 change: 1 addition & 0 deletions src/lib/theme/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ const theme: FlowbiteTheme = {
base: 'w-12 border border-gray-300 bg-white py-2 leading-tight text-gray-500 hover:bg-gray-100 hover:text-gray-700 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white',
active:
'bg-blue-50 text-blue-600 hover:bg-blue-100 hover:text-blue-700 dark:border-gray-700 dark:bg-gray-700 dark:text-white',
disabled: 'opacity-50 cursor-normal',
},
},
},
Expand Down

0 comments on commit 8e8531b

Please sign in to comment.