Skip to content
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: 4 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- DataViews: Use regular casing for bulk selection count. [#74573](https://github.com/WordPress/gutenberg/pull/74573)

### Code Quality

- DataViews: Move filtering logic in field types. [#74733](https://github.com/WordPress/gutenberg/pull/74733)

## 11.2.0 (2026-01-16)

### Code Quality
Expand Down
2 changes: 2 additions & 0 deletions packages/dataviews/src/field-types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { default as color } from './color';
import { default as url } from './url';
import { default as noType } from './no-type';
import getIsValid from './utils/get-is-valid';
import getFilter from './utils/get-filter';
import getFormat from './utils/get-format';

/**
Expand Down Expand Up @@ -111,6 +112,7 @@ export default function normalizeFields< Item >(
fieldType.defaultOperators,
fieldType.validOperators
),
filter: getFilter( fieldType ),
format: getFormat( field, fieldType ),
getValueFormatted:
field.getValueFormatted ?? fieldType.getValueFormatted,
Expand Down
18 changes: 18 additions & 0 deletions packages/dataviews/src/field-types/utils/get-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Internal dependencies
*/
import type { FilterOperatorMap } from '../../types';
import type { FieldType } from '../../types/private';
import { getOperatorByName } from '../../utils/operators';

export default function getFilter< Item >(
fieldType: FieldType< Item >
): FilterOperatorMap< Item > {
return fieldType.validOperators.reduce( ( accumulator, operator ) => {
const operatorObj = getOperatorByName( operator );
if ( operatorObj?.filter ) {
accumulator[ operator ] = operatorObj.filter;
}
return accumulator;
}, {} as FilterOperatorMap< Item > );
}
11 changes: 11 additions & 0 deletions packages/dataviews/src/types/field-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ export type CustomValidator< Item > =
field: NormalizedField< Item >
) => Promise< null | string > );

export type FilterOperator< Item > = (
item: Item,
field: NormalizedField< Item >,
filterValue: any
) => boolean;

export type FilterOperatorMap< Item > = Partial<
Record< Operator, FilterOperator< Item > >
>;

type NormalizedRule< Item, ConstraintType > = {
constraint: ConstraintType;
validate: Validator< Item >;
Expand Down Expand Up @@ -356,6 +366,7 @@ export type NormalizedField< Item > = Omit<
enableHiding: boolean;
enableSorting: boolean;
filterBy: Required< FilterByConfig > | false;
filter: FilterOperatorMap< Item >;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just want to emphasize that this filter prop is only available to the runtime type (NormalizedField), so it's not extensible/overridable by the field definitions provided by the downstream consumers, neither the field type. I agree this is how we should start. I only mention it because the PR description may induce folks to believe this is extensible. :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a strong opinion, but I wonder what you think of merging this filter property with the existing filterBy. Or perhaps we could take this opportunity to rename filterBy to filterConfig.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I had thought about that while implementing. First of all renaming filterBy to filterConfig would be nice independently from moving the filter prop.

I wasn't 100% sold by having it separately, but it felt more similar to sort where they are both implementations. filterBy is a filter config mostly used for UI filter management and easy to reason with - contains operators and isPrimary. Having the separate prop feels to me a bit more semantically correct, but still no strong opinions.

I'll merge this and let's reconsider in your other PR.

readOnly: boolean;
format:
| {}
Expand Down
Loading
Loading