| 1 |
import { useEffect, useRef } from 'react'; |
| 2 |
import { ToggleTemplate, PaginationToggleTemplateProps } from './ToggleTemplate'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Pagination/pagination'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import { fillTemplate } from '../../helpers'; |
| 6 |
import { PaginationNavigation } from './PaginationNavigation'; |
| 7 |
import { PaginationOptionsMenu } from './PaginationOptionsMenu'; |
| 8 |
import { useOUIAProps, OUIAProps } from '../../helpers'; |
| 9 |
import { formatBreakpointMods } from '../../helpers/util'; |
| 10 |
import widthChars from '@patternfly/react-tokens/dist/esm/c_pagination__nav_page_select_c_form_control_width_chars'; |
| 11 |
|
| 12 |
export enum PaginationVariant { |
| 13 |
bottom = 'bottom', |
| 14 |
top = 'top' |
| 15 |
} |
| 16 |
|
| 17 |
const defaultPerPageOptions = [ |
| 18 |
{ |
| 19 |
title: '10', |
| 20 |
value: 10 |
| 21 |
}, |
| 22 |
{ |
| 23 |
title: '20', |
| 24 |
value: 20 |
| 25 |
}, |
| 26 |
{ |
| 27 |
title: '50', |
| 28 |
value: 50 |
| 29 |
}, |
| 30 |
{ |
| 31 |
title: '100', |
| 32 |
value: 100 |
| 33 |
} |
| 34 |
]; |
| 35 |
|
| 36 |
/** Properties to customize the content and behavior of the pagination dropdown options. These |
| 37 |
* properties should be passed into the pagination component's perPageOptions property. |
| 38 |
*/ |
| 39 |
|
| 40 |
export interface PerPageOptions { |
| 41 |
/** The text title of the option, which is rendered inside the pagination dropdown menu. */ |
| 42 |
title?: string; |
| 43 |
/** The value of the option, which determines how many items are displayed per page. */ |
| 44 |
value?: number; |
| 45 |
} |
| 46 |
|
| 47 |
/** Properties to customize various pagination titles. The following properties should be |
| 48 |
* passed into the pagination component's title property. |
| 49 |
*/ |
| 50 |
|
| 51 |
export interface PaginationTitles { |
| 52 |
/** Accessible label for the input displaying the current page. */ |
| 53 |
currPageAriaLabel?: string; |
| 54 |
/** The type or title of the items being paginated. */ |
| 55 |
items?: string; |
| 56 |
/** The title of the pagination options menu. */ |
| 57 |
itemsPerPage?: string; |
| 58 |
/** Label for the English word "of". */ |
| 59 |
ofWord?: string; |
| 60 |
/** Accessible label for the options toggle. */ |
| 61 |
optionsToggleAriaLabel?: string; |
| 62 |
/** The title of a page displayed beside the page number. */ |
| 63 |
page?: string; |
| 64 |
/** The title of a page displayed beside the page number (plural form). */ |
| 65 |
pages?: string; |
| 66 |
/** Accessible label for the pagination component. */ |
| 67 |
paginationAriaLabel?: string; |
| 68 |
/** The suffix to be displayed after each option on the options menu dropdown. */ |
| 69 |
perPageSuffix?: string; |
| 70 |
/** Accessible label for the button which moves to the first page. */ |
| 71 |
toFirstPageAriaLabel?: string; |
| 72 |
/** Accessible label for the button which moves to the last page. */ |
| 73 |
toLastPageAriaLabel?: string; |
| 74 |
/** Accessible label for the button which moves to the next page. */ |
| 75 |
toNextPageAriaLabel?: string; |
| 76 |
/** Accessible label for the button which moves to the previous page. */ |
| 77 |
toPreviousPageAriaLabel?: string; |
| 78 |
} |
| 79 |
|
| 80 |
export type OnSetPage = ( |
| 81 |
event: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 82 |
newPage: number, |
| 83 |
perPage?: number, |
| 84 |
startIdx?: number, |
| 85 |
endIdx?: number |
| 86 |
) => void; |
| 87 |
|
| 88 |
export type OnPerPageSelect = ( |
| 89 |
event: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 90 |
newPerPage: number, |
| 91 |
newPage: number, |
| 92 |
startIdx?: number, |
| 93 |
endIdx?: number |
| 94 |
) => void; |
| 95 |
|
| 96 |
/** The main pagination component. */ |
| 97 |
|
| 98 |
export interface PaginationProps extends React.HTMLProps<HTMLDivElement>, OUIAProps { |
| 99 |
/** What should be rendered inside the pagination. */ |
| 100 |
children?: React.ReactNode; |
| 101 |
/** Additional classes for the pagination container. */ |
| 102 |
className?: string; |
| 103 |
/** Indicate whether to show last full page of results when user selects perPage value |
| 104 |
* greater than remaining rows. |
| 105 |
*/ |
| 106 |
isLastFullPageShown?: boolean; |
| 107 |
/** Direction of dropdown context menu. */ |
| 108 |
dropDirection?: 'up' | 'down'; |
| 109 |
/** Page to start at. */ |
| 110 |
firstPage?: number; |
| 111 |
/** Flag indicating that pagination should use page insets. */ |
| 112 |
usePageInsets?: boolean; |
| 113 |
/** Insets at various breakpoints. */ |
| 114 |
inset?: { |
| 115 |
default?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 116 |
sm?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 117 |
md?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 118 |
lg?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 119 |
xl?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 120 |
'2xl'?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 121 |
}; |
| 122 |
/** Flag indicating if pagination is disabled. */ |
| 123 |
isDisabled?: boolean; |
| 124 |
/** Flag indicating if pagination is compact. */ |
| 125 |
isCompact?: boolean; |
| 126 |
/** Flag indicating if pagination should not be sticky on mobile. */ |
| 127 |
isStatic?: boolean; |
| 128 |
/** Flag indicating if pagination should stick to its position (based on variant). For dynamic sticky control, use isStickyBase |
| 129 |
* and isStickyStuck instead. |
| 130 |
*/ |
| 131 |
isSticky?: boolean; |
| 132 |
/** @beta Flag indicating the pagination should have sticky positioning relative to its container. */ |
| 133 |
isStickyBase?: boolean; |
| 134 |
/** @beta Flag indicating the pagination should have stuck styling, when the pagination is not at the top (for top variant) or bottom (for bottom variant) of the scroll container. */ |
| 135 |
isStickyStuck?: boolean; |
| 136 |
/** Total number of items. */ |
| 137 |
itemCount?: number; |
| 138 |
/** Last index of items on current page. */ |
| 139 |
itemsEnd?: number; |
| 140 |
/** First index of items on current page. */ |
| 141 |
itemsStart?: number; |
| 142 |
/** Start index of rows to display, used in place of providing page. */ |
| 143 |
offset?: number; |
| 144 |
/** Current page number. */ |
| 145 |
page?: number; |
| 146 |
/** Number of items per page. */ |
| 147 |
perPage?: number; |
| 148 |
/** Array of the number of items per page options. */ |
| 149 |
perPageOptions?: PerPageOptions[]; |
| 150 |
/** Function called when user clicks on navigate to first page. */ |
| 151 |
onFirstClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 152 |
/** Function called when user clicks on navigate to last page. */ |
| 153 |
onLastClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 154 |
/** Function called when user clicks on navigate to next page. */ |
| 155 |
onNextClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 156 |
/** Function called when user inputs page number. */ |
| 157 |
onPageInput?: (event: React.KeyboardEvent<HTMLInputElement>, page: number) => void; |
| 158 |
/** Function called when user selects number of items per page. */ |
| 159 |
onPerPageSelect?: OnPerPageSelect; |
| 160 |
/** Function called when user clicks on navigate to previous page. */ |
| 161 |
onPreviousClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 162 |
/** Function called when user sets page. */ |
| 163 |
onSetPage?: OnSetPage; |
| 164 |
/** Object with titles to display in pagination. */ |
| 165 |
titles?: PaginationTitles; |
| 166 |
/** This will be shown in pagination toggle span. You can use firstIndex, lastIndex, |
| 167 |
* itemCount, itemsTitle, and/or ofWord props. |
| 168 |
*/ |
| 169 |
toggleTemplate?: ((props: PaginationToggleTemplateProps) => React.ReactElement<any>) | string; |
| 170 |
/** Position where pagination is rendered. */ |
| 171 |
variant?: 'top' | 'bottom' | PaginationVariant; |
| 172 |
/** Id to identify widget on page. */ |
| 173 |
widgetId?: string; |
| 174 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 175 |
ouiaId?: number | string; |
| 176 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 177 |
ouiaSafe?: boolean; |
| 178 |
/** @beta The container to append the pagination options menu to. */ |
| 179 |
menuAppendTo?: HTMLElement | (() => HTMLElement) | 'inline'; |
| 180 |
} |
| 181 |
|
| 182 |
const handleInputWidth = (lastPage: number, node: HTMLDivElement) => { |
| 183 |
if (!node) { |
| 184 |
return; |
| 185 |
} |
| 186 |
const len = String(lastPage).length; |
| 187 |
if (len >= 3) { |
| 188 |
node.style.setProperty(widthChars.name, `${len}`); |
| 189 |
} else { |
| 190 |
node.style.setProperty(widthChars.name, '2'); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
export const Pagination: React.FunctionComponent<PaginationProps> = ({ |
| 195 |
children, |
| 196 |
className = '', |
| 197 |
variant = PaginationVariant.top, |
| 198 |
isDisabled = false, |
| 199 |
isCompact = false, |
| 200 |
isStatic = false, |
| 201 |
isSticky = false, |
| 202 |
isStickyBase = false, |
| 203 |
isStickyStuck = false, |
| 204 |
dropDirection: dropDirectionProp, |
| 205 |
toggleTemplate, |
| 206 |
perPage = defaultPerPageOptions[0].value, |
| 207 |
titles = { |
| 208 |
items: '', |
| 209 |
page: '', |
| 210 |
pages: '', |
| 211 |
itemsPerPage: 'Items per page', |
| 212 |
perPageSuffix: 'per page', |
| 213 |
toFirstPageAriaLabel: 'Go to first page', |
| 214 |
toPreviousPageAriaLabel: 'Go to previous page', |
| 215 |
toLastPageAriaLabel: 'Go to last page', |
| 216 |
toNextPageAriaLabel: 'Go to next page', |
| 217 |
optionsToggleAriaLabel: '', |
| 218 |
currPageAriaLabel: 'Current page', |
| 219 |
paginationAriaLabel: 'Pagination', |
| 220 |
ofWord: 'of' |
| 221 |
}, |
| 222 |
firstPage = 1, |
| 223 |
page: pageProp = 1, |
| 224 |
offset = null, |
| 225 |
isLastFullPageShown = false, |
| 226 |
itemsStart = null, |
| 227 |
itemsEnd = null, |
| 228 |
itemCount, |
| 229 |
perPageOptions = defaultPerPageOptions, |
| 230 |
widgetId = 'options-menu', |
| 231 |
onSetPage = () => undefined, |
| 232 |
onPerPageSelect = () => undefined, |
| 233 |
onFirstClick = () => undefined, |
| 234 |
onPreviousClick = () => undefined, |
| 235 |
onNextClick = () => undefined, |
| 236 |
onPageInput = () => undefined, |
| 237 |
onLastClick = () => undefined, |
| 238 |
ouiaId, |
| 239 |
ouiaSafe = true, |
| 240 |
usePageInsets, |
| 241 |
inset, |
| 242 |
menuAppendTo, |
| 243 |
...props |
| 244 |
}: PaginationProps) => { |
| 245 |
const paginationRef = useRef<HTMLDivElement>(null); |
| 246 |
const containerRef = useRef<HTMLDivElement>(null); |
| 247 |
|
| 248 |
const getLastPage = () => |
| 249 |
// when itemCount is not known let's set lastPage as page+1 as we don't know the total count |
| 250 |
itemCount || itemCount === 0 ? Math.ceil(itemCount / perPage) || 0 : page + 1; |
| 251 |
|
| 252 |
useEffect(() => { |
| 253 |
const node = paginationRef.current; |
| 254 |
handleInputWidth(getLastPage(), node); |
| 255 |
}, [perPage, itemCount]); |
| 256 |
|
| 257 |
const dropDirection = dropDirectionProp || (variant === 'bottom' && !isStatic ? 'up' : 'down'); |
| 258 |
|
| 259 |
let page = pageProp; |
| 260 |
if (offset !== null) { |
| 261 |
itemsStart = offset + 1; |
| 262 |
page = Math.max(Math.ceil(itemsStart / perPage), 1); |
| 263 |
itemsEnd = offset + perPage; |
| 264 |
} |
| 265 |
|
| 266 |
const lastPage = getLastPage(); |
| 267 |
let firstIndex = (page - 1) * perPage + 1; |
| 268 |
let lastIndex = page * perPage; |
| 269 |
|
| 270 |
if (itemCount || itemCount === 0) { |
| 271 |
firstIndex = itemCount <= 0 ? 0 : (page - 1) * perPage + 1; |
| 272 |
|
| 273 |
if (page < firstPage && itemCount > 0) { |
| 274 |
page = firstPage; |
| 275 |
} else if (page > lastPage) { |
| 276 |
page = lastPage; |
| 277 |
} |
| 278 |
|
| 279 |
if (itemCount >= 0) { |
| 280 |
lastIndex = page === lastPage || itemCount === 0 ? itemCount : page * perPage; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
const PaginationToggleTemplateProps = { |
| 285 |
firstIndex, |
| 286 |
lastIndex, |
| 287 |
itemCount, |
| 288 |
itemsTitle: titles.items, |
| 289 |
ofWord: titles.ofWord |
| 290 |
}; |
| 291 |
|
| 292 |
return ( |
| 293 |
<div |
| 294 |
ref={paginationRef} |
| 295 |
className={css( |
| 296 |
styles.pagination, |
| 297 |
variant === PaginationVariant.bottom && styles.modifiers.bottom, |
| 298 |
usePageInsets && styles.modifiers.pageInsets, |
| 299 |
formatBreakpointMods(inset, styles), |
| 300 |
isStatic && styles.modifiers.static, |
| 301 |
isSticky && !isStickyBase && !isStickyStuck && styles.modifiers.sticky, |
| 302 |
isStickyBase && styles.modifiers.stickyBase, |
| 303 |
isStickyStuck && styles.modifiers.stickyStuck, |
| 304 |
className |
| 305 |
)} |
| 306 |
{...(widgetId && { id: `${widgetId}-${variant}-pagination` })} |
| 307 |
{...useOUIAProps(Pagination.displayName, ouiaId, ouiaSafe, variant)} |
| 308 |
{...props} |
| 309 |
> |
| 310 |
{variant === PaginationVariant.top && ( |
| 311 |
<div className={css(styles.paginationTotalItems)}> |
| 312 |
{toggleTemplate && |
| 313 |
typeof toggleTemplate === 'string' && |
| 314 |
fillTemplate(toggleTemplate, PaginationToggleTemplateProps)} |
| 315 |
{toggleTemplate && |
| 316 |
typeof toggleTemplate !== 'string' && |
| 317 |
(toggleTemplate as (props: PaginationToggleTemplateProps) => React.ReactElement<any>)( |
| 318 |
PaginationToggleTemplateProps |
| 319 |
)} |
| 320 |
{!toggleTemplate && ( |
| 321 |
<ToggleTemplate |
| 322 |
firstIndex={firstIndex} |
| 323 |
lastIndex={lastIndex} |
| 324 |
itemCount={itemCount} |
| 325 |
itemsTitle={titles.items} |
| 326 |
ofWord={titles.ofWord} |
| 327 |
/> |
| 328 |
)} |
| 329 |
</div> |
| 330 |
)} |
| 331 |
{perPageOptions && perPageOptions.length > 0 && ( |
| 332 |
<PaginationOptionsMenu |
| 333 |
itemsPerPageTitle={titles.itemsPerPage} |
| 334 |
perPageSuffix={titles.perPageSuffix} |
| 335 |
itemsTitle={isCompact ? '' : titles.items} |
| 336 |
optionsToggleAriaLabel={titles.optionsToggleAriaLabel} |
| 337 |
perPageOptions={perPageOptions} |
| 338 |
firstIndex={itemsStart !== null ? itemsStart : firstIndex} |
| 339 |
lastIndex={itemsEnd !== null ? itemsEnd : lastIndex} |
| 340 |
ofWord={titles.ofWord} |
| 341 |
isLastFullPageShown={isLastFullPageShown} |
| 342 |
itemCount={itemCount} |
| 343 |
page={page} |
| 344 |
perPage={perPage} |
| 345 |
lastPage={lastPage} |
| 346 |
onPerPageSelect={onPerPageSelect} |
| 347 |
dropDirection={dropDirection} |
| 348 |
widgetId={`${widgetId}-${variant}`} |
| 349 |
toggleTemplate={toggleTemplate} |
| 350 |
isDisabled={isDisabled} |
| 351 |
containerRef={containerRef} |
| 352 |
appendTo={menuAppendTo} |
| 353 |
/> |
| 354 |
)} |
| 355 |
<PaginationNavigation |
| 356 |
pagesTitle={titles.page} |
| 357 |
pagesTitlePlural={titles.pages} |
| 358 |
toLastPageAriaLabel={titles.toLastPageAriaLabel} |
| 359 |
toPreviousPageAriaLabel={titles.toPreviousPageAriaLabel} |
| 360 |
toNextPageAriaLabel={titles.toNextPageAriaLabel} |
| 361 |
toFirstPageAriaLabel={titles.toFirstPageAriaLabel} |
| 362 |
currPageAriaLabel={titles.currPageAriaLabel} |
| 363 |
paginationAriaLabel={titles.paginationAriaLabel} |
| 364 |
ofWord={titles.ofWord} |
| 365 |
page={itemCount && itemCount <= 0 ? 0 : page} |
| 366 |
perPage={perPage} |
| 367 |
itemCount={itemCount} |
| 368 |
firstPage={itemsStart !== null ? itemsStart : 1} |
| 369 |
lastPage={lastPage} |
| 370 |
onSetPage={onSetPage} |
| 371 |
onFirstClick={onFirstClick} |
| 372 |
onPreviousClick={onPreviousClick} |
| 373 |
onNextClick={onNextClick} |
| 374 |
onLastClick={onLastClick} |
| 375 |
onPageInput={onPageInput} |
| 376 |
isDisabled={isDisabled} |
| 377 |
isCompact={isCompact} |
| 378 |
/> |
| 379 |
{children} |
| 380 |
</div> |
| 381 |
); |
| 382 |
}; |
| 383 |
|
| 384 |
Pagination.displayName = 'Pagination'; |