Skip to content

Commit

Permalink
Make all combined fields mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jul 8, 2024
1 parent 1680214 commit 277a884
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
27 changes: 27 additions & 0 deletions packages/dataviews/src/layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ViewTable from './view-table';
import ViewGrid from './view-grid';
import ViewList from './view-list';
import { LAYOUT_GRID, LAYOUT_LIST, LAYOUT_TABLE } from './constants';
import type { View } from './types';

export const VIEW_LAYOUTS = [
{
Expand All @@ -37,3 +38,29 @@ export const VIEW_LAYOUTS = [
icon: isRTL() ? formatListBulletsRTL : formatListBullets,
},
];

export function getMandatoryFields( view: View ): string[] {
if ( view.type === 'table' ) {
return [ view.layout?.primaryField ]
.concat(
view.layout?.combinedFields?.flatMap(
( field ) => field.children
) ?? []
)
.filter( ( item ): item is string => !! item );
}

if ( view.type === 'grid' ) {
return [ view.layout?.primaryField, view.layout?.mediaField ].filter(
( item ): item is string => !! item
);
}

if ( view.type === 'list' ) {
return [ view.layout?.primaryField, view.layout?.mediaField ].filter(
( item ): item is string => !! item
);
}

return [];
}
5 changes: 0 additions & 5 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,6 @@ export interface ViewTable extends ViewBase {
*/
primaryField?: string;

/**
* The field to use as the media field.
*/
mediaField?: string;

/**
* The fields to use as columns.
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/dataviews/src/view-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { cog } from '@wordpress/icons';
*/
import { unlock } from './lock-unlock';
import { SORTING_DIRECTIONS, sortLabels } from './constants';
import { VIEW_LAYOUTS } from './layouts';
import { VIEW_LAYOUTS, getMandatoryFields } from './layouts';
import type { NormalizedField, View } from './types';

const {
Expand Down Expand Up @@ -166,9 +166,11 @@ function FieldsVisibilityMenu< Item >( {
onChangeView,
fields,
}: FieldsVisibilityMenuProps< Item > ) {
const mandatoryFields = getMandatoryFields( view );
const hidableFields = fields.filter(
( field ) =>
field.enableHiding !== false && field.id !== view.layout.mediaField
field.enableHiding !== false &&
! mandatoryFields.includes( field.id )
);
const viewFields = view.fields || fields.map( ( field ) => field.id );
if ( ! hidableFields?.length ) {
Expand Down

0 comments on commit 277a884

Please sign in to comment.