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: update field API to generate filters based on type #55996

Merged
merged 6 commits into from
Nov 17, 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
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ Example:
<a href="...">{ item.author }</a>
);
},
type: 'enumeration',
elements: [
{ value: 1, label: 'Admin' }
{ value: 2, label: 'User' }
]
filters: [ 'in' ],
enableSorting: false
}
]
Expand All @@ -169,7 +169,7 @@ Example:
- `getValue`: function that returns the value of the field.
- `render`: function that renders the field.
- `elements`: the set of valid values for the field's value.
- `filters`: what filter operators are available for the user to use over this field. Only `in` available at the moment.
- `type`: the type of the field. Used to generate the proper filters. Only `enumeration` available at the moment.
- `enableSorting`: whether the data can be sorted by the given field. True by default.
- `enableHiding`: whether the field can be hidden. True by default.

Expand Down
18 changes: 9 additions & 9 deletions packages/edit-site/src/components/dataviews/add-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ const {
DropdownMenuItemV2,
} = unlock( componentsPrivateApis );

const VALID_OPERATORS = [ OPERATOR_IN ];
// TODO: find a place where these constants can be shared across components.
const ENUMERATION_TYPE = 'enumeration';

export default function AddFilter( { fields, view, onChangeView } ) {
const filters = [];
fields.forEach( ( field ) => {
if ( ! field.filters ) {
if ( ! field.type ) {
return;
}

field.filters.forEach( ( filter ) => {
if ( VALID_OPERATORS.some( ( operator ) => operator === filter ) ) {
switch ( field.type ) {
case ENUMERATION_TYPE:
filters.push( {
field: field.id,
name: field.header,
operator: filter,
elements: field.elements || [],
isVisible: view.filters.some(
( f ) => f.field === field.id && f.operator === filter
( f ) =>
f.field === field.id && f.operator === OPERATOR_IN
),
} );
}
} );
}
} );

if ( filters.length === 0 ) {
Expand Down Expand Up @@ -92,7 +92,7 @@ export default function AddFilter( { fields, view, onChangeView } ) {
...currentView.filters,
{
field: filter.field,
operator: 'in',
operator: OPERATOR_IN,
value: element.value,
},
],
Expand Down
37 changes: 16 additions & 21 deletions packages/edit-site/src/components/dataviews/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import { default as InFilter, OPERATOR_IN } from './in-filter';
import AddFilter from './add-filter';
import ResetFilters from './reset-filters';

const VALID_OPERATORS = [ OPERATOR_IN ];
const ENUMERATION_TYPE = 'enumeration';

export default function Filters( { fields, view, onChangeView } ) {
const filters = [];
fields.forEach( ( field ) => {
if ( ! field.filters ) {
if ( ! field.type ) {
return;
}

field.filters.forEach( ( filter ) => {
if ( VALID_OPERATORS.some( ( operator ) => operator === filter ) ) {
switch ( field.type ) {
case ENUMERATION_TYPE:
filters.push( {
field: field.id,
name: field.header,
operator: filter,
elements: [
{
value: '',
Expand All @@ -33,30 +32,26 @@ export default function Filters( { fields, view, onChangeView } ) {
...( field.elements || [] ),
],
isVisible: view.filters.some(
( f ) => f.field === field.id && f.operator === filter
( f ) =>
f.field === field.id && f.operator === OPERATOR_IN
),
} );
}
} );
}
} );

const filterComponents = filters?.map( ( filter ) => {
const filterComponents = filters.map( ( filter ) => {
if ( ! filter.isVisible ) {
return null;
}

if ( OPERATOR_IN === filter.operator ) {
return (
<InFilter
key={ filter.field + '.' + filter.operator }
filter={ filter }
view={ view }
onChangeView={ onChangeView }
/>
);
}

return null;
return (
<InFilter
key={ filter.field + '.' + filter.operator }
filter={ filter }
view={ view }
onChangeView={ onChangeView }
/>
);
} );

filterComponents.push(
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/dataviews/in-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

// TODO: find a place where these constants can be shared across components.
export const OPERATOR_IN = 'in';

export default ( { filter, view, onChangeView } ) => {
Expand Down
15 changes: 8 additions & 7 deletions packages/edit-site/src/components/dataviews/view-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const sortingItemsInfo = {
desc: { icon: arrowDown, label: __( 'Sort descending' ) },
};
const sortIcons = { asc: chevronUp, desc: chevronDown };

// TODO: find a place where these constants can be shared across components.
const ENUMERATION_TYPE = 'enumeration';
const OPERATOR_IN = 'in';

function HeaderMenu( { dataView, header } ) {
if ( header.isPlaceholder ) {
return null;
Expand All @@ -68,12 +73,7 @@ function HeaderMenu( { dataView, header } ) {
const sortedDirection = header.column.getIsSorted();

let filter;
if (
header.column.columnDef.filters?.length > 0 &&
header.column.columnDef.filters.some(
( f ) => 'string' === typeof f && f === 'in'
)
) {
if ( header.column.columnDef.type === ENUMERATION_TYPE ) {
filter = {
field: header.column.columnDef.id,
elements: [
Expand Down Expand Up @@ -202,7 +202,8 @@ function HeaderMenu( { dataView, header } ) {
return (
field !==
filter.field ||
operator !== 'in'
operator !==
OPERATOR_IN
);
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default function PagePages() {
</a>
);
},
filters: [ 'in' ],
type: 'enumeration',
elements:
authors?.map( ( { id, name } ) => ( {
value: id,
Expand All @@ -240,7 +240,7 @@ export default function PagePages() {
getValue: ( { item } ) =>
STATUSES.find( ( { value } ) => value === item.status )
?.label ?? item.status,
filters: [ 'in' ],
type: 'enumeration',
elements: STATUSES,
enableSorting: false,
},
Expand Down
Loading