/
PageItem.tsx
98 lines (84 loc) · 2.57 KB
/
PageItem.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
/* eslint-disable react/no-multi-comp */
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { ReactNode } from 'react';
import Anchor from '@restart/ui/Anchor';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
export interface PageItemProps
extends React.HTMLAttributes<HTMLElement>,
BsPrefixProps {
disabled?: boolean;
active?: boolean;
activeLabel?: string;
href?: string;
}
const propTypes = {
/** Disables the PageItem */
disabled: PropTypes.bool,
/** Styles PageItem as active, and renders a `<span>` instead of an `<a>`. */
active: PropTypes.bool,
/** An accessible label indicating the active state. */
activeLabel: PropTypes.string,
/** The HTML href attribute for the `PageItem`. */
href: PropTypes.string,
/** A callback function for when this component is clicked. */
onClick: PropTypes.func,
};
const defaultProps = {
active: false,
disabled: false,
activeLabel: '(current)',
};
const PageItem: BsPrefixRefForwardingComponent<'li', PageItemProps> =
React.forwardRef<HTMLLIElement, PageItemProps>(
(
{
active,
disabled,
className,
style,
activeLabel,
children,
...props
}: PageItemProps,
ref,
) => {
const Component = active || disabled ? 'span' : Anchor;
return (
<li
ref={ref}
style={style}
className={classNames(className, 'page-item', { active, disabled })}
>
<Component className="page-link" {...props}>
{children}
{active && activeLabel && (
<span className="visually-hidden">{activeLabel}</span>
)}
</Component>
</li>
);
},
);
PageItem.propTypes = propTypes;
PageItem.defaultProps = defaultProps;
PageItem.displayName = 'PageItem';
export default PageItem;
function createButton(name: string, defaultValue: ReactNode, label = name) {
const Button = React.forwardRef(
({ children, ...props }: PageItemProps, ref) => (
<PageItem {...props} ref={ref}>
<span aria-hidden="true">{children || defaultValue}</span>
<span className="visually-hidden">{label}</span>
</PageItem>
),
);
Button.displayName = name;
return Button;
}
export const First = createButton('First', '«');
export const Prev = createButton('Prev', '‹', 'Previous');
export const Ellipsis = createButton('Ellipsis', '…', 'More');
export const Next = createButton('Next', '›');
export const Last = createButton('Last', '»');