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: pass search filter as global, unattached from fields #55475

Merged
merged 2 commits into from
Oct 19, 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
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 @@ -20,6 +20,7 @@ export default function DataViews( {
view,
onChangeView,
fields,
filters,
actions,
data,
isLoading = false,
Expand All @@ -38,6 +39,7 @@ export default function DataViews( {
<HStack>
<HStack justify="start">
<Filters
filters={ filters }
fields={ fields }
view={ view }
onChangeView={ onChangeView }
Expand Down
24 changes: 16 additions & 8 deletions packages/edit-site/src/components/dataviews/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import { __ } from '@wordpress/i18n';
import TextFilter from './text-filter';
import InFilter from './in-filter';

export default function Filters( { fields, view, onChangeView } ) {
const filters = {};
export default function Filters( { filters, fields, view, onChangeView } ) {
const filterIndex = {};
filters.forEach( ( filter ) => {
if ( 'object' !== typeof filter || ! filter?.id || ! filter?.type ) {
return;
}

filterIndex[ filter.id ] = filter;
} );

fields.forEach( ( field ) => {
if ( ! field.filters ) {
return;
Expand All @@ -19,7 +27,7 @@ export default function Filters( { fields, view, onChangeView } ) {
field.filters.forEach( ( filter ) => {
let id = field.id;
if ( 'string' === typeof filter ) {
filters[ id ] = {
filterIndex[ id ] = {
id,
name: field.header,
type: filter,
Expand All @@ -28,23 +36,23 @@ export default function Filters( { fields, view, onChangeView } ) {

if ( 'object' === typeof filter ) {
id = filter.id || field.id;
filters[ id ] = {
filterIndex[ id ] = {
id,
name: filter.name || field.header,
type: filter.type,
};
}

if ( 'enumeration' === filters[ id ]?.type ) {
if ( 'enumeration' === filterIndex[ id ]?.type ) {
const elements = [
{
value: filter.resetValue || '',
label: filter.resetLabel || __( 'All' ),
},
...( field.elements || [] ),
];
filters[ id ] = {
...filters[ id ],
filterIndex[ id ] = {
...filterIndex[ id ],
elements,
};
}
Expand All @@ -53,7 +61,7 @@ export default function Filters( { fields, view, onChangeView } ) {

return (
view.visibleFilters?.map( ( filterName ) => {
const filter = filters[ filterName ];
const filter = filterIndex[ filterName ];

if ( ! filter ) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ export default function PagePages() {
</VStack>
);
},
filters: [
{ id: 'search', type: 'search', name: __( 'Search' ) },
],
maxWidth: 400,
sortingFn: 'alphanumeric',
enableHiding: false,
Expand Down Expand Up @@ -200,6 +197,8 @@ export default function PagePages() {
[ postStatuses, authors ]
);

const filters = useMemo( () => [ { id: 'search', type: 'search' } ] );
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 not sure we're expecting more global filters like that, do we? The only piece of info needed with the current architecture is the key to be used in the query, and I believe we can safely start with a TextFilter outside of Filters component(render in dataviews.js).

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer not to make it a special case, to be honest. This way, DataViews is already future-proof for any other page/endpoint/etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Though it is a special case, no? It's a global filter. How it could affect any other page etc..?

Copy link
Member Author

@oandregal oandregal Oct 19, 2023

Choose a reason for hiding this comment

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

My point is that I prefer to conceptualize it as a filter and do this:

<DataViews
  filters={ filters }
/>

rather than making it a special case and do this:

<DataViews
  search={search}
/>

The reason is this: what if a different page/endpoint has a global filter that is an enumeration or any other type of filter?

The first approach is future-proof: it requires no changes in the future, it already supports any type of filter (search, enumeration, and whatever we add in the future). The second approach would require us to make changes in DataViews API (new properties, etc.) even when it already supports those other filter types.

Copy link
Contributor

Choose a reason for hiding this comment

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

No strong opinions here, but I still don't get it. We still add the filters prop to the API and we're trying to support a use case that we don't have right now. The concept of having a text global filter in a list or in a collection in general is the most common one, by far I think. Maybe some others can chime in, but it's not something that important for now IMO. In my mind the prop would be something like globalFilter="$key".

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe some others can chime in, but it's not something that important for now IMO. In my mind the prop would be something like globalFilter="$key".

What you are suggesting would be (note we still have to pass the label as well):

<DataViews
  globalFilter={ {id: "key", label: "Label" } }
/>

And what this PR proposes is:

<DataViews
  filters=[
    { id: "key", label: "Label", type: "type" }
  ]
/>

It's not that different :)

The only differences I see are:

Alternative Existing
Property name globalFilter filters
Property type object array of objects
Shape of objects Only id and label The same as field filters.

I feel strongly about the shape of filters being identical between field filters and global filters: I don't think we should do anything differently.

If it helps unlock this review and merge the PR, I am open to use globalFilter as name and use an object instead of an array. We could change that later if needed be.

Copy link
Contributor

@ntsekouras ntsekouras Oct 19, 2023

Choose a reason for hiding this comment

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

After thinking more about it, while I still believe the global filter is a special case and will be almost a text filter every time, passing filters array allows a consumer to have more custom filters(if they need to) that are outside the specifics of a field. Let's land this.

Copy link
Member Author

Choose a reason for hiding this comment

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


const trashPostAction = useTrashPostAction();
const actions = useMemo( () => [ trashPostAction ], [ trashPostAction ] );
const onChangeView = useCallback(
Expand Down Expand Up @@ -228,6 +227,7 @@ export default function PagePages() {
<DataViews
paginationInfo={ paginationInfo }
fields={ fields }
filters={ filters }
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this prop? What is it for?

actions={ actions }
data={ pages || EMPTY_ARRAY }
isLoading={ isLoadingPages }
Expand Down
Loading