Skip to content

Commit

Permalink
Replace inline with labelPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Nov 5, 2024
1 parent 0c67f64 commit 4cd314b
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 180 deletions.
104 changes: 70 additions & 34 deletions packages/dataviews/src/components/dataform/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useMemo, useState } from '@wordpress/element';
import { ToggleControl } from '@wordpress/components';

/**
Expand Down Expand Up @@ -29,7 +29,12 @@ const meta = {
control: { type: 'select' },
description:
'Chooses the default layout of each field. "regular" is the default layout.',
options: [ 'regular', 'panel', 'inline' ],
options: [ 'regular', 'panel' ],
},
labelPosition: {
control: { type: 'select' },
description: 'Chooses the label position of the layout.',
options: [ 'default', 'top', 'side' ],
},
},
};
Expand Down Expand Up @@ -120,8 +125,10 @@ const fields = [

export const Default = ( {
type,
labelPosition,
}: {
type: 'panel' | 'regular' | 'inline';
type: 'panel' | 'regular';
labelPosition: 'default' | 'top' | 'side';
} ) => {
const [ post, setPost ] = useState( {
title: 'Hello, World!',
Expand All @@ -134,21 +141,37 @@ export const Default = ( {
sticky: false,
} );

const form = {
fields: [
'title',
'order',
{
id: 'sticky',
layout: type === 'regular' ? 'regular' : 'inline',
},
'author',
'reviewer',
'password',
'date',
'birthdate',
],
} as Form;
const form = useMemo(
() => ( {
fields: [
'title',
'order',
{
id: 'sticky',
layout: 'regular',
labelPosition: type === 'regular' ? labelPosition : 'side',
},
'author',
'reviewer',
'password',
'date',
'birthdate',
].map( ( field ) => {
if (
labelPosition !== 'default' &&
typeof field === 'string'
) {
return {
id: field,
layout: type,
labelPosition,
};
}
return field;
} ),
} ),
[ type, labelPosition ]
) as Form;

return (
<DataForm< SamplePost >
Expand All @@ -170,8 +193,10 @@ export const Default = ( {

const CombinedFieldsComponent = ( {
type = 'regular',
labelPosition,
}: {
type: 'panel' | 'regular' | 'inline';
type: 'panel' | 'regular';
labelPosition: 'default' | 'top' | 'side';
} ) => {
const [ post, setPost ] = useState< SamplePost >( {
title: 'Hello, World!',
Expand All @@ -183,21 +208,32 @@ const CombinedFieldsComponent = ( {
birthdate: '1950-02-23T12:00:00',
} );

const form = {
fields: [
'title',
...( type === 'inline'
? [ 'status', 'password' ]
: [
{
id: 'status',
children: [ 'status', 'password' ],
},
] ),
'order',
'author',
],
} as Form;
const form = useMemo(
() => ( {
fields: [
'title',
{
id: 'status',
children: [ 'status', 'password' ],
},
'order',
'author',
].map( ( field ) => {
if (
labelPosition !== 'default' &&
typeof field === 'string'
) {
return {
id: field,
layout: type,
labelPosition,
};
}
return field;
} ),
} ),
[ type, labelPosition ]
) as Form;

return (
<DataForm< SamplePost >
Expand Down
5 changes: 0 additions & 5 deletions packages/dataviews/src/dataforms-layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import FormRegularField from './regular';
import FormPanelField from './panel';
import FormInlineField from './inline';

const FORM_FIELD_LAYOUTS = [
{
Expand All @@ -14,10 +13,6 @@ const FORM_FIELD_LAYOUTS = [
type: 'panel',
component: FormPanelField,
},
{
type: 'inline',
component: FormInlineField,
},
];

export function getFormFieldLayout( type: string ) {
Expand Down
45 changes: 0 additions & 45 deletions packages/dataviews/src/dataforms-layouts/inline/index.tsx

This file was deleted.

Loading

0 comments on commit 4cd314b

Please sign in to comment.