From a41cf1a541be8ceae87baa796aa39249b044662d Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 29 Aug 2024 19:06:58 +0200 Subject: [PATCH] Add: Reorder control at the field level on the new view config UI. (#64381) Co-authored-by: jorgefilipecosta Co-authored-by: jameskoster Co-authored-by: paulwilde --- .../dataviews-view-config/index.tsx | 296 +++++++++++++++--- .../dataviews-view-config/style.scss | 19 ++ 2 files changed, 277 insertions(+), 38 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-view-config/index.tsx b/packages/dataviews/src/components/dataviews-view-config/index.tsx index bd9bc1b19803e..331d0d62dca00 100644 --- a/packages/dataviews/src/components/dataviews-view-config/index.tsx +++ b/packages/dataviews/src/components/dataviews-view-config/index.tsx @@ -21,10 +21,11 @@ import { __experimentalHeading as Heading, __experimentalText as Text, privateApis as componentsPrivateApis, + BaseControl, } from '@wordpress/components'; -import { __, _x } from '@wordpress/i18n'; +import { __, _x, sprintf } from '@wordpress/i18n'; import { memo, useContext, useState, useMemo } from '@wordpress/element'; -import { cog, seen, unseen } from '@wordpress/icons'; +import { chevronDown, chevronUp, cog, seen, unseen } from '@wordpress/icons'; import warning from '@wordpress/warning'; /** @@ -33,11 +34,12 @@ import warning from '@wordpress/warning'; import { SORTING_DIRECTIONS, LAYOUT_GRID, + LAYOUT_TABLE, sortIcons, sortLabels, } from '../../constants'; import { VIEW_LAYOUTS, getMandatoryFields } from '../../dataviews-layouts'; -import type { SupportedLayouts } from '../../types'; +import type { NormalizedField, SupportedLayouts, View } from '../../types'; import DataViewsContext from '../dataviews-context'; import { unlock } from '../../lock-unlock'; import DensityPicker from '../../dataviews-layouts/grid/density-picker'; @@ -230,50 +232,268 @@ function ItemsPerPageControl() { ); } -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 ) { - return null; +function FieldItem( { + fields, + fieldId, + mandatoryFields, + viewFields, + view, + onChangeView, +}: { + fields: NormalizedField< any >[]; + fieldId: string; + mandatoryFields: string | any[]; + viewFields: string[]; + view: View; + onChangeView: ( view: View ) => void; +} ) { + let fieldLabel; + let fieldIsHidable; + const fieldObject = fields.find( + ( f ) => f.id === fieldId + ) as NormalizedField< any >; + if ( fieldObject ) { + fieldLabel = fieldObject.label; + fieldIsHidable = + fieldObject.enableHiding !== false && + ! mandatoryFields.includes( fieldId ); + } else if ( view.type === LAYOUT_TABLE ) { + const combinedFieldObject = view.layout?.combinedFields?.find( + ( f ) => f.id === fieldId + ); + if ( combinedFieldObject ) { + fieldLabel = combinedFieldObject.label; + fieldIsHidable = ! mandatoryFields.includes( fieldId ); + } } + + const index = view.fields?.indexOf( fieldId ) as number; + const isVisible = viewFields.includes( fieldId ); return ( - - { hidableFields?.map( ( field ) => { - const isVisible = viewFields.includes( field.id ); - return ( - - - { field.label } + + + { fieldLabel } + + { view.type === LAYOUT_TABLE && isVisible && ( + <>