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

Dataviews: Ensure items and fields are using a unique id #56366

Merged
merged 1 commit into from
Nov 21, 2023
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
1 change: 1 addition & 0 deletions packages/edit-site/src/components/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This file documents the DataViews UI component, which provides an API to render
```js
<DataViews
data={ pages }
getItemId={ ( item ) => item.id }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm hesitant to make this a separate prop or a special field but this works for me. We can see over time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, should we document the prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's do the documentation in a follow up and add more info on the other props too.

isLoading={ isLoadingPages }
view={ view }
onChangeView={ onChangeView }
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function DataViews( {
searchLabel = undefined,
actions,
data,
getItemId,
isLoading = false,
paginationInfo,
supportedLayouts,
Expand Down Expand Up @@ -86,6 +87,7 @@ export default function DataViews( {
paginationInfo={ paginationInfo }
actions={ actions }
data={ data }
getItemId={ getItemId }
isLoading={ isLoading }
/>
<Pagination
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/dataviews/view-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
*/
import ItemActions from './item-actions';

export function ViewGrid( { data, fields, view, actions } ) {
export function ViewGrid( { data, fields, view, actions, getItemId } ) {
const mediaField = fields.find(
( field ) => field.id === view.layout.mediaField
);
Expand All @@ -27,7 +27,7 @@ export function ViewGrid( { data, fields, view, actions } ) {
<Grid gap={ 8 } columns={ 2 } alignment="top">
{ data.map( ( item, index ) => {
return (
<VStack key={ index }>
<VStack key={ getItemId?.( item ) || index }>
<div className="dataviews-view-grid__media">
{ mediaField?.render( { item, view } ) || (
<Placeholder
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-site/src/components/dataviews/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function ViewList( {
fields,
actions,
data,
getItemId,
isLoading = false,
paginationInfo,
} ) {
Expand Down Expand Up @@ -370,6 +371,7 @@ function ViewList( {
},
columnVisibility: columnVisibility ?? EMPTY_OBJECT,
},
getRowId: getItemId,
onSortingChange: ( sortingUpdater ) => {
onChangeView( ( currentView ) => {
const sort =
Expand Down Expand Up @@ -488,7 +490,7 @@ function ViewList( {
<tr key={ row.id }>
{ row.getVisibleCells().map( ( cell ) => (
<td
key={ cell.id }
key={ cell.column.id }
style={ {
width:
cell.column.columnDef.width ||
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ export default function PagePages() {
fields={ fields }
actions={ actions }
data={ pages || EMPTY_ARRAY }
getItemId={ ( item ) => item.id }
isLoading={ isLoadingPages || isLoadingAuthors }
view={ view }
onChangeView={ onChangeView }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export default function DataviewsTemplates() {
fields={ fields }
actions={ actions }
data={ shownTemplates }
getItemId={ ( item ) => item.id }
isLoading={ isLoadingData }
view={ view }
onChangeView={ onChangeView }
Expand Down
Loading