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 1 commit
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
8 changes: 8 additions & 0 deletions packages/edit-site/src/components/dataviews/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* WordPress dependencies
*/
import { createContext, useContext } from '@wordpress/element';

const DataViewsContext = createContext( {} );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
export const useDataViewsContext = () => useContext( DataViewsContext );
export default DataViewsContext;
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 GlobalSearchInput from './global-search-input';
import DataViewsContext from './context';

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 (
<DataViewsContext.Provider value={ { ...dataView } }>
<div className="dataviews-wrapper">
<VStack spacing={ 4 }>
<HStack justify="space-between">
<GlobalSearchInput />
<ViewActions />
</HStack>
{ /* This component will be selected based on viewConfigs. Now we only have the list view. */ }
<ListView isLoading={ isLoading } />
<Pagination totalItems={ paginationInfo?.totalItems } />
</VStack>
</div>
</DataViewsContext.Provider>
);
}
10 changes: 10 additions & 0 deletions packages/edit-site/src/components/dataviews/global-search-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Internal dependencies
*/
import TextFilter from './text-filter';
import { useDataViewsContext } from './context';

export default function GlobalSearchInput( props ) {
const dataView = useDataViewsContext();
return <TextFilter { ...props } onChange={ dataView.setGlobalFilter } />;
}
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';
112 changes: 112 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,112 @@
/**
* 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';

/**
* Internal dependencies
*/
import { useDataViewsContext } from './context';

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( { className, isLoading = false }, ref ) {
const dataView = useDataViewsContext();
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