File size: 5,108 Bytes
1e92f2d | 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 | import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { translatable } from 'react-instantsearch-core';
import { createClassNames, capitalize, range } from '../core/utils';
import LinkList from './LinkList';
const cx = createClassNames('Pagination');
// Determines the size of the widget (the number of pages displayed - that the user can directly click on)
function calculateSize(padding, maxPages) {
return Math.min(2 * padding + 1, maxPages);
}
function calculatePaddingLeft(currentPage, padding, maxPages, size) {
if (currentPage <= padding) {
return currentPage;
}
if (currentPage >= maxPages - padding) {
return size - (maxPages - currentPage);
}
return padding + 1;
}
// Retrieve the correct page range to populate the widget
function getPages(currentPage, maxPages, padding) {
const size = calculateSize(padding, maxPages);
// If the widget size is equal to the max number of pages, return the entire page range
if (size === maxPages) return range({ start: 1, end: maxPages + 1 });
const paddingLeft = calculatePaddingLeft(
currentPage,
padding,
maxPages,
size
);
const paddingRight = size - paddingLeft;
const first = currentPage - paddingLeft;
const last = currentPage + paddingRight;
return range({ start: first + 1, end: last + 1 });
}
class Pagination extends Component {
static propTypes = {
nbPages: PropTypes.number.isRequired,
currentRefinement: PropTypes.number.isRequired,
refine: PropTypes.func.isRequired,
createURL: PropTypes.func.isRequired,
canRefine: PropTypes.bool.isRequired,
translate: PropTypes.func.isRequired,
listComponent: PropTypes.func,
showFirst: PropTypes.bool,
showPrevious: PropTypes.bool,
showNext: PropTypes.bool,
showLast: PropTypes.bool,
padding: PropTypes.number,
totalPages: PropTypes.number,
className: PropTypes.string,
};
static defaultProps = {
listComponent: LinkList,
showFirst: true,
showPrevious: true,
showNext: true,
showLast: false,
padding: 3,
totalPages: Infinity,
className: '',
};
getItem(modifier, translationKey, value) {
const { nbPages, totalPages, translate } = this.props;
return {
key: `${modifier}.${value}`,
modifier,
disabled: value < 1 || value >= Math.min(totalPages, nbPages),
label: translate(translationKey, value),
value,
ariaLabel: translate(`aria${capitalize(translationKey)}`, value),
};
}
render() {
const {
listComponent: ListComponent,
nbPages,
totalPages,
currentRefinement,
padding,
showFirst,
showPrevious,
showNext,
showLast,
refine,
createURL,
canRefine,
translate,
className,
...otherProps
} = this.props;
const maxPages = Math.min(nbPages, totalPages);
const lastPage = maxPages;
let items = [];
if (showFirst) {
items.push({
key: 'first',
modifier: 'item--firstPage',
disabled: currentRefinement === 1,
label: translate('first'),
value: 1,
ariaLabel: translate('ariaFirst'),
});
}
if (showPrevious) {
items.push({
key: 'previous',
modifier: 'item--previousPage',
disabled: currentRefinement === 1,
label: translate('previous'),
value: currentRefinement - 1,
ariaLabel: translate('ariaPrevious'),
});
}
items = items.concat(
getPages(currentRefinement, maxPages, padding).map((value) => ({
key: value,
modifier: 'item--page',
label: translate('page', value),
value,
selected: value === currentRefinement,
ariaLabel: translate('ariaPage', value),
}))
);
if (showNext) {
items.push({
key: 'next',
modifier: 'item--nextPage',
disabled: currentRefinement === lastPage || lastPage <= 1,
label: translate('next'),
value: currentRefinement + 1,
ariaLabel: translate('ariaNext'),
});
}
if (showLast) {
items.push({
key: 'last',
modifier: 'item--lastPage',
disabled: currentRefinement === lastPage || lastPage <= 1,
label: translate('last'),
value: lastPage,
ariaLabel: translate('ariaLast'),
});
}
return (
<div
className={classNames(cx('', !canRefine && '-noRefinement'), className)}
>
<ListComponent
{...otherProps}
cx={cx}
items={items}
onSelect={refine}
createURL={createURL}
canRefine={canRefine}
/>
</div>
);
}
}
export default translatable({
previous: '‹',
next: '›',
first: '«',
last: '»',
page: (currentRefinement) => currentRefinement.toString(),
ariaPrevious: 'Previous page',
ariaNext: 'Next page',
ariaFirst: 'First page',
ariaLast: 'Last page',
ariaPage: (currentRefinement) => `Page ${currentRefinement.toString()}`,
})(Pagination);
|