Skip to content

Commit eed7986

Browse files
committed
feat(Pagination): adds pager and simple pagination
1 parent fd4b864 commit eed7986

File tree

5 files changed

+232
-1
lines changed

5 files changed

+232
-1
lines changed

src/components/Pagination/Pager.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import cx from 'classnames';
4+
import { noop } from '../../common/helpers';
5+
6+
/**
7+
* Pager component for Patternfly React
8+
*/
9+
const Pager = ({
10+
baseClassName,
11+
className,
12+
messages,
13+
disableNext,
14+
onNextPage,
15+
disablePrevious,
16+
onPreviousPage
17+
}) => {
18+
const classes = cx('pager', className);
19+
return (
20+
<ul className={classes}>
21+
<li className={disablePrevious ? 'previous disabled' : 'previous'}>
22+
<a
23+
href="#"
24+
className={disablePrevious ? 'disabled' : ''}
25+
onClick={e => {
26+
e.preventDefault();
27+
if (!disablePrevious) {
28+
onPreviousPage(e);
29+
}
30+
}}
31+
>
32+
<span className="i fa fa-angle-left" />
33+
{messages.previousPage}
34+
</a>
35+
</li>
36+
<li className={disableNext ? 'next disabled' : 'next'}>
37+
<a
38+
href="#"
39+
className={disableNext ? 'disabled' : ''}
40+
onClick={e => {
41+
e.preventDefault();
42+
if (!disableNext) {
43+
onNextPage(e);
44+
}
45+
}}
46+
>
47+
{messages.nextPage}
48+
<span className="i fa fa-angle-right" />
49+
</a>
50+
</li>
51+
</ul>
52+
);
53+
};
54+
Pager.propTypes = {
55+
/** Base css class */
56+
baseClassName: PropTypes.string,
57+
/** Additional css classes */
58+
className: PropTypes.string,
59+
/** message text inputs for i18n */
60+
messages: PropTypes.shape({
61+
nextPage: PropTypes.string,
62+
previousPage: PropTypes.string
63+
}),
64+
/** next button disabled */
65+
disableNext: PropTypes.bool,
66+
/** next page callback */
67+
onNextPage: PropTypes.func,
68+
/** previous button disabled */
69+
disablePrevious: PropTypes.bool,
70+
/** previous page callback */
71+
onPreviousPage: PropTypes.func
72+
};
73+
Pager.defaultProps = {
74+
baseClassName: 'content-view-pf-pagination',
75+
className: '',
76+
messages: {
77+
nextPage: 'Next',
78+
previousPage: 'Previous'
79+
},
80+
disableNext: false,
81+
onNextPage: noop,
82+
disablePrevious: false,
83+
onPreviousPage: noop
84+
};
85+
export default Pager;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-env jest */
2+
3+
import React from 'react';
4+
import renderer from 'react-test-renderer';
5+
import { Pager } from './index';
6+
7+
const testPagerSnapshot = props => <Pager {...props} />;
8+
9+
test('Pager default renders properly', () => {
10+
const component = renderer.create(testPagerSnapshot(Pager));
11+
expect(component.toJSON()).toMatchSnapshot();
12+
});
13+
14+
test('Pager mini size renders properly', () => {
15+
const component = renderer.create(
16+
testPagerSnapshot({
17+
className: 'pager-sm',
18+
messages: {
19+
nextPage: 'The Next Page',
20+
previousPage: 'The Previous Page'
21+
},
22+
disableNext: true,
23+
disablePrevious: true
24+
})
25+
);
26+
expect(component.toJSON()).toMatchSnapshot();
27+
});

src/components/Pagination/Pagination.stories.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
import { inlineTemplate } from '../../../storybook/decorators/storyTemplates';
1313
import { DOCUMENTATION_URL } from '../../../storybook/constants';
1414

15-
import { PaginationRow, Paginator, PAGINATION_VIEW_TYPES } from './index';
15+
import {
16+
Pager,
17+
PaginationRow,
18+
Paginator,
19+
PAGINATION_VIEW_TYPES
20+
} from './index';
1621
import {
1722
MockPaginationRow,
1823
mockPaginationSource
@@ -21,6 +26,47 @@ import {
2126
const stories = storiesOf('Pagination', module);
2227
stories.addDecorator(withKnobs);
2328

29+
stories.add(
30+
'Pager',
31+
withInfo()(() => {
32+
const story = (
33+
<div>
34+
<h2>Default size</h2>
35+
<Pager
36+
onNextPage={action('onNextPage')}
37+
onPreviousPage={action('onPreviousPage')}
38+
/>
39+
<hr />
40+
<h2>Mini size</h2>
41+
<Pager
42+
className="pager-sm"
43+
messages={{
44+
nextPage: 'The Next Page',
45+
previousPage: 'The Previous Page'
46+
}}
47+
onNextPage={action('onNextPage')}
48+
onPreviousPage={action('onPreviousPage')}
49+
disableNext={boolean('Next button disabled', true)}
50+
disablePrevious={boolean('Previous button disabled', false)}
51+
/>
52+
</div>
53+
);
54+
return inlineTemplate({
55+
title: 'Pager',
56+
documentationLink: `${
57+
DOCUMENTATION_URL.PATTERNFLY_ORG_WIDGETS
58+
}#pagination`,
59+
story,
60+
description: (
61+
<div>
62+
Pager is a stateless functional component which previous and next
63+
links. See Action Logger for details.
64+
</div>
65+
)
66+
});
67+
})
68+
);
69+
2470
stories.add(
2571
'Pagination row',
2672
withInfo({
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Pager default renders properly 1`] = `
4+
<ul
5+
className="pager"
6+
>
7+
<li
8+
className="previous"
9+
>
10+
<a
11+
className=""
12+
href="#"
13+
onClick={[Function]}
14+
>
15+
<span
16+
className="i fa fa-angle-left"
17+
/>
18+
Previous
19+
</a>
20+
</li>
21+
<li
22+
className="next"
23+
>
24+
<a
25+
className=""
26+
href="#"
27+
onClick={[Function]}
28+
>
29+
Next
30+
<span
31+
className="i fa fa-angle-right"
32+
/>
33+
</a>
34+
</li>
35+
</ul>
36+
`;
37+
38+
exports[`Pager mini size renders properly 1`] = `
39+
<ul
40+
className="pager pager-sm"
41+
>
42+
<li
43+
className="previous disabled"
44+
>
45+
<a
46+
className="disabled"
47+
href="#"
48+
onClick={[Function]}
49+
>
50+
<span
51+
className="i fa fa-angle-left"
52+
/>
53+
The Previous Page
54+
</a>
55+
</li>
56+
<li
57+
className="next disabled"
58+
>
59+
<a
60+
className="disabled"
61+
href="#"
62+
onClick={[Function]}
63+
>
64+
The Next Page
65+
<span
66+
className="i fa fa-angle-right"
67+
/>
68+
</a>
69+
</li>
70+
</ul>
71+
`;

src/components/Pagination/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import paginate from './paginate';
22
import { PAGINATION_VIEW, PAGINATION_VIEW_TYPES } from './PaginationConstants';
3+
import Pager from './Pager';
34
import Paginator from './Paginator';
45
import PaginationRow from './PaginationRow';
56
import PaginationRowAmountOfPages from './PaginationRowAmountOfPages';
@@ -20,6 +21,7 @@ PaginationRow.PAGINATION_VIEW_TYPES = PAGINATION_VIEW_TYPES;
2021

2122
export {
2223
paginate,
24+
Pager,
2325
Paginator,
2426
PAGINATION_VIEW,
2527
PAGINATION_VIEW_TYPES,

0 commit comments

Comments
 (0)