Skip to content

Commit

Permalink
feat: Add onPerPageChanged prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Tupalov authored and Dmitriy Tupalov committed Feb 18, 2019
1 parent 50eebc5 commit a27a956
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ For more details see __[Storybook](https://dtupalov.github.io/react-material-ui-
| page | number | 0 | Current page. Start with 0 |
| onPageChanged | Function | | Function trigger when `page` was changed. Signature ```(page: number) => any``` |
| perPage | number | 5 | Quantity of displaying items per page. |
| onPerPageChanged | Function | | Function trigger when `perPage` was changed. Signature ```(perPage: number) => any``` |
| perPageOption | number[] | [5, 10, 15] | Per page option. Displayed on the paging panel |
| selectedRows | number[] | [] | Indexes of selected rows |
| selectable | boolean | true | Enable selections |
Expand Down
6 changes: 5 additions & 1 deletion src/withReactMUIDatatableModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ export default compose(

props.onPageChanged && props.onPageChanged(page);
},
changePerPage: props => count => props.setPerPage(count),
changePerPage: props => count => {
props.setPerPage(count);

props.onPerPageChanged && props.onPerPageChanged(count);
},
handleSelect: props => selectedRows => props.setSelectedRows(selectedRows),
handleDelete: props => selectedRows => {
const nextData = props.data.filter(
Expand Down
8 changes: 8 additions & 0 deletions stories/ReactMUIDatatable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ storiesOf('ReactMUIDatatable/Props', module)
perPage={15}
/>
))
.add('onPerPageChanged', () => (
<ReactMUIDatatable
columns={columns}
data={data}
title={title}
onPerPageChanged={action('onPerPageChanged called')}
/>
))
.add('perPageOption', () => (
<ReactMUIDatatable
columns={columns}
Expand Down
9 changes: 9 additions & 0 deletions test/withReactMUIDatatableModel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let onSearchValueChanged;
let onSortChanged;
let onFilterValuesChanged;
let onPageChanged;
let onPerPageChanged;

class MockedComponent extends React.Component {
render() {
Expand All @@ -27,6 +28,7 @@ describe('withReactMUIDatatableModel', () => {
onSortChanged = jest.fn(({ columnName, direction }) => {});
onFilterValuesChanged = jest.fn(filterValues => {});
onPageChanged = jest.fn(page => {});
onPerPageChanged = jest.fn(perPage => {});

data = [
{
Expand Down Expand Up @@ -93,6 +95,7 @@ describe('withReactMUIDatatableModel', () => {
onSortChanged={onSortChanged}
onFilterValuesChanged={onFilterValuesChanged}
onPageChanged={onPageChanged}
onPerPageChanged={onPerPageChanged}
/>
)
.root.findByType(MockedComponent);
Expand Down Expand Up @@ -408,6 +411,12 @@ describe('withReactMUIDatatableModel', () => {
);
});

it('should call onPerPageChanged if perPage was changed', () => {
RenderedMockedComponent.props.changePerPage(10);

expect(onPerPageChanged).toBeCalledWith(10);
});

it('should paginate', () => {
const expectedDisplayDataOnSecondPage = [
{
Expand Down

0 comments on commit a27a956

Please sign in to comment.