-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPrevNextPaginationRow.js
134 lines (123 loc) · 3.92 KB
/
PrevNextPaginationRow.js
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
import React from 'react';
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';
import Icon from '../Icon';
import PagingButton from './PagingButton';
import css from './MCLRenderer.css';
const propTypes = {
activeNext: PropTypes.bool,
activePrevious: PropTypes.bool,
containerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
dataEndIndex: PropTypes.number,
dataEndReached: PropTypes.bool,
dataStartIndex: PropTypes.number,
handleLoadMore: PropTypes.func,
hidePageIndices: PropTypes.bool,
id: PropTypes.string,
keyId: PropTypes.string,
loading: PropTypes.bool,
loadingNext: PropTypes.bool,
loadingPrevious: PropTypes.bool,
pageAmount: PropTypes.number,
pagingOffset: PropTypes.number,
rowIndex: PropTypes.number,
sendMessage: PropTypes.func,
setFocusIndex: PropTypes.func,
};
const PrevNextPaginationRow = ({
activeNext,
activePrevious,
containerRef,
dataEndReached,
handleLoadMore,
id,
keyId,
loading,
loadingNext,
loadingPrevious,
pageAmount,
pagingOffset,
rowIndex,
sendMessage,
dataStartIndex,
dataEndIndex,
hidePageIndices,
setFocusIndex,
}) => {
const { formatMessage } = useIntl();
if (dataEndReached) { return null; }
const isNextButtonDisabled = loading || loadingNext || !activeNext;
const isPrevButtonDisabled = loading || loadingPrevious || !activePrevious;
const previousLabel = (
<div data-test-pagination-previous>
<Icon size="small" icon="caret-left">
<span>{formatMessage({ id: 'stripes-components.previous' })}</span>
</Icon>
</div>
);
const nextLabel = (
<div data-test-pagination-next>
<Icon size="small" icon="caret-right" iconPosition="end">
<span>{formatMessage({ id: 'stripes-components.next' })}</span>
</Icon>
</div>
);
const message = formatMessage({ id: 'stripes-components.mcl.itemsRequestedMessage' });
return (
<div style={{ padding: '8px 0' }} ref={containerRef}>
<div className={css.mclPaginationCenterContainer}>
<PagingButton
onClick={() => {
if (typeof pagingOffset !== 'undefined') {
setFocusIndex(0);
handleLoadMore(pageAmount, pagingOffset - pageAmount, 'prev');
} else {
const remainder = rowIndex % pageAmount;
const rowIndexForPrevButton = remainder === 0
? rowIndex - (pageAmount * 2)
: rowIndex - (pageAmount + remainder);
setFocusIndex(rowIndexForPrevButton);
handleLoadMore(pageAmount, rowIndexForPrevButton, 'prev');
}
}}
sendMessage={sendMessage}
loading={loadingPrevious}
loadingMessage={message}
disabled={isPrevButtonDisabled}
pagingButtonLabel={previousLabel}
buttonStyle="none"
bottomMargin0
data-test-prev-paging-button
id={`${id || keyId}-prev-paging-button`}
/>
{!hidePageIndices && (
<div className={css.mclPrevNextPageInfoContainer}>
<div>{dataStartIndex}</div> - <div>{dataEndIndex}</div>
</div>
)}
<PagingButton
onClick={() => {
if (typeof pagingOffset !== 'undefined') {
setFocusIndex(0);
handleLoadMore(pageAmount, pagingOffset + pageAmount, 'next');
} else {
setFocusIndex(rowIndex);
handleLoadMore(pageAmount, rowIndex, 'next');
}
}}
sendMessage={sendMessage}
loading={loadingNext}
loadingMessage={message}
disabled={isNextButtonDisabled}
pagingButtonLabel={nextLabel}
buttonStyle="none"
bottomMargin0
data-test-next-paging-button
id={`${id || keyId}-next-paging-button`}
/>
</div>
</div>
);
};
PrevNextPaginationRow.propTypes = propTypes;
export default PrevNextPaginationRow;