import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, tagPropType } from './utils';
const propTypes = {
'aria-label': PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
cssModule: PropTypes.object,
next: PropTypes.bool,
previous: PropTypes.bool,
first: PropTypes.bool,
last: PropTypes.bool,
tag: tagPropType,
};
const defaultProps = {
tag: 'a',
};
const PaginationLink = (props) => {
let {
className,
cssModule,
next,
previous,
first,
last,
tag: Tag,
...attributes
} = props;
const classes = mapToCssModules(classNames(
className,
'page-link'
), cssModule);
let defaultAriaLabel;
if (previous) {
defaultAriaLabel = 'Previous';
} else if (next) {
defaultAriaLabel = 'Next';
} else if (first) {
defaultAriaLabel = 'First';
} else if (last) {
defaultAriaLabel = 'Last';
}
const ariaLabel = props['aria-label'] || defaultAriaLabel;
let defaultCaret;
if (previous) {
defaultCaret = '\u2039';
} else if (next) {
defaultCaret = '\u203A';
} else if (first) {
defaultCaret = '\u00ab';
} else if (last) {
defaultCaret = '\u00bb';
}
let children = props.children;
if (children && Array.isArray(children) && children.length === 0) {
children = null;
}
if (!attributes.href && Tag === 'a') {
Tag = 'button';
}
if (previous || next || first || last) {
children = [
{children || defaultCaret}
,
{ariaLabel}
,
];
}
return (
{children}
);
};
PaginationLink.propTypes = propTypes;
PaginationLink.defaultProps = defaultProps;
export default PaginationLink;