-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from 1 commit
8572fec
3a1f7c6
fcb8cc4
faf901d
5cb2991
5d19c9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 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 |
---|---|---|
|
@@ -53,6 +53,26 @@ export type ValidationContext = { | |
elements?: Option[]; | ||
}; | ||
|
||
/** | ||
* An abstract interface for Field based on the field type. | ||
*/ | ||
export type FieldTypeDefinition< Item > = { | ||
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. 👍 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. |
||
/** | ||
* 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. | ||
|
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.
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.