Skip to content

Commit

Permalink
[Data views]: Fix pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Oct 16, 2023
1 parent 5a04a2b commit d7372bf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
29 changes: 17 additions & 12 deletions packages/edit-site/src/components/dataviews/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function PageSizeControl( { view, onChangeView } ) {
label: pageSize,
} ) ) }
onChange={ ( value ) =>
onChangeView( { ...view, perPage: value } )
onChangeView( { ...view, perPage: value, page: 1 } )
}
/>
);
Expand All @@ -50,7 +50,6 @@ function Pagination( {
onChangeView,
paginationInfo: { totalItems = 0, totalPages },
} ) {
const currentPage = view.page + 1;
if ( ! totalItems || ! totalPages ) {
return null;
}
Expand All @@ -75,8 +74,8 @@ function Pagination( {
<HStack expanded={ false } spacing={ 1 }>
<Button
variant="tertiary"
onClick={ () => onChangeView( { ...view, page: 0 } ) }
disabled={ view.page === 0 }
onClick={ () => onChangeView( { ...view, page: 1 } ) }
disabled={ view.page === 1 }
aria-label={ __( 'First page' ) }
>
«
Expand All @@ -86,7 +85,7 @@ function Pagination( {
onClick={ () =>
onChangeView( { ...view, page: view.page - 1 } )
}
disabled={ view.page === 0 }
disabled={ view.page === 1 }
aria-label={ __( 'Previous page' ) }
>
Expand All @@ -100,7 +99,7 @@ function Pagination( {
sprintf(
// translators: %1$s: Current page number, %2$s: Total number of pages.
_x( '<CurrenPageControl /> of %2$s', 'paging' ),
currentPage,
view.page,
totalPages
),
{
Expand All @@ -110,14 +109,20 @@ function Pagination( {
min={ 1 }
max={ totalPages }
onChange={ ( value ) => {
if ( value > totalPages ) return;
if (
! value ||
value < 1 ||
value > totalPages
) {
return;
}
onChangeView( {
...view,
page: view.page - 1,
page: value,
} );
} }
step="1"
value={ currentPage }
value={ view.page }
isDragEnabled={ false }
spinControls="none"
/>
Expand All @@ -130,17 +135,17 @@ function Pagination( {
onClick={ () =>
onChangeView( { ...view, page: view.page + 1 } )
}
disabled={ view.page >= totalPages - 1 }
disabled={ view.page >= totalPages }
aria-label={ __( 'Next page' ) }
>
</Button>
<Button
variant="tertiary"
onClick={ () =>
onChangeView( { ...view, page: totalPages - 1 } )
onChangeView( { ...view, page: totalPages } )
}
disabled={ view.page >= totalPages - 1 }
disabled={ view.page >= totalPages }
aria-label={ __( 'Last page' ) }
>
»
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/dataviews/text-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function TextFilter( { view, onChangeView } ) {
onChangeView( ( currentView ) => ( {
...currentView,
search: debouncedSearch,
page: 1,
} ) );
}, [ debouncedSearch, onChangeView ] );
const searchLabel = __( 'Filter list' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function PageSizeMenu( { view, onChangeView } ) {
onSelect={ ( event ) => {
// We need to handle this on DropDown component probably..
event.preventDefault();
onChangeView( { ...view, perPage: size, page: 0 } );
onChangeView( { ...view, perPage: size, page: 1 } );
} }
// TODO: check about role and a11y.
role="menuitemcheckbox"
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function PagePages() {
const [ view, setView ] = useState( {
type: 'list',
search: '',
page: 0,
page: 1,
perPage: 5,
sort: {
field: 'date',
Expand All @@ -52,7 +52,7 @@ export default function PagePages() {
const queryArgs = useMemo(
() => ( {
per_page: view.perPage,
page: view.page + 1, // tanstack starts from zero.
page: view.page,
_embed: 'author',
order: view.sort?.direction,
orderby: view.sort?.field,
Expand Down

0 comments on commit d7372bf

Please sign in to comment.