@instructure/ui-pagination 11.6.0 → 11.6.1-snapshot-129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/CHANGELOG.md +42 -296
  2. package/es/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/index.js +2 -2
  3. package/es/Pagination/{PaginationButton → v1/PaginationButton}/index.js +1 -1
  4. package/es/Pagination/{PaginationPageInput → v1/PaginationPageInput}/index.js +1 -1
  5. package/es/Pagination/{index.js → v1/index.js} +1 -1
  6. package/es/Pagination/v2/PaginationArrowButton/index.js +109 -0
  7. package/es/Pagination/v2/PaginationArrowButton/props.js +30 -0
  8. package/es/Pagination/v2/PaginationButton/index.js +77 -0
  9. package/es/Pagination/v2/PaginationButton/props.js +29 -0
  10. package/es/Pagination/v2/PaginationPageInput/index.js +189 -0
  11. package/es/Pagination/v2/PaginationPageInput/props.js +26 -0
  12. package/es/Pagination/v2/PaginationPageInput/styles.js +56 -0
  13. package/es/Pagination/v2/index.js +481 -0
  14. package/es/Pagination/v2/props.js +26 -0
  15. package/es/Pagination/v2/styles.js +58 -0
  16. package/es/{index.js → exports/a.js} +1 -1
  17. package/es/exports/b.js +24 -0
  18. package/lib/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/index.js +8 -8
  19. package/lib/Pagination/{PaginationButton → v1/PaginationButton}/index.js +2 -2
  20. package/lib/Pagination/{PaginationPageInput → v1/PaginationPageInput}/index.js +2 -2
  21. package/lib/Pagination/{index.js → v1/index.js} +6 -6
  22. package/lib/Pagination/v2/PaginationArrowButton/index.js +115 -0
  23. package/lib/Pagination/v2/PaginationArrowButton/props.js +35 -0
  24. package/lib/Pagination/v2/PaginationButton/index.js +83 -0
  25. package/lib/Pagination/v2/PaginationButton/props.js +34 -0
  26. package/lib/Pagination/v2/PaginationPageInput/index.js +194 -0
  27. package/lib/Pagination/v2/PaginationPageInput/props.js +31 -0
  28. package/lib/Pagination/v2/PaginationPageInput/styles.js +62 -0
  29. package/lib/Pagination/v2/index.js +496 -0
  30. package/lib/Pagination/v2/props.js +31 -0
  31. package/lib/Pagination/v2/styles.js +64 -0
  32. package/lib/{index.js → exports/a.js} +3 -3
  33. package/lib/exports/b.js +18 -0
  34. package/package.json +48 -25
  35. package/src/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/index.tsx +2 -2
  36. package/src/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/props.ts +1 -1
  37. package/src/Pagination/{PaginationButton → v1/PaginationButton}/index.tsx +1 -1
  38. package/src/Pagination/{PaginationButton → v1/PaginationButton}/props.ts +1 -1
  39. package/src/Pagination/{PaginationPageInput → v1/PaginationPageInput}/index.tsx +1 -1
  40. package/src/Pagination/{index.tsx → v1/index.tsx} +1 -1
  41. package/src/Pagination/v2/PaginationArrowButton/index.tsx +120 -0
  42. package/src/Pagination/v2/PaginationArrowButton/props.ts +74 -0
  43. package/src/Pagination/v2/PaginationButton/index.tsx +82 -0
  44. package/src/Pagination/v2/PaginationButton/props.ts +77 -0
  45. package/src/Pagination/v2/PaginationPageInput/index.tsx +242 -0
  46. package/src/Pagination/v2/PaginationPageInput/props.ts +95 -0
  47. package/src/Pagination/v2/PaginationPageInput/styles.ts +62 -0
  48. package/src/Pagination/v2/README.md +417 -0
  49. package/src/Pagination/v2/index.tsx +665 -0
  50. package/src/Pagination/v2/props.ts +245 -0
  51. package/src/Pagination/v2/styles.ts +64 -0
  52. package/src/exports/a.ts +29 -0
  53. package/src/exports/b.ts +29 -0
  54. package/tsconfig.build.json +1 -0
  55. package/tsconfig.build.tsbuildinfo +1 -1
  56. package/types/Pagination/v1/PaginationArrowButton/index.d.ts.map +1 -0
  57. package/types/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/props.d.ts +1 -1
  58. package/types/Pagination/v1/PaginationArrowButton/props.d.ts.map +1 -0
  59. package/types/Pagination/v1/PaginationButton/index.d.ts.map +1 -0
  60. package/types/Pagination/{PaginationButton → v1/PaginationButton}/props.d.ts +1 -1
  61. package/types/Pagination/v1/PaginationButton/props.d.ts.map +1 -0
  62. package/types/Pagination/v1/PaginationPageInput/index.d.ts.map +1 -0
  63. package/types/Pagination/v1/PaginationPageInput/props.d.ts.map +1 -0
  64. package/types/Pagination/v1/PaginationPageInput/styles.d.ts.map +1 -0
  65. package/types/Pagination/v1/PaginationPageInput/theme.d.ts.map +1 -0
  66. package/types/Pagination/v1/index.d.ts.map +1 -0
  67. package/types/Pagination/v1/props.d.ts.map +1 -0
  68. package/types/Pagination/v1/styles.d.ts.map +1 -0
  69. package/types/Pagination/v1/theme.d.ts.map +1 -0
  70. package/types/Pagination/v2/PaginationArrowButton/index.d.ts +27 -0
  71. package/types/Pagination/v2/PaginationArrowButton/index.d.ts.map +1 -0
  72. package/types/Pagination/v2/PaginationArrowButton/props.d.ts +17 -0
  73. package/types/Pagination/v2/PaginationArrowButton/props.d.ts.map +1 -0
  74. package/types/Pagination/v2/PaginationButton/index.d.ts +26 -0
  75. package/types/Pagination/v2/PaginationButton/index.d.ts.map +1 -0
  76. package/types/Pagination/v2/PaginationButton/props.d.ts +30 -0
  77. package/types/Pagination/v2/PaginationButton/props.d.ts.map +1 -0
  78. package/types/Pagination/v2/PaginationPageInput/index.d.ts +50 -0
  79. package/types/Pagination/v2/PaginationPageInput/index.d.ts.map +1 -0
  80. package/types/Pagination/v2/PaginationPageInput/props.d.ts +45 -0
  81. package/types/Pagination/v2/PaginationPageInput/props.d.ts.map +1 -0
  82. package/types/Pagination/v2/PaginationPageInput/styles.d.ts +16 -0
  83. package/types/Pagination/v2/PaginationPageInput/styles.d.ts.map +1 -0
  84. package/types/Pagination/v2/index.d.ts +106 -0
  85. package/types/Pagination/v2/index.d.ts.map +1 -0
  86. package/types/Pagination/v2/props.d.ts +138 -0
  87. package/types/Pagination/v2/props.d.ts.map +1 -0
  88. package/types/Pagination/v2/styles.d.ts +16 -0
  89. package/types/Pagination/v2/styles.d.ts.map +1 -0
  90. package/types/exports/a.d.ts +5 -0
  91. package/types/exports/a.d.ts.map +1 -0
  92. package/types/exports/b.d.ts +5 -0
  93. package/types/exports/b.d.ts.map +1 -0
  94. package/src/index.ts +0 -29
  95. package/types/Pagination/PaginationArrowButton/index.d.ts.map +0 -1
  96. package/types/Pagination/PaginationArrowButton/props.d.ts.map +0 -1
  97. package/types/Pagination/PaginationButton/index.d.ts.map +0 -1
  98. package/types/Pagination/PaginationButton/props.d.ts.map +0 -1
  99. package/types/Pagination/PaginationPageInput/index.d.ts.map +0 -1
  100. package/types/Pagination/PaginationPageInput/props.d.ts.map +0 -1
  101. package/types/Pagination/PaginationPageInput/styles.d.ts.map +0 -1
  102. package/types/Pagination/PaginationPageInput/theme.d.ts.map +0 -1
  103. package/types/Pagination/index.d.ts.map +0 -1
  104. package/types/Pagination/props.d.ts.map +0 -1
  105. package/types/Pagination/styles.d.ts.map +0 -1
  106. package/types/Pagination/theme.d.ts.map +0 -1
  107. package/types/index.d.ts +0 -5
  108. package/types/index.d.ts.map +0 -1
  109. /package/es/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/props.js +0 -0
  110. /package/es/Pagination/{PaginationButton → v1/PaginationButton}/props.js +0 -0
  111. /package/es/Pagination/{PaginationPageInput → v1/PaginationPageInput}/props.js +0 -0
  112. /package/es/Pagination/{PaginationPageInput → v1/PaginationPageInput}/styles.js +0 -0
  113. /package/es/Pagination/{PaginationPageInput → v1/PaginationPageInput}/theme.js +0 -0
  114. /package/es/Pagination/{props.js → v1/props.js} +0 -0
  115. /package/es/Pagination/{styles.js → v1/styles.js} +0 -0
  116. /package/es/Pagination/{theme.js → v1/theme.js} +0 -0
  117. /package/lib/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/props.js +0 -0
  118. /package/lib/Pagination/{PaginationButton → v1/PaginationButton}/props.js +0 -0
  119. /package/lib/Pagination/{PaginationPageInput → v1/PaginationPageInput}/props.js +0 -0
  120. /package/lib/Pagination/{PaginationPageInput → v1/PaginationPageInput}/styles.js +0 -0
  121. /package/lib/Pagination/{PaginationPageInput → v1/PaginationPageInput}/theme.js +0 -0
  122. /package/lib/Pagination/{props.js → v1/props.js} +0 -0
  123. /package/lib/Pagination/{styles.js → v1/styles.js} +0 -0
  124. /package/lib/Pagination/{theme.js → v1/theme.js} +0 -0
  125. /package/src/Pagination/{PaginationPageInput → v1/PaginationPageInput}/props.ts +0 -0
  126. /package/src/Pagination/{PaginationPageInput → v1/PaginationPageInput}/styles.ts +0 -0
  127. /package/src/Pagination/{PaginationPageInput → v1/PaginationPageInput}/theme.ts +0 -0
  128. /package/src/Pagination/{README.md → v1/README.md} +0 -0
  129. /package/src/Pagination/{props.ts → v1/props.ts} +0 -0
  130. /package/src/Pagination/{styles.ts → v1/styles.ts} +0 -0
  131. /package/src/Pagination/{theme.ts → v1/theme.ts} +0 -0
  132. /package/types/Pagination/{PaginationArrowButton → v1/PaginationArrowButton}/index.d.ts +0 -0
  133. /package/types/Pagination/{PaginationButton → v1/PaginationButton}/index.d.ts +0 -0
  134. /package/types/Pagination/{PaginationPageInput → v1/PaginationPageInput}/index.d.ts +0 -0
  135. /package/types/Pagination/{PaginationPageInput → v1/PaginationPageInput}/props.d.ts +0 -0
  136. /package/types/Pagination/{PaginationPageInput → v1/PaginationPageInput}/styles.d.ts +0 -0
  137. /package/types/Pagination/{PaginationPageInput → v1/PaginationPageInput}/theme.d.ts +0 -0
  138. /package/types/Pagination/{index.d.ts → v1/index.d.ts} +0 -0
  139. /package/types/Pagination/{props.d.ts → v1/props.d.ts} +0 -0
  140. /package/types/Pagination/{styles.d.ts → v1/styles.d.ts} +0 -0
  141. /package/types/Pagination/{theme.d.ts → v1/theme.d.ts} +0 -0
@@ -0,0 +1,665 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import {
25
+ cloneElement,
26
+ Children,
27
+ Component,
28
+ ReactElement,
29
+ ReactNode
30
+ } from 'react'
31
+
32
+ import { Text } from '@instructure/ui-text/latest'
33
+ import { View } from '@instructure/ui-view/latest'
34
+ import { omitProps, withDeterministicId } from '@instructure/ui-react-utils'
35
+ import { hasVisibleChildren } from '@instructure/ui-a11y-utils'
36
+ import { findTabbable, getActiveElement } from '@instructure/ui-dom-utils'
37
+ import { withStyle } from '@instructure/emotion'
38
+
39
+ import { PaginationButton } from './PaginationButton'
40
+ import { PaginationArrowButton } from './PaginationArrowButton'
41
+ import { PaginationPageInput } from './PaginationPageInput'
42
+
43
+ import generateStyle from './styles'
44
+
45
+ import type { PaginationPageProps } from './PaginationButton/props'
46
+ import type { PaginationArrowDirections } from './PaginationArrowButton/props'
47
+
48
+ import { allowedProps } from './props'
49
+ import type { PaginationProps, PaginationSnapshot, ChildPage } from './props'
50
+
51
+ /** This is an [].findIndex optimized to work on really big, but sparse, arrays */
52
+ const fastFindIndex = (
53
+ arr: ChildPage[],
54
+ fn: (page: ChildPage) => boolean | undefined
55
+ ) => Number(Object.keys(arr).find((k) => fn(arr[Number(k)])))
56
+
57
+ const childrenArray = (props: PaginationProps) => {
58
+ const { children } = props
59
+
60
+ if (!children) {
61
+ return []
62
+ }
63
+
64
+ return Array.isArray(children)
65
+ ? (children as React.ReactElement<PaginationPageProps>[])
66
+ : ([children] as React.ReactElement<PaginationPageProps>[])
67
+ }
68
+
69
+ function propsHaveCompactView(props: PaginationProps) {
70
+ if (props.children)
71
+ return props.variant === 'compact' && childrenArray(props).length > 5
72
+ return props.variant === 'compact' && props.totalPageNumber! > 5
73
+ }
74
+
75
+ type ArrowConfig = {
76
+ pageIndex: number
77
+ label: string
78
+ shouldEnableIcon: boolean
79
+ handleButtonRef: (el: Element | null) => void
80
+ }
81
+
82
+ /**
83
+ ---
84
+ category: components
85
+ ---
86
+ **/
87
+ @withDeterministicId()
88
+ @withStyle(generateStyle)
89
+ class Pagination extends Component<PaginationProps> {
90
+ static readonly componentId = 'Pagination'
91
+
92
+ static allowedProps = allowedProps
93
+ static defaultProps = {
94
+ disabled: false,
95
+ withFirstAndLastButton: false,
96
+ showDisabledButtons: false,
97
+ variant: 'full',
98
+ as: 'div',
99
+ labelNumberInput: (numberOfPages: number) => `of ${numberOfPages}`,
100
+ screenReaderLabelNumberInput: (
101
+ currentPage: number,
102
+ numberOfPages: number
103
+ ) => `Select page (${currentPage} of ${numberOfPages})`,
104
+ shouldHandleFocus: true,
105
+ totalPageNumber: 0,
106
+ currentPage: 1,
107
+ siblingCount: 1,
108
+ boundaryCount: 1,
109
+ ellipsis: '…',
110
+ renderPageIndicator: (page: number) => page,
111
+ margin: 'space8'
112
+ }
113
+
114
+ static Page = PaginationButton
115
+ static Navigation = PaginationArrowButton
116
+ static PageInput = PaginationPageInput
117
+
118
+ private readonly _labelId: string
119
+
120
+ private _firstButton: HTMLButtonElement | null = null
121
+ private _prevButton: HTMLButtonElement | null = null
122
+ private _nextButton: HTMLButtonElement | null = null
123
+ private _lastButton: HTMLButtonElement | null = null
124
+
125
+ ref: Element | null = null
126
+ currentPageRef: PaginationButton | null = null
127
+
128
+ constructor(props: PaginationProps) {
129
+ super(props)
130
+
131
+ this._labelId = props.deterministicId!()
132
+ }
133
+
134
+ get _root() {
135
+ console.warn(
136
+ '_root property is deprecated and will be removed in v9, please use ref instead'
137
+ )
138
+
139
+ return this.ref
140
+ }
141
+
142
+ get inputMode() {
143
+ return this.props.variant === 'input'
144
+ }
145
+
146
+ get childPages() {
147
+ return childrenArray(this.props)
148
+ }
149
+
150
+ get withFirstAndLastButton() {
151
+ return this.inputMode || this.props.withFirstAndLastButton
152
+ }
153
+
154
+ get showDisabledButtons() {
155
+ return this.inputMode || this.props.showDisabledButtons
156
+ }
157
+
158
+ getSnapshotBeforeUpdate(): PaginationSnapshot {
159
+ const activeElement = getActiveElement()
160
+
161
+ if (
162
+ activeElement === this._firstButton ||
163
+ activeElement === this._prevButton ||
164
+ activeElement === this._nextButton ||
165
+ activeElement === this._lastButton
166
+ ) {
167
+ return { lastFocusedButton: activeElement as HTMLButtonElement }
168
+ } else {
169
+ return { lastFocusedButton: undefined }
170
+ }
171
+ }
172
+
173
+ componentDidMount() {
174
+ this.props.makeStyles?.()
175
+ }
176
+
177
+ componentDidUpdate(
178
+ prevProps: PaginationProps,
179
+ _prevState: unknown,
180
+ snapshot: PaginationSnapshot
181
+ ) {
182
+ this.props.makeStyles?.()
183
+
184
+ // set focus on the currently active page
185
+ if (
186
+ this.props.currentPage !== prevProps.currentPage &&
187
+ document.activeElement !== this._firstButton &&
188
+ document.activeElement !== this._prevButton &&
189
+ document.activeElement !== this._nextButton &&
190
+ document.activeElement !== this._lastButton
191
+ ) {
192
+ // @ts-expect-error fix typing
193
+ this.currentPageRef?.ref?.focus?.()
194
+ }
195
+
196
+ if (
197
+ !this.props.shouldHandleFocus ||
198
+ (!propsHaveCompactView(prevProps) && !propsHaveCompactView(this.props))
199
+ ) {
200
+ return
201
+ } else {
202
+ this.focusElementAfterUpdate(snapshot)
203
+ }
204
+ }
205
+
206
+ focusElementAfterUpdate(snapshot: PaginationSnapshot) {
207
+ const { lastFocusedButton } = snapshot
208
+
209
+ if (lastFocusedButton) {
210
+ const focusable = findTabbable(this.ref)
211
+ const direction = lastFocusedButton.dataset.direction
212
+
213
+ // By default we want to focus the previously focused button
214
+ let nextFocusElement: Element = lastFocusedButton
215
+
216
+ // In case the button is not focusable anymore
217
+ // (disabled or not in the DOM), we focus to the next available page
218
+ if (!focusable.includes(nextFocusElement)) {
219
+ if (direction === 'first' || direction === 'prev') {
220
+ nextFocusElement = focusable[0]
221
+ }
222
+ if (direction === 'next' || direction === 'last') {
223
+ nextFocusElement = focusable[focusable.length - 1]
224
+ }
225
+ }
226
+
227
+ ;(nextFocusElement as HTMLElement)?.focus()
228
+ }
229
+ }
230
+
231
+ get compactView() {
232
+ return propsHaveCompactView(this.props)
233
+ }
234
+
235
+ transferDisabledPropToChildren(children: PaginationProps['children']) {
236
+ return children && this.props.disabled
237
+ ? Children.map(children, (page) =>
238
+ cloneElement(page, { disabled: this.props.disabled })
239
+ )
240
+ : children
241
+ }
242
+
243
+ handleElementRef = (el: Element | null) => {
244
+ this.ref = el
245
+ if (el && typeof this.props.elementRef === 'function') {
246
+ this.props.elementRef(el)
247
+ }
248
+ }
249
+
250
+ handleInputRef = (el: HTMLInputElement | null) => {
251
+ if (typeof this.props.inputRef === 'function') {
252
+ this.props.inputRef(el)
253
+ }
254
+ }
255
+
256
+ renderLabel() {
257
+ const display = this.props.variant === 'full' ? 'inline-block' : 'block'
258
+ const visibleLabel = hasVisibleChildren(this.props.label)
259
+
260
+ return (
261
+ <View
262
+ padding={visibleLabel ? 'small' : '0'}
263
+ display={visibleLabel ? display : 'auto'}
264
+ id={this._labelId}
265
+ >
266
+ <Text variant="content">{this.props.label}</Text>
267
+ </View>
268
+ )
269
+ }
270
+
271
+ renderDefaultPageInput = () => {
272
+ const { currentPage, totalPageNumber } = this.props
273
+ return (
274
+ <PaginationPageInput
275
+ numberOfPages={totalPageNumber!}
276
+ currentPageIndex={currentPage! - 1}
277
+ onChange={(_e, nextPageIndex) =>
278
+ this.props.onPageChange?.(nextPageIndex + 1, currentPage!)
279
+ }
280
+ screenReaderLabel={this.props.screenReaderLabelNumberInput!}
281
+ label={this.props.labelNumberInput}
282
+ disabled={this.props.disabled}
283
+ inputRef={this.handleInputRef}
284
+ />
285
+ )
286
+ }
287
+
288
+ renderPageInput(currentPageIndex: number) {
289
+ if (!this.props.children) return this.renderDefaultPageInput()
290
+ return (
291
+ <PaginationPageInput
292
+ numberOfPages={this.childPages.length}
293
+ currentPageIndex={currentPageIndex}
294
+ onChange={this.handleInputChange.bind(this)}
295
+ screenReaderLabel={this.props.screenReaderLabelNumberInput!}
296
+ label={this.props.labelNumberInput}
297
+ disabled={this.props.disabled}
298
+ inputRef={this.handleInputRef}
299
+ />
300
+ )
301
+ }
302
+
303
+ handleInputChange(
304
+ event:
305
+ | React.KeyboardEvent<HTMLInputElement>
306
+ | React.MouseEvent<HTMLButtonElement>
307
+ | React.FocusEvent<HTMLInputElement>,
308
+ pageIndex: number
309
+ ) {
310
+ this.childPages[pageIndex].props.onClick?.(event)
311
+ }
312
+
313
+ handleNavigation = (nextIndex: number, previousIndex: number) => {
314
+ const { onPageChange } = this.props
315
+ if (typeof onPageChange === 'function') {
316
+ onPageChange(nextIndex, previousIndex)
317
+ }
318
+ }
319
+
320
+ renderPagesInInterval = (from: number, to: number, currentPage: number) => {
321
+ if (to - from > 1000)
322
+ throw new Error('Pagination: too many pages (more than 1000)')
323
+ const pages: ReactElement[] = []
324
+ for (let i = from; i <= to; i++) {
325
+ pages.push(
326
+ <Pagination.Page
327
+ ref={(e) => {
328
+ if (i === currentPage) {
329
+ this.currentPageRef = e
330
+ }
331
+ }}
332
+ key={i}
333
+ onClick={() => this.handleNavigation(i, currentPage)}
334
+ onMouseEnter={() => this.handleOnMouseEnter(i)}
335
+ current={i === currentPage}
336
+ {...(this.props.screenReaderLabelPageButton
337
+ ? {
338
+ screenReaderLabel: this.props.screenReaderLabelPageButton(
339
+ i,
340
+ this.props.totalPageNumber!
341
+ )
342
+ }
343
+ : {})}
344
+ >
345
+ {this.props.renderPageIndicator?.(i, currentPage)}
346
+ </Pagination.Page>
347
+ )
348
+ }
349
+ return pages
350
+ }
351
+
352
+ renderDefaultPages = () => {
353
+ const {
354
+ ellipsis,
355
+ currentPage,
356
+ totalPageNumber,
357
+ siblingCount,
358
+ boundaryCount,
359
+ variant
360
+ } = this.props
361
+ const pages: ReactNode[] = []
362
+ if (
363
+ totalPageNumber! <= 2 * boundaryCount! ||
364
+ totalPageNumber! <= 1 + siblingCount! + boundaryCount! ||
365
+ variant === 'full'
366
+ ) {
367
+ return (
368
+ <ul css={this.props.styles?.pageIndicatorList}>
369
+ {this.renderPagesInInterval(1, totalPageNumber!, currentPage!)}
370
+ </ul>
371
+ )
372
+ }
373
+
374
+ if (currentPage! > boundaryCount! + siblingCount! + 1) {
375
+ pages.push(this.renderPagesInInterval(1, boundaryCount!, currentPage!))
376
+ pages.push(
377
+ <li key="ellipsis1" style={{ all: 'unset' }}>
378
+ <Text>{ellipsis}</Text>
379
+ </li>
380
+ )
381
+ if (
382
+ currentPage! - siblingCount! >
383
+ totalPageNumber! - boundaryCount! + 1
384
+ ) {
385
+ pages.push(
386
+ this.renderPagesInInterval(
387
+ totalPageNumber! - boundaryCount! + 1,
388
+ totalPageNumber!,
389
+ currentPage!
390
+ )
391
+ )
392
+ return <ul css={this.props.styles?.pageIndicatorList}>{pages}</ul>
393
+ }
394
+ pages.push(
395
+ this.renderPagesInInterval(
396
+ currentPage! - siblingCount!,
397
+ currentPage!,
398
+ currentPage!
399
+ )
400
+ )
401
+ } else {
402
+ pages.push(
403
+ this.renderPagesInInterval(
404
+ 1,
405
+ Math.max(currentPage!, boundaryCount!),
406
+ currentPage!
407
+ )
408
+ )
409
+ }
410
+
411
+ if (currentPage! < totalPageNumber! - (siblingCount! + boundaryCount!)) {
412
+ pages.push(
413
+ this.renderPagesInInterval(
414
+ Math.max(currentPage!, boundaryCount!) + 1,
415
+ currentPage! + siblingCount!,
416
+ currentPage!
417
+ )
418
+ )
419
+ pages.push(
420
+ <li key="ellipsis2" style={{ all: 'unset' }}>
421
+ <Text>{ellipsis}</Text>
422
+ </li>
423
+ )
424
+ pages.push(
425
+ this.renderPagesInInterval(
426
+ totalPageNumber! - boundaryCount! + 1,
427
+ totalPageNumber!,
428
+ currentPage!
429
+ )
430
+ )
431
+ } else {
432
+ pages.push(
433
+ this.renderPagesInInterval(
434
+ currentPage! + 1,
435
+ totalPageNumber!,
436
+ currentPage!
437
+ )
438
+ )
439
+ }
440
+ return <ul css={this.props.styles?.pageIndicatorList}>{pages}</ul>
441
+ }
442
+
443
+ renderPages(currentPageIndex: number) {
444
+ if (!this.props.children) return this.renderDefaultPages()
445
+ const allPages = this.childPages
446
+ let visiblePages = allPages
447
+
448
+ if (this.compactView) {
449
+ const firstIndex = 0
450
+ const lastIndex = allPages.length - 1
451
+
452
+ const sliceStart = Math.min(
453
+ lastIndex - 3,
454
+ Math.max(currentPageIndex - 1, firstIndex)
455
+ )
456
+ const sliceEnd = Math.min(currentPageIndex + 4, lastIndex)
457
+
458
+ visiblePages = allPages.slice(sliceStart, sliceEnd)
459
+
460
+ const firstPage = allPages[firstIndex]
461
+ const lastPage = allPages[lastIndex]
462
+
463
+ if (sliceStart - firstIndex > 1)
464
+ visiblePages.unshift(
465
+ <li style={{ all: 'unset' }} key="first" aria-hidden="true">
466
+ <Text>{this.props.ellipsis}</Text>
467
+ </li>
468
+ )
469
+ if (sliceStart - firstIndex > 0) visiblePages.unshift(firstPage)
470
+ if (lastIndex - sliceEnd + 1 > 1)
471
+ visiblePages.push(
472
+ <li style={{ all: 'unset' }} key="last" aria-hidden="true">
473
+ <Text>{this.props.ellipsis}</Text>
474
+ </li>
475
+ )
476
+ if (lastIndex - sliceEnd + 1 > 0) visiblePages.push(lastPage)
477
+ }
478
+
479
+ return (
480
+ <View display="inline-block" as="ul" margin="0" padding="0">
481
+ {this.transferDisabledPropToChildren(visiblePages)}
482
+ </View>
483
+ )
484
+ }
485
+
486
+ getArrowVariant(
487
+ direction: PaginationArrowDirections,
488
+ currentPageIndex: number,
489
+ pagesCount: number
490
+ ): ArrowConfig {
491
+ switch (direction) {
492
+ case 'first':
493
+ return {
494
+ pageIndex: 0,
495
+ label: this.props.labelFirst || 'First Page',
496
+ shouldEnableIcon: currentPageIndex > 1,
497
+ handleButtonRef: (el) => {
498
+ this._firstButton = el as HTMLButtonElement
499
+ }
500
+ }
501
+ case 'prev':
502
+ return {
503
+ pageIndex: currentPageIndex - 1,
504
+ label: this.props.labelPrev || 'Previous Page',
505
+ shouldEnableIcon: currentPageIndex > 0,
506
+ handleButtonRef: (el) => {
507
+ this._prevButton = el as HTMLButtonElement
508
+ }
509
+ }
510
+ case 'next':
511
+ return {
512
+ pageIndex: currentPageIndex + 1,
513
+ label: this.props.labelNext || 'Next Page',
514
+ shouldEnableIcon: currentPageIndex < pagesCount - 1,
515
+ handleButtonRef: (el) => {
516
+ this._nextButton = el as HTMLButtonElement
517
+ }
518
+ }
519
+ case 'last':
520
+ return {
521
+ pageIndex: pagesCount - 1,
522
+ label: this.props.labelLast || 'Last Page',
523
+ shouldEnableIcon: currentPageIndex < pagesCount - 2,
524
+ handleButtonRef: (el) => {
525
+ this._lastButton = el as HTMLButtonElement
526
+ }
527
+ }
528
+ }
529
+ }
530
+
531
+ renderDefaultArrowButton = (direction: PaginationArrowDirections) => {
532
+ if (
533
+ !this.withFirstAndLastButton &&
534
+ (direction === 'first' || direction === 'last')
535
+ ) {
536
+ return null
537
+ }
538
+ // We don't display the arrows in "compact" variant under 6 items
539
+ if (!(propsHaveCompactView(this.props) || this.inputMode)) {
540
+ return null
541
+ }
542
+ const { totalPageNumber, currentPage } = this.props
543
+ const { label, shouldEnableIcon, handleButtonRef } = this.getArrowVariant(
544
+ direction,
545
+ currentPage! - 1,
546
+ totalPageNumber!
547
+ )
548
+
549
+ const disabled = this.props.disabled || !shouldEnableIcon
550
+ const onClick = () => {
551
+ if (direction === 'first') {
552
+ this.handleNavigation(1, currentPage!)
553
+ }
554
+ if (direction === 'prev') {
555
+ this.handleNavigation(Math.max(currentPage! - 1, 1), currentPage!)
556
+ }
557
+ if (direction === 'next') {
558
+ this.handleNavigation(
559
+ Math.min(currentPage! + 1, totalPageNumber!),
560
+ currentPage!
561
+ )
562
+ }
563
+ if (direction === 'last') {
564
+ this.handleNavigation(totalPageNumber!, currentPage!)
565
+ }
566
+ }
567
+
568
+ return shouldEnableIcon || this.showDisabledButtons ? (
569
+ <PaginationArrowButton
570
+ direction={direction}
571
+ data-direction={direction}
572
+ label={label}
573
+ onClick={onClick}
574
+ disabled={disabled}
575
+ buttonRef={handleButtonRef}
576
+ />
577
+ ) : null
578
+ }
579
+
580
+ renderArrowButton(
581
+ direction: PaginationArrowDirections,
582
+ currentPageIndex: number
583
+ ) {
584
+ if (!this.props.children) return this.renderDefaultArrowButton(direction)
585
+ const { childPages } = this
586
+ // We don't display the arrows in "compact" variant under 6 items
587
+ if (!(propsHaveCompactView(this.props) || this.inputMode)) {
588
+ return null
589
+ }
590
+
591
+ if (
592
+ !this.withFirstAndLastButton &&
593
+ (direction === 'first' || direction === 'last')
594
+ ) {
595
+ return null
596
+ }
597
+
598
+ const { pageIndex, label, shouldEnableIcon, handleButtonRef } =
599
+ this.getArrowVariant(direction, currentPageIndex, childPages.length)
600
+
601
+ const page = childPages[pageIndex]
602
+
603
+ const disabled =
604
+ page?.props?.disabled || this.props.disabled || !shouldEnableIcon
605
+ const onClick = page?.props?.onClick
606
+
607
+ return shouldEnableIcon || this.showDisabledButtons ? (
608
+ <PaginationArrowButton
609
+ direction={direction}
610
+ data-direction={direction}
611
+ label={label}
612
+ onClick={onClick}
613
+ disabled={disabled}
614
+ buttonRef={handleButtonRef}
615
+ />
616
+ ) : null
617
+ }
618
+
619
+ handleOnMouseEnter = (page: number) => {
620
+ if (typeof this.props.onMouseEnter === 'function') {
621
+ this.props.onMouseEnter(page)
622
+ }
623
+ }
624
+
625
+ render() {
626
+ const currentPageIndex = fastFindIndex(
627
+ this.childPages,
628
+ (p) => p && p.props && p.props.current
629
+ )
630
+
631
+ const passthroughProps = View.omitViewProps(
632
+ omitProps(this.props, Pagination.allowedProps),
633
+ Pagination
634
+ )
635
+
636
+ return (
637
+ <View
638
+ {...passthroughProps}
639
+ role="navigation"
640
+ as={this.props.as}
641
+ elementRef={this.handleElementRef}
642
+ margin={this.props.margin}
643
+ css={this.props.styles?.pagination}
644
+ aria-labelledby={this.props.label ? this._labelId : undefined}
645
+ data-cid="Pagination"
646
+ >
647
+ {this.props.label && this.renderLabel()}
648
+ <View display="inline-block" css={this.props.styles?.pages}>
649
+ {this.renderArrowButton('first', currentPageIndex)}
650
+ {this.renderArrowButton('prev', currentPageIndex)}
651
+
652
+ {this.inputMode
653
+ ? this.renderPageInput(currentPageIndex)
654
+ : this.renderPages(currentPageIndex)}
655
+
656
+ {this.renderArrowButton('next', currentPageIndex)}
657
+ {this.renderArrowButton('last', currentPageIndex)}
658
+ </View>
659
+ </View>
660
+ )
661
+ }
662
+ }
663
+
664
+ export default Pagination
665
+ export { Pagination, PaginationButton }