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: paginationNav support high page counts #15219

Merged
merged 10 commits into from
Dec 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5845,6 +5845,9 @@ Map {
"className": Object {
"type": "string",
},
"disableOverflow": Object {
"type": "bool",
},
"itemsShown": Object {
"type": "number",
},
Expand Down
38 changes: 37 additions & 1 deletion packages/react/src/components/PaginationNav/PaginationNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,32 @@ function PaginationOverflow({
fromIndex,
count,
onSelect,
// eslint-disable-next-line react/prop-types
disableOverflow,
translateWithId: t = translateWithId,
}) {
const prefix = usePrefix();

//If overflow is disabled, return a select tag with no select options
if (disableOverflow === true && count > 1) {
return (
<li className={`${prefix}--pagination-nav__list-item`}>
<div className={`${prefix}--pagination-nav__select`}>
{/* eslint-disable-next-line jsx-a11y/no-onchange */}
<select
className={`${prefix}--pagination-nav__page ${prefix}--pagination-nav__page--select`}
aria-label={`Select ${t('carbon.pagination-nav.item')} number`}
disabled></select>
<div className={`${prefix}--pagination-nav__select-icon-wrapper`}>
<OverflowMenuHorizontal
className={`${prefix}--pagination-nav__select-icon`}
/>
</div>
</div>
</li>
);
}

if (count > 1) {
return (
<li className={`${prefix}--pagination-nav__list-item`}>
Expand Down Expand Up @@ -175,6 +198,7 @@ const PaginationNav = React.forwardRef(function PaginationNav(
className,
onChange = () => {},
totalItems,
disableOverflow,
itemsShown = 10,
page = 0,
loop = false,
Expand All @@ -192,7 +216,7 @@ const PaginationNav = React.forwardRef(function PaginationNav(
);
const prevPage = usePrevious(currentPage);
const prefix = usePrefix();

const [isOverflowDisabled, setIsOverFlowDisabled] = useState(disableOverflow);
function jumpToItem(index) {
if (index >= 0 && index < totalItems) {
setCurrentPage(index);
Expand Down Expand Up @@ -260,6 +284,10 @@ const PaginationNav = React.forwardRef(function PaginationNav(
}
}, [currentPage]); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
setIsOverFlowDisabled(disableOverflow);
}, [disableOverflow]);

const classNames = classnames(`${prefix}--pagination-nav`, className);

const backwardButtonDisabled = !loop && currentPage === 0;
Expand Down Expand Up @@ -297,6 +325,7 @@ const PaginationNav = React.forwardRef(function PaginationNav(
fromIndex={startOffset}
count={cuts.front}
onSelect={jumpToItem}
disableOverflow={isOverflowDisabled}
/>

{
Expand All @@ -322,6 +351,7 @@ const PaginationNav = React.forwardRef(function PaginationNav(
fromIndex={totalItems - cuts.back - 1}
count={cuts.back}
onSelect={jumpToItem}
disableOverflow={isOverflowDisabled}
/>

{
Expand Down Expand Up @@ -432,6 +462,12 @@ PaginationNav.propTypes = {
*/
className: PropTypes.string,

/**
* If true, the '...' pagination overflow will not render page links between the first and last rendered buttons.
* Set this to true if you are having performance problems with large data sets.
*/
disableOverflow: PropTypes.bool, // eslint-disable-line react/prop-types

/**
* The number of items to be shown.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Playground.args = {
itemsShown: 10,
page: 0,
totalItems: 25,
disableOverflow: false,
};

Playground.argTypes = {
Expand All @@ -55,4 +56,9 @@ Playground.argTypes = {
type: 'number',
},
},
disableOverflow: {
control: {
type: 'boolean',
},
},
};