Skip to content

Commit

Permalink
[DataViews]: Update the view config to include fields visibility (#55247
Browse files Browse the repository at this point in the history
)

* [DataViews]: Update the view config to include fields visibility

* fix typo

* use array instead of Set for `hidden`

* use `hiddenFields` top level prop
  • Loading branch information
ntsekouras authored Oct 11, 2023
1 parent 3c75843 commit 0c216e4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
39 changes: 38 additions & 1 deletion packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import ViewActions from './view-actions';
import TextFilter from './text-filter';
import { moreVertical } from '@wordpress/icons';

const EMPTY_OBJECT = {};

export default function DataViews( {
actions,
data,
Expand All @@ -44,7 +46,7 @@ export default function DataViews( {
} ) {
const columns = useMemo( () => {
const _columns = [ ...fields ];
if ( actions && actions.length ) {
if ( actions?.length ) {
_columns.push( {
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
id: 'actions',
Expand Down Expand Up @@ -83,6 +85,19 @@ export default function DataViews( {
return _columns;
}, [ fields, actions ] );

const columnVisibility = useMemo( () => {
if ( ! view.hiddenFields?.length ) {
return;
}
return view.hiddenFields.reduce(
( accumulator, fieldId ) => ( {
...accumulator,
[ fieldId ]: false,
} ),
{}
);
}, [ view.hiddenFields ] );

const dataView = useReactTable( {
data,
columns,
Expand All @@ -104,6 +119,7 @@ export default function DataViews( {
pageIndex: view.page,
pageSize: view.perPage,
},
columnVisibility: columnVisibility ?? EMPTY_OBJECT,
},
onSortingChange: ( sortingUpdater ) => {
onChangeView( ( currentView ) => {
Expand Down Expand Up @@ -135,6 +151,27 @@ export default function DataViews( {
};
} );
},
onColumnVisibilityChange: ( columnVisibilityUpdater ) => {
onChangeView( ( currentView ) => {
const hiddenFields = Object.entries(
columnVisibilityUpdater()
).reduce(
( accumulator, [ fieldId, value ] ) => {
if ( value ) {
return accumulator.filter(
( id ) => id !== fieldId
);
}
return [ ...accumulator, fieldId ];
},
[ ...( currentView.hiddenFields || [] ) ]
);
return {
...currentView,
hiddenFields,
};
} );
},
onGlobalFilterChange: ( value ) => {
onChangeView( { ...view, search: value, page: 0 } );
},
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/dataviews/view-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ function PageSizeMenu( { dataView } ) {
}

function FieldsVisibilityMenu( { dataView } ) {
const hideableFields = dataView
const hidableFields = dataView
.getAllColumns()
.filter( ( columnn ) => columnn.getCanHide() );
if ( ! hideableFields?.length ) {
if ( ! hidableFields?.length ) {
return null;
}
return (
Expand All @@ -118,7 +118,7 @@ function FieldsVisibilityMenu( { dataView } ) {
</DropdownSubMenuTriggerV2>
}
>
{ hideableFields?.map( ( field ) => {
{ hidableFields?.map( ( field ) => {
return (
<DropdownMenuItemV2
key={ field.id }
Expand Down
16 changes: 16 additions & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useMemo } from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';

/**
* Internal dependencies
Expand All @@ -31,6 +32,9 @@ export default function PagePages() {
field: 'date',
direction: 'desc',
},
// All fields are visible by default, so it's
// better to keep track of the hidden ones.
hiddenFields: [ 'date' ],
} );
// Request post statuses to get the proper labels.
const { records: statuses } = useEntityRecords( 'root', 'status' );
Expand Down Expand Up @@ -117,6 +121,18 @@ export default function PagePages() {
postStatuses[ page.status ] ?? page.status,
enableSorting: false,
},
{
header: 'Date',
id: 'date',
cell: ( props ) => {
const formattedDate = dateI18n(
getSettings().formats.datetimeAbbreviated,
getDate( props.row.original.date )
);
return <time>{ formattedDate }</time>;
},
enableSorting: false,
},
],
[ postStatuses ]
);
Expand Down

1 comment on commit 0c216e4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 0c216e4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6482028215
📝 Reported issues:

Please sign in to comment.