Skip to content

Commit

Permalink
Introduce normalizeFormFields & form.labelPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 19, 2024
1 parent fa872ee commit 3f709f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
24 changes: 10 additions & 14 deletions packages/dataviews/src/dataforms-layouts/data-form-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __experimentalVStack as VStack } from '@wordpress/components';
import { useContext } from '@wordpress/element';
import { useContext, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -11,6 +11,7 @@ import type { Form, FormField, SimpleFormField } from '../types';
import { getFormFieldLayout } from './index';
import DataFormContext from '../components/dataform-context';
import { isCombinedField } from './is-combined-field';
import normalizeFormFields from '../normalize-form-fields';

export function DataFormLayout< Item >( {
data,
Expand Down Expand Up @@ -41,21 +42,16 @@ export function DataFormLayout< Item >( {
);
}

const normalizedFormFields = useMemo(
() => normalizeFormFields( form ),
[ form ]
);

return (
<VStack spacing={ 2 }>
{ form?.fields?.map( ( field ) => {
const formField: FormField =
typeof field !== 'string'
? field
: {
id: field,
};
const fieldLayoutId = formField.layout
? formField.layout
: form.type;
const FieldLayout = getFormFieldLayout(
fieldLayoutId ?? 'regular'
)?.component;
{ normalizedFormFields.map( ( formField ) => {
const FieldLayout = getFormFieldLayout( formField.layout )
?.component;

if ( ! FieldLayout ) {
return null;
Expand Down
42 changes: 42 additions & 0 deletions packages/dataviews/src/normalize-form-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Internal dependencies
*/
import type { Form } from './types';

interface NormalizedFormField {
id: string;
layout: 'regular' | 'panel';
labelPosition: 'side' | 'top' | 'none';
}

export default function normalizeFormFields(
form: Form
): NormalizedFormField[] {
let layout: 'regular' | 'panel' = 'regular';
if ( [ 'regular', 'panel' ].includes( form.type ?? '' ) ) {
layout = form.type as 'regular' | 'panel';
}

const labelPosition =
form.labelPosition ?? ( layout === 'regular' ? 'top' : 'side' );

return ( form.fields ?? [] ).map( ( field ) => {
if ( typeof field === 'string' ) {
return {
id: field,
layout,
labelPosition,
};
}

const fieldLayout = field.layout ?? layout;
const fieldLabelPosition =
field.labelPosition ??
( fieldLayout === 'regular' ? 'top' : 'side' );
return {
...field,
layout: fieldLayout,
labelPosition: fieldLabelPosition,
};
} );
}
3 changes: 2 additions & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export type SimpleFormField = {
};

export type CombinedFormField = {
id?: string;
id: string;
label?: string;
layout?: 'regular' | 'panel';
labelPosition?: 'side' | 'top' | 'none';
Expand All @@ -549,6 +549,7 @@ export type FormField = SimpleFormField | CombinedFormField;
export type Form = {
type?: 'regular' | 'panel';
fields?: Array< FormField | string >;
labelPosition?: 'side' | 'top' | 'none';
};

export interface DataFormProps< Item > {
Expand Down

0 comments on commit 3f709f9

Please sign in to comment.