Skip to content

Commit

Permalink
DataViews: pass search filter as global, unattached from fields (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Oct 19, 2023
1 parent fba216c commit 8f65e35
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
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 @@ -157,9 +157,6 @@ export default function PagePages() {
</VStack>
);
},
filters: [
{ id: 'search', type: 'search', name: __( 'Search' ) },
],
maxWidth: 400,
sortingFn: 'alphanumeric',
enableHiding: false,
Expand Down Expand Up @@ -219,6 +216,10 @@ export default function PagePages() {
[ statuses, authors ]
);

const filters = useMemo( () => [
{ id: 'search', type: 'search', name: __( 'Filter list' ) },
] );

const trashPostAction = useTrashPostAction();
const actions = useMemo( () => [ trashPostAction ], [ trashPostAction ] );
const onChangeView = useCallback(
Expand Down Expand Up @@ -247,6 +248,7 @@ export default function PagePages() {
<DataViews
paginationInfo={ paginationInfo }
fields={ fields }
filters={ filters }
actions={ actions }
data={ pages || EMPTY_ARRAY }
isLoading={ isLoadingPages }
Expand Down

0 comments on commit 8f65e35

Please sign in to comment.