Skip to content

Commit

Permalink
Remove the enumeration type as redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Mar 21, 2024
1 parent 9c0a5ac commit b55e345
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
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 ) {
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
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

0 comments on commit b55e345

Please sign in to comment.