-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
129 lines (123 loc) · 2.87 KB
/
index.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
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
/**
* WordPress dependencies
*/
import {
Button,
__experimentalHStack as HStack,
SelectControl,
} from '@wordpress/components';
import { createInterpolateElement, memo, useContext } from '@wordpress/element';
import { sprintf, __, _x, isRTL } from '@wordpress/i18n';
import { next, previous } from '@wordpress/icons';
/**
* Internal dependencies
*/
import DataViewsContext from '../dataviews-context';
function DataViewsPagination() {
const {
view,
onChangeView,
paginationInfo: { totalItems = 0, totalPages },
} = useContext( DataViewsContext );
if ( ! totalItems || ! totalPages ) {
return null;
}
const currentPage = view.page ?? 1;
const pageSelectOptions = Array.from( Array( totalPages ) ).map(
( _, i ) => {
const page = i + 1;
return {
value: page.toString(),
label: page.toString(),
'aria-label':
currentPage === page
? sprintf(
// translators: Current page number in total number of pages
__( 'Page %1$s of %2$s' ),
currentPage,
totalPages
)
: page.toString(),
};
}
);
return (
!! totalItems &&
totalPages !== 1 && (
<HStack
expanded={ false }
className="dataviews-pagination"
justify="end"
spacing={ 6 }
>
<HStack
justify="flex-start"
expanded={ false }
spacing={ 1 }
className="dataviews-pagination__page-select"
>
{ createInterpolateElement(
sprintf(
// translators: 1: Current page number, 2: Total number of pages.
_x(
'<div>Page</div>%1$s<div>of %2$s</div>',
'paging'
),
'<CurrentPage />',
totalPages
),
{
div: <div aria-hidden />,
CurrentPage: (
<SelectControl
aria-label={ __( 'Current page' ) }
value={ currentPage.toString() }
options={ pageSelectOptions }
onChange={ ( newValue ) => {
onChangeView( {
...view,
page: +newValue,
} );
} }
size="small"
__nextHasNoMarginBottom
variant="minimal"
/>
),
}
) }
</HStack>
<HStack expanded={ false } spacing={ 1 }>
<Button
onClick={ () =>
onChangeView( {
...view,
page: currentPage - 1,
} )
}
disabled={ currentPage === 1 }
accessibleWhenDisabled
label={ __( 'Previous page' ) }
icon={ isRTL() ? next : previous }
showTooltip
size="compact"
tooltipPosition="top"
/>
<Button
onClick={ () =>
onChangeView( { ...view, page: currentPage + 1 } )
}
disabled={ currentPage >= totalPages }
accessibleWhenDisabled
label={ __( 'Next page' ) }
icon={ isRTL() ? previous : next }
showTooltip
size="compact"
tooltipPosition="top"
/>
</HStack>
</HStack>
)
);
}
export default memo( DataViewsPagination );