Skip to content

Commit 1ef8311

Browse files
committed
[pagination] Use compact layout when counts are zero
Change-Id: I899a187b211702ec415758944ff1ed0404944470
1 parent d514a95 commit 1ef8311

2 files changed

Lines changed: 39 additions & 35 deletions

File tree

packages/mui-material/src/usePagination/usePagination.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,39 @@ export default function usePagination(props = {}) {
6666
count - boundaryCount - 1,
6767
);
6868

69+
// Start ellipsis
70+
const startEllipsis = [];
71+
if (siblingsStart > boundaryCount + 2) {
72+
startEllipsis.push('start-ellipsis');
73+
} else if (boundaryCount + 1 < count - boundaryCount) {
74+
startEllipsis.push(boundaryCount + 1);
75+
}
76+
77+
// End ellipsis
78+
const endEllipsis = [];
79+
if (siblingsEnd < count - boundaryCount - 1) {
80+
endEllipsis.push('end-ellipsis');
81+
} else if (count - boundaryCount > boundaryCount) {
82+
endEllipsis.push(count - boundaryCount);
83+
}
84+
85+
const body =
86+
boundaryCount === 0 && siblingCount === 0
87+
? [page]
88+
: [
89+
...startPages,
90+
...startEllipsis,
91+
...range(siblingsStart, siblingsEnd),
92+
...endEllipsis,
93+
...endPages,
94+
];
95+
6996
// Basic list of items to render
7097
// for example itemList = ['first', 'previous', 1, 'ellipsis', 4, 5, 6, 'ellipsis', 10, 'next', 'last']
7198
const itemList = [
7299
...(showFirstButton ? ['first'] : []),
73100
...(hidePrevButton ? [] : ['previous']),
74-
...startPages,
75-
76-
// Start ellipsis
77-
// eslint-disable-next-line no-nested-ternary
78-
...(siblingsStart > boundaryCount + 2
79-
? ['start-ellipsis']
80-
: boundaryCount + 1 < count - boundaryCount
81-
? [boundaryCount + 1]
82-
: []),
83-
84-
// Sibling pages
85-
...range(siblingsStart, siblingsEnd),
86-
87-
// End ellipsis
88-
// eslint-disable-next-line no-nested-ternary
89-
...(siblingsEnd < count - boundaryCount - 1
90-
? ['end-ellipsis']
91-
: count - boundaryCount > boundaryCount
92-
? [count - boundaryCount]
93-
: []),
94-
95-
...endPages,
101+
...body,
96102
...(hideNextButton ? [] : ['next']),
97103
...(showLastButton ? ['last'] : []),
98104
];

packages/mui-material/src/usePagination/usePagination.test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,21 @@ describe('usePagination', () => {
151151
expect(items[9]).to.have.property('page', 11);
152152
});
153153

154-
it('should support boundaryCount={0}', () => {
155-
let items;
154+
it('uses a compact layout when boundaryCount and siblingCount are zero', () => {
155+
[1, 6, 11].forEach((page) => {
156+
const items = renderHook(() =>
157+
usePagination({ count: 11, page, boundaryCount: 0, siblingCount: 0 }),
158+
).result.current.items;
156159

157-
items = renderHook(() =>
158-
usePagination({ count: 11, page: 6, boundaryCount: 0, siblingCount: 0 }),
159-
).result.current.items;
160-
expect(serialize(items)).to.deep.equal([
161-
'previous',
162-
'start-ellipsis',
163-
6,
164-
'end-ellipsis',
165-
'next',
166-
]);
160+
expect(serialize(items)).to.deep.equal(['previous', page, 'next']);
161+
});
162+
});
167163

168-
items = renderHook(() =>
164+
it('should support boundaryCount={0}', () => {
165+
const items = renderHook(() =>
169166
usePagination({ count: 11, page: 6, boundaryCount: 0, siblingCount: 1 }),
170167
).result.current.items;
168+
171169
expect(serialize(items)).to.deep.equal([
172170
'previous',
173171
'start-ellipsis',

0 commit comments

Comments
 (0)