Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core-fields/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Expand Down
56 changes: 56 additions & 0 deletions packages/core-fields/src/post_type_supports/status/edit.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<RadioGroupControl
label={ label }
// The control describes itself with plain text only.
description={
typeof description === 'string' ? description : undefined
}
hideLabelFromVision={ hideLabelFromVision }
items={ items }
value={ getValue( { item: data } ) ?? '' }
onValueChange={ ( value ) =>
onChange( setValue( { item: data, value } ) )
}
disabled={ field.isDisabled( { item: data, field } ) }
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
/**
* The status of a post, for every post type but the design ones. Its
* JavaScript parts (`getValue`, `render`, and `isDisabled`) are in
* `field.tsx`, next to this file; the render adds the icon of each status.
* JavaScript parts (`getValue`, `setValue`, `render`, `Edit`, and
* `isDisabled`) are in `field.tsx`, next to this file; the render adds the
* icon of each status, and `Edit` leaves out the statuses that can't be
* picked.
*
* Custom statuses are not supported, see
* https://github.com/WordPress/gutenberg/issues/55886.
Expand All @@ -13,7 +15,6 @@
return array(
'type' => 'text',
'label' => __( 'Status' ),
'Edit' => 'radio',

@oandregal oandregal Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's potential to do this differently: instead of implementing a custom Edit, we could pass some meta info to the elements to control where each element is displayed. For example, something along the lines of visibility: [ 'filter', 'view' ] (to hide the element on edit) or just more directly visibility: cannot-edit. I went for the custom Edit to minimize API changes until more use cases emerge for this kind of thing.

'elements' => array(
array(
'value' => 'draft',
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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' ),
};
71 changes: 71 additions & 0 deletions packages/core-fields/src/test/status-edit.jsdom.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<StatusEdit field={ field } data={ post } onChange={ vi.fn() } />
);
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(
<StatusEdit field={ field } data={ post } onChange={ onChange } />
);
fireEvent.click( screen.getByRole( 'radio', { name: 'Published' } ) );
expect( onChange ).toHaveBeenCalledWith( { status: 'publish' } );
} );

it( 'is disabled for a user who cannot publish', () => {
render(
<StatusEdit
field={ field }
data={ { status: 'draft', _links: {} } }
onChange={ vi.fn() }
/>
);
for ( const radio of screen.getAllByRole( 'radio' ) ) {
expect( radio ).toHaveAttribute( 'aria-disabled', 'true' );
}
} );
} );

describe( 'StatusView', () => {
it( 'still labels a trashed post', () => {
render( <StatusView field={ field } item={ { status: 'trash' } } /> );
expect( screen.getByText( 'Trash' ) ).toBeInTheDocument();
} );
} );
20 changes: 2 additions & 18 deletions packages/edit-site/src/components/post-list/quick-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -73,23 +73,7 @@ export function QuickEditModal( {
const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreDataStore );

const _fields = usePostFields( { postType } );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was great to achieve.

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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions routes/post-list/quick-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading