UNPKG

2.24 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import classNames from 'classnames';
4import { mapToCssModules, tagPropType } from './utils';
5
6const propTypes = {
7 'aria-label': PropTypes.string,
8 children: PropTypes.node,
9 /** Add custom class */
10 className: PropTypes.string,
11 /** Change underlying component's CSS base class name */
12 cssModule: PropTypes.object,
13 /** Add to next button to add default aria label and icon */
14 next: PropTypes.bool,
15 /** Add to previous button to add default aria label and icon */
16 previous: PropTypes.bool,
17 /** Add to first button to add default aria label and icon */
18 first: PropTypes.bool,
19 /** Add to last button to add default aria label and icon */
20 last: PropTypes.bool,
21 /** Set a custom element for this component */
22 tag: tagPropType,
23};
24
25function PaginationLink(props) {
26 let {
27 className,
28 cssModule,
29 next,
30 previous,
31 first,
32 last,
33 tag: Tag = 'a',
34 ...attributes
35 } = props;
36
37 const classes = mapToCssModules(
38 classNames(className, 'page-link'),
39 cssModule,
40 );
41
42 let defaultAriaLabel;
43 if (previous) {
44 defaultAriaLabel = 'Previous';
45 } else if (next) {
46 defaultAriaLabel = 'Next';
47 } else if (first) {
48 defaultAriaLabel = 'First';
49 } else if (last) {
50 defaultAriaLabel = 'Last';
51 }
52
53 const ariaLabel = props['aria-label'] || defaultAriaLabel;
54
55 let defaultCaret;
56 if (previous) {
57 defaultCaret = '\u2039';
58 } else if (next) {
59 defaultCaret = '\u203A';
60 } else if (first) {
61 defaultCaret = '\u00ab';
62 } else if (last) {
63 defaultCaret = '\u00bb';
64 }
65
66 let { children } = props;
67 if (children && Array.isArray(children) && children.length === 0) {
68 children = null;
69 }
70
71 if (!attributes.href && Tag === 'a') {
72 Tag = 'button';
73 }
74
75 if (previous || next || first || last) {
76 children = [
77 <span aria-hidden="true" key="caret">
78 {children || defaultCaret}
79 </span>,
80 <span className="visually-hidden" key="ariaLabel">
81 {ariaLabel}
82 </span>,
83 ];
84 }
85
86 return (
87 <Tag {...attributes} className={classes} aria-label={ariaLabel}>
88 {children}
89 </Tag>
90 );
91}
92
93PaginationLink.propTypes = propTypes;
94
95export default PaginationLink;