Skip to content

Commit

Permalink
Add mixed symbol value for bulk editing
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Dec 3, 2024
1 parent ac03ed1 commit c27bd65
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { DataFormProps } from '../../types';
import { DataFormProvider } from '../dataform-context';
import { normalizeFields } from '../../normalize-fields';
import { DataFormLayout } from '../../dataforms-layouts/data-form-layout';
import { MIXED_VALUE } from '../../constants';

/**
* Loops through the list of data items and returns an object with the intersecting ( same ) key and values.
Expand All @@ -26,6 +27,8 @@ function getIntersectingValues< Item extends object >( data: Item[] ): Item {
} );
if ( intersects ) {
intersectingValues[ key ] = firstRecord[ key ];
} else {
intersectingValues[ key ] = MIXED_VALUE as Item[ keyof Item ];
}
}
return intersectingValues;
Expand Down
3 changes: 3 additions & 0 deletions packages/dataviews/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ export const sortIcons = {
export const LAYOUT_TABLE = 'table';
export const LAYOUT_GRID = 'grid';
export const LAYOUT_LIST = 'list';

// Dataform mixed value.
export const MIXED_VALUE = Symbol.for( 'DATAFORM_MIXED_VALUE' );
4 changes: 2 additions & 2 deletions packages/dataviews/src/dataforms-layouts/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
import DataFormContext from '../../components/dataform-context';
import { DataFormLayout } from '../data-form-layout';
import { isCombinedField } from '../is-combined-field';
import { MIXED_VALUE } from '../../constants';

function DropdownHeader( {
title,
Expand Down Expand Up @@ -114,8 +115,7 @@ function PanelDropdown< Item extends object >( {
);

const fieldValue = fieldDefinition.getValue( { item: data } );
const showMixedValue =
isBulkEditing && ( fieldValue === undefined || fieldValue === '' );
const showMixedValue = isBulkEditing && fieldValue === MIXED_VALUE;

return (
<Dropdown
Expand Down
1 change: 1 addition & 0 deletions packages/dataviews/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { VIEW_LAYOUTS } from './dataviews-layouts';
export { filterSortAndPaginate } from './filter-and-sort-data-view';
export type * from './types';
export { isItemValid } from './validation';
export { MIXED_VALUE as DATAFORM_MIXED_VALUE } from './constants';
9 changes: 7 additions & 2 deletions packages/fields/src/fields/title/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { type Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -16,7 +16,12 @@ const titleField: Field< BasePost > = {
id: 'title',
label: __( 'Title' ),
placeholder: __( 'No title' ),
getValue: ( { item } ) => getItemTitle( item ),
getValue: ( { item } ) => {
if ( typeof item.title === 'symbol' ) {
return item.title;
}
return getItemTitle( item );
},
render: TitleView,
enableHiding: false,
};
Expand Down

0 comments on commit c27bd65

Please sign in to comment.