-
Notifications
You must be signed in to change notification settings - Fork 28
/
PaginationBar.tsx
348 lines (304 loc) · 9.46 KB
/
PaginationBar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Web Pagination Component
*
*/
import React, { useContext, useRef, useEffect, useState } from 'react'
import classnames from 'classnames'
import {
dispatchCustomElementEvent,
extendPropsWithContext,
} from '../../shared/component-helper'
import {
calculatePagination,
getDotsAriaLabel,
} from './PaginationCalculation'
import PaginationContext from './PaginationContext'
import Context from '../../shared/Context'
import Button from '../button/Button'
import IconPrimary from '../icon-primary/IconPrimary'
import styleProperties from '../../style/properties'
import { LocaleProps } from '../../shared/types'
import { SkeletonShow } from '../Skeleton'
export type PaginationBarProps = {
/**
* The title used in every button shown in the bar. Defaults to Side %s.
*/
button_title?: string
/**
* The title used in the previous page button. Defaults to Forrige side.
*/
prev_title?: string
/**
* The title used in the next page button. Defaults to Neste side.
*/
next_title?: string
/**
* The title used in the dots. Relevant for screen-readers. Defaults to %s flere sider.
*/
more_pages?: string
/**
* Reference to the parent component. Used to contain height between updates.
*/
contentRef?: React.RefObject<HTMLElement>
/**
* the given content can be either a function or a React node, depending on your needs. A function contains several helper functions. More details down below and have a look at the examples in the demos section.
*/
children?: React.ReactNode | (() => React.ReactNode)
skeleton: SkeletonShow
}
export type PaginationBarAllProps = PaginationBarProps &
LocaleProps &
React.HTMLProps<HTMLElement>
type PaginationBarContext = {
currentPage: number
pageCount: number
disabled: boolean
onPageUpdate: (cb: () => void) => void
setState: (state: { currentPage: number }) => void
updatePageContent: (currentPage: number) => void
}
const defaultProps = {
button_title: null,
prev_title: null,
next_title: null,
more_pages: null,
contentRef: null,
children: null,
}
const PaginationBar = (localProps: PaginationBarAllProps) => {
const context = useContext(PaginationContext)
const props = extendPropsWithContext(
localProps,
defaultProps,
context.pagination
) as PaginationBarProps & PaginationBarContext
const { currentPage, pageCount, disabled, skeleton } = props
// because of accessibility
const focusPage = () => {
const { onPageUpdate, contentRef } = props
onPageUpdate(() => {
try {
const elem = contentRef.current
elem.focus()
} catch (e) {
//
}
})
}
const keepPageHeight = () => {
try {
const elem = props.contentRef.current
const pageHeight = elem.offsetHeight
elem.style.height = `${pageHeight / 16}rem`
elem.style.minHeight = elem.style.height // because of the "min-height: inherit;" in &__indicator
} catch (e) {
//
}
const { onPageUpdate } = props
onPageUpdate(() => {
try {
const elem = props.contentRef.current
elem.style.height = 'auto'
elem.style.minHeight = elem.style.height
} catch (e) {
//
}
})
}
const setPage = (currentPage: number, event = null) => {
keepPageHeight()
const { setState: setContextState, updatePageContent } = props
setContextState({
currentPage,
})
updatePageContent(currentPage)
dispatchCustomElementEvent(props, 'on_change', {
page: currentPage, // deprecated
pageNo: currentPage, // deprecated
pageNumber: currentPage,
...props,
event,
})
}
const setPrevPage = () => {
setPage(props.currentPage - 1)
}
const setNextPage = () => {
setPage(props.currentPage + 1)
}
const clickHandler = ({ pageNumber, event }) => {
setPage(pageNumber, event)
focusPage()
}
const { getTranslation } = useContext(Context)
const { button_title, prev_title, next_title, more_pages } =
extendPropsWithContext(
props,
defaultProps,
getTranslation(props as LocaleProps).Pagination
)
const prevIsDisabled = currentPage > -1 ? currentPage === 1 : true
const nextIsDisabled =
currentPage > -1 ? currentPage === pageCount || pageCount === 0 : true
const paginationBarRef = useRef(null)
const currentScreenSize = useResizeObserver(paginationBarRef)
const pageNumberGroups = calculatePagination(
pageCount,
currentPage,
currentScreenSize === 'small'
)
return (
<div
ref={paginationBarRef}
className={classnames(
'dnb-pagination__bar',
pageCount >= 8 && 'dnb-pagination--many-pages'
)}
>
<div className="dnb-pagination__bar__wrapper">
<div className="dnb-pagination__bar__skip">
<Button
key="left-arrow"
disabled={disabled || prevIsDisabled}
skeleton={skeleton}
variant="tertiary"
icon="chevron_left"
icon_position="left"
text={prev_title}
on_click={setPrevPage}
title={prevIsDisabled ? null : prev_title}
/>
<Button
key="right-arrow"
disabled={disabled || nextIsDisabled}
skeleton={skeleton}
variant="tertiary"
icon="chevron_right"
icon_position="right"
text={next_title}
on_click={setNextPage}
title={nextIsDisabled ? null : next_title}
/>
</div>
<div className="dnb-pagination__bar__inner">
{(pageNumberGroups?.[0] || []).map((pageNumber) => (
<Button
key={pageNumber}
className="dnb-pagination__button"
text={String(pageNumber)}
aria-label={button_title.replace('%s', String(pageNumber))}
variant={
pageNumber === currentPage ? 'primary' : 'secondary'
}
disabled={disabled}
skeleton={skeleton}
aria-current={pageNumber === currentPage ? 'page' : null}
on_click={(event) => clickHandler({ pageNumber, event })}
/>
))}
{pageNumberGroups.slice(1).map((numbersList, idx) => (
<React.Fragment key={idx}>
<IconPrimary
role="separator"
aria-orientation="vertical"
aria-hidden={false}
title={getDotsAriaLabel({
more_pages,
numbersList,
pageNumberGroups,
})}
className="dnb-pagination__dots"
icon="more"
size="medium"
/>
{numbersList.map((pageNumber) => {
return (
<Button
key={(pageNumber || 0) + idx}
className={classnames(
'dnb-pagination__button',
String(pageNumber).length > 3
? 'dnb-pagination__button--large-number'
: null
)}
text={String(pageNumber)}
aria-label={button_title.replace(
'%s',
String(pageNumber)
)}
variant={
pageNumber === currentPage ? 'primary' : 'secondary'
}
disabled={disabled}
skeleton={skeleton}
aria-current={
pageNumber === currentPage ? 'page' : null
}
on_click={(event) =>
clickHandler({ pageNumber, event })
}
/>
)
})}
</React.Fragment>
))}
</div>
</div>
<span className="dnb-sr-only" aria-live="assertive">
{button_title.replace('%s', String(currentPage))}
</span>
</div>
)
}
export const useResizeObserver = (element) => {
const [currentSize, setSize] = useState('large')
const resizeObserver = useRef(null)
useEffect(() => {
try {
const handleSizeChange = (width) => {
if (width <= getSizeInPx('small') && currentSize !== 'small') {
setSize('small')
} else if (
width <= getSizeInPx('medium') &&
currentSize !== 'medium'
) {
setSize('medium')
} else if (
width <= getSizeInPx('large') &&
currentSize !== 'large'
) {
setSize('large')
} else if (
width <= getSizeInPx('x-large') &&
currentSize !== 'x-large'
) {
setSize('x-large')
} else if (
width <= getSizeInPx('xx-large') &&
currentSize !== 'xx-large'
) {
setSize('xx-large')
}
}
resizeObserver.current = new ResizeObserver((entries) => {
handleSizeChange(entries[0].contentRect.width)
})
resizeObserver.current?.observe(element.current)
handleSizeChange(element.current.clientWidth)
} catch (e) {
//
}
return () => {
resizeObserver.current?.disconnect()
}
}, [element]) // eslint-disable-line
return currentSize
}
const getSizeInPx = (size) => {
const styleSize = styleProperties[`--layout-${size}`]
if (styleSize.includes('em')) {
return parseFloat(styleSize.replace(/(rem|em)$/, '')) * 16
}
return parseFloat(styleSize.replace(/(px)$/, ''))
}
export default PaginationBar