From 9ff0ba0f0a48155c99d61f3531d067b51cac54c8 Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Tue, 10 Dec 2024 10:38:36 +0100 Subject: [PATCH] Add WithBulkEditing type --- .../src/dataform-controls/datetime.tsx | 4 ++-- .../dataviews/src/dataform-controls/integer.tsx | 4 ++-- .../dataviews/src/dataform-controls/radio.tsx | 4 ++-- .../dataviews/src/dataform-controls/select.tsx | 4 ++-- .../dataviews/src/dataform-controls/text.tsx | 4 ++-- packages/dataviews/src/types.ts | 17 ++++++++--------- packages/fields/src/fields/parent/index.ts | 2 +- .../fields/src/fields/parent/parent-edit.tsx | 2 +- packages/fields/src/fields/password/edit.tsx | 10 ++++++++-- packages/fields/src/fields/slug/index.ts | 2 +- packages/fields/src/fields/slug/slug-edit.tsx | 2 +- packages/fields/src/fields/template/index.ts | 2 +- .../src/fields/template/template-edit.tsx | 2 +- 13 files changed, 32 insertions(+), 27 deletions(-) diff --git a/packages/dataviews/src/dataform-controls/datetime.tsx b/packages/dataviews/src/dataform-controls/datetime.tsx index 53dc2b8187f4ac..f50bc835dfe6e5 100644 --- a/packages/dataviews/src/dataform-controls/datetime.tsx +++ b/packages/dataviews/src/dataform-controls/datetime.tsx @@ -7,14 +7,14 @@ import { useCallback } from '@wordpress/element'; /** * Internal dependencies */ -import type { DataFormControlProps } from '../types'; +import type { DataFormControlProps, WithBulkEditing } from '../types'; export default function DateTime< Item >( { field, onChange, hideLabelFromVision, value, -}: DataFormControlProps< Item > ) { +}: DataFormControlProps< Item > & WithBulkEditing< Item > ) { const { id, label } = field; const onChangeControl = useCallback( diff --git a/packages/dataviews/src/dataform-controls/integer.tsx b/packages/dataviews/src/dataform-controls/integer.tsx index d40bb7dd098bd8..aaa85fdb230640 100644 --- a/packages/dataviews/src/dataform-controls/integer.tsx +++ b/packages/dataviews/src/dataform-controls/integer.tsx @@ -7,14 +7,14 @@ import { useCallback } from '@wordpress/element'; /** * Internal dependencies */ -import type { DataFormControlProps } from '../types'; +import type { DataFormControlProps, WithBulkEditing } from '../types'; export default function Integer< Item >( { field, onChange, hideLabelFromVision, value, -}: DataFormControlProps< Item > ) { +}: DataFormControlProps< Item > & WithBulkEditing< Item > ) { const { id, label, description } = field; const onChangeControl = useCallback( ( newValue: string | undefined ) => diff --git a/packages/dataviews/src/dataform-controls/radio.tsx b/packages/dataviews/src/dataform-controls/radio.tsx index b5aea66d7672a3..4f965a2558ac49 100644 --- a/packages/dataviews/src/dataform-controls/radio.tsx +++ b/packages/dataviews/src/dataform-controls/radio.tsx @@ -7,14 +7,14 @@ import { useCallback } from '@wordpress/element'; /** * Internal dependencies */ -import type { DataFormControlProps } from '../types'; +import type { DataFormControlProps, WithBulkEditing } from '../types'; export default function Radio< Item >( { field, onChange, hideLabelFromVision, value, -}: DataFormControlProps< Item > ) { +}: DataFormControlProps< Item > & WithBulkEditing< Item > ) { const { id, label } = field; const onChangeControl = useCallback( diff --git a/packages/dataviews/src/dataform-controls/select.tsx b/packages/dataviews/src/dataform-controls/select.tsx index eca0323db8f5de..ddb7179ed357a1 100644 --- a/packages/dataviews/src/dataform-controls/select.tsx +++ b/packages/dataviews/src/dataform-controls/select.tsx @@ -8,14 +8,14 @@ import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import type { DataFormControlProps } from '../types'; +import type { DataFormControlProps, WithBulkEditing } from '../types'; export default function Select< Item >( { field, onChange, hideLabelFromVision, value, -}: DataFormControlProps< Item > ) { +}: DataFormControlProps< Item > & WithBulkEditing< Item > ) { const { id, label } = field; const onChangeControl = useCallback( ( newValue: any ) => diff --git a/packages/dataviews/src/dataform-controls/text.tsx b/packages/dataviews/src/dataform-controls/text.tsx index 638aeab484c9c5..00fc603870de60 100644 --- a/packages/dataviews/src/dataform-controls/text.tsx +++ b/packages/dataviews/src/dataform-controls/text.tsx @@ -7,14 +7,14 @@ import { useCallback } from '@wordpress/element'; /** * Internal dependencies */ -import type { DataFormControlProps } from '../types'; +import type { DataFormControlProps, WithBulkEditing } from '../types'; export default function Text< Item >( { field, onChange, hideLabelFromVision, value, -}: DataFormControlProps< Item > ) { +}: DataFormControlProps< Item > & WithBulkEditing< Item > ) { const { id, label, placeholder } = field; const onChangeControl = useCallback( diff --git a/packages/dataviews/src/types.ts b/packages/dataviews/src/types.ts index 6bade32d711e6f..43afb21512a616 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, SupportsBulkEditing extends boolean = true > = { +export type Field< Item > = { /** * Type of the fields. */ @@ -111,9 +111,7 @@ export type Field< Item, SupportsBulkEditing extends boolean = true > = { /** * Callback used to render an edit control for the field. */ - Edit?: - | ComponentType< DataFormControlProps< Item, SupportsBulkEditing > > - | string; + Edit?: ComponentType< DataFormControlProps< Item > > | string; /** * Callback used to sort the field. @@ -186,17 +184,18 @@ export type Fields< Item > = Field< Item >[]; export type Data< Item > = Item[]; -export type DataFormControlProps< - Item, - SupportsBulkEditing extends boolean = true, -> = { - data: SupportsBulkEditing extends true ? Item | Item[] : Item; +export type DataFormControlProps< Item > = { + data: Item; field: NormalizedField< Item >; onChange: ( value: Record< string, any > ) => void; hideLabelFromVision?: boolean; value: any; }; +export type WithBulkEditing< Item > = { + data: Item | Item[]; +}; + export type DataViewRenderFieldProps< Item > = { item: Item; }; diff --git a/packages/fields/src/fields/parent/index.ts b/packages/fields/src/fields/parent/index.ts index 84749b08bd180a..f974608710bf7e 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, false > = { +const parentField: Field< BasePost > = { 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 da8cb6591590af..21cdbee7a365a4 100644 --- a/packages/fields/src/fields/parent/parent-edit.tsx +++ b/packages/fields/src/fields/parent/parent-edit.tsx @@ -285,7 +285,7 @@ export const ParentEdit = ( { data, field, onChange, -}: DataFormControlProps< BasePost, false > ) => { +}: DataFormControlProps< BasePost > ) => { const { id } = field; const homeUrl = useSelect( ( select ) => { diff --git a/packages/fields/src/fields/password/edit.tsx b/packages/fields/src/fields/password/edit.tsx index 3c63be5a2d5967..d6e92e8f5991d5 100644 --- a/packages/fields/src/fields/password/edit.tsx +++ b/packages/fields/src/fields/password/edit.tsx @@ -6,7 +6,10 @@ import { __experimentalVStack as VStack, TextControl, } from '@wordpress/components'; -import type { DataFormControlProps } from '@wordpress/dataviews'; +import type { + DataFormControlProps, + WithBulkEditing, +} from '@wordpress/dataviews'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; @@ -15,7 +18,10 @@ import { __ } from '@wordpress/i18n'; */ import type { BasePost } from '../../types'; -function PasswordEdit( { onChange, value }: DataFormControlProps< BasePost > ) { +function PasswordEdit( { + onChange, + value, +}: DataFormControlProps< BasePost > & WithBulkEditing< BasePost > ) { const [ showPassword, setShowPassword ] = useState( !! value ); const handleTogglePassword = ( newValue: boolean ) => { diff --git a/packages/fields/src/fields/slug/index.ts b/packages/fields/src/fields/slug/index.ts index bfb72655c67ab5..7311b1d10a66f6 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, false > = { +const slugField: Field< BasePost > = { 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 002e20495bdb42..aad6610550069c 100644 --- a/packages/fields/src/fields/slug/slug-edit.tsx +++ b/packages/fields/src/fields/slug/slug-edit.tsx @@ -27,7 +27,7 @@ const SlugEdit = ( { field, onChange, data, -}: DataFormControlProps< BasePost, false > ) => { +}: DataFormControlProps< BasePost > ) => { const { id } = field; const slug = field.getValue( { item: data } ) || getSlug( data ); diff --git a/packages/fields/src/fields/template/index.ts b/packages/fields/src/fields/template/index.ts index 925b9b8ca8fb13..c419adedb537dd 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, false > = { +const templateField: Field< BasePost > = { 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 c94e3be5e958b3..c17364568a4578 100644 --- a/packages/fields/src/fields/template/template-edit.tsx +++ b/packages/fields/src/fields/template/template-edit.tsx @@ -32,7 +32,7 @@ export const TemplateEdit = ( { data, field, onChange, -}: DataFormControlProps< BasePost, false > ) => { +}: DataFormControlProps< BasePost > ) => { const { id } = field; const postType = data.type; const postId =