Repository navigation
Status field: Own the rule that leaves Trash out of the status options #87552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/core-fields/src/post_type_supports/status/edit.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } ) } | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 } ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 onedit) or just more directlyvisibility: cannot-edit. I went for the custom Edit to minimize API changes until more use cases emerge for this kind of thing.