From 7c0638b5a5ad278da458ff5d3fa4cd8a753e301d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:18:38 +0200 Subject: [PATCH] DataViews: fix field reordering and visibility logic (#64999) Co-authored-by: oandregal Co-authored-by: youknowriad Co-authored-by: jorgefilipecosta --- .../dataviews-view-config/index.tsx | 251 ++++++++---------- .../dataviews/src/dataviews-layouts/index.ts | 65 ++++- .../table/column-header-menu.tsx | 105 ++++---- .../src/dataviews-layouts/table/index.tsx | 5 +- packages/dataviews/src/normalize-fields.ts | 2 + packages/dataviews/src/types.ts | 2 + 6 files changed, 238 insertions(+), 192 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-view-config/index.tsx b/packages/dataviews/src/components/dataviews-view-config/index.tsx index 331d0d62dca00..c01c72d2ebc69 100644 --- a/packages/dataviews/src/components/dataviews-view-config/index.tsx +++ b/packages/dataviews/src/components/dataviews-view-config/index.tsx @@ -38,8 +38,13 @@ import { sortIcons, sortLabels, } from '../../constants'; -import { VIEW_LAYOUTS, getMandatoryFields } from '../../dataviews-layouts'; -import type { NormalizedField, SupportedLayouts, View } from '../../types'; +import { + VIEW_LAYOUTS, + getNotHidableFieldIds, + getVisibleFieldIds, + getHiddenFieldIds, +} from '../../dataviews-layouts'; +import type { SupportedLayouts, View, Field } from '../../types'; import DataViewsContext from '../dataviews-context'; import { unlock } from '../../lock-unlock'; import DensityPicker from '../../dataviews-layouts/grid/density-picker'; @@ -232,50 +237,34 @@ function ItemsPerPageControl() { ); } +interface FieldItemProps { + id: any; + label: string; + index: number; + isVisible: boolean; + isHidable: boolean; +} + function FieldItem( { + field: { id, label, index, isVisible, isHidable }, fields, - fieldId, - mandatoryFields, - viewFields, view, onChangeView, }: { - fields: NormalizedField< any >[]; - fieldId: string; - mandatoryFields: string | any[]; - viewFields: string[]; + field: FieldItemProps; + fields: Field< any >[]; 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 visibleFieldIds = getVisibleFieldIds( view, fields ); - const index = view.fields?.indexOf( fieldId ) as number; - const isVisible = viewFields.includes( fieldId ); return ( - + - { fieldLabel } + { label }