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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement radio control
  • Loading branch information
oandregal committed Aug 9, 2024
commit faf901db36f5643f098ea31d35c5511077c847a5
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;
}
18 changes: 1 addition & 17 deletions packages/dataviews/src/field-types/text.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* WordPress dependencies
*/
import {
SelectControl,
TextControl,
RadioControl,
} from '@wordpress/components';
import { SelectControl, TextControl } from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

@@ -53,18 +49,6 @@ function Edit< Item >( {
[ id, onChange ]
);

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

if ( field.elements ) {
const elements = [
/*
3 changes: 2 additions & 1 deletion packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
@@ -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.
@@ -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 } );
27 changes: 21 additions & 6 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
*/
@@ -90,12 +110,7 @@ export type Field< Item > = {
/**
* Callback used to render an edit control for the field.
*/
Edit?: ComponentType< DataFormControlProps< Item > >;

/**
* Optional config for editing the field.
*/
editAs?: 'radio';
Edit?: ComponentType< DataFormControlProps< Item > > | 'radio';

/**
* Callback used to sort the field.
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
@@ -349,7 +349,7 @@ function usePostFields( viewType ) {
id: 'comment_status',
oandregal marked this conversation as resolved.
Show resolved Hide resolved
label: __( 'Discussion' ),
type: 'text',
editAs: 'radio',
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [],