From b55e3451eca668520b6efc70d798ba882ffadff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:45:35 +0100 Subject: [PATCH 1/2] Remove the enumeration type as redundant --- packages/dataviews/README.md | 8 ++--- packages/dataviews/src/constants.js | 3 -- packages/dataviews/src/filters.js | 52 +++++++++++----------------- packages/dataviews/src/view-table.js | 4 +-- 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/packages/dataviews/README.md b/packages/dataviews/README.md index 113f83897e6e1f..efe7a8d3be8b53 100644 --- a/packages/dataviews/README.md +++ b/packages/dataviews/README.md @@ -90,7 +90,6 @@ const fields = [ { item.author } ); }, - type: 'enumeration', elements: [ { value: 1, label: 'Admin' } { value: 2, label: 'User' } @@ -106,7 +105,6 @@ const fields = [ getValue: ( { item } ) => STATUSES.find( ( { value } ) => value === item.status ) ?.label ?? item.status, - type: 'enumeration', elements: STATUSES, filterBy: { operators: [ 'isAny' ], @@ -123,7 +121,7 @@ Each field is an object with the following properties: - `getValue`: function that returns the value of the field, defaults to `field[id]`. - `render`: function that renders the field. Optional, `getValue` will be used if `render` is not defined. - `elements`: the set of valid values for the field's value. -- `type`: the type of the field. Used to generate the proper filters. Only `enumeration` available at the moment. See "Field types". +- `type`: the type of the field. See "Field types". - `enableSorting`: whether the data can be sorted by the given field. True by default. - `enableHiding`: whether the field can be hidden. True by default. - `filterBy`: configuration for the filters. @@ -299,11 +297,11 @@ Callback that signals the user triggered the details for one of more items, and ### Fields -- `enumeration`: the field value should be taken and can be filtered from a closed list of elements. +> The `enumeration` type was removed as it was deemed redundant with the field.elements metadata. New types will be introduced soon. ### Operators -Allowed operators for fields of type `enumeration`: +Allowed operators: | Operator | Selection | Description | Example | | --- | --- | --- | --- | diff --git a/packages/dataviews/src/constants.js b/packages/dataviews/src/constants.js index 7dda927ea4c07c..e8149562b5f35a 100644 --- a/packages/dataviews/src/constants.js +++ b/packages/dataviews/src/constants.js @@ -16,9 +16,6 @@ import ViewTable from './view-table'; import ViewGrid from './view-grid'; import ViewList from './view-list'; -// Field types. -export const ENUMERATION_TYPE = 'enumeration'; - // Filter operators. export const OPERATOR_IS = 'is'; export const OPERATOR_IS_NOT = 'isNot'; diff --git a/packages/dataviews/src/filters.js b/packages/dataviews/src/filters.js index 9f5cb0aedf7d89..aae4d3871ab1ee 100644 --- a/packages/dataviews/src/filters.js +++ b/packages/dataviews/src/filters.js @@ -10,12 +10,7 @@ import FilterSummary from './filter-summary'; import AddFilter from './add-filter'; import ResetFilters from './reset-filters'; import { sanitizeOperators } from './utils'; -import { - ENUMERATION_TYPE, - ALL_OPERATORS, - OPERATOR_IS, - OPERATOR_IS_NOT, -} from './constants'; +import { ALL_OPERATORS, OPERATOR_IS, OPERATOR_IS_NOT } from './constants'; import { __experimentalHStack as HStack } from '@wordpress/components'; const Filters = memo( function Filters( { @@ -28,7 +23,7 @@ const Filters = memo( function Filters( { const addFilterRef = useRef(); const filters = []; fields.forEach( ( field ) => { - if ( ! field.type ) { + if ( ! field.elements?.length ) { return; } @@ -37,31 +32,24 @@ const Filters = memo( function Filters( { return; } - switch ( field.type ) { - case ENUMERATION_TYPE: - if ( ! field.elements?.length ) { - return; - } - - const isPrimary = !! field.filterBy?.isPrimary; - filters.push( { - field: field.id, - name: field.header, - elements: field.elements, - singleSelection: operators.some( ( op ) => - [ OPERATOR_IS, OPERATOR_IS_NOT ].includes( op ) - ), - operators, - isVisible: - isPrimary || - view.filters.some( - ( f ) => - f.field === field.id && - ALL_OPERATORS.includes( f.operator ) - ), - isPrimary, - } ); - } + const isPrimary = !! field.filterBy?.isPrimary; + filters.push( { + field: field.id, + name: field.header, + elements: field.elements, + singleSelection: operators.some( ( op ) => + [ OPERATOR_IS, OPERATOR_IS_NOT ].includes( op ) + ), + operators, + isVisible: + isPrimary || + view.filters.some( + ( f ) => + f.field === field.id && + ALL_OPERATORS.includes( f.operator ) + ), + isPrimary, + } ); } ); // Sort filters by primary property. We need the primary filters to be first. // Then we sort by name. diff --git a/packages/dataviews/src/view-table.js b/packages/dataviews/src/view-table.js index 5ef9d612523a00..b87a7adc671f44 100644 --- a/packages/dataviews/src/view-table.js +++ b/packages/dataviews/src/view-table.js @@ -34,7 +34,7 @@ import SingleSelectionCheckbox from './single-selection-checkbox'; import { unlock } from './lock-unlock'; import ItemActions from './item-actions'; import { sanitizeOperators } from './utils'; -import { ENUMERATION_TYPE, SORTING_DIRECTIONS } from './constants'; +import { SORTING_DIRECTIONS } from './constants'; import { useSomeItemHasAPossibleBulkAction, useHasAPossibleBulkAction, @@ -76,7 +76,7 @@ const HeaderMenu = forwardRef( function HeaderMenu( // 3. If it's not primary. If it is, it should be already visible. const canAddFilter = ! view.filters?.some( ( _filter ) => field.id === _filter.field ) && - field.type === ENUMERATION_TYPE && + !! field.elements?.length && !! operators.length && ! field.filterBy?.isPrimary; if ( ! isSortable && ! isHidable && ! canAddFilter ) { From 841383b6659de72d5f612b3831bc01ed00e431a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:04:08 +0100 Subject: [PATCH 2/2] Update changelog --- packages/dataviews/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index cd61d8d5918b27..5ac7c2adbab91d 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancement + +- The `enumeration` type has been removed and we'll introduce new field types soon. The existing filters will still work as before given they checked for field.elements, which is still a condition filters should have. + ## 0.8.0 (2024-03-21) ### Enhancement