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: remove the enumeration type as redundant #60084

Merged
merged 2 commits into from
Mar 22, 2024
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 @@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This check was already in place, just after the type.

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;
Copy link
Member Author

Choose a reason for hiding this comment

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

No change here other than un-indentation from removing the switch.

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
Original file line number Diff line number Diff line change
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
Loading