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 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
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
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/dataviews/text-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function TextFilter( { filter, view, onChangeView } ) {
},
} ) );
}, [ debouncedSearch ] );
const searchLabel = __( 'Filter list' );
const searchLabel = filter?.name || __( 'Filter list' );
return (
<SearchControl
onChange={ setSearch }
Expand Down
8 changes: 5 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,10 @@ export default function PagePages() {
[ postStatuses, authors ]
);

const filters = useMemo( () => [
Copy link
Contributor

Choose a reason for hiding this comment

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

To be honest, I'm not sure I'd consider "search" a filter at all. It may be its own thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

{ id: 'search', type: 'search', name: __( 'Filter list' ) },
] );

const trashPostAction = useTrashPostAction();
const actions = useMemo( () => [ trashPostAction ], [ trashPostAction ] );
const onChangeView = useCallback(
Expand Down Expand Up @@ -228,6 +229,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