Skip to content

Commit

Permalink
Merge pull request #189 from IBM/feat-tilecatalog-pages
Browse files Browse the repository at this point in the history
feat(tilecatalog): add page of pages label option
  • Loading branch information
davidicus authored Apr 22, 2019
2 parents abac2db + 70e004a commit c0b879b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 242 deletions.
17 changes: 14 additions & 3 deletions src/components/SimplePagination/SimplePagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const propTypes = {
page: PropTypes.number.isRequired,
/** The maximum page number that can be navigated to */
maxPage: PropTypes.number.isRequired,
/** Gets called back with arguments (page, maxPage) */
pageOfPagesText: PropTypes.func,
/** Internationalized label for the word 'Page' */
pageText: PropTypes.string,
/** Internationalized label for the word 'Next page' */
Expand All @@ -66,13 +68,22 @@ const propTypes = {
};

const defaultProps = {
pageText: 'Page',
pageOfPagesText: (page, maxPage) => `Page ${page} of ${maxPage}`,
pageText: null,
nextPageText: 'Next page',
prevPageText: 'Prev page',
};

/** This is a lighter weight pagination component than the default Carbon one */
const SimplePagination = ({ pageText, prevPageText, nextPageText, page, maxPage, onPage }) => {
const SimplePagination = ({
pageText,
prevPageText,
nextPageText,
pageOfPagesText,
page,
maxPage,
onPage,
}) => {
const hasPrev = page > 1;
const hasNext = page <= maxPage - 1;

Expand All @@ -82,7 +93,7 @@ const SimplePagination = ({ pageText, prevPageText, nextPageText, page, maxPage,
return (
<StyledContainer>
<StyledPageLabel>
{pageText} {page}
{pageText ? `${pageText} ${page}` : pageOfPagesText(page, maxPage)}
</StyledPageLabel>
<StyledButton
role="button"
Expand Down
6 changes: 3 additions & 3 deletions src/components/TileCatalog/StatefulTileCatalog.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('StatefulTileCatalog', () => {
.find('span')
.at(1)
.text()
).toEqual('Page 1');
).toContain('Page 1');
const nextButton = wrapper.find('div[tabIndex=0]');
nextButton.simulate('click');
// on Page 2
Expand All @@ -80,7 +80,7 @@ describe('StatefulTileCatalog', () => {
.find('span')
.at(1)
.text()
).toEqual('Page 2');
).toContain('Page 2');

const newTiles = commonTileProps.tiles.slice(1, 5);
// Back to Page 1
Expand All @@ -91,7 +91,7 @@ describe('StatefulTileCatalog', () => {
.find('span')
.at(1)
.text()
).toEqual('Page 1');
).toContain('Page 1');

// The new first tile should be selected
expect(
Expand Down
4 changes: 3 additions & 1 deletion src/components/TileCatalog/TileCatalog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export const propTypes = {
pagination: PropTypes.shape({
pageSize: PropTypes.number,
pageText: PropTypes.string,
/** Gets called back with arguments (page, maxPage) */
pageOfPagesText: PropTypes.func,
nextPageText: PropTypes.string,
prevPageText: PropTypes.string,
onPage: PropTypes.func,
Expand Down Expand Up @@ -168,7 +170,7 @@ const TileCatalog = ({
)}
</StyledEmptyTile>
)}
{pagination ? (
{!isLoading && tiles.length > 0 && !error && pagination ? (
<SimplePagination {...pagination} maxPage={Math.ceil(totalTiles / pageSize)} />
) : null}
</StyledContainerDiv>
Expand Down
Loading

0 comments on commit c0b879b

Please sign in to comment.