-
Notifications
You must be signed in to change notification settings - Fork 800
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
Protect: Add FreeList and PaidList pagination #39058
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aafab14
Add threats list pagination
dkmyta b532fc1
changelog
dkmyta 53bec44
Add items per page constant, and handling
dkmyta 7d05854
Fix mobile view
dkmyta 30ff337
Simplify and consolidate
dkmyta 36572b5
Fix versions
dkmyta 747775e
Use custom media query to improve responsiveness
dkmyta 31bbab3
Merge branch 'trunk' into add/protect-threats-list-pagination
dkmyta 7fb67ed
Revert versions updates
dkmyta 0091879
Update and improve implementation
dkmyta 907e538
Compact
dkmyta 34e9ee8
Set iconSize on parent component level
dkmyta 34ea4ec
Adjustments to pagination design and accessibility (#39249)
nateweller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
159 changes: 159 additions & 0 deletions
159
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,159 @@ | ||
import { Button } from '@automattic/jetpack-components'; | ||
import React, { useCallback, useEffect, useState, useMemo, memo } from 'react'; | ||
import styles from './styles.module.scss'; | ||
|
||
const PaginationButton = memo( ( { pageNumber, currentPage, onPageChange } ) => { | ||
const handleClick = useCallback( () => { | ||
onPageChange( pageNumber ); | ||
}, [ onPageChange, pageNumber ] ); | ||
|
||
return ( | ||
<Button | ||
onClick={ handleClick } | ||
variant={ currentPage === pageNumber ? 'primary' : 'secondary' } | ||
aria-current={ currentPage === pageNumber ? 'page' : undefined } | ||
> | ||
{ pageNumber } | ||
</Button> | ||
); | ||
} ); | ||
|
||
const Pagination = ( { list, itemPerPage = 10, children } ) => { | ||
const [ currentPage, setCurrentPage ] = useState( 1 ); | ||
|
||
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 onPageChange = useCallback( pageNumber => { | ||
setCurrentPage( pageNumber ); | ||
}, [] ); | ||
|
||
const [ isSmall, setIsSmall ] = useState( window.matchMedia( '(max-width: 1220px)' ).matches ); | ||
|
||
useEffect( () => { | ||
const mediaQuery = window.matchMedia( '(max-width: 1220px)' ); | ||
|
||
const handleMediaChange = event => { | ||
setIsSmall( event.matches ); | ||
}; | ||
|
||
// Add event listeners | ||
mediaQuery.addEventListener( 'change', handleMediaChange ); | ||
|
||
// Cleanup listeners on component unmount | ||
return () => { | ||
mediaQuery.removeEventListener( 'change', handleMediaChange ); | ||
}; | ||
}, [] ); | ||
|
||
const handleFirstPageClick = useCallback( () => { | ||
onPageChange( 1 ); | ||
}, [ onPageChange ] ); | ||
|
||
const handlePreviousPageClick = useCallback( () => { | ||
onPageChange( currentPage - 1 ); | ||
}, [ currentPage, onPageChange ] ); | ||
|
||
const handleNextPageClick = useCallback( () => { | ||
onPageChange( currentPage + 1 ); | ||
}, [ currentPage, onPageChange ] ); | ||
|
||
const handleLastPageClick = useCallback( () => { | ||
onPageChange( totalPages ); | ||
}, [ onPageChange, totalPages ] ); | ||
|
||
const getPageNumbers = useCallback( () => { | ||
const pageNumbers = []; | ||
|
||
if ( ! isSmall ) { | ||
pageNumbers.push( 1 ); | ||
} | ||
|
||
const start = isSmall ? 1 : 2; | ||
const offset = isSmall ? 0 : 2; | ||
const end = isSmall ? 0 : 1; | ||
const startPage = Math.max( start, currentPage - offset ); | ||
const endPage = Math.min( totalPages - end, currentPage + offset ); | ||
|
||
if ( startPage > 2 && ! isSmall ) { | ||
pageNumbers.push( '...' ); | ||
} | ||
|
||
for ( let i = startPage; i <= endPage; i++ ) { | ||
pageNumbers.push( i ); | ||
} | ||
|
||
if ( endPage < totalPages - 1 && ! isSmall ) { | ||
pageNumbers.push( '...' ); | ||
} | ||
|
||
if ( totalPages > 1 && ! isSmall ) { | ||
pageNumbers.push( totalPages ); | ||
} | ||
|
||
return pageNumbers; | ||
}, [ currentPage, totalPages, isSmall ] ); | ||
|
||
return ( | ||
<> | ||
{ children( { currentItems } ) } | ||
{ totalPages > 1 && ( | ||
<div className={ styles[ 'pagination-container' ] }> | ||
{ isSmall && ( | ||
<Button | ||
onClick={ handleFirstPageClick } | ||
disabled={ currentPage === 1 } | ||
variant={ 'secondary' } | ||
> | ||
{ 1 } | ||
</Button> | ||
) } | ||
<Button | ||
onClick={ handlePreviousPageClick } | ||
disabled={ currentPage === 1 } | ||
variant={ 'secondary' } | ||
> | ||
{ '<' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: could use an icon here from |
||
</Button> | ||
{ getPageNumbers().map( ( pageNumber, index ) => | ||
typeof pageNumber === 'number' ? ( | ||
<PaginationButton | ||
key={ index } | ||
pageNumber={ pageNumber } | ||
currentPage={ currentPage } | ||
onPageChange={ onPageChange } | ||
/> | ||
) : ( | ||
<span key={ index } className={ styles.ellipsis }> | ||
{ pageNumber } | ||
</span> | ||
) | ||
) } | ||
<Button | ||
onClick={ handleNextPageClick } | ||
disabled={ currentPage === totalPages } | ||
variant={ 'secondary' } | ||
> | ||
{ '>' } | ||
</Button> | ||
{ isSmall && ( | ||
<Button | ||
onClick={ handleLastPageClick } | ||
disabled={ currentPage === totalPages } | ||
variant={ 'secondary' } | ||
> | ||
{ totalPages } | ||
</Button> | ||
) } | ||
</div> | ||
) } | ||
</> | ||
); | ||
}; | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
useBreakpointMatch
be useful here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started with
useBreakpointMatch
but found that we seem to have a unique requirement with how our UI shifts in various viewports that was not exactly covered by the options it provided (isSm
,isMd
,isLg
).