-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 bulk editing support by passing array down to the fields #67584
base: trunk
Are you sure you want to change the base?
Changes from all commits
9f2211c
6072d55
572c16a
55b0cdc
5095f59
459c823
39ba9f6
3038717
afffeed
48cb500
bb9b2a6
6c629e6
b9ca481
022ef59
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 |
---|---|---|
|
@@ -26,6 +26,8 @@ import type { | |
import DataFormContext from '../../components/dataform-context'; | ||
import { DataFormLayout } from '../data-form-layout'; | ||
import { isCombinedField } from '../is-combined-field'; | ||
import { MIXED_VALUE } from '../../constants'; | ||
import useFieldValue from '../use-field-value'; | ||
|
||
function DropdownHeader( { | ||
title, | ||
|
@@ -70,7 +72,7 @@ function PanelDropdown< Item >( { | |
fieldDefinition: NormalizedField< Item >; | ||
popoverAnchor: HTMLElement | null; | ||
labelPosition: 'side' | 'top' | 'none'; | ||
data: Item; | ||
data: Item | Item[]; | ||
onChange: ( value: any ) => void; | ||
field: FormField; | ||
} ) { | ||
|
@@ -98,6 +100,8 @@ function PanelDropdown< Item >( { | |
}; | ||
}, [ field ] ); | ||
|
||
const fieldValue = useFieldValue( data, field.id ); | ||
|
||
// Memoize popoverProps to avoid returning a new object every time. | ||
const popoverProps = useMemo( | ||
() => ( { | ||
|
@@ -111,6 +115,8 @@ function PanelDropdown< Item >( { | |
[ popoverAnchor ] | ||
); | ||
|
||
const showMixedValue = Array.isArray( data ) && fieldValue === MIXED_VALUE; | ||
|
||
return ( | ||
<Dropdown | ||
contentClassName="dataforms-layouts-panel__field-dropdown" | ||
|
@@ -138,7 +144,13 @@ function PanelDropdown< Item >( { | |
) } | ||
onClick={ onToggle } | ||
> | ||
<fieldDefinition.render item={ data } /> | ||
{ showMixedValue ? ( | ||
__( 'Mixed' ) | ||
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. In one of the issues, @jasmussen mentions the possibility to render a different "mixed" label/component based on the "type" of the control. So potentially we could defer this to the control/field itself at some point. 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. The context is loosely discussed here. |
||
) : ( | ||
<fieldDefinition.render | ||
item={ Array.isArray( data ) ? data[ 0 ] : data } | ||
/> | ||
) } | ||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DataFormContext from '../components/dataform-context'; | ||
import { MIXED_VALUE } from '../constants'; | ||
|
||
export default function useFieldValue< Item >( | ||
data: Item | Item[], | ||
fieldId: string | ||
) { | ||
const { fields } = useContext( DataFormContext ); | ||
return useMemo( () => { | ||
const fieldDefinition = fields.find( | ||
( fieldDef ) => fieldDef.id === fieldId | ||
); | ||
if ( ! fieldDefinition ) { | ||
return undefined; | ||
} | ||
if ( Array.isArray( data ) ) { | ||
const [ firstRecord, ...remainingRecords ] = data; | ||
const firstValue = fieldDefinition.getValue( { | ||
item: firstRecord, | ||
} ); | ||
const intersects = remainingRecords.every( ( item ) => { | ||
return fieldDefinition.getValue( { item } ) === firstValue; | ||
} ); | ||
return intersects ? firstValue : MIXED_VALUE; | ||
} | ||
return fieldDefinition.getValue( { | ||
item: data, | ||
} ); | ||
}, [ data, fields, fieldId ] ); | ||
} |
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 don't see the check about symbol in other controls like integer, why?
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 will update it, the TimePicker component it-self seemed to handle it gracefully, but its not expected.