-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpagination-item.tsx
126 lines (121 loc) · 4.07 KB
/
pagination-item.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
import React, { FC, Key, ReactNode, RefObject, StyleHTMLAttributes, ComponentProps } from 'react'
import { EbayIcon } from '../ebay-icon'
import { withForwardRef } from '../common/component-utils'
import classNames from 'classnames'
import { EbayEventHandler } from '../common/event-utils/types'
export type PaginationItemType = 'previous' | 'next' | 'page' | 'separator'
type HtmlProps = Omit<ComponentProps<'button'>, 'type'> & ComponentProps<'a'> & ComponentProps<'li'>
export type PaginationItemProps = HtmlProps & {
pageIndex?: number;
key?: Key;
type?: PaginationItemType;
current?: boolean;
disabled?: boolean;
href?: string;
hide?: boolean;
a11yPreviousText?: string;
a11yNextText?: string;
onPrevious?: EbayEventHandler;
onNext?: EbayEventHandler;
onSelect?: EbayEventHandler<{ value: string, index: number }>;
style?: StyleHTMLAttributes<HTMLButtonElement & HTMLAnchorElement>;
forwardedRef?: RefObject<HTMLAnchorElement & HTMLButtonElement>;
children?: ReactNode;
};
const EbayPaginationItem: FC<PaginationItemProps> = ({
pageIndex = 0,
key,
current,
disabled,
type = 'page',
href,
hide,
children,
a11yPreviousText = 'Previous page',
a11yNextText = 'Next page',
onSelect,
onNext,
onPrevious,
className,
style,
forwardedRef,
...rest
}) => {
const handlePageNumber = (e) => {
onSelect(e, { value: e.currentTarget?.innerText || '', index: pageIndex })
}
const handleNextPage = (e) => {
if (!e.currentTarget.getAttribute('aria-disabled')) {
onNext(e)
}
}
const handlePreviousPage = (e) => {
if (!e.currentTarget.getAttribute('aria-disabled')) {
onPrevious(e)
}
}
const isAnchor = !!href
const ButtonOrAnchor = isAnchor ? 'a' : 'button'
const iconClassName = isAnchor ? 'icon-link' : 'icon-btn'
const arrowStyle = { ...style, minWidth: '40px' }
switch (type) {
case 'previous':
return (
<ButtonOrAnchor
{...rest}
ref={forwardedRef}
aria-disabled={disabled ? 'true' : undefined}
aria-label={a11yPreviousText}
href={disabled ? undefined : href}
className={classNames(iconClassName, 'pagination__previous')}
style={arrowStyle}
onClick={handlePreviousPage}
>
<EbayIcon name="arrowLeft16" />
</ButtonOrAnchor >
)
case 'next':
return (
<ButtonOrAnchor
{...rest}
ref={forwardedRef}
aria-disabled={disabled ? 'true' : undefined}
aria-label={a11yNextText}
href={disabled ? undefined : href}
className={classNames(iconClassName, 'pagination__next')}
style={arrowStyle}
onClick={handleNextPage}
>
<EbayIcon name="arrowRight16" />
</ButtonOrAnchor >
)
case 'separator':
return (
<span
key={key}
style={style}
className="pagination__item"
ref={forwardedRef}
role="separator">
{children}
</span>
)
default:
return (
<li {...rest} hidden={hide}>
<ButtonOrAnchor
ref={forwardedRef}
aria-current={current ? 'page' : undefined}
href={href}
className="pagination__item"
style={style}
key={key}
onClick={handlePageNumber}
>
{children}
</ButtonOrAnchor>
</li >
)
}
}
export default withForwardRef(EbayPaginationItem)