Skip to content
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

Add comment_status field to quick edit #64370

Merged
merged 6 commits into from
Aug 9, 2024
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
46 changes: 46 additions & 0 deletions packages/dataviews/src/components/dataform-controls/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import type { ComponentType } from 'react';

/**
* Internal dependencies
*/
import type {
DataFormControlProps,
Field,
FieldTypeDefinition,
} from '../../types';
import radio from './radio';

interface FormControls {
[ key: string ]: ComponentType< DataFormControlProps< any > >;
}

const FORM_CONTROLS: FormControls = {
radio,
};

export function getControl< Item >(
field: Field< Item >,
fieldTypeDefinition: FieldTypeDefinition< Item >
) {
if ( typeof field.Edit === 'function' ) {
return field.Edit;
}

let control;
if ( typeof field.Edit === 'string' ) {
control = getControlByType( field.Edit );
}

return control || fieldTypeDefinition.Edit;
}

export function getControlByType( type: string ) {
if ( Object.keys( FORM_CONTROLS ).includes( type ) ) {
return FORM_CONTROLS[ type ];
}

return null;
}
43 changes: 43 additions & 0 deletions packages/dataviews/src/components/dataform-controls/radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I would move the "text", "radio", "select" and "integer control to. src/controls or something like that. But can be done in a separate PR.

* WordPress dependencies
*/
import { RadioControl } from '@wordpress/components';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import type { DataFormControlProps } from '../../types';

export default function Edit< Item >( {
data,
field,
onChange,
hideLabelFromVision,
}: DataFormControlProps< Item > ) {
const { id, label } = field;
const value = field.getValue( { item: data } );

const onChangeControl = useCallback(
( newValue: string ) =>
onChange( ( prevItem: Item ) => ( {
...prevItem,
[ id ]: newValue,
} ) ),
[ id, onChange ]
);

if ( field.elements ) {
return (
<RadioControl
label={ label }
onChange={ onChangeControl }
options={ field.elements }
selected={ value }
hideLabelFromVision={ hideLabelFromVision }
/>
);
}

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const fields = [
{ value: 2, label: 'John' },
],
},
{
id: 'reviewer',
label: 'Reviewer',
type: 'text' as const,
Edit: 'radio' as const,
elements: [
{ value: 'fulano', label: 'Fulano' },
{ value: 'mengano', label: 'Mengano' },
{ value: 'zutano', label: 'Zutano' },
],
},
{
id: 'status',
label: 'Status',
Expand All @@ -73,12 +84,21 @@ export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
order: 2,
author: 1,
status: 'draft',
reviewer: 'fulano',
date: '2021-01-01T12:00:00',
birthdate: '1950-02-23T12:00:00',
} );

const form = {
fields: [ 'title', 'order', 'author', 'status', 'date', 'birthdate' ],
fields: [
'title',
'order',
'author',
'reviewer',
'status',
'date',
'birthdate',
],
};

return (
Expand Down
3 changes: 2 additions & 1 deletion packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import getFieldTypeDefinition from './field-types';
import type { Field, NormalizedField } from './types';
import { getControl } from './components/dataform-controls';

/**
* Apply default values and normalize the fields config.
Expand Down Expand Up @@ -38,7 +39,7 @@ export function normalizeFields< Item >(
);
};

const Edit = field.Edit || fieldTypeDefinition.Edit;
const Edit = getControl( field, fieldTypeDefinition );

const renderFromElements = ( { item }: { item: Item } ) => {
const value = getValue( { item } );
Expand Down
22 changes: 21 additions & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ export type ValidationContext = {
elements?: Option[];
};

/**
* An abstract interface for Field based on the field type.
*/
export type FieldTypeDefinition< Item > = {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

One thing I'm wondering is that "field types" shouldn't depend on "Item" they should work for any item value of the defined type. So I think we have somethings wrong in the abstraction. sort, isValid and Edit should probably not receive items at all and just receive values. But it's also something that should be solved in a separate PR.

/**
* Callback used to sort the field.
*/
sort: ( a: Item, b: Item, direction: SortDirection ) => number;

/**
* Callback used to validate the field.
*/
isValid: ( item: Item, context?: ValidationContext ) => boolean;

/**
* Callback used to render an edit control for the field.
*/
Edit: ComponentType< DataFormControlProps< Item > >;
};

/**
* A dataview field for a specific property of a data type.
*/
Expand Down Expand Up @@ -90,7 +110,7 @@ export type Field< Item > = {
/**
* Callback used to render an edit control for the field.
*/
Edit?: ComponentType< DataFormControlProps< Item > >;
Edit?: ComponentType< DataFormControlProps< Item > > | 'radio';

/**
* Callback used to sort the field.
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function PostEditForm( { postType, postId } ) {
const { fields } = usePostFields();
const form = {
type: 'panel',
fields: [ 'title', 'author', 'date' ],
fields: [ 'title', 'author', 'date', 'comment_status' ],
};
const [ edits, setEdits ] = useState( initialEdits );
const itemWithEdits = useMemo( () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,32 @@ function usePostFields( viewType ) {
return <time>{ getFormattedDate( item.date ) }</time>;
},
},
{
id: 'comment_status',
oandregal marked this conversation as resolved.
Show resolved Hide resolved
label: __( 'Discussion' ),
type: 'text',
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [],
},
elements: [
{
value: 'open',
label: __( 'Open' ),
description: __(
'Visitors can add new comments and replies.'
),
},
{
value: 'closed',
label: __( 'Closed' ),
description: __(
'Visitors cannot add new comments or replies. Existing comments remain visible.'
),
},
],
},
],
[ authors, viewType, frontPageId, postsPageId ]
);
Expand Down
Loading