| 1 |
import { Component } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Pagination/pagination'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon'; |
| 5 |
import RhMicronsDoubleCaretLeftIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-double-caret-left-icon'; |
| 6 |
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon'; |
| 7 |
import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-right-icon'; |
| 8 |
import { Button, ButtonVariant } from '../Button'; |
| 9 |
import { TextInput } from '../TextInput'; |
| 10 |
import { OnSetPage } from './Pagination'; |
| 11 |
import { pluralize, PickOptional } from '../../helpers'; |
| 12 |
import { KeyTypes } from '../../helpers/constants'; |
| 13 |
|
| 14 |
export interface PaginationNavigationProps extends React.HTMLProps<HTMLElement> { |
| 15 |
/** Additional classes for the pagination navigation container. */ |
| 16 |
className?: string; |
| 17 |
/** Accessible label for the input displaying the current page. */ |
| 18 |
currPageAriaLabel?: string; |
| 19 |
/** The number of first page where pagination starts. */ |
| 20 |
firstPage?: number; |
| 21 |
/** Flag indicating if the pagination is compact. */ |
| 22 |
isCompact?: boolean; |
| 23 |
/** Flag indicating if the pagination is disabled. */ |
| 24 |
isDisabled?: boolean; |
| 25 |
/** Total number of items. */ |
| 26 |
itemCount?: number; |
| 27 |
/** The number of the last page. */ |
| 28 |
lastPage?: number; |
| 29 |
/** Label for the English word "of". */ |
| 30 |
ofWord?: string; |
| 31 |
/** The number of the current page. */ |
| 32 |
page: number; |
| 33 |
/** The title of a page displayed beside the page number. */ |
| 34 |
pagesTitle?: string; |
| 35 |
/** The title of a page displayed beside the page number (the plural form). */ |
| 36 |
pagesTitlePlural?: string; |
| 37 |
/** Accessible label for the pagination component. */ |
| 38 |
paginationAriaLabel?: string; |
| 39 |
/** Number of items per page. */ |
| 40 |
perPage?: number; |
| 41 |
/** Accessible label for the button which moves to the first page. */ |
| 42 |
toFirstPageAriaLabel?: string; |
| 43 |
/** Accessible label for the button which moves to the last page. */ |
| 44 |
toLastPageAriaLabel?: string; |
| 45 |
/** Accessible label for the button which moves to the next page. */ |
| 46 |
toNextPageAriaLabel?: string; |
| 47 |
/** Accessible label for the button which moves to the previous page. */ |
| 48 |
toPreviousPageAriaLabel?: string; |
| 49 |
/** Function called when user clicks to navigate to first page. */ |
| 50 |
onFirstClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 51 |
/** Function called when user clicks to navigate to last page. */ |
| 52 |
onLastClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 53 |
/** Function called when user clicks to navigate to next page. */ |
| 54 |
onNextClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 55 |
/** Function called when user clicks to navigate to previous page. */ |
| 56 |
onPreviousClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void; |
| 57 |
/** Function called when user inputs page number. */ |
| 58 |
onPageInput?: (event: React.KeyboardEvent<HTMLInputElement>, page: number) => void; |
| 59 |
/** Function called when page is changed. */ |
| 60 |
onSetPage: OnSetPage; |
| 61 |
} |
| 62 |
|
| 63 |
export interface PaginationNavigationState { |
| 64 |
userInputPage?: number | string; |
| 65 |
} |
| 66 |
|
| 67 |
class PaginationNavigation extends Component<PaginationNavigationProps, PaginationNavigationState> { |
| 68 |
static displayName = 'PaginationNavigation'; |
| 69 |
constructor(props: PaginationNavigationProps) { |
| 70 |
super(props); |
| 71 |
this.state = { userInputPage: this.props.page }; |
| 72 |
} |
| 73 |
|
| 74 |
static defaultProps: PickOptional<PaginationNavigationProps> = { |
| 75 |
className: '', |
| 76 |
isDisabled: false, |
| 77 |
isCompact: false, |
| 78 |
lastPage: 0, |
| 79 |
firstPage: 0, |
| 80 |
pagesTitle: '', |
| 81 |
pagesTitlePlural: '', |
| 82 |
toLastPageAriaLabel: 'Go to last page', |
| 83 |
toNextPageAriaLabel: 'Go to next page', |
| 84 |
toFirstPageAriaLabel: 'Go to first page', |
| 85 |
toPreviousPageAriaLabel: 'Go to previous page', |
| 86 |
currPageAriaLabel: 'Current page', |
| 87 |
paginationAriaLabel: 'Pagination', |
| 88 |
ofWord: 'of', |
| 89 |
onNextClick: () => undefined as any, |
| 90 |
onPreviousClick: () => undefined as any, |
| 91 |
onFirstClick: () => undefined as any, |
| 92 |
onLastClick: () => undefined as any, |
| 93 |
onPageInput: () => undefined as any |
| 94 |
}; |
| 95 |
|
| 96 |
private static parseInteger(input: number | string, lastPage: number): number { |
| 97 |
// eslint-disable-next-line radix |
| 98 |
let inputPage = Number.parseInt(input as string, 10); |
| 99 |
if (!Number.isNaN(inputPage)) { |
| 100 |
inputPage = inputPage > lastPage ? lastPage : inputPage; |
| 101 |
inputPage = inputPage < 1 ? 1 : inputPage; |
| 102 |
} |
| 103 |
return inputPage; |
| 104 |
} |
| 105 |
|
| 106 |
private onChange(event: React.FormEvent<HTMLInputElement>, lastPage: number): void { |
| 107 |
const inputPage = PaginationNavigation.parseInteger(event.currentTarget.value, lastPage); |
| 108 |
this.setState({ userInputPage: Number.isNaN(inputPage) ? event.currentTarget.value : inputPage }); |
| 109 |
} |
| 110 |
|
| 111 |
private onKeyDown( |
| 112 |
event: React.KeyboardEvent<HTMLInputElement>, |
| 113 |
page: number, |
| 114 |
lastPage: number, |
| 115 |
onPageInput: (event: React.KeyboardEvent<HTMLInputElement>, page: number) => void |
| 116 |
): void { |
| 117 |
const allowedKeys = [ |
| 118 |
'Tab', |
| 119 |
'Backspace', |
| 120 |
'Delete', |
| 121 |
'ArrowLeft', |
| 122 |
'ArrowRight', |
| 123 |
'Home', |
| 124 |
'End', |
| 125 |
'ArrowUp', |
| 126 |
'ArrowDown' |
| 127 |
]; |
| 128 |
if (event.key === KeyTypes.Enter) { |
| 129 |
const inputPage = PaginationNavigation.parseInteger(this.state.userInputPage, lastPage); |
| 130 |
onPageInput(event, Number.isNaN(inputPage) ? page : inputPage); |
| 131 |
this.handleNewPage(event, Number.isNaN(inputPage) ? page : inputPage); |
| 132 |
} else if (!/^\d*$/.test(event.key) && !allowedKeys.includes(event.key)) { |
| 133 |
event.preventDefault(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
handleNewPage = (_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPage: number) => { |
| 138 |
const { perPage, onSetPage } = this.props; |
| 139 |
const startIdx = (newPage - 1) * perPage; |
| 140 |
const endIdx = newPage * perPage; |
| 141 |
return onSetPage(_evt, newPage, perPage, startIdx, endIdx); |
| 142 |
}; |
| 143 |
|
| 144 |
componentDidUpdate(lastState: PaginationNavigationProps) { |
| 145 |
if ( |
| 146 |
this.props.page !== lastState.page && |
| 147 |
this.props.page <= this.props.lastPage && |
| 148 |
this.state.userInputPage !== this.props.page |
| 149 |
) { |
| 150 |
this.setState({ userInputPage: this.props.page }); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
render() { |
| 155 |
const { |
| 156 |
page, |
| 157 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 158 |
perPage, |
| 159 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 160 |
onSetPage, |
| 161 |
isDisabled, |
| 162 |
itemCount, |
| 163 |
lastPage, |
| 164 |
firstPage, |
| 165 |
pagesTitle, |
| 166 |
pagesTitlePlural, |
| 167 |
toLastPageAriaLabel, |
| 168 |
toNextPageAriaLabel, |
| 169 |
toFirstPageAriaLabel, |
| 170 |
toPreviousPageAriaLabel, |
| 171 |
currPageAriaLabel, |
| 172 |
paginationAriaLabel, |
| 173 |
ofWord, |
| 174 |
onNextClick, |
| 175 |
onPreviousClick, |
| 176 |
onFirstClick, |
| 177 |
onLastClick, |
| 178 |
onPageInput, |
| 179 |
className, |
| 180 |
isCompact, |
| 181 |
...props |
| 182 |
} = this.props; |
| 183 |
const { userInputPage } = this.state; |
| 184 |
return ( |
| 185 |
<nav className={css(styles.paginationNav, className)} aria-label={paginationAriaLabel} {...props}> |
| 186 |
{!isCompact && ( |
| 187 |
<div className={css(styles.paginationNavControl, styles.modifiers.first)}> |
| 188 |
<Button |
| 189 |
variant={ButtonVariant.plain} |
| 190 |
isDisabled={isDisabled || page === firstPage || page === 0} |
| 191 |
aria-label={toFirstPageAriaLabel} |
| 192 |
data-action="first" |
| 193 |
onClick={(event) => { |
| 194 |
onFirstClick(event, 1); |
| 195 |
this.handleNewPage(event, 1); |
| 196 |
this.setState({ userInputPage: 1 }); |
| 197 |
}} |
| 198 |
icon={<RhMicronsDoubleCaretLeftIcon />} |
| 199 |
/> |
| 200 |
</div> |
| 201 |
)} |
| 202 |
<div className={styles.paginationNavControl}> |
| 203 |
<Button |
| 204 |
variant={ButtonVariant.plain} |
| 205 |
isDisabled={isDisabled || page === firstPage || page === 0} |
| 206 |
data-action="previous" |
| 207 |
onClick={(event) => { |
| 208 |
const newPage = page - 1 >= 1 ? page - 1 : 1; |
| 209 |
onPreviousClick(event, newPage); |
| 210 |
this.handleNewPage(event, newPage); |
| 211 |
this.setState({ userInputPage: newPage }); |
| 212 |
}} |
| 213 |
aria-label={toPreviousPageAriaLabel} |
| 214 |
icon={<AngleLeftIcon />} |
| 215 |
/> |
| 216 |
</div> |
| 217 |
{!isCompact && ( |
| 218 |
<div className={styles.paginationNavPageSelect}> |
| 219 |
<TextInput |
| 220 |
aria-label={currPageAriaLabel} |
| 221 |
type="number" |
| 222 |
isDisabled={ |
| 223 |
isDisabled || (itemCount && page === firstPage && page === lastPage && itemCount >= 0) || page === 0 |
| 224 |
} |
| 225 |
min={lastPage <= 0 && firstPage <= 0 ? 0 : 1} |
| 226 |
max={lastPage} |
| 227 |
value={userInputPage} |
| 228 |
onKeyDown={(event) => this.onKeyDown(event, page, lastPage, onPageInput)} |
| 229 |
onChange={(event) => this.onChange(event, lastPage)} |
| 230 |
/> |
| 231 |
{(itemCount || itemCount === 0) && ( |
| 232 |
<span aria-hidden="true"> |
| 233 |
{ofWord} {pagesTitle ? pluralize(lastPage, pagesTitle, pagesTitlePlural) : lastPage} |
| 234 |
</span> |
| 235 |
)} |
| 236 |
</div> |
| 237 |
)} |
| 238 |
<div className={styles.paginationNavControl}> |
| 239 |
<Button |
| 240 |
variant={ButtonVariant.plain} |
| 241 |
isDisabled={isDisabled || page === lastPage} |
| 242 |
aria-label={toNextPageAriaLabel} |
| 243 |
data-action="next" |
| 244 |
onClick={(event) => { |
| 245 |
const newPage = page + 1 <= lastPage ? page + 1 : lastPage; |
| 246 |
onNextClick(event, newPage); |
| 247 |
this.handleNewPage(event, newPage); |
| 248 |
this.setState({ userInputPage: newPage }); |
| 249 |
}} |
| 250 |
icon={<AngleRightIcon />} |
| 251 |
/> |
| 252 |
</div> |
| 253 |
{!isCompact && ( |
| 254 |
<div className={css(styles.paginationNavControl, styles.modifiers.last)}> |
| 255 |
<Button |
| 256 |
variant={ButtonVariant.plain} |
| 257 |
isDisabled={isDisabled || page === lastPage} |
| 258 |
aria-label={toLastPageAriaLabel} |
| 259 |
data-action="last" |
| 260 |
onClick={(event) => { |
| 261 |
onLastClick(event, lastPage); |
| 262 |
this.handleNewPage(event, lastPage); |
| 263 |
this.setState({ userInputPage: lastPage }); |
| 264 |
}} |
| 265 |
icon={<AngleDoubleRightIcon />} |
| 266 |
/> |
| 267 |
</div> |
| 268 |
)} |
| 269 |
</nav> |
| 270 |
); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
export { PaginationNavigation }; |