-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect: Add FreeList and PaidList pagination (#39058)
* Add threats list pagination * changelog * Add items per page constant, and handling * Fix mobile view * Simplify and consolidate * Fix versions * Use custom media query to improve responsiveness * Revert versions updates * Update and improve implementation * Compact * Set iconSize on parent component level * Adjustments to pagination design and accessibility (#39249) --------- Co-authored-by: Nate Weller <[email protected]>
- Loading branch information
1 parent
90fc4d9
commit b0faef5
Showing
5 changed files
with
265 additions
and
81 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/protect/changelog/add-protect-threats-list-pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Adds threats list pagination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
projects/plugins/protect/src/js/components/threats-list/pagination.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { Button, useBreakpointMatch } from '@automattic/jetpack-components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { chevronLeft, chevronRight } from '@wordpress/icons'; | ||
import React, { useCallback, useState, useMemo } from 'react'; | ||
import styles from './styles.module.scss'; | ||
|
||
const PaginationButton = ( { pageNumber, currentPage, onPageChange } ) => { | ||
const isCurrentPage = useMemo( () => currentPage === pageNumber, [ currentPage, pageNumber ] ); | ||
|
||
const handleClick = useCallback( () => { | ||
onPageChange( pageNumber ); | ||
}, [ onPageChange, pageNumber ] ); | ||
|
||
return ( | ||
<Button | ||
size={ 'medium' } | ||
className={ ! isCurrentPage ? styles.unfocused : null } | ||
onClick={ handleClick } | ||
aria-current={ isCurrentPage ? 'page' : undefined } | ||
aria-label={ sprintf( | ||
/* translators: placeholder is a page number, i.e. "Page 123" */ | ||
__( 'Page %d', 'jetpack-protect' ), | ||
pageNumber | ||
) } | ||
> | ||
{ pageNumber } | ||
</Button> | ||
); | ||
}; | ||
|
||
const Pagination = ( { list, itemPerPage = 10, children } ) => { | ||
const [ isSm ] = useBreakpointMatch( 'sm' ); | ||
|
||
const [ currentPage, setCurrentPage ] = useState( 1 ); | ||
|
||
const handlePreviousPageClick = useCallback( | ||
() => setCurrentPage( currentPage - 1 ), | ||
[ currentPage, setCurrentPage ] | ||
); | ||
const handleNextPageClick = useCallback( | ||
() => setCurrentPage( currentPage + 1 ), | ||
[ currentPage, setCurrentPage ] | ||
); | ||
|
||
const totalPages = useMemo( () => Math.ceil( list.length / itemPerPage ), [ list, itemPerPage ] ); | ||
|
||
const currentItems = useMemo( () => { | ||
const indexOfLastItem = currentPage * itemPerPage; | ||
const indexOfFirstItem = indexOfLastItem - itemPerPage; | ||
return list.slice( indexOfFirstItem, indexOfLastItem ); | ||
}, [ currentPage, list, itemPerPage ] ); | ||
|
||
const pageNumbers = useMemo( () => { | ||
if ( isSm ) { | ||
return [ currentPage ]; | ||
} | ||
|
||
const result = [ 1 ]; | ||
if ( currentPage > 3 && totalPages > 4 ) { | ||
result.push( '…' ); | ||
} | ||
|
||
if ( currentPage === 1 ) { | ||
// Current page is the first page. | ||
// i.e. [ 1 ] 2 3 4 ... 10 | ||
result.push( currentPage + 1, currentPage + 2, currentPage + 3 ); | ||
} else if ( currentPage === 2 ) { | ||
// Current page is the second to first page. | ||
// i.e. 1 [ 2 ] 3 4 ... 10 | ||
result.push( currentPage, currentPage + 1, currentPage + 2 ); | ||
} else if ( currentPage < totalPages - 1 ) { | ||
// Current page is positioned in the middle of the pagination. | ||
// i.e. 1 ... 3 [ 4 ] 5 ... 10 | ||
result.push( currentPage - 1, currentPage, currentPage + 1 ); | ||
} else if ( currentPage === totalPages - 1 ) { | ||
// Current page is the second to last page. | ||
// i.e. 1 ... 7 8 [ 9 ] 10 | ||
currentPage > 3 && result.push( currentPage - 2 ); | ||
currentPage > 2 && result.push( currentPage - 1 ); | ||
result.push( currentPage ); | ||
} else if ( currentPage === totalPages ) { | ||
// Current page is the last page. | ||
// i.e. 1 ... 7 8 9 [ 10 ] | ||
currentPage >= 5 && result.push( currentPage - 3 ); | ||
currentPage >= 4 && result.push( currentPage - 2 ); | ||
result.push( currentPage - 1 ); | ||
} | ||
|
||
if ( result[ result.length - 1 ] < totalPages - 1 ) { | ||
result.push( '…' ); | ||
result.push( totalPages ); | ||
} else if ( result[ result.length - 1 ] < totalPages ) { | ||
result.push( totalPages ); | ||
} | ||
|
||
return result.filter( pageNumber => pageNumber <= totalPages || isNaN( pageNumber ) ); | ||
}, [ currentPage, isSm, totalPages ] ); | ||
|
||
return ( | ||
<> | ||
{ children( { currentItems } ) } | ||
{ totalPages > 1 && ( | ||
<nav | ||
role="navigation" | ||
aria-label={ __( 'Threat list pages', 'jetpack-protect' ) } | ||
className={ styles[ 'pagination-container' ] } | ||
> | ||
<Button | ||
onClick={ handlePreviousPageClick } | ||
disabled={ currentPage === 1 } | ||
variant={ 'link' } | ||
icon={ chevronLeft } | ||
iconSize={ 24 } | ||
aria-label={ __( 'Previous page', 'jetpack-protect' ) } | ||
/> | ||
{ pageNumbers.map( ( pageNumber, index ) => | ||
typeof pageNumber === 'number' ? ( | ||
<PaginationButton | ||
key={ pageNumber } | ||
pageNumber={ pageNumber } | ||
currentPage={ currentPage } | ||
onPageChange={ setCurrentPage } | ||
/> | ||
) : ( | ||
<span key={ `ellipses_${ index }` }>{ pageNumber }</span> | ||
) | ||
) } | ||
<Button | ||
onClick={ handleNextPageClick } | ||
disabled={ currentPage === totalPages } | ||
variant={ 'link' } | ||
icon={ chevronRight } | ||
iconSize={ 24 } | ||
aria-label={ __( 'Next page', 'jetpack-protect' ) } | ||
/> | ||
</nav> | ||
) } | ||
</> | ||
); | ||
}; | ||
|
||
export default Pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters