Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/components/Pagination/Pager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Icon } from '../Icon';
import { noop } from '../../common/helpers';

/**
* Pager component for Patternfly React
*/
const Pager = ({
baseClassName,
className,
messages,
disableNext,
onNextPage,
disablePrevious,
onPreviousPage
}) => {
const classes = classNames('pager', className);
return (
<ul className={classes}>
<li className={classNames({ disabled: disablePrevious }, 'previous')}>
<a
href="#"
className={classNames({ disabled: disablePrevious })}
onClick={e => {
e.preventDefault();
if (!disablePrevious) {
onPreviousPage(e);
}
}}
>
<Icon className="i" name="angle-left" />
{messages.previousPage}
</a>
</li>
<li className={classNames({ disabled: disableNext }, 'next')}>
<a
href="#"
className={classNames({ disabled: disableNext })}
onClick={e => {
e.preventDefault();
if (!disableNext) {
onNextPage(e);
}
}}
>
{messages.nextPage}
<Icon className="i" name="angle-right" />
</a>
</li>
</ul>
);
};
Pager.propTypes = {
/** Base css class */
baseClassName: PropTypes.string,
/** Additional css classes */
className: PropTypes.string,
/** message text inputs for i18n */
messages: PropTypes.shape({
nextPage: PropTypes.string,
previousPage: PropTypes.string
}),
/** next button disabled */
disableNext: PropTypes.bool,
/** next page callback */
onNextPage: PropTypes.func,
/** previous button disabled */
disablePrevious: PropTypes.bool,
/** previous page callback */
onPreviousPage: PropTypes.func
};
Pager.defaultProps = {
baseClassName: 'content-view-pf-pagination',
className: '',
messages: {
nextPage: 'Next',
previousPage: 'Previous'
},
disableNext: false,
onNextPage: noop,
disablePrevious: false,
onPreviousPage: noop
};
export default Pager;
71 changes: 71 additions & 0 deletions src/components/Pagination/Pager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import { mount } from 'enzyme';
import { Pager } from './index';

const testPagerSnapshot = props => <Pager {...props} />;

test('Pager default renders properly', () => {
const component = mount(testPagerSnapshot(Pager));

expect(component.render()).toMatchSnapshot();
});

test('Pager mini size renders properly', () => {
const component = mount(
testPagerSnapshot({
className: 'pager-sm',
messages: {
nextPage: 'The Next Page',
previousPage: 'The Previous Page'
},
disableNext: true,
disablePrevious: true
})
);

expect(component.render()).toMatchSnapshot();
});

test('Pager buttons fire when enabled', () => {
let eventCount = 0;
const component = mount(
testPagerSnapshot({
onPreviousPage: () => eventCount++,
onNextPage: () => eventCount++
})
);

component
.find('a')
.first()
.simulate('click');
component
.find('a')
.last()
.simulate('click');

expect(eventCount).toBe(2);
});

test('Pager buttons do not fire when disabled', () => {
let eventCount = 0;
const component = mount(
testPagerSnapshot({
onPreviousPage: () => eventCount++,
onNextPage: () => eventCount++,
disableNext: true,
disablePrevious: true
})
);

component
.find('a')
.first()
.simulate('click');
component
.find('a')
.last()
.simulate('click');

expect(eventCount).toBe(0);
});
50 changes: 49 additions & 1 deletion src/components/Pagination/Pagination.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
import { inlineTemplate } from '../../../storybook/decorators/storyTemplates';
import { DOCUMENTATION_URL } from '../../../storybook/constants';

import { PaginationRow, Paginator, PAGINATION_VIEW_TYPES } from './index';
import {
Pager,
PaginationRow,
Paginator,
PAGINATION_VIEW_TYPES
} from './index';
import {
MockPaginationRow,
mockPaginationSource
Expand All @@ -21,6 +26,49 @@ import {
const stories = storiesOf('Pagination', module);
stories.addDecorator(withKnobs);

stories.add(
'Pager',
withInfo()(() => {
const story = (
<div>
<h2>Default size</h2>
<Pager
onNextPage={action('onNextPage')}
onPreviousPage={action('onPreviousPage')}
disableNext={boolean('Next button disabled', true)}
disablePrevious={boolean('Previous button disabled', false)}
/>
<hr />
<h2>Mini size</h2>
<Pager
className="pager-sm"
messages={{
nextPage: 'The Next Page',
previousPage: 'The Previous Page'
}}
onNextPage={action('onNextPage')}
onPreviousPage={action('onPreviousPage')}
disableNext={boolean('Next button disabled', true)}
disablePrevious={boolean('Previous button disabled', false)}
/>
</div>
);
return inlineTemplate({
title: 'Pager',
documentationLink: `${
DOCUMENTATION_URL.PATTERNFLY_ORG_WIDGETS
}#pagination`,
story,
description: (
<div>
Pager is a stateless functional component which previous and next
links. See Action Logger for details.
</div>
)
});
})
);

stories.add(
'Pagination row',
withInfo({
Expand Down
71 changes: 71 additions & 0 deletions src/components/Pagination/__snapshots__/Pager.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Pager default renders properly 1`] = `
<ul
class="pager"
>
<li
class="previous"
>
<a
class=""
href="#"
>
<span
aria-hidden="true"
class="fa fa-angle-left i"
/>
Previous
</a>
</li>
<li
class="next"
>
<a
class=""
href="#"
>
Next
<span
aria-hidden="true"
class="fa fa-angle-right i"
/>
</a>
</li>
</ul>
`;

exports[`Pager mini size renders properly 1`] = `
<ul
class="pager pager-sm"
>
<li
class="disabled previous"
>
<a
class="disabled"
href="#"
>
<span
aria-hidden="true"
class="fa fa-angle-left i"
/>
The Previous Page
</a>
</li>
<li
class="disabled next"
>
<a
class="disabled"
href="#"
>
The Next Page
<span
aria-hidden="true"
class="fa fa-angle-right i"
/>
</a>
</li>
</ul>
`;
2 changes: 2 additions & 0 deletions src/components/Pagination/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import paginate from './paginate';
import { PAGINATION_VIEW, PAGINATION_VIEW_TYPES } from './PaginationConstants';
import Pager from './Pager';
import Paginator from './Paginator';
import PaginationRow from './PaginationRow';
import PaginationRowAmountOfPages from './PaginationRowAmountOfPages';
Expand All @@ -20,6 +21,7 @@ PaginationRow.PAGINATION_VIEW_TYPES = PAGINATION_VIEW_TYPES;

export {
paginate,
Pager,
Paginator,
PAGINATION_VIEW,
PAGINATION_VIEW_TYPES,
Expand Down