Skip to content

Commit 9045d92

Browse files
feat(PaginationNav): expose tooltip align props for next/prev buttons (#20451)
* feat(PaginationNav): expose tooltip align props for next/prev buttons * chore: update snapshot * chore: fix ts error --------- Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
1 parent ff8a265 commit 9045d92

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7701,6 +7701,27 @@ Map {
77017701
],
77027702
"type": "oneOf",
77037703
},
7704+
"tooltipAlignment": {
7705+
"args": [
7706+
[
7707+
"start",
7708+
"center",
7709+
"end",
7710+
],
7711+
],
7712+
"type": "oneOf",
7713+
},
7714+
"tooltipPosition": {
7715+
"args": [
7716+
[
7717+
"top",
7718+
"right",
7719+
"bottom",
7720+
"left",
7721+
],
7722+
],
7723+
"type": "oneOf",
7724+
},
77047725
"totalItems": {
77057726
"type": "number",
77067727
},

packages/react/src/components/PaginationNav/PaginationNav-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ describe('PaginationNav', () => {
126126
);
127127
expect(screen.getByLabelText('Select Page number')).toBeInTheDocument();
128128
});
129+
130+
it('should respect tooltipAlignment and tooltipPosition props', () => {
131+
render(
132+
<PaginationNav
133+
totalItems={10}
134+
tooltipAlignment="end"
135+
tooltipPosition="right"
136+
/>
137+
);
138+
139+
expect(
140+
document.querySelector('.cds--popover--right-end')
141+
).toBeInTheDocument();
142+
});
129143
});
130144

131145
describe('behaves as expected', () => {

packages/react/src/components/PaginationNav/PaginationNav.tsx

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { TranslateWithId } from '../../types/common';
1919
import { breakpoints } from '@carbon/layout';
2020
import { useMatchMedia } from '../../internal/useMatchMedia';
2121
import { clamp } from '../../internal/clamp';
22+
import { PopoverAlignment } from '../Popover';
23+
24+
type TooltipAlignment = 'start' | 'center' | 'end';
25+
type TooltipPosition = 'top' | 'right' | 'bottom' | 'left';
2226

2327
const translationIds = {
2428
'carbon.pagination-nav.next': 'Next',
@@ -55,10 +59,7 @@ function calculateCuts(
5559
splitPoint: number | null = null
5660
) {
5761
if (itemsDisplayedOnPage >= totalItems) {
58-
return {
59-
front: 0,
60-
back: 0,
61-
};
62+
return { front: 0, back: 0 };
6263
}
6364

6465
const split = splitPoint || Math.ceil(itemsDisplayedOnPage / 2) - 1;
@@ -76,10 +77,7 @@ function calculateCuts(
7677
backHidden = 0;
7778
}
7879

79-
return {
80-
front: frontHidden,
81-
back: backHidden,
82-
};
80+
return { front: frontHidden, back: backHidden };
8381
}
8482

8583
export interface DirectionButtonProps {
@@ -102,20 +100,37 @@ export interface DirectionButtonProps {
102100
* The callback function called when the button is clicked.
103101
*/
104102
onClick?: React.MouseEventHandler;
103+
104+
/**
105+
* Specify the alignment of the tooltip for the icon-only next/prev buttons.
106+
*/
107+
tooltipAlignment?: TooltipAlignment;
108+
109+
/**
110+
* Specify the position of the tooltip for the icon-only next/prev buttons.
111+
*/
112+
tooltipPosition?: TooltipPosition;
105113
}
106114

107115
function DirectionButton({
108116
direction,
109117
label,
110118
disabled,
111119
onClick,
120+
tooltipAlignment = 'center',
121+
tooltipPosition = 'bottom',
112122
}: DirectionButtonProps) {
113123
const prefix = usePrefix();
114124

125+
const align: PopoverAlignment =
126+
tooltipAlignment === 'center'
127+
? (tooltipPosition as PopoverAlignment)
128+
: (`${tooltipPosition}-${tooltipAlignment}` as PopoverAlignment);
129+
115130
return (
116131
<li className={`${prefix}--pagination-nav__list-item`}>
117132
<IconButton
118-
align="bottom"
133+
align={align}
119134
disabled={disabled}
120135
kind="ghost"
121136
label={label}
@@ -319,6 +334,18 @@ export interface PaginationNavProps
319334
*/
320335
size?: 'sm' | 'md' | 'lg';
321336

337+
/**
338+
* Specify the alignment of the tooltip for the icon-only next/prev buttons.
339+
* Can be one of: start, center, or end.
340+
*/
341+
tooltipAlignment?: TooltipAlignment;
342+
343+
/**
344+
* Specify the position of the tooltip for the icon-only next/prev buttons.
345+
* Can be one of: top, right, bottom, or left.
346+
*/
347+
tooltipPosition?: TooltipPosition;
348+
322349
/**
323350
* The total number of items.
324351
*/
@@ -336,6 +363,8 @@ const PaginationNav = React.forwardRef<HTMLElement, PaginationNavProps>(
336363
page = 0,
337364
loop = false,
338365
size = 'lg',
366+
tooltipAlignment,
367+
tooltipPosition,
339368
translateWithId: t = translateWithId,
340369
...rest
341370
},
@@ -479,6 +508,8 @@ const PaginationNav = React.forwardRef<HTMLElement, PaginationNavProps>(
479508
label={t('carbon.pagination-nav.previous')}
480509
disabled={backwardButtonDisabled}
481510
onClick={jumpToPrevious}
511+
tooltipAlignment={tooltipAlignment}
512+
tooltipPosition={tooltipPosition}
482513
/>
483514

484515
{
@@ -551,6 +582,8 @@ const PaginationNav = React.forwardRef<HTMLElement, PaginationNavProps>(
551582
label={t('carbon.pagination-nav.next')}
552583
disabled={forwardButtonDisabled}
553584
onClick={jumpToNext}
585+
tooltipAlignment={tooltipAlignment}
586+
tooltipPosition={tooltipPosition}
554587
/>
555588
</ul>
556589
<div
@@ -586,6 +619,18 @@ DirectionButton.propTypes = {
586619
* The callback function called when the button is clicked.
587620
*/
588621
onClick: PropTypes.func,
622+
623+
/**
624+
* Specify how the tooltip should align with the navigation button.
625+
* Can be one of: start, center, or end.
626+
*/
627+
tooltipAlignment: PropTypes.oneOf(['start', 'center', 'end']),
628+
629+
/**
630+
* Specify the position of the tooltip relative to the navigation button.
631+
* Can be one of: top, right, bottom, or left.
632+
*/
633+
tooltipPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
589634
};
590635

591636
PaginationItem.propTypes = {
@@ -672,6 +717,18 @@ PaginationNav.propTypes = {
672717
*/
673718
size: PropTypes.oneOf(['sm', 'md', 'lg']),
674719

720+
/**
721+
* Specify the alignment of the tooltip for the icon-only prev/next buttons.
722+
* Can be one of: start, center, or end.
723+
*/
724+
tooltipAlignment: PropTypes.oneOf(['start', 'center', 'end']),
725+
726+
/**
727+
* Specify the position of the tooltip for the icon-only prev/next buttons.
728+
* Can be one of: top, right, bottom, or left.
729+
*/
730+
tooltipPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
731+
675732
/**
676733
* The total number of items.
677734
*/

0 commit comments

Comments
 (0)