diff --git a/packages/core-fields/CHANGELOG.md b/packages/core-fields/CHANGELOG.md index 4e3681513bace8..7a316a4fa19497 100644 --- a/packages/core-fields/CHANGELOG.md +++ b/packages/core-fields/CHANGELOG.md @@ -8,6 +8,7 @@ ### Enhancements +- Status field: Leave the trash out of the status options, so forms no longer have to filter it out ([#87552](https://github.com/WordPress/gutenberg/pull/87552)). - Template field: Disable the field for the front page and the posts page, whose template comes from the reading settings ([#87541](https://github.com/WordPress/gutenberg/pull/87541)). - Author fields: Show initials when the author's profile image is unavailable ([#85208](https://github.com/WordPress/gutenberg/pull/85208)). diff --git a/packages/core-fields/src/post_type_supports/status/edit.tsx b/packages/core-fields/src/post_type_supports/status/edit.tsx new file mode 100644 index 00000000000000..14023df3e2cafa --- /dev/null +++ b/packages/core-fields/src/post_type_supports/status/edit.tsx @@ -0,0 +1,56 @@ +import { useMemo } from '@wordpress/element'; +import type { DataFormControlProps } from '@wordpress/dataviews'; +import { RadioGroupControl } from '@wordpress/ui'; +import type { PostWithStatus } from './types'; + +/* + * The statuses that can't be picked. A post goes to the trash through the + * "Move to trash" action, not by editing its status, yet the field keeps the + * `trash` element so that filters and views can label a trashed post. + */ +const STATUSES_NOT_EDITABLE = [ 'trash' ]; + +/* + * The status control: a radio group of the field's elements, without the + * statuses that can't be picked. + */ +export default function StatusEdit( { + data, + field, + onChange, + hideLabelFromVision, +}: DataFormControlProps< PostWithStatus > ) { + const { label, description, elements, getValue, setValue } = field; + const items = useMemo( + () => + ( elements ?? [] ) + .filter( + ( element ) => + ! STATUSES_NOT_EDITABLE.includes( + String( element.value ) + ) + ) + .map( ( element ) => ( { + value: String( element.value ), + label: element.label, + description: element.description, + } ) ), + [ elements ] + ); + return ( + + onChange( setValue( { item: data, value } ) ) + } + disabled={ field.isDisabled( { item: data, field } ) } + /> + ); +} diff --git a/packages/core-fields/src/post_type_supports/status/field.php b/packages/core-fields/src/post_type_supports/status/field.php index 00ca88fab20535..c7c81eac8ab43f 100644 --- a/packages/core-fields/src/post_type_supports/status/field.php +++ b/packages/core-fields/src/post_type_supports/status/field.php @@ -1,8 +1,10 @@ 'text', 'label' => __( 'Status' ), - 'Edit' => 'radio', 'elements' => array( array( 'value' => 'draft', diff --git a/packages/core-fields/src/post_type_supports/status/field.tsx b/packages/core-fields/src/post_type_supports/status/field.tsx index 5c6b73c4ef206e..71c9b30870c5af 100644 --- a/packages/core-fields/src/post_type_supports/status/field.tsx +++ b/packages/core-fields/src/post_type_supports/status/field.tsx @@ -1,6 +1,7 @@ import { isInTheFuture } from '@wordpress/date'; import type { FieldsScriptParts } from '../../types'; import { hasActionLink } from '../../shared/has-action-link'; +import StatusEdit from './edit'; import type { PostWithStatus } from './types'; import StatusView from './view'; @@ -31,6 +32,7 @@ export const fieldExtensions: FieldsScriptParts< PostWithStatus >[ string ] = { return edits; }, render: StatusView, + Edit: StatusEdit, // A user who can't publish can't change the status either. isDisabled: ( { item } ) => ! hasActionLink( item, 'wp:action-publish' ), }; diff --git a/packages/core-fields/src/test/status-edit.jsdom.test.tsx b/packages/core-fields/src/test/status-edit.jsdom.test.tsx new file mode 100644 index 00000000000000..1327eb6b28fffa --- /dev/null +++ b/packages/core-fields/src/test/status-edit.jsdom.test.tsx @@ -0,0 +1,71 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import StatusEdit from '../post_type_supports/status/edit'; +import { fieldExtensions } from '../post_type_supports/status/field'; +import StatusView from '../post_type_supports/status/view'; +import type { PostWithStatus } from '../post_type_supports/status/types'; + +const elements = [ + { value: 'draft', label: 'Draft' }, + { value: 'publish', label: 'Published' }, + { value: 'trash', label: 'Trash' }, +]; + +const field = { + id: 'status', + label: 'Status', + elements, + getValue: fieldExtensions.getValue!, + setValue: fieldExtensions.setValue!, + isDisabled: fieldExtensions.isDisabled!, +} as any; + +const post: PostWithStatus = { + status: 'draft', + _links: { 'wp:action-publish': [ { href: '' } ] }, +}; + +describe( 'StatusEdit', () => { + it( 'offers every status but the trash', () => { + render( + + ); + expect( screen.getAllByRole( 'radio' ) ).toHaveLength( 2 ); + expect( + screen.getByRole( 'radio', { name: 'Published' } ) + ).toBeInTheDocument(); + expect( + screen.queryByRole( 'radio', { name: 'Trash' } ) + ).not.toBeInTheDocument(); + expect( screen.getByRole( 'radio', { name: 'Draft' } ) ).toBeChecked(); + } ); + + it( 'sends the change through the field', () => { + const onChange = vi.fn(); + render( + + ); + fireEvent.click( screen.getByRole( 'radio', { name: 'Published' } ) ); + expect( onChange ).toHaveBeenCalledWith( { status: 'publish' } ); + } ); + + it( 'is disabled for a user who cannot publish', () => { + render( + + ); + for ( const radio of screen.getAllByRole( 'radio' ) ) { + expect( radio ).toHaveAttribute( 'aria-disabled', 'true' ); + } + } ); +} ); + +describe( 'StatusView', () => { + it( 'still labels a trashed post', () => { + render( ); + expect( screen.getByText( 'Trash' ) ).toBeInTheDocument(); + } ); +} ); diff --git a/packages/edit-site/src/components/post-list/quick-edit-modal.tsx b/packages/edit-site/src/components/post-list/quick-edit-modal.tsx index 42ff553f3371e9..9ef61bc158fa1a 100644 --- a/packages/edit-site/src/components/post-list/quick-edit-modal.tsx +++ b/packages/edit-site/src/components/post-list/quick-edit-modal.tsx @@ -3,7 +3,7 @@ import { useDispatch, useSelect } from '@wordpress/data'; import { store as coreDataStore } from '@wordpress/core-data'; import type { Post } from '@wordpress/core-data'; import { DataForm } from '@wordpress/dataviews'; -import type { Field, Form } from '@wordpress/dataviews'; +import type { Form } from '@wordpress/dataviews'; import { Button, Modal, @@ -73,23 +73,7 @@ export function QuickEditModal( { const { editEntityRecord, saveEditedEntityRecord } = useDispatch( coreDataStore ); - const _fields = usePostFields( { postType } ); - const fields = useMemo( - () => - _fields?.map( ( field: Field< Post > ) => { - if ( field.id === 'status' ) { - return { - ...field, - elements: field.elements?.filter( - ( element ) => element.value !== 'trash' - ), - }; - } - - return field; - } ), - [ _fields ] - ); + const fields = usePostFields( { postType } ); const form = useMemo( (): Form => { if ( ! quickEditForm ) { diff --git a/packages/editor/src/components/sidebar/dataform-post-summary.jsx b/packages/editor/src/components/sidebar/dataform-post-summary.jsx index 2a8a00f454f4a9..f97d60423c5a28 100644 --- a/packages/editor/src/components/sidebar/dataform-post-summary.jsx +++ b/packages/editor/src/components/sidebar/dataform-post-summary.jsx @@ -296,14 +296,6 @@ export default function DataFormPostSummary( { onActionPerformed } ) { ENTITIES[ postType ]?.[ namespace ]?.isVisible ); } - if ( field.id === 'status' ) { - return { - ...field, - elements: field.elements.filter( - ( element ) => element.value !== 'trash' - ), - }; - } if ( field.id === 'template' ) { // `usePostTemplatePanelMode` is reused in the Post Template panel to match // the existing behavior. If the panel rendered nothing we should exclude the diff --git a/routes/post-list/quick-edit-modal.tsx b/routes/post-list/quick-edit-modal.tsx index 2cae95e49a2fb2..78450250a3727d 100644 --- a/routes/post-list/quick-edit-modal.tsx +++ b/routes/post-list/quick-edit-modal.tsx @@ -111,15 +111,6 @@ export function QuickEditModal( { const fields = useMemo( () => _fields?.map( ( field: any ) => { - if ( field.id === 'status' ) { - return { - ...field, - elements: field.elements.filter( - ( element: { value: string } ) => - element.value !== 'trash' - ), - }; - } if ( field.id === 'featured_media' && field.Edit ) { return { ...field,