-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathgrid.js
142 lines (132 loc) · 2.94 KB
/
grid.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
135
136
137
138
139
140
141
142
/**
* WordPress dependencies
*/
import {
__experimentalHStack as HStack,
__experimentalText as Text,
Button,
} from '@wordpress/components';
import { useRef, useMemo } from '@wordpress/element';
import { __, _x, _n, sprintf } from '@wordpress/i18n';
import { useAsyncList } from '@wordpress/compose';
/**
* Internal dependencies
*/
import GridItem from './grid-item';
const PAGE_SIZE = 20;
function Pagination( { currentPage, numPages, changePage, totalItems } ) {
return (
<HStack
expanded={ false }
spacing={ 3 }
className="edit-site-patterns__grid-pagination"
>
<Text variant="muted">
{
// translators: %s: Total number of patterns.
sprintf(
// translators: %s: Total number of patterns.
_n( '%s item', '%s items', totalItems ),
totalItems
)
}
</Text>
<HStack expanded={ false } spacing={ 1 }>
<Button
variant="tertiary"
onClick={ () => changePage( 1 ) }
disabled={ currentPage === 1 }
aria-label={ __( 'First page' ) }
>
«
</Button>
<Button
variant="tertiary"
onClick={ () => changePage( currentPage - 1 ) }
disabled={ currentPage === 1 }
aria-label={ __( 'Previous page' ) }
>
‹
</Button>
</HStack>
<Text variant="muted">
{ sprintf(
// translators: %1$s: Current page number, %2$s: Total number of pages.
_x( '%1$s of %2$s', 'paging' ),
currentPage,
numPages
) }
</Text>
<HStack expanded={ false } spacing={ 1 }>
<Button
variant="tertiary"
onClick={ () => changePage( currentPage + 1 ) }
disabled={ currentPage === numPages }
aria-label={ __( 'Next page' ) }
>
›
</Button>
<Button
variant="tertiary"
onClick={ () => changePage( numPages ) }
disabled={ currentPage === numPages }
aria-label={ __( 'Last page' ) }
>
»
</Button>
</HStack>
</HStack>
);
}
export default function Grid( {
categoryId,
items,
currentPage,
setCurrentPage,
...props
} ) {
const gridRef = useRef();
const totalItems = items.length;
const pageIndex = currentPage - 1;
const list = useMemo(
() =>
items.slice(
pageIndex * PAGE_SIZE,
pageIndex * PAGE_SIZE + PAGE_SIZE
),
[ pageIndex, items ]
);
const asyncList = useAsyncList( list, { step: 10 } );
if ( ! list?.length ) {
return null;
}
const numPages = Math.ceil( items.length / PAGE_SIZE );
const changePage = ( page ) => {
const scrollContainer = document.querySelector( '.edit-site-patterns' );
scrollContainer?.scrollTo( 0, 0 );
setCurrentPage( page );
};
return (
<>
<ul
role="listbox"
className="edit-site-patterns__grid"
{ ...props }
ref={ gridRef }
>
{ asyncList.map( ( item ) => (
<GridItem
key={ item.name }
item={ item }
categoryId={ categoryId }
/>
) ) }
</ul>
{ numPages > 1 && (
<Pagination
{ ...{ currentPage, numPages, changePage, totalItems } }
/>
) }
</>
);
}