diff --git a/packages/dataviews/src/types.ts b/packages/dataviews/src/types.ts index 2fec464c6accb..6bade32d711e6 100644 --- a/packages/dataviews/src/types.ts +++ b/packages/dataviews/src/types.ts @@ -71,7 +71,7 @@ export type FieldTypeDefinition< Item > = { /** * A dataview field for a specific property of a data type. */ -export type Field< Item > = { +export type Field< Item, SupportsBulkEditing extends boolean = true > = { /** * Type of the fields. */ @@ -111,7 +111,9 @@ export type Field< Item > = { /** * Callback used to render an edit control for the field. */ - Edit?: ComponentType< DataFormControlProps< Item > > | string; + Edit?: + | ComponentType< DataFormControlProps< Item, SupportsBulkEditing > > + | string; /** * Callback used to sort the field. @@ -184,8 +186,11 @@ export type Fields< Item > = Field< Item >[]; export type Data< Item > = Item[]; -export type DataFormControlProps< Item > = { - data: Item | Item[]; +export type DataFormControlProps< + Item, + SupportsBulkEditing extends boolean = true, +> = { + data: SupportsBulkEditing extends true ? Item | Item[] : Item; field: NormalizedField< Item >; onChange: ( value: Record< string, any > ) => void; hideLabelFromVision?: boolean; diff --git a/packages/fields/src/fields/parent/index.ts b/packages/fields/src/fields/parent/index.ts index f974608710bf7..84749b08bd180 100644 --- a/packages/fields/src/fields/parent/index.ts +++ b/packages/fields/src/fields/parent/index.ts @@ -11,7 +11,7 @@ import type { BasePost } from '../../types'; import { ParentEdit } from './parent-edit'; import { ParentView } from './parent-view'; -const parentField: Field< BasePost > = { +const parentField: Field< BasePost, false > = { id: 'parent', type: 'text', label: __( 'Parent' ), diff --git a/packages/fields/src/fields/parent/parent-edit.tsx b/packages/fields/src/fields/parent/parent-edit.tsx index 4179cdb7e5fc5..da8cb6591590a 100644 --- a/packages/fields/src/fields/parent/parent-edit.tsx +++ b/packages/fields/src/fields/parent/parent-edit.tsx @@ -285,9 +285,8 @@ export const ParentEdit = ( { data, field, onChange, -}: DataFormControlProps< BasePost > ) => { +}: DataFormControlProps< BasePost, false > ) => { const { id } = field; - data = Array.isArray( data ) ? data[ 0 ] : data; const homeUrl = useSelect( ( select ) => { // @ts-expect-error getEntityRecord is not typed with unstableBase as argument. diff --git a/packages/fields/src/fields/slug/index.ts b/packages/fields/src/fields/slug/index.ts index 7311b1d10a66f..bfb72655c67ab 100644 --- a/packages/fields/src/fields/slug/index.ts +++ b/packages/fields/src/fields/slug/index.ts @@ -11,7 +11,7 @@ import type { BasePost } from '../../types'; import SlugEdit from './slug-edit'; import SlugView from './slug-view'; -const slugField: Field< BasePost > = { +const slugField: Field< BasePost, false > = { id: 'slug', type: 'text', label: __( 'Slug' ), diff --git a/packages/fields/src/fields/slug/slug-edit.tsx b/packages/fields/src/fields/slug/slug-edit.tsx index 535b7ea1476bb..002e20495bdb4 100644 --- a/packages/fields/src/fields/slug/slug-edit.tsx +++ b/packages/fields/src/fields/slug/slug-edit.tsx @@ -27,9 +27,8 @@ const SlugEdit = ( { field, onChange, data, -}: DataFormControlProps< BasePost > ) => { +}: DataFormControlProps< BasePost, false > ) => { const { id } = field; - data = Array.isArray( data ) ? data[ 0 ] : data; const slug = field.getValue( { item: data } ) || getSlug( data ); const permalinkTemplate = data.permalink_template || ''; diff --git a/packages/fields/src/fields/template/index.ts b/packages/fields/src/fields/template/index.ts index c419adedb537d..925b9b8ca8fb1 100644 --- a/packages/fields/src/fields/template/index.ts +++ b/packages/fields/src/fields/template/index.ts @@ -10,7 +10,7 @@ import { __ } from '@wordpress/i18n'; import type { BasePost } from '../../types'; import { TemplateEdit } from './template-edit'; -const templateField: Field< BasePost > = { +const templateField: Field< BasePost, false > = { id: 'template', type: 'text', label: __( 'Template' ), diff --git a/packages/fields/src/fields/template/template-edit.tsx b/packages/fields/src/fields/template/template-edit.tsx index 56304008f2338..c94e3be5e958b 100644 --- a/packages/fields/src/fields/template/template-edit.tsx +++ b/packages/fields/src/fields/template/template-edit.tsx @@ -32,9 +32,8 @@ export const TemplateEdit = ( { data, field, onChange, -}: DataFormControlProps< BasePost > ) => { +}: DataFormControlProps< BasePost, false > ) => { const { id } = field; - data = Array.isArray( data ) ? data[ 0 ] : data; const postType = data.type; const postId = typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );