-
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
DataForm: Support multiple layouts and introduce the panel layout #64299
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 |
---|---|---|
@@ -1,55 +1,17 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalVStack as VStack } from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeFields } from '../../normalize-fields'; | ||
import type { Field, Form } from '../../types'; | ||
|
||
type DataFormProps< Item > = { | ||
data: Item; | ||
fields: Field< Item >[]; | ||
form: Form; | ||
onChange: Dispatch< SetStateAction< Item > >; | ||
}; | ||
import type { DataFormProps } from '../../types'; | ||
import { getFormLayout } from '../../dataforms-layouts'; | ||
|
||
export default function DataForm< Item >( { | ||
data, | ||
fields, | ||
form, | ||
onChange, | ||
...props | ||
}: DataFormProps< Item > ) { | ||
const visibleFields = useMemo( | ||
() => | ||
normalizeFields( | ||
fields.filter( | ||
( { id } ) => !! form.visibleFields?.includes( id ) | ||
) | ||
), | ||
[ fields, form.visibleFields ] | ||
); | ||
const layout = getFormLayout( form.type ?? 'regular' ); | ||
if ( ! layout ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<VStack spacing={ 4 }> | ||
{ visibleFields.map( ( field ) => { | ||
return ( | ||
<field.Edit | ||
key={ field.id } | ||
data={ data } | ||
field={ field } | ||
onChange={ onChange } | ||
/> | ||
); | ||
} ) } | ||
</VStack> | ||
); | ||
return <layout.component form={ form } { ...props } />; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import FormRegular from './regular'; | ||
import FormPanel from './panel'; | ||
|
||
const FORM_LAYOUTS = [ | ||
{ | ||
type: 'regular', | ||
component: FormRegular, | ||
}, | ||
{ | ||
type: 'panel', | ||
component: FormPanel, | ||
}, | ||
]; | ||
|
||
export function getFormLayout( type: string ) { | ||
return FORM_LAYOUTS.find( ( layout ) => layout.type === type ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalVStack as VStack, | ||
__experimentalHStack as HStack, | ||
__experimentalHeading as Heading, | ||
__experimentalSpacer as Spacer, | ||
Dropdown, | ||
Button, | ||
} from '@wordpress/components'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { sprintf, __ } from '@wordpress/i18n'; | ||
import { closeSmall } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeFields } from '../../normalize-fields'; | ||
import type { DataFormProps, NormalizedField } from '../../types'; | ||
|
||
interface FormFieldProps< Item > { | ||
data: Item; | ||
field: NormalizedField< Item >; | ||
onChange: ( value: any ) => void; | ||
} | ||
|
||
function DropdownHeader( { | ||
title, | ||
onClose, | ||
}: { | ||
title: string; | ||
onClose: () => void; | ||
} ) { | ||
return ( | ||
<VStack | ||
className="dataforms-layouts-panel__dropdown-header" | ||
spacing={ 4 } | ||
> | ||
<HStack alignment="center"> | ||
<Heading level={ 2 } size={ 13 }> | ||
{ title } | ||
</Heading> | ||
<Spacer /> | ||
{ onClose && ( | ||
<Button | ||
className="dataforms-layouts-panel__dropdown-header-action" | ||
label={ __( 'Close' ) } | ||
icon={ closeSmall } | ||
onClick={ onClose } | ||
/> | ||
) } | ||
</HStack> | ||
</VStack> | ||
); | ||
} | ||
|
||
function FormField< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
}: FormFieldProps< Item > ) { | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ popoverAnchor, setPopoverAnchor ] = useState< HTMLElement | null >( | ||
null | ||
); | ||
// Memoize popoverProps to avoid returning a new object every time. | ||
const popoverProps = useMemo( | ||
() => ( { | ||
// Anchor the popover to the middle of the entire row so that it doesn't | ||
// move around when the label changes. | ||
anchor: popoverAnchor, | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
} ), | ||
[ popoverAnchor ] | ||
); | ||
|
||
return ( | ||
<HStack | ||
ref={ setPopoverAnchor } | ||
className="dataforms-layouts-panel__field" | ||
> | ||
<div className="dataforms-layouts-panel__field-label"> | ||
{ field.label } | ||
</div> | ||
<div> | ||
<Dropdown | ||
contentClassName="dataforms-layouts-panel__field-dropdown" | ||
popoverProps={ popoverProps } | ||
focusOnMount | ||
toggleProps={ { | ||
size: 'compact', | ||
variant: 'tertiary', | ||
tooltipPosition: 'middle left', | ||
} } | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
className="dataforms-layouts-panel__field-control" | ||
size="compact" | ||
variant="tertiary" | ||
aria-expanded={ isOpen } | ||
aria-label={ sprintf( | ||
// translators: %s: Field name. | ||
__( 'Edit %s' ), | ||
field.label | ||
) } | ||
onClick={ onToggle } | ||
> | ||
<field.render item={ data } /> | ||
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. Haven't yet looked deeply into the code but isn't this where we render the item for the panel? I wonder why items with Gravacao.do.ecra.2024-08-07.as.14.19.10.movThere 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 the storybook, none of those fields provide a 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.
Indeed that could be a good improvement. 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. Fix at #64338 |
||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
<> | ||
<DropdownHeader | ||
title={ field.label } | ||
onClose={ onClose } | ||
/> | ||
<field.Edit | ||
key={ field.id } | ||
data={ data } | ||
field={ field } | ||
onChange={ onChange } | ||
hideLabelFromVision | ||
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. Should this be part of the form config? With @oandregal's comment, I'm not sure if the 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.
|
||
/> | ||
</> | ||
) } | ||
/> | ||
</div> | ||
</HStack> | ||
); | ||
} | ||
|
||
export default function FormPanel< Item >( { | ||
data, | ||
fields, | ||
form, | ||
onChange, | ||
}: DataFormProps< Item > ) { | ||
const visibleFields = useMemo( | ||
() => | ||
normalizeFields( | ||
fields.filter( ( { id } ) => !! form.fields?.includes( id ) ) | ||
), | ||
[ fields, form.fields ] | ||
); | ||
|
||
return ( | ||
<VStack spacing={ 2 }> | ||
{ visibleFields.map( ( field ) => { | ||
return ( | ||
<FormField | ||
key={ field.id } | ||
data={ data } | ||
field={ field } | ||
onChange={ onChange } | ||
/> | ||
); | ||
} ) } | ||
</VStack> | ||
); | ||
} |
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.
The "size" definitely impacts the heading rendering, so it's a valid
HeadingProps
. cc @ciampo