Skip to content
This repository was archived by the owner on Aug 26, 2021. It is now read-only.

WIP: pagination component #890

Open
wants to merge 35 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
43d4006
wip pagination work
Oct 2, 2019
6fd8f6d
changes
Oct 22, 2019
a844a71
adding pagination test data
Oct 22, 2019
4bd0e8e
add pagination
Oct 27, 2019
7926fc2
add link-list dependencies
Oct 27, 2019
cb54631
set aria label attribute
Oct 28, 2019
22d3b6b
add disabled option
Oct 28, 2019
7d2cbf0
add onChange, populate test array
Oct 28, 2019
8448b8a
changing pagination to be stateful
Oct 28, 2019
cb4f392
updating pagination constructor
Oct 28, 2019
2e194f1
add disabled states to pagination
Oct 30, 2019
b4dd1d9
adding disabled and onClick events to pagination
Nov 4, 2019
f08a8fe
add pagination styling
Nov 11, 2019
58c4f3a
add html example of pagination
Nov 11, 2019
8f311ca
remove unnecessary props
Nov 11, 2019
02076f5
remove zero
Nov 11, 2019
0983535
fix a11y errors
Nov 11, 2019
fe8985f
code cleanup
Nov 25, 2019
7df6cc6
formatting in pagination
Nov 25, 2019
5ba4ce9
adding comments
Nov 25, 2019
9c26952
adding more comments
Nov 25, 2019
80eebdb
fixing some spacing
Nov 25, 2019
b2e0ebe
tidy up css and comments
Nov 25, 2019
49550a2
adding comments and updating scss
Nov 25, 2019
6b5ea09
fix up code formatting
Dec 1, 2019
b364b99
possible pagination summary
Dec 2, 2019
680c131
update html tests markup for pagination
Dec 2, 2019
786ff95
add margin to pagination
Dec 2, 2019
6ea0b9b
fix failed tests in pagination
Dec 2, 2019
0620078
add alignment prop to pagination
Dec 2, 2019
a5b2758
updating css for pagination
Dec 11, 2019
e8541dd
remove unnecessary
Dec 12, 2019
de6b538
removed files fixed package json for pagination
Dec 12, 2019
7f82ee1
removing unneeded stuff
Dec 17, 2019
6424e5e
remove unneeded code
Dec 18, 2019
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
Prev Previous commit
Next Next commit
add disabled states to pagination
KiriHoy committed Oct 30, 2019

Unverified

No user is associated with the committer email.
commit 2e194f1b0f730ec38a5ad0f40196a0702cb66c47
131 changes: 117 additions & 14 deletions packages/pagination/src/js/react.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,26 @@
import React from 'react';
import PropTypes from 'prop-types';

const LEFT_PAGE = '...';
const RIGHT_PAGE = '...';
const NEXT = 'Next';
const PREVIOUS = 'Previous';

//Helper method for creating array of numbers for pagination
const createPaginationarray = (from, to) => {
let i = from;
const PaginationItems = [];

while (i <= to) {
PaginationItems.push(i);
i += 1;
}

return PaginationItems;
}



class AUPagination extends React.Component {

/**
@@ -19,32 +39,115 @@ class AUPagination extends React.Component {

constructor( props ) {
super( props );
const { totalRecords, recordsPerPage, pageNeighbours, totalPaginationItems, className = '', children, ...attributeOptions } = props;
const { totalResults, recordsPerPage, totalPaginationItems, className = ''} = props;

this.fetchPaginationItems = this. fetchPaginationItems.bind(this);

this.recordsPerPage = typeof recordsPerPage === 'number' ? recordsPerPage : 10;
this.totalRecords = typeof totalRecords === 'number' ? totalRecords : 0;

// pageNeighbours can be: 0, 1 or 2
this.pageNeighbours = typeof pageNeighbours === 'number'
? Math.max(0, Math.min(pageNeighbours, 2))
: 0;

this.totalPaginationItems = Math.ceil( this.totalRecords / this.pageLimit );

this.totalResults = typeof totalResults === 'number' ? totalResults : 0;

//calculate number of pagination items
this.totalPaginationItems = Math.ceil( this.totalResults / this.recordsPerPage );
this.state = { currentPage: 1 };
}

fetchPaginationItems() {

const totalPaginationItems = this.totalPaginationItems;
const currentPage = this.state.currentPage;

const startPage = Math.max(2, currentPage);
const endPage = Math.min(totalPaginationItems - 1, currentPage);

let pages = (startPage, endPage);

return createPaginationarray(1, totalPaginationItems) ;

}

render() {

if (!this.totalResults || this.totalPaginationItems === 1) return null;

const { currentPage } = this.state;
const items = this.fetchPaginationItems();

return (

<nav role="navigation" aria-label="Pagination Navigation" className={`au-pagination ` + `${className} `}>
<ul className={ ` au-link-list au-link-list--inline ${ className } ` }>
<AUPaginationItem className={` ${currentPage === 1 ? 'disabled' : ''} `}>Next</AUPaginationItem>
{ items.map(( item, i ) => {

return (
<AUPaginationItem key={ i }>{ item }</AUPaginationItem>
);

}) }
<AUPaginationItem className={` ${currentPage === totalPaginationItems ? 'disabled' : ''} `}>Previous</AUPaginationItem>

</ul>
</nav>

);

}


}

AUPagination.propTypes = {
totalRecords: PropTypes.number.isRequired,
pageLimit: PropTypes.number,
pageNeighbours: PropTypes.number,
onPageChanged: PropTypes.func
totalResults: PropTypes.number.isRequired,
recordsPerPage: PropTypes.number,
onChanged: PropTypes.func
};

AUPagination.defaultProps = {
recordsPerPage: 10,
totalResults: 0,
className: ''

};

/**
* The pagination item component
* @param { string } className - An additional class, optional
* @param { object } attributeOptions - Default HTML attributes
* @param { string } label - aria-label for pagination items
* @param { bool } disabled - aria-label for pagination items
* @param {object} onClick - The onChange event handler
*/
export const AUPaginationItem = ( { children, className, label, onClick, ...attributeOptions } ) => {

// set aria label attribute
if ( children === 'Previous' ) {
label = "go to previous page";

}
else if (children === 'Next') {

label = "go to next page";

}
else {
label = "Page " + children;
}


return <li className={`au-pagination__item ${ className }`}>
<a href="#0" className={`au-pagination__link ${ className }`} aria-label={label}>
{children}
</a>
</li>
};

AUPaginationItem.propTypes = {
children: PropTypes.node,
className: PropTypes.string
}

AUPaginationItem.defaultProps = {
className: ''
};

export default AUPagination;
7 changes: 1 addition & 6 deletions packages/pagination/tests/react/index.js
Original file line number Diff line number Diff line change
@@ -3,16 +3,11 @@ import ReactDOM from 'react-dom';

import AUPagination from './pagination.js';

const paginationNumbers = [];
for (var i = 1; i <= 10; i++) {
paginationNumbers.push(i);
}


ReactDOM.render(
<div>
<AUPagination items={paginationNumbers}>
</AUPagination>
<AUPagination totalResults={500} recordsPerPage={10} />
</div>,

document.getElementById('root'),