From 0c67f64725552f0a266a4c3c13ae41e23f6dd4bb Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Mon, 4 Nov 2024 16:59:36 +0100 Subject: [PATCH] Add children support to regular layout --- .../dataform/stories/index.story.tsx | 3 +- .../dataforms-layouts/data-form-layout.tsx | 4 +- .../src/dataforms-layouts/panel/index.tsx | 5 +- .../src/dataforms-layouts/regular/index.tsx | 47 ++++++++++++++++++- packages/dataviews/src/types.ts | 2 + 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/packages/dataviews/src/components/dataform/stories/index.story.tsx b/packages/dataviews/src/components/dataform/stories/index.story.tsx index fecf607b8cfe5..d17e6bd02c600 100644 --- a/packages/dataviews/src/components/dataform/stories/index.story.tsx +++ b/packages/dataviews/src/components/dataform/stories/index.story.tsx @@ -186,12 +186,11 @@ const CombinedFieldsComponent = ( { const form = { fields: [ 'title', - ...( type === 'regular' + ...( type === 'inline' ? [ 'status', 'password' ] : [ { id: 'status', - layout: 'panel', children: [ 'status', 'password' ], }, ] ), diff --git a/packages/dataviews/src/dataforms-layouts/data-form-layout.tsx b/packages/dataviews/src/dataforms-layouts/data-form-layout.tsx index a50cdee7d12e9..bd7d53b431752 100644 --- a/packages/dataviews/src/dataforms-layouts/data-form-layout.tsx +++ b/packages/dataviews/src/dataforms-layouts/data-form-layout.tsx @@ -38,7 +38,9 @@ export function DataFormLayout< Item >( { { fields.map( ( field ) => { const fieldLayoutId = - typeof field === 'string' ? defaultLayout : field.layout; + typeof field !== 'string' && field.layout + ? field.layout + : defaultLayout; const FieldLayout = getFormFieldLayout( fieldLayoutId ?? 'regular' )?.component; diff --git a/packages/dataviews/src/dataforms-layouts/panel/index.tsx b/packages/dataviews/src/dataforms-layouts/panel/index.tsx index b6c42d9d64ba6..46ebc554f0724 100644 --- a/packages/dataviews/src/dataforms-layouts/panel/index.tsx +++ b/packages/dataviews/src/dataforms-layouts/panel/index.tsx @@ -24,6 +24,7 @@ interface FormFieldProps< Item > { data: Item; field: FormField; onChange: ( value: any ) => void; + defaultLayout?: string; } function DropdownHeader( { @@ -60,13 +61,14 @@ export default function FormPanelField< Item >( { data, field, onChange, + defaultLayout, }: FormFieldProps< Item > ) { const { getFieldDefinition } = useContext( DataFormContext ); const fieldDefinition = getFieldDefinition( field ); const childrenFields = useMemo( () => { if ( typeof field !== 'string' && - field.layout === 'panel' && + // field.layout === 'panel' && field.children ) { return field.children; @@ -139,6 +141,7 @@ export default function FormPanelField< Item >( { data={ data } fields={ childrenFields } onChange={ onChange } + defaultLayout={ defaultLayout } > { ( FieldLayout, nestedField ) => ( { data: Item; field: FormField; onChange: ( value: any ) => void; hideLabelFromVision?: boolean; + defaultLayout?: string; +} + +function Header( { title }: { title: string } ) { + return ( + + + + { title } + + + + + ); } export default function FormRegularField< Item >( { @@ -21,13 +42,37 @@ export default function FormRegularField< Item >( { field, onChange, hideLabelFromVision, + defaultLayout, }: FormFieldProps< Item > ) { const { getFieldDefinition } = useContext( DataFormContext ); const fieldDefinition = getFieldDefinition( field ); + const childrenFields = useMemo( () => { + if ( typeof field !== 'string' && field.children ) { + return field.children; + } + return []; + }, [ field ] ); + if ( ! fieldDefinition ) { return null; } + if ( childrenFields.length > 0 ) { + return ( + <> + { ! hideLabelFromVision && ( +
+ ) } + + + ); + } + return (