Skip to content

Commit

Permalink
Add view config control to switch on/off hierarchical vizualization
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 3, 2024
1 parent f42e9fe commit 292f8d9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,12 @@ function DataviewsViewConfigDropdown() {
<SortFieldControl />
<SortDirectionControl />
</HStack>
{ !! activeLayout?.viewConfigOptions && (
<activeLayout.viewConfigOptions />
) }
{ !! activeLayout?.viewConfigOptions &&
activeLayout.viewConfigOptions.map(
( Control, index ) => (
<Control key={ index } />
)
) }
<ItemsPerPageControl />
</SettingsSection>
<SettingsSection title={ __( 'Properties' ) }>
Expand Down
5 changes: 3 additions & 2 deletions packages/dataviews/src/dataviews-layouts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ import { LAYOUT_GRID, LAYOUT_LIST, LAYOUT_TABLE } from '../constants';
import type { View, Field } from '../types';
import PreviewSizePicker from './grid/preview-size-picker';
import DensityPicker from './table/density-picker';
import SortHierarchical from './table/sort-hierarchical';

export const VIEW_LAYOUTS = [
{
type: LAYOUT_TABLE,
label: __( 'Table' ),
component: ViewTable,
icon: blockTable,
viewConfigOptions: DensityPicker,
viewConfigOptions: [ SortHierarchical, DensityPicker ],
},
{
type: LAYOUT_GRID,
label: __( 'Grid' ),
component: ViewGrid,
icon: category,
viewConfigOptions: PreviewSizePicker,
viewConfigOptions: [ PreviewSizePicker ],
},
{
type: LAYOUT_LIST,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* WordPress dependencies
*/
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import DataViewsContext from '../../components/dataviews-context';
import type { ViewTable } from '../../types';

function SortHierarchicalControl() {
const context = useContext( DataViewsContext );
const view = context.view as ViewTable;
const onChangeView = context.onChangeView;

return (
<ToggleGroupControl
__nextHasNoMarginBottom
__next40pxDefaultSize
isBlock
label={ __( 'Hierarchy' ) }
value={
view.layout?.hierarchicalSort === true ? 'enabled' : 'disabled'
}
onChange={ ( value ) => {
onChangeView( {
...view,
layout: {
...view.layout,
hierarchicalSort: value === 'enabled',
},
} );
} }
>
<ToggleGroupControlOption
key="enabled"
value="enabled"
label={ __( 'Enabled' ) }
/>
<ToggleGroupControlOption
key="disabled"
value="disabled"
label={ __( 'Disabled' ) }
/>
</ToggleGroupControl>
);
}

export default SortHierarchicalControl;
5 changes: 5 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ export interface ViewTable extends ViewBase {
* The field to visualize hierarchical data.
*/
hierarchical?: string;

/**
* Switch on/off hierarchical sorting.
*/
hierarchicalSort?: boolean;
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export default function PostList( { postType } ) {
_embed: 'author',
order: view.sort?.direction,
orderby: view.sort?.field,
orderby_hierarchy:
view.type === 'table' && !! view.layout?.hierarchicalSort
? true
: false,
search: view.search,
...filters,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const defaultLayouts = {
layout: {
primaryField: 'title',
hierarchical: 'parent',
hierarchicalSort: true,
styles: {
title: {
maxWidth: 300,
Expand All @@ -43,13 +44,15 @@ export const defaultLayouts = {
mediaField: 'featured_media',
primaryField: 'title',
hierarchical: 'parent', // TODO: remove this
hierarchicalSort: true, // TODO: remove this
},
},
[ LAYOUT_LIST ]: {
layout: {
primaryField: 'title',
mediaField: 'featured_media',
hierarchical: 'parent', // TODO: remove this
hierarchicalSort: true, // TODO: remove this
},
},
};
Expand Down

0 comments on commit 292f8d9

Please sign in to comment.