Skip to content

Commit

Permalink
DataViews: remove the enumeration type as redundant (WordPress#60084)
Browse files Browse the repository at this point in the history
Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored and carstingaxion committed Mar 27, 2024
1 parent 1b7804e commit 299fd3b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 42 deletions.
4 changes: 4 additions & 0 deletions packages/dataviews/CHANGELOG.md
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions packages/dataviews/README.md
Expand Up @@ -90,7 +90,6 @@ const fields = [
<a href="...">{ item.author }</a>
);
},
type: 'enumeration',
elements: [
{ value: 1, label: 'Admin' }
{ value: 2, label: 'User' }
Expand All @@ -106,7 +105,6 @@ const fields = [
getValue: ( { item } ) =>
STATUSES.find( ( { value } ) => value === item.status )
?.label ?? item.status,
type: 'enumeration',
elements: STATUSES,
filterBy: {
operators: [ 'isAny' ],
Expand All @@ -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.
Expand Down Expand Up @@ -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 |
| --- | --- | --- | --- |
Expand Down
3 changes: 0 additions & 3 deletions packages/dataviews/src/constants.js
Expand Up @@ -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';
Expand Down
52 changes: 20 additions & 32 deletions packages/dataviews/src/filters.js
Expand Up @@ -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( {
Expand All @@ -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;
}

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions packages/dataviews/src/view-table.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) {
Expand Down

0 comments on commit 299fd3b

Please sign in to comment.