Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Pagination): Fix pagination to update page count #2639

Merged
merged 4 commits into from
Aug 6, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface NavigationProps extends React.HTMLProps<HTMLElement> {
paginationTitle?: string;
/** The number of the current page */
page: React.ReactText;
/** Function called when user sets page */
/** Function called when page is changed */
onSetPage: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void;
/** Function called when user clicks to navigate to next page */
onNextClick?: (event: React.SyntheticEvent<HTMLButtonElement>, page: number) => void;
Expand Down Expand Up @@ -93,6 +93,12 @@ export class Navigation extends React.Component<NavigationProps, NavigationState
}
}

componentDidUpdate(lastState: NavigationProps) {
if (this.props.page !== lastState.page && this.props.page <= this.props.lastPage && this.state.userInputPage !== this.props.page) {
this.setState({ userInputPage: this.props.page });
}
}

render () {
const {
page,
Expand Down Expand Up @@ -150,7 +156,7 @@ export class Navigation extends React.Component<NavigationProps, NavigationState
aria-label={currPage}
type="number"
disabled={page === firstPage && page === lastPage}
min={lastPage <= 0 && firstPage <=0 ? 0 : 1}
min={lastPage <= 0 && firstPage <= 0 ? 0 : 1}
max={lastPage}
value={userInputPage}
onKeyDown={event => this.onKeyDown(event, page, lastPage, onPageInput, onSetPage)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface PaginationProps extends React.HTMLProps<HTMLDivElement> {
perPage?: number;
/** Select from options to number of items per page. */
perPageOptions?: PerPageOptions[];
/** Page we start at. */
firstPage?: number;
/** Current page number. */
page?: number;
/** First index of items on current page. */
Expand Down Expand Up @@ -112,6 +114,7 @@ export const Pagination: React.FunctionComponent<PaginationProps> = ({
currPage: 'Current page',
paginationTitle: 'Pagination'
},
firstPage = 1,
page = 1,
itemCount,
itemsStart = null,
Expand All @@ -130,6 +133,12 @@ export const Pagination: React.FunctionComponent<PaginationProps> = ({
...props
}: PaginationProps) => {
const lastPage = Math.ceil(itemCount / perPage);
if (page < firstPage) {
page = firstPage;
} else if (page > lastPage) {
page = lastPage;
}

const firstIndex = itemCount <= 0 ? 0 : (page - 1) * perPage + 1;
let lastIndex;
if (itemCount <= 0) {
Expand Down