Skip to content

Commit 116b219

Browse files
DataViews: remove the enumeration type as redundant (#60084)
Co-authored-by: oandregal <[email protected]> Co-authored-by: youknowriad <[email protected]>
1 parent 8e1a7bc commit 116b219

File tree

5 files changed

+29
-42
lines changed

5 files changed

+29
-42
lines changed

packages/dataviews/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancement
6+
7+
- 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.
8+
59
## 0.8.0 (2024-03-21)
610

711
### Enhancement

packages/dataviews/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ const fields = [
9090
<a href="...">{ item.author }</a>
9191
);
9292
},
93-
type: 'enumeration',
9493
elements: [
9594
{ value: 1, label: 'Admin' }
9695
{ value: 2, label: 'User' }
@@ -106,7 +105,6 @@ const fields = [
106105
getValue: ( { item } ) =>
107106
STATUSES.find( ( { value } ) => value === item.status )
108107
?.label ?? item.status,
109-
type: 'enumeration',
110108
elements: STATUSES,
111109
filterBy: {
112110
operators: [ 'isAny' ],
@@ -123,7 +121,7 @@ Each field is an object with the following properties:
123121
- `getValue`: function that returns the value of the field, defaults to `field[id]`.
124122
- `render`: function that renders the field. Optional, `getValue` will be used if `render` is not defined.
125123
- `elements`: the set of valid values for the field's value.
126-
- `type`: the type of the field. Used to generate the proper filters. Only `enumeration` available at the moment. See "Field types".
124+
- `type`: the type of the field. See "Field types".
127125
- `enableSorting`: whether the data can be sorted by the given field. True by default.
128126
- `enableHiding`: whether the field can be hidden. True by default.
129127
- `filterBy`: configuration for the filters.
@@ -299,11 +297,11 @@ Callback that signals the user triggered the details for one of more items, and
299297

300298
### Fields
301299

302-
- `enumeration`: the field value should be taken and can be filtered from a closed list of elements.
300+
> The `enumeration` type was removed as it was deemed redundant with the field.elements metadata. New types will be introduced soon.
303301
304302
### Operators
305303

306-
Allowed operators for fields of type `enumeration`:
304+
Allowed operators:
307305

308306
| Operator | Selection | Description | Example |
309307
| --- | --- | --- | --- |

packages/dataviews/src/constants.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import ViewTable from './view-table';
1616
import ViewGrid from './view-grid';
1717
import ViewList from './view-list';
1818

19-
// Field types.
20-
export const ENUMERATION_TYPE = 'enumeration';
21-
2219
// Filter operators.
2320
export const OPERATOR_IS = 'is';
2421
export const OPERATOR_IS_NOT = 'isNot';

packages/dataviews/src/filters.js

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import FilterSummary from './filter-summary';
1010
import AddFilter from './add-filter';
1111
import ResetFilters from './reset-filters';
1212
import { sanitizeOperators } from './utils';
13-
import {
14-
ENUMERATION_TYPE,
15-
ALL_OPERATORS,
16-
OPERATOR_IS,
17-
OPERATOR_IS_NOT,
18-
} from './constants';
13+
import { ALL_OPERATORS, OPERATOR_IS, OPERATOR_IS_NOT } from './constants';
1914
import { __experimentalHStack as HStack } from '@wordpress/components';
2015

2116
const Filters = memo( function Filters( {
@@ -28,7 +23,7 @@ const Filters = memo( function Filters( {
2823
const addFilterRef = useRef();
2924
const filters = [];
3025
fields.forEach( ( field ) => {
31-
if ( ! field.type ) {
26+
if ( ! field.elements?.length ) {
3227
return;
3328
}
3429

@@ -37,31 +32,24 @@ const Filters = memo( function Filters( {
3732
return;
3833
}
3934

40-
switch ( field.type ) {
41-
case ENUMERATION_TYPE:
42-
if ( ! field.elements?.length ) {
43-
return;
44-
}
45-
46-
const isPrimary = !! field.filterBy?.isPrimary;
47-
filters.push( {
48-
field: field.id,
49-
name: field.header,
50-
elements: field.elements,
51-
singleSelection: operators.some( ( op ) =>
52-
[ OPERATOR_IS, OPERATOR_IS_NOT ].includes( op )
53-
),
54-
operators,
55-
isVisible:
56-
isPrimary ||
57-
view.filters.some(
58-
( f ) =>
59-
f.field === field.id &&
60-
ALL_OPERATORS.includes( f.operator )
61-
),
62-
isPrimary,
63-
} );
64-
}
35+
const isPrimary = !! field.filterBy?.isPrimary;
36+
filters.push( {
37+
field: field.id,
38+
name: field.header,
39+
elements: field.elements,
40+
singleSelection: operators.some( ( op ) =>
41+
[ OPERATOR_IS, OPERATOR_IS_NOT ].includes( op )
42+
),
43+
operators,
44+
isVisible:
45+
isPrimary ||
46+
view.filters.some(
47+
( f ) =>
48+
f.field === field.id &&
49+
ALL_OPERATORS.includes( f.operator )
50+
),
51+
isPrimary,
52+
} );
6553
} );
6654
// Sort filters by primary property. We need the primary filters to be first.
6755
// Then we sort by name.

packages/dataviews/src/view-table.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import SingleSelectionCheckbox from './single-selection-checkbox';
3434
import { unlock } from './lock-unlock';
3535
import ItemActions from './item-actions';
3636
import { sanitizeOperators } from './utils';
37-
import { ENUMERATION_TYPE, SORTING_DIRECTIONS } from './constants';
37+
import { SORTING_DIRECTIONS } from './constants';
3838
import {
3939
useSomeItemHasAPossibleBulkAction,
4040
useHasAPossibleBulkAction,
@@ -76,7 +76,7 @@ const HeaderMenu = forwardRef( function HeaderMenu(
7676
// 3. If it's not primary. If it is, it should be already visible.
7777
const canAddFilter =
7878
! view.filters?.some( ( _filter ) => field.id === _filter.field ) &&
79-
field.type === ENUMERATION_TYPE &&
79+
!! field.elements?.length &&
8080
!! operators.length &&
8181
! field.filterBy?.isPrimary;
8282
if ( ! isSortable && ! isHidable && ! canAddFilter ) {

0 commit comments

Comments
 (0)