-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Fix: Support setValue in fields to handle nested data updates in DataViews filters and forms #70989
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,11 @@ export default function FormRegularField< Item >( { | |
return null; | ||
} | ||
if ( labelPosition === 'side' ) { | ||
const handleFieldChange = (value: Record<string, any>) => { | ||
// Use setValue to update the item for this field | ||
const updatedData = fieldDefinition.setValue({ item: data, value: value[fieldDefinition.id] }); | ||
onChange(updatedData); | ||
}; | ||
return ( | ||
<HStack className="dataforms-layouts-regular__field"> | ||
<div | ||
|
@@ -109,7 +114,7 @@ export default function FormRegularField< Item >( { | |
key={ fieldDefinition.id } | ||
data={ data } | ||
field={ fieldDefinition } | ||
onChange={ onChange } | ||
onChange={ handleFieldChange } | ||
hideLabelFromVision | ||
/> | ||
) } | ||
|
@@ -118,6 +123,10 @@ export default function FormRegularField< Item >( { | |
); | ||
} | ||
|
||
const handleFieldChange = (value: Record<string, any>) => { | ||
const updatedData = fieldDefinition.setValue({ item: data, value: value[fieldDefinition.id] }); | ||
onChange(updatedData); | ||
}; | ||
return ( | ||
<div className="dataforms-layouts-regular__field"> | ||
{ fieldDefinition.readOnly === true ? ( | ||
|
@@ -138,7 +147,7 @@ export default function FormRegularField< Item >( { | |
<fieldDefinition.Edit | ||
data={ data } | ||
field={ fieldDefinition } | ||
onChange={ onChange } | ||
onChange={ handleFieldChange } | ||
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. Instead of handling this at the layout-level, it should be handled at the Edit (controls) level. For example, in this control. Fields can provide a custom |
||
hideLabelFromVision={ | ||
labelPosition === 'none' ? true : hideLabelFromVision | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ import type { useFocusOnMount } from '@wordpress/compose'; | |
|
||
export type SortDirection = 'asc' | 'desc'; | ||
|
||
/** | ||
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. What is this comment for? |
||
* Generic option type. | ||
*/ | ||
|
||
/** | ||
* Generic option type. | ||
*/ | ||
|
@@ -108,6 +112,11 @@ export type FieldTypeDefinition< Item > = { | |
* Callback used to sort the field. | ||
*/ | ||
sort: ( a: Item, b: Item, direction: SortDirection ) => number; | ||
/** | ||
* Callback used to set the value of the field in the item. | ||
* Defaults to `{ ...item, [field.id]: value }`. | ||
*/ | ||
setValue?: ( args: { item: Item; value: any } ) => 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. It'd be nice to rename |
||
|
||
/** | ||
* Callback used to validate the field. | ||
|
@@ -242,12 +251,23 @@ export type Field< Item > = { | |
* Defaults to `item[ field.id ]`. | ||
*/ | ||
getValue?: ( args: { item: Item } ) => any; | ||
|
||
/** | ||
* Callback used to set the value of the field in the item. | ||
* Defaults to `{ ...item, [field.id]: value }`. | ||
*/ | ||
setValue?: ( args: { item: Item; value: any } ) => Item; | ||
}; | ||
|
||
export type NormalizedField< Item > = Omit< Field< Item >, 'Edit' > & { | ||
label: string; | ||
header: string | ReactElement; | ||
getValue: ( args: { item: Item } ) => any; | ||
/** | ||
* Callback used to set the value of the field in the item. | ||
* Defaults to `{ ...item, [field.id]: value }`. | ||
*/ | ||
setValue: ( args: { item: Item; value: any } ) => Item; | ||
render: ComponentType< DataViewRenderFieldProps< Item > >; | ||
Edit: ComponentType< DataFormControlProps< Item > > | null; | ||
sort: ( a: Item, b: Item, direction: SortDirection ) => number; | ||
|
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.
At this point, all fields are normalized, and so they'll have a proper
setValue
. Additionally, if we handle this at the controls-level (see comment below), updatedData will be fine already. The only thing we'd need to do is making sure nextValue is calculated from getValue.