Skip to content

Commit

Permalink
Display parent fields when hierarchy is active
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 12, 2024
1 parent 70c097d commit c053e1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
18 changes: 16 additions & 2 deletions packages/dataviews/src/dataviews-layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function TableRow< Item >( {
// `onClick` and can be used to exclude touchscreen devices from certain
// behaviours.
const isTouchDeviceRef = useRef( false );
const columns = view.fields ?? [];
const columns = getFieldsToDisplay( view );
const hasPrimaryColumn =
( titleField && showTitle ) ||
( mediaField && showMedia ) ||
Expand Down Expand Up @@ -208,6 +208,20 @@ function TableRow< Item >( {
);
}

const getFieldsToDisplay = ( view: ViewTableType ) => {
if (
! view.layout?.hierarchicalSort ||
typeof view.layout?.hierarchicalSort === 'boolean'
) {
return view.fields || [];
}

// Make parent field the first field after the primary column.
return Array.from(
new Set( [ view.layout?.hierarchicalSort, ...( view.fields || [] ) ] )
);
};

function ViewTable< Item >( {
actions,
data,
Expand Down Expand Up @@ -270,7 +284,7 @@ function ViewTable< Item >( {
( titleField && showTitle ) ||
( mediaField && showMedia ) ||
( descriptionField && showDescription );
const columns = view.fields ?? [];
const columns = getFieldsToDisplay( view );
const headerMenuRef =
( column: string, index: number ) => ( node: HTMLButtonElement ) => {
if ( node ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export interface ViewTable extends ViewBase {
/**
* Switch on/off hierarchical sorting.
*/
hierarchicalSort?: boolean;
hierarchicalSort?: boolean | string;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
export const defaultLayouts = {
[ LAYOUT_TABLE ]: {
layout: {
hierarchicalSort: true,
hierarchicalSort: 'parent',
},
},
[ LAYOUT_GRID ]: {},
Expand All @@ -48,7 +48,7 @@ const DEFAULT_POST_BASE = {
},
titleField: 'title',
mediaField: 'featured_media',
fields: [ 'parent', 'author', 'status' ],
fields: [ 'author', 'status' ],
...defaultLayouts[ LAYOUT_LIST ],
};

Expand Down

0 comments on commit c053e1e

Please sign in to comment.