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

[Experimental]: First version of pages list in site editor #54966

Merged
merged 3 commits into from
Oct 4, 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
3 changes: 3 additions & 0 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-view', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutView = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-dataviews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalAdminViews = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' );
}
Expand Down
12 changes: 12 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ function gutenberg_initialize_experiments_settings() {
)
);

add_settings_field(
'gutenberg-dataviews',
__( 'New admin views', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Test the new views for different entities like pages.', 'gutenberg' ),
'id' => 'gutenberg-dataviews',
)
);

add_settings_field(
'gutenberg-color-randomizer',
__( 'Color randomizer ', 'gutenberg' ),
Expand Down
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@tanstack/react-table": "^8.10.3",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
Expand Down
60 changes: 60 additions & 0 deletions packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* External dependencies
*/
import {
getCoreRowModel,
getFilteredRowModel,
getSortedRowModel,
getPaginationRowModel,
useReactTable,
} from '@tanstack/react-table';

/**
* WordPress dependencies
*/
import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import ListView from './list-view';
import { Pagination } from './pagination';
import ViewActions from './view-actions';
import TextFilter from './text-filter';

export default function DataViews( {
data,
fields,
isLoading,
paginationInfo,
options,
} ) {
const dataView = useReactTable( {
data,
columns: fields,
...options,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
} );
return (
<div className="dataviews-wrapper">
<VStack spacing={ 4 }>
<HStack justify="space-between">
<TextFilter onChange={ dataView.setGlobalFilter } />
<ViewActions dataView={ dataView } />
</HStack>
{ /* This component will be selected based on viewConfigs. Now we only have the list view. */ }
<ListView dataView={ dataView } isLoading={ isLoading } />
<Pagination
dataView={ dataView }
totalItems={ paginationInfo?.totalItems }
/>
</VStack>
</div>
);
}
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/dataviews/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as DataViews } from './dataviews';
export { PAGE_SIZE_VALUES } from './view-actions';
106 changes: 106 additions & 0 deletions packages/edit-site/src/components/dataviews/list-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { flexRender } from '@tanstack/react-table';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { chevronDown, chevronUp } from '@wordpress/icons';
import { Button } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';

const sortIcons = { asc: chevronUp, desc: chevronDown };
function Header( { header } ) {
if ( header.isPlaceholder ) {
return null;
}
const text = flexRender(
header.column.columnDef.header,
header.getContext()
);
if ( ! header.column.getCanSort() ) {
return text;
}
const sortDirection = header.column.getIsSorted();
return (
<Button
onClick={ header.column.getToggleSortingHandler() }
icon={ sortIcons[ sortDirection ] }
iconPosition="right"
text={ text }
style={ { padding: 0 } }
/>
);
}

function ListView( { dataView, className, isLoading = false }, ref ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a bit of a question for design across some of this work, but is it possible for us to come up with a more unique name for the list view of table data, so that we don't confuse it with the ListView component in the sidebar of the post and site editors? Maybe prefixing with Table would be enough — TableListView?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we could rename, but this component will not be exposed and will only be used internally by the high level DataViews component.

Copy link
Contributor

Choose a reason for hiding this comment

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

this could be named "TableView" maybe to avoid confusion, we'll have things like "GridView", "KanbanView"... later

const { rows } = dataView.getRowModel();
const hasRows = !! rows?.length;
if ( isLoading ) {
// TODO:Add spinner or progress bar..
return <h3>{ __( 'Loading' ) }</h3>;
}
return (
<div className="dataviews-list-view-wrapper">
{ hasRows && (
<table
ref={ ref }
className={ classnames( 'dataviews-list-view', className ) }
>
<thead>
{ dataView.getHeaderGroups().map( ( headerGroup ) => (
<tr key={ headerGroup.id }>
{ headerGroup.headers.map( ( header ) => (
<th
key={ header.id }
colSpan={ header.colSpan }
style={ {
width:
header.column.columnDef.width ||
undefined,
maxWidth:
header.column.columnDef
.maxWidth || undefined,
} }
>
<Header header={ header } />
</th>
) ) }
</tr>
) ) }
</thead>
<tbody>
{ rows.map( ( row ) => (
<tr key={ row.id }>
{ row.getVisibleCells().map( ( cell ) => (
<td
key={ cell.id }
style={ {
width:
cell.column.columnDef.width ||
undefined,
maxWidth:
cell.column.columnDef
.maxWidth || undefined,
} }
>
{ flexRender(
cell.column.columnDef.cell,
cell.getContext()
) }
</td>
) ) }
</tr>
) ) }
</tbody>
</table>
) }
{ ! hasRows && <p>{ __( 'no results' ) }</p> }
</div>
);
}

export default forwardRef( ListView );
Loading
Loading