From c5af0aeda5769742bd9bcc2f5c96f2baf64403e9 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 8 Aug 2024 18:46:16 +0100 Subject: [PATCH] Add: Sort control at the field level on the new view config UI. --- .../dataviews-view-config/index.tsx | 128 ++++++++++++++---- 1 file changed, 99 insertions(+), 29 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-view-config/index.tsx b/packages/dataviews/src/components/dataviews-view-config/index.tsx index 28e151079008e..ad4eccde2de30 100644 --- a/packages/dataviews/src/components/dataviews-view-config/index.tsx +++ b/packages/dataviews/src/components/dataviews-view-config/index.tsx @@ -24,7 +24,7 @@ import { } from '@wordpress/components'; import { __, _x } from '@wordpress/i18n'; import { memo, useContext, useState, useMemo } from '@wordpress/element'; -import { cog, seen, unseen } from '@wordpress/icons'; +import { arrowDown, arrowUp, cog, seen, unseen } from '@wordpress/icons'; import warning from '@wordpress/warning'; /** @@ -32,7 +32,7 @@ import warning from '@wordpress/warning'; */ import { SORTING_DIRECTIONS, sortIcons, sortLabels } from '../../constants'; import { VIEW_LAYOUTS, getMandatoryFields } from '../../dataviews-layouts'; -import type { SupportedLayouts } from '../../types'; +import type { NormalizedField, SupportedLayouts } from '../../types'; import DataViewsContext from '../dataviews-context'; import { unlock } from '../../lock-unlock'; @@ -218,46 +218,116 @@ function ItemsPerPageControl() { ); } +function isFieldHidable( + field: NormalizedField< any >, + mandatoryFields: string | any[] +) { + return ( + field.enableHiding !== false && ! mandatoryFields.includes( field.id ) + ); +} +function isFieldSortable( field: NormalizedField< any > ) { + return field.enableSorting !== false; +} + function FieldControl() { const { view, fields, onChangeView } = useContext( DataViewsContext ); const mandatoryFields = getMandatoryFields( view ); - const hidableFields = fields.filter( - ( field ) => - field.enableHiding !== false && - ! mandatoryFields.includes( field.id ) - ); const viewFields = view.fields || fields.map( ( field ) => field.id ); - if ( ! hidableFields?.length ) { + if ( + ! fields?.some( + ( field ) => + isFieldHidable( field, mandatoryFields ) || + isFieldSortable( field ) + ) + ) { return null; } return ( - { hidableFields?.map( ( field ) => { + { fields?.map( ( field ) => { + const isHidable = isFieldHidable( field, mandatoryFields ); + const isSortable = isFieldSortable( field ); + if ( ! isHidable && ! isSortable ) { + return null; + } const isVisible = viewFields.includes( field.id ); return ( { field.label } -