Skip to content
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 @@ -10,14 +10,29 @@ import { createInterpolateElement, memo } from '@wordpress/element';
import { sprintf, __, _x } from '@wordpress/i18n';
import { chevronRight, chevronLeft } from '@wordpress/icons';

/**
* Internal dependencies
*/
import type { View } from './types';

interface PaginationProps {
view: View;
onChangeView: ( view: View ) => void;
paginationInfo: {
totalItems: number;
totalPages: number;
};
}

const Pagination = memo( function Pagination( {
view,
onChangeView,
paginationInfo: { totalItems = 0, totalPages },
} ) {
}: PaginationProps ) {
if ( ! totalItems || ! totalPages ) {
return null;
}
const currentPage = view.page ?? 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this might fix some subtle bugs where the "page" was undefined in views so next/previous buttons might have had bad behavior.

return (
!! totalItems &&
totalPages !== 1 && (
Expand All @@ -43,12 +58,15 @@ const Pagination = memo( function Pagination( {
CurrentPageControl: (
<SelectControl
aria-label={ __( 'Current page' ) }
value={ view.page }
value={ view.page?.toString() }
options={ Array.from(
Array( totalPages )
).map( ( _, i ) => {
const page = i + 1;
return { value: page, label: page };
return {
value: page.toString(),
label: page.toString(),
};
} ) }
onChange={ ( newValue ) => {
onChangeView( {
Expand All @@ -66,9 +84,12 @@ const Pagination = memo( function Pagination( {
<HStack expanded={ false } spacing={ 1 }>
<Button
onClick={ () =>
onChangeView( { ...view, page: view.page - 1 } )
onChangeView( {
...view,
page: currentPage - 1,
} )
}
disabled={ view.page === 1 }
disabled={ currentPage === 1 }
__experimentalIsFocusable
label={ __( 'Previous page' ) }
icon={ chevronLeft }
Expand All @@ -78,9 +99,9 @@ const Pagination = memo( function Pagination( {
/>
<Button
onClick={ () =>
onChangeView( { ...view, page: view.page + 1 } )
onChangeView( { ...view, page: currentPage + 1 } )
}
disabled={ view.page >= totalPages }
disabled={ currentPage >= totalPages }
__experimentalIsFocusable
label={ __( 'Next page' ) }
icon={ chevronRight }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
import { __, sprintf } from '@wordpress/i18n';
import { CheckboxControl } from '@wordpress/components';

/**
* Internal dependencies
*/
import type { Data, Field, Item } from './types';

interface SingleSelectionCheckboxProps {
selection: string[];
onSelectionChange: ( selection: Item[] ) => void;
item: Item;
data: Data;
getItemId: ( item: Item ) => string;
primaryField?: Field;
disabled: boolean;
}

export default function SingleSelectionCheckbox( {
selection,
onSelectionChange,
Expand All @@ -12,7 +27,7 @@ export default function SingleSelectionCheckbox( {
getItemId,
primaryField,
disabled,
} ) {
}: SingleSelectionCheckboxProps ) {
const id = getItemId( item );
const isSelected = selection.includes( id );
let selectionLabel;
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/view-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface ListViewProps {
id: string;
isLoading: boolean;
onSelectionChange: ( selection: Item[] ) => void;
selection: Item[];
selection: string[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the selection objects holds just the ids of the items but it's weird that the onSelectionChange receives the whole objects. It's not consistent but it's the way it is for now.

view: ViewListType;
}

Expand Down