Skip to content

Commit 9017b72

Browse files
authored
fix(pagination): disable buttons if not results (#2643)
* fix(pagination): if the results contains no hits, disable the buttons * fix(pagination): add tests for disabled behaviour * chore: add story to test #2014 * fix(pagination): display the current if no results We're still on page 1 after all. fix #2014
1 parent e73ff13 commit 9017b72

File tree

5 files changed

+115
-16
lines changed

5 files changed

+115
-16
lines changed

dev/app/builtin/stories/pagination.stories.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ import { wrapWithHits } from '../../utils/wrap-with-hits.js';
77
const stories = storiesOf('Pagination');
88

99
export default () => {
10-
stories.add(
11-
'default',
12-
wrapWithHits(container => {
13-
window.search.addWidget(
14-
instantsearch.widgets.pagination({
15-
container,
16-
maxPages: 20,
17-
})
18-
);
19-
})
20-
);
10+
stories
11+
.add(
12+
'default',
13+
wrapWithHits(container => {
14+
window.search.addWidget(
15+
instantsearch.widgets.pagination({
16+
container,
17+
maxPages: 20,
18+
})
19+
);
20+
})
21+
)
22+
.add(
23+
'without autoHideContainer',
24+
wrapWithHits(container => {
25+
window.search.addWidget(
26+
instantsearch.widgets.pagination({
27+
container,
28+
autoHideContainer: false,
29+
})
30+
);
31+
})
32+
);
2133
};

src/components/Pagination/Pagination.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class RawPagination extends React.Component {
4444
cssClasses={cssClasses}
4545
handleClick={this.handleClick}
4646
isDisabled={isDisabled}
47-
key={label + pageNumber}
47+
key={label + pageNumber + ariaLabel}
4848
label={label}
4949
pageNumber={pageNumber}
5050
url={url}
@@ -56,7 +56,7 @@ export class RawPagination extends React.Component {
5656
return this.pageLink({
5757
ariaLabel: 'Previous',
5858
additionalClassName: this.props.cssClasses.previous,
59-
isDisabled: pager.isFirstPage(),
59+
isDisabled: this.props.nbHits === 0 || pager.isFirstPage(),
6060
label: this.props.labels.previous,
6161
pageNumber: pager.currentPage - 1,
6262
createURL,
@@ -67,7 +67,7 @@ export class RawPagination extends React.Component {
6767
return this.pageLink({
6868
ariaLabel: 'Next',
6969
additionalClassName: this.props.cssClasses.next,
70-
isDisabled: pager.isLastPage(),
70+
isDisabled: this.props.nbHits === 0 || pager.isLastPage(),
7171
label: this.props.labels.next,
7272
pageNumber: pager.currentPage + 1,
7373
createURL,
@@ -78,7 +78,7 @@ export class RawPagination extends React.Component {
7878
return this.pageLink({
7979
ariaLabel: 'First',
8080
additionalClassName: this.props.cssClasses.first,
81-
isDisabled: pager.isFirstPage(),
81+
isDisabled: this.props.nbHits === 0 || pager.isFirstPage(),
8282
label: this.props.labels.first,
8383
pageNumber: 0,
8484
createURL,
@@ -89,7 +89,7 @@ export class RawPagination extends React.Component {
8989
return this.pageLink({
9090
ariaLabel: 'Last',
9191
additionalClassName: this.props.cssClasses.last,
92-
isDisabled: pager.isLastPage(),
92+
isDisabled: this.props.nbHits === 0 || pager.isLastPage(),
9393
label: this.props.labels.last,
9494
pageNumber: pager.total - 1,
9595
createURL,

src/components/Pagination/Paginator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Paginator {
1010
pages() {
1111
const { total, currentPage, padding } = this;
1212

13+
if (total === 0) return [0];
14+
1315
const totalDisplayedPages = this.nbPagesDisplayed(padding, total);
1416
if (totalDisplayedPages === total) return range(0, total);
1517

src/components/Pagination/__tests__/Pagination-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ describe('Pagination', () => {
6767
);
6868
expect(preventDefault.calledOnce).toBe(true, 'preventDefault called once');
6969
});
70+
71+
it('should have all buttons disabled if there are no results', () => {
72+
const tree = renderer
73+
.create(
74+
<Pagination
75+
{...defaultProps}
76+
showFirstLast
77+
currentPage={0}
78+
nbHits={0}
79+
nbPages={0}
80+
/>
81+
)
82+
.toJSON();
83+
expect(tree).toMatchSnapshot();
84+
});
7085
});

src/components/Pagination/__tests__/__snapshots__/Pagination-test.js.snap

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,76 @@ exports[`Pagination should display the first/last link 1`] = `
332332
</ul>
333333
`;
334334

335+
exports[`Pagination should have all buttons disabled if there are no results 1`] = `
336+
<ul
337+
className="root"
338+
>
339+
<li
340+
className="item first disabled"
341+
>
342+
<span
343+
className=""
344+
dangerouslySetInnerHTML={
345+
Object {
346+
"__html": "",
347+
}
348+
}
349+
/>
350+
</li>
351+
<li
352+
className="item previous disabled"
353+
>
354+
<span
355+
className=""
356+
dangerouslySetInnerHTML={
357+
Object {
358+
"__html": "",
359+
}
360+
}
361+
/>
362+
</li>
363+
<li
364+
className="item page active"
365+
>
366+
<a
367+
aria-label={1}
368+
className=""
369+
dangerouslySetInnerHTML={
370+
Object {
371+
"__html": 1,
372+
}
373+
}
374+
href="[0]"
375+
onClick={[Function]}
376+
/>
377+
</li>
378+
<li
379+
className="item next disabled"
380+
>
381+
<span
382+
className=""
383+
dangerouslySetInnerHTML={
384+
Object {
385+
"__html": "",
386+
}
387+
}
388+
/>
389+
</li>
390+
<li
391+
className="item last disabled"
392+
>
393+
<span
394+
className=""
395+
dangerouslySetInnerHTML={
396+
Object {
397+
"__html": "",
398+
}
399+
}
400+
/>
401+
</li>
402+
</ul>
403+
`;
404+
335405
exports[`Pagination should render five elements 1`] = `
336406
<ul
337407
className="root"

0 commit comments

Comments
 (0)