);
},
- elements: authors,
+ elements:
+ ( authors &&
+ authors.map( ( { id, name } ) => ( {
+ value: id,
+ label: name,
+ } ) ) ) ||
+ [],
},
{
header: __( 'Status' ),
id: 'status',
accessorFn: ( page ) =>
postStatuses[ page.status ] ?? page.status,
+ elements:
+ ( postStatuses &&
+ Object.entries( postStatuses )
+ .filter( ( [ slug ] ) =>
+ [ 'publish', 'draft' ].includes( slug )
+ )
+ .map( ( [ slug, name ] ) => ( {
+ value: slug,
+ label: name,
+ } ) ) ) ||
+ [],
enableSorting: false,
},
{
From ba828103f03e1a50b6802f7c60b31751a242b085 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 13:09:39 +0200
Subject: [PATCH 33/41] Make filters take the data from the fields
---
.../src/components/dataviews/dataviews.js | 13 ++--------
.../src/components/dataviews/in-filter.js | 4 ++-
.../src/components/page-pages/index.js | 25 +++++++++++--------
3 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/packages/edit-site/src/components/dataviews/dataviews.js b/packages/edit-site/src/components/dataviews/dataviews.js
index 055434cbd37336..c28c1a6fd08e0f 100644
--- a/packages/edit-site/src/components/dataviews/dataviews.js
+++ b/packages/edit-site/src/components/dataviews/dataviews.js
@@ -5,7 +5,6 @@ import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
-import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
@@ -27,14 +26,6 @@ export default function DataViews( {
paginationInfo,
} ) {
const ViewComponent = view.type === 'list' ? ViewList : ViewGrid;
- const authors = [
- { label: __( 'All authors' ), value: '' },
- ...( fields.find( ( f ) => f.id === 'author' ).elements || [] ),
- ];
- const statuses = [
- { label: __( 'All statuses' ), value: [ 'publish', 'draft' ] },
- ...( fields.find( ( f ) => f.id === 'status' ).elements || [] ),
- ];
return (
@@ -47,13 +38,13 @@ export default function DataViews( {
/>
diff --git a/packages/edit-site/src/components/dataviews/in-filter.js b/packages/edit-site/src/components/dataviews/in-filter.js
index f53690083d44a7..d94dad4feb182e 100644
--- a/packages/edit-site/src/components/dataviews/in-filter.js
+++ b/packages/edit-site/src/components/dataviews/in-filter.js
@@ -3,7 +3,9 @@
*/
import { SelectControl } from '@wordpress/components';
-export default ( { id, options, view, onChangeView } ) => {
+export default ( { id, fields, view, onChangeView } ) => {
+ const options = fields.find( ( f ) => f.id === id )?.elements || [];
+
return (
);
},
- elements:
- ( authors &&
- authors.map( ( { id, name } ) => ( {
- value: id,
- label: name,
- } ) ) ) ||
- [],
+ elements: [
+ {
+ value: '',
+ label: __( 'All authors' ),
+ },
+ ...( authors?.map( ( { id, name } ) => ( {
+ value: id,
+ label: name,
+ } ) ) || [] ),
+ ],
},
{
header: __( 'Status' ),
id: 'status',
accessorFn: ( page ) =>
postStatuses[ page.status ] ?? page.status,
- elements:
- ( postStatuses &&
+ elements: [
+ { label: __( 'All statuses' ), value: 'publish,draft' },
+ ...( ( postStatuses &&
Object.entries( postStatuses )
.filter( ( [ slug ] ) =>
[ 'publish', 'draft' ].includes( slug )
@@ -171,7 +175,8 @@ export default function PagePages() {
value: slug,
label: name,
} ) ) ) ||
- [],
+ [] ),
+ ],
enableSorting: false,
},
{
From 27bcf7800c34c816f6f7c083763c9880a7b5a040 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 13:18:09 +0200
Subject: [PATCH 34/41] Use a scalar value instead of an array
The Select component works either with scalar or arrays.
We cannot mix both.
---
packages/edit-site/src/components/page-pages/index.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js
index c0ae7395df28f8..007d456d8e398f 100644
--- a/packages/edit-site/src/components/page-pages/index.js
+++ b/packages/edit-site/src/components/page-pages/index.js
@@ -34,7 +34,7 @@ export default function PagePages() {
type: 'list',
filters: {
search: '',
- status: [ 'publish', 'draft' ],
+ status: 'publish, draft',
},
page: 1,
perPage: 5,
From 9ed4bddb8296d9f3c44f2527283a2025cbb4bac8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 13:27:03 +0200
Subject: [PATCH 35/41] Nicer looking select controls
---
.../src/components/dataviews/in-filter.js | 17 ++++++++++++++---
.../src/components/page-pages/index.js | 4 ++--
2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/packages/edit-site/src/components/dataviews/in-filter.js b/packages/edit-site/src/components/dataviews/in-filter.js
index d94dad4feb182e..51f7ca4db81ca1 100644
--- a/packages/edit-site/src/components/dataviews/in-filter.js
+++ b/packages/edit-site/src/components/dataviews/in-filter.js
@@ -1,15 +1,26 @@
/**
* WordPress dependencies
*/
-import { SelectControl } from '@wordpress/components';
+import {
+ __experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
+ SelectControl,
+} from '@wordpress/components';
export default ( { id, fields, view, onChangeView } ) => {
- const options = fields.find( ( f ) => f.id === id )?.elements || [];
+ const field = fields.find( ( f ) => f.id === id );
return (
+ { field.header + ':' }
+
+ }
+ options={ field?.elements || [] }
onChange={ ( value ) => {
if ( value === '' ) {
value = undefined;
diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js
index 007d456d8e398f..4d3a6910b63bf9 100644
--- a/packages/edit-site/src/components/page-pages/index.js
+++ b/packages/edit-site/src/components/page-pages/index.js
@@ -151,7 +151,7 @@ export default function PagePages() {
elements: [
{
value: '',
- label: __( 'All authors' ),
+ label: __( 'All' ),
},
...( authors?.map( ( { id, name } ) => ( {
value: id,
@@ -165,7 +165,7 @@ export default function PagePages() {
accessorFn: ( page ) =>
postStatuses[ page.status ] ?? page.status,
elements: [
- { label: __( 'All statuses' ), value: 'publish,draft' },
+ { label: __( 'All' ), value: 'publish,draft' },
...( ( postStatuses &&
Object.entries( postStatuses )
.filter( ( [ slug ] ) =>
From 61c556586e5113a9c6f484cb44e4b3aa92bad6cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 13:28:34 +0200
Subject: [PATCH 36/41] Make CSS class generic
---
packages/edit-site/src/components/dataviews/in-filter.js | 2 +-
packages/edit-site/src/components/dataviews/pagination.js | 2 +-
packages/edit-site/src/components/dataviews/style.scss | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/packages/edit-site/src/components/dataviews/in-filter.js b/packages/edit-site/src/components/dataviews/in-filter.js
index 51f7ca4db81ca1..516cac6233ee44 100644
--- a/packages/edit-site/src/components/dataviews/in-filter.js
+++ b/packages/edit-site/src/components/dataviews/in-filter.js
@@ -15,7 +15,7 @@ export default ( { id, fields, view, onChangeView } ) => {
prefix={
{ field.header + ':' }
diff --git a/packages/edit-site/src/components/dataviews/pagination.js b/packages/edit-site/src/components/dataviews/pagination.js
index 2f52d8da637e8c..5fbe4669768e8b 100644
--- a/packages/edit-site/src/components/dataviews/pagination.js
+++ b/packages/edit-site/src/components/dataviews/pagination.js
@@ -25,7 +25,7 @@ function PageSizeControl( { view, onChangeView } ) {
prefix={
{ label }
diff --git a/packages/edit-site/src/components/dataviews/style.scss b/packages/edit-site/src/components/dataviews/style.scss
index 1b27c0833c836c..ff3bbbbff5db02 100644
--- a/packages/edit-site/src/components/dataviews/style.scss
+++ b/packages/edit-site/src/components/dataviews/style.scss
@@ -30,7 +30,7 @@
}
}
-.dataviews__per-page-control-prefix {
+.dataviews__select-control-prefix {
color: $gray-700;
text-wrap: nowrap;
}
From 1fd75cdda18dfa489bd76be291a74c504f70cb6a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 13:57:01 +0200
Subject: [PATCH 37/41] Generate filters based on view.layout metadata
---
.../src/components/dataviews/dataviews.js | 43 +++++++++++--------
.../src/components/page-pages/index.js | 7 +++
2 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/packages/edit-site/src/components/dataviews/dataviews.js b/packages/edit-site/src/components/dataviews/dataviews.js
index c28c1a6fd08e0f..27a9897d673e96 100644
--- a/packages/edit-site/src/components/dataviews/dataviews.js
+++ b/packages/edit-site/src/components/dataviews/dataviews.js
@@ -5,6 +5,7 @@ import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
@@ -31,23 +32,31 @@ export default function DataViews( {
-
-
-
+ { view.layout?.filters?.map( ( filter ) => {
+ if ( filter.type === 'search' ) {
+ return (
+
+ );
+ }
+ if ( filter.type === 'in' ) {
+ return (
+
+ );
+ }
+
+ return null;
+ } ) || __( 'No filters available' ) }
Date: Tue, 17 Oct 2023 16:49:07 +0200
Subject: [PATCH 38/41] fixup rebase
---
packages/edit-site/src/components/dataviews/view-list.js | 1 -
1 file changed, 1 deletion(-)
diff --git a/packages/edit-site/src/components/dataviews/view-list.js b/packages/edit-site/src/components/dataviews/view-list.js
index 244e2944d9a7dd..d675720c86f83f 100644
--- a/packages/edit-site/src/components/dataviews/view-list.js
+++ b/packages/edit-site/src/components/dataviews/view-list.js
@@ -21,7 +21,6 @@ import {
check,
arrowUp,
arrowDown,
- moreVertical,
} from '@wordpress/icons';
import {
Button,
From f7b0708cc67c440c8e57ec53afbde9991ad5ccec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 17:12:38 +0200
Subject: [PATCH 39/41] Migrate view.layout.filters to view.visibleFilters and
field.filters
---
.../src/components/dataviews/dataviews.js | 34 ++---------
.../src/components/dataviews/filters.js | 57 +++++++++++++++++++
.../src/components/page-pages/index.js | 10 +---
3 files changed, 66 insertions(+), 35 deletions(-)
create mode 100644 packages/edit-site/src/components/dataviews/filters.js
diff --git a/packages/edit-site/src/components/dataviews/dataviews.js b/packages/edit-site/src/components/dataviews/dataviews.js
index 27a9897d673e96..c7e584d95fa99f 100644
--- a/packages/edit-site/src/components/dataviews/dataviews.js
+++ b/packages/edit-site/src/components/dataviews/dataviews.js
@@ -5,7 +5,6 @@ import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
-import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
@@ -13,8 +12,7 @@ import { __ } from '@wordpress/i18n';
import ViewList from './view-list';
import Pagination from './pagination';
import ViewActions from './view-actions';
-import TextFilter from './text-filter';
-import InFilter from './in-filter';
+import Filters from './filters';
import { ViewGrid } from './view-grid';
export default function DataViews( {
@@ -32,31 +30,11 @@ export default function DataViews( {
- { view.layout?.filters?.map( ( filter ) => {
- if ( filter.type === 'search' ) {
- return (
-
- );
- }
- if ( filter.type === 'in' ) {
- return (
-
- );
- }
-
- return null;
- } ) || __( 'No filters available' ) }
+
{
+ if ( ! field.filters ) {
+ return;
+ }
+
+ field.filters.forEach( ( f ) => {
+ filters[ f.id ] = { type: f.type };
+ } );
+ } );
+
+ return (
+ view.visibleFilters?.map( ( filterName ) => {
+ const filter = filters[ filterName ];
+
+ if ( ! filter ) {
+ return null;
+ }
+
+ if ( filter.type === 'search' ) {
+ return (
+
+ );
+ }
+ if ( filter.type === 'enumeration' ) {
+ return (
+
+ );
+ }
+
+ return null;
+ } ) || __( 'No filters available' )
+ );
+}
diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js
index c02de9f8595022..d1bfe353a28133 100644
--- a/packages/edit-site/src/components/page-pages/index.js
+++ b/packages/edit-site/src/components/page-pages/index.js
@@ -42,13 +42,7 @@ export default function PagePages() {
field: 'date',
direction: 'desc',
},
- layout: {
- filters: [
- { type: 'search', id: 'search' },
- { type: 'in', id: 'author' },
- { type: 'in', id: 'status' },
- ],
- },
+ visibleFilters: [ 'search', 'author', 'status' ],
// All fields are visible by default, so it's
// better to keep track of the hidden ones.
hiddenFields: [ 'date', 'featured-image' ],
@@ -155,6 +149,7 @@ export default function PagePages() {
);
},
+ filters: [ { id: 'author', type: 'enumeration' } ],
elements: [
{
value: '',
@@ -171,6 +166,7 @@ export default function PagePages() {
id: 'status',
accessorFn: ( page ) =>
postStatuses[ page.status ] ?? page.status,
+ filters: [ { type: 'enumeration', id: 'status' } ],
elements: [
{ label: __( 'All' ), value: 'publish,draft' },
...( ( postStatuses &&
From 14b2ab8d1f060698d3c514dd8ace414a36450745 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 17:16:41 +0200
Subject: [PATCH 40/41] Add search filter
---
packages/edit-site/src/components/page-pages/index.js | 1 +
1 file changed, 1 insertion(+)
diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js
index d1bfe353a28133..0ce7bbc96b6b96 100644
--- a/packages/edit-site/src/components/page-pages/index.js
+++ b/packages/edit-site/src/components/page-pages/index.js
@@ -133,6 +133,7 @@ export default function PagePages() {
);
},
+ filters: [ { id: 'search', type: 'search' } ],
maxWidth: 400,
sortingFn: 'alphanumeric',
enableHiding: false,
From 4bd098767ab009c6e35b87031a50407aca51ec44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Maneiro?=
<583546+oandregal@users.noreply.github.com>
Date: Tue, 17 Oct 2023 17:52:21 +0200
Subject: [PATCH 41/41] view.filters: remove undefined values
---
.../edit-site/src/components/dataviews/in-filter.js | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/packages/edit-site/src/components/dataviews/in-filter.js b/packages/edit-site/src/components/dataviews/in-filter.js
index 516cac6233ee44..0b62b59a3028b0 100644
--- a/packages/edit-site/src/components/dataviews/in-filter.js
+++ b/packages/edit-site/src/components/dataviews/in-filter.js
@@ -5,6 +5,14 @@ import {
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
SelectControl,
} from '@wordpress/components';
+import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
+
+/**
+ * Internal dependencies
+ */
+import { unlock } from '../../lock-unlock';
+
+const { cleanEmptyObject } = unlock( blockEditorPrivateApis );
export default ( { id, fields, view, onChangeView } ) => {
const field = fields.find( ( f ) => f.id === id );
@@ -28,10 +36,10 @@ export default ( { id, fields, view, onChangeView } ) => {
onChangeView( ( currentView ) => ( {
...currentView,
- filters: {
+ filters: cleanEmptyObject( {
...currentView.filters,
[ id ]: value,
- },
+ } ),
} ) );
} }
/>