Skip to content
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 - Add combined fields support #65399

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions packages/dataviews/src/components/dataform-combined-edit/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* WordPress dependencies
*/
import {
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalHeading as Heading,
__experimentalSpacer as Spacer,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import type { DataFormCombinedEditProps, NormalizedField } from '../../types';

function Header( { title }: { title: string } ) {
return (
<VStack className="dataforms-layouts__dropdown-header" spacing={ 4 }>
<HStack alignment="center">
<Heading level={ 2 } size={ 13 }>
{ title }
</Heading>
<Spacer />
</HStack>
</VStack>
);
}

function DataFormCombinedEdit< Item >( {
field,
data,
onChange,
hideLabelFromVision,
}: DataFormCombinedEditProps< Item > ) {
const className = 'dataforms-combined-edit';
const visibleChildren = ( field.children ?? [] )
.map( ( fieldId ) => field.fields.find( ( { id } ) => id === fieldId ) )
.filter(
( childField ): childField is NormalizedField< Item > =>
!! childField
);
const children = visibleChildren.map( ( child, index ) => {
return (
<div className="dataforms-combined-edit__field" key={ child.id }>
{ index !== 0 && hideLabelFromVision && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary?

<Header title={ child.label } />
) }
<child.Edit
data={ data }
field={ child }
onChange={ onChange }
hideLabelFromVision={ hideLabelFromVision }
/>
</div>
);
} );

const Stack = field.direction === 'horizontal' ? HStack : VStack;

return (
<>
{ ! hideLabelFromVision && <Header title={ field.label } /> }
<Stack spacing={ 4 } className={ className }>
{ children }
</Stack>
</>
);
}

export default DataFormCombinedEdit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dataforms-layouts-panel__field-dropdown {
.dataforms-combined-edit {
&__field:not(:first-child) {
border-top: $border-width solid $gray-200;
padding-top: $grid-unit-20;
}
}
}
47 changes: 47 additions & 0 deletions packages/dataviews/src/components/dataform/stories/index.story.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a minor, but I noticed that the panel to customize props doesn't appear on the new story:

image

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useState } from '@wordpress/element';
* Internal dependencies
*/
import DataForm from '../index';
import type { CombinedFormField } from '../../../types';

const meta = {
title: 'DataViews/DataForm',
Expand Down Expand Up @@ -76,6 +77,11 @@ const fields = [
{ value: 'published', label: 'Published' },
],
},
{
id: 'password',
label: 'Password',
type: 'text' as const,
},
];

export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
Expand Down Expand Up @@ -118,3 +124,44 @@ export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
/>
);
};

export const CombinedFields = ( { type }: { type: 'panel' | 'regular' } ) => {
const [ post, setPost ] = useState( {
title: 'Hello, World!',
order: 2,
author: 1,
status: 'draft',
} );

const form = {
fields: [ 'title', 'status_and_visibility', 'order', 'author' ],
layout: {
combinedFields: [
{
id: 'status_and_visibility',
label: 'Status & Visibility',
children: [ 'status', 'password' ],
direction: 'vertical',
render: ( { item } ) => item.status,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function about?

The thing that I'm wondering personally is whether for "forms" these are combined fields or more things like "groups"...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having more example of how this translates in actual forms and panels would help make the right abstraction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function about?

Its suppose to be the same as the render function of the field config, but its acting more like the getValue function.

The thing that I'm wondering personally is whether for "forms" these are combined fields or more things like "groups"...
I think having more example of how this translates in actual forms and panels would help make the right abstraction.

Groups would probably fit this use case better, the main example is the status field that includes the status radio control, posts schedule, the password field and also the sticky checkbox:

password-conditional.mp4

We could also create a single status field that includes all of this logic, we did have to remove the radio dependency here:

{
label: __( 'Status' ),
id: 'status',
type: 'text',
elements: STATUSES,
render: PostStatusField,
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [ OPERATOR_IS_ANY ],
},
},

This is mostly a use case for the panel view. For the regular form one could render multiple regular data forms to create the grouping. Although there would be some additional use cases like the column use case for displaying two fields beside each other ( we have a similar use case in the product form for regular and sale price ):
Screenshot 2024-09-18 at 10 35 38 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing that I'm wondering personally is whether for "forms" these are combined fields or more things like "groups"...

Would you envision "groups" to be like field types that accept children, or act in a similar manner as the combinedField API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its suppose to be the same as the render function of the field config, but its acting more like the getValue function.

Do you think we can guess this from the "fields" or is the "render" function mandatory here? I'm also curious how the "combined fields" or "groups" feature translate in regular forms. What's the output there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can guess this from the "fields" or is the "render" function mandatory here?

In this specific example it only shows the status value in the panel view so the render or a getValue would be mandatory.
If we wanted, we could guess it from the fields and show a comma delimited list of the nested field values.
The panel view is an exception here as it separates the edit view from the visual.

I'm also curious how the "combined fields" or "groups" feature translate in regular forms. What's the output there?

In regular forms the main difference would be a title for the grouped fields and the option to display them in a column layout. Similar to the JSON Forms layout examples: https://jsonforms.io/examples/layouts/#group

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In regular forms the main difference would be a title for the grouped fields and the option to display them in a column layout. Similar to the JSON Forms layout examples: https://jsonforms.io/examples/layouts/#group

I think that makes sense. Is it already working in this PR? I think my main concern here is to avoid special cases for layouts (panel or default)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't update the default data form yet, let me update it to support that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to support the regular data form type as well as part of this commit: 42131f3

},
] as CombinedFormField< any >[],
},
};

return (
<DataForm
data={ post }
fields={ fields }
form={ {
...form,
type,
} }
onChange={ ( edits ) =>
setPost( ( prev ) => ( {
...prev,
...edits,
} ) )
}
/>
);
};
29 changes: 29 additions & 0 deletions packages/dataviews/src/dataforms-layouts/get-visible-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Internal dependencies
*/
import { normalizeCombinedFields } from '../normalize-fields';
import type {
Field,
CombinedFormField,
NormalizedCombinedFormField,
} from '../types';

export function getVisibleFields(
fields: Field< any >[],
formFields: string[] = [],
combinedFields?: CombinedFormField< any >[]
): Field< any >[] {
const visibleFields: Array<
Field< any > | NormalizedCombinedFormField< any >
> = [ ...fields ];
if ( combinedFields ) {
visibleFields.push(
...normalizeCombinedFields( combinedFields, fields )
);
}
return formFields
.map( ( fieldId ) =>
visibleFields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< any > => !! field );
}
Comment on lines +11 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest maintaining consistency with the current typing by using Item instead of any.

Suggested change
export function getVisibleFields(
fields: Field< any >[],
formFields: string[] = [],
combinedFields?: CombinedFormField< any >[]
): Field< any >[] {
const visibleFields: Array<
Field< any > | NormalizedCombinedFormField< any >
> = [ ...fields ];
if ( combinedFields ) {
visibleFields.push(
...normalizeCombinedFields( combinedFields, fields )
);
}
return formFields
.map( ( fieldId ) =>
visibleFields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< any > => !! field );
}
export function getVisibleFields<Item>(
fields: Field< Item >[],
formFields: string[] = [],
combinedFields?: CombinedFormField< Item >[]
): Field< Item >[] {
const visibleFields: Array<
Field< Item > | NormalizedCombinedFormField< Item >
> = [ ...fields ];
if ( combinedFields ) {
visibleFields.push(
...normalizeCombinedFields( combinedFields, fields )
);
}
return formFields
.map( ( fieldId ) =>
visibleFields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< Item > => !! field );
}

15 changes: 8 additions & 7 deletions packages/dataviews/src/dataforms-layouts/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { closeSmall } from '@wordpress/icons';
* Internal dependencies
*/
import { normalizeFields } from '../../normalize-fields';
import type { DataFormProps, NormalizedField, Field } from '../../types';
import { getVisibleFields } from '../get-visible-fields';
import type { DataFormProps, NormalizedField } from '../../types';

interface FormFieldProps< Item > {
data: Item;
Expand Down Expand Up @@ -144,13 +145,13 @@ export default function FormPanel< Item >( {
const visibleFields = useMemo(
() =>
normalizeFields(
( form.fields ?? [] )
.map( ( fieldId ) =>
fields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< Item > => !! field )
getVisibleFields(
fields,
form.fields,
form.layout?.combinedFields
)
),
[ fields, form.fields ]
[ fields, form.fields, form.layout?.combinedFields ]
);

return (
Expand Down
15 changes: 8 additions & 7 deletions packages/dataviews/src/dataforms-layouts/regular/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useMemo } from '@wordpress/element';
* Internal dependencies
*/
import { normalizeFields } from '../../normalize-fields';
import type { DataFormProps, Field } from '../../types';
import { getVisibleFields } from '../get-visible-fields';
import type { DataFormProps } from '../../types';

export default function FormRegular< Item >( {
data,
Expand All @@ -19,13 +20,13 @@ export default function FormRegular< Item >( {
const visibleFields = useMemo(
() =>
normalizeFields(
( form.fields ?? [] )
.map( ( fieldId ) =>
fields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< Item > => !! field )
getVisibleFields(
fields,
form.fields,
form.layout?.combinedFields
)
),
[ fields, form.fields ]
[ fields, form.fields, form.layout?.combinedFields ]
);

return (
Expand Down
34 changes: 33 additions & 1 deletion packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
* Internal dependencies
*/
import getFieldTypeDefinition from './field-types';
import type { Field, NormalizedField } from './types';
import type {
CombinedFormField,
Field,
NormalizedField,
NormalizedCombinedFormField,
} from './types';
import { getControl } from './dataform-controls';
import DataFormCombinedEdit from './components/dataform-combined-edit';

/**
* Apply default values and normalize the fields config.
Expand Down Expand Up @@ -66,3 +72,29 @@ export function normalizeFields< Item >(
};
} );
}

/**
* Apply default values and normalize the fields config.
*
* @param combinedFields combined field list.
* @param fields Fields config.
* @return Normalized fields config.
*/
export function normalizeCombinedFields< Item >(
combinedFields: CombinedFormField< Item >[],
fields: Field< Item >[]
): NormalizedCombinedFormField< Item >[] {
return combinedFields.map( ( combinedField ) => {
return {
...combinedField,
Edit: DataFormCombinedEdit,
fields: normalizeFields(
combinedField.children
.map( ( fieldId ) =>
fields.find( ( { id } ) => id === fieldId )
)
.filter( ( field ): field is Field< any > => !! field )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter( ( field ): field is Field< any > => !! field )
.filter( ( field ): field is Field< Item > => !! field )

),
};
} );
}
1 change: 1 addition & 0 deletions packages/dataviews/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import "./components/dataviews-item-actions/style.scss";
@import "./components/dataviews-selection-checkbox/style.scss";
@import "./components/dataviews-view-config/style.scss";
@import "./components/dataform-combined-edit/style.scss";

@import "./dataviews-layouts/grid/style.scss";
@import "./dataviews-layouts/list/style.scss";
Expand Down
40 changes: 31 additions & 9 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,6 @@ export type Fields< Item > = Field< Item >[];

export type Data< Item > = Item[];

/**
* The form configuration.
*/
export type Form = {
type?: 'regular' | 'panel';
fields?: string[];
};

export type DataFormControlProps< Item > = {
data: Item;
field: NormalizedField< Item >;
Expand Down Expand Up @@ -524,9 +516,39 @@ export interface SupportedLayouts {
table?: Omit< ViewTable, 'type' >;
}

export interface CombinedFormField< Item > extends CombinedField {
render?: ComponentType< { item: Item } >;
}

export interface DataFormCombinedEditProps< Item > {
field: NormalizedCombinedFormField< Item >;
data: Item;
onChange: ( value: Record< string, any > ) => void;
hideLabelFromVision?: boolean;
}

export type NormalizedCombinedFormField< Item > = CombinedFormField< Item > & {
fields: NormalizedField< Item >[];
Edit?: ComponentType< DataFormCombinedEditProps< Item > >;
};

/**
* The form configuration.
*/
export type Form< Item > = {
type?: 'regular' | 'panel';
fields?: string[];
layout?: {
/**
* The fields to combine.
*/
combinedFields?: CombinedFormField< Item >[];
};
};

export interface DataFormProps< Item > {
data: Item;
fields: Field< Item >[];
form: Form;
form: Form< Item >;
onChange: ( value: Record< string, any > ) => void;
}
2 changes: 1 addition & 1 deletion packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Field, Form } from './types';
export function isItemValid< Item >(
item: Item,
fields: Field< Item >[],
form: Form
form: Form< Item >
): boolean {
const _fields = normalizeFields(
fields.filter( ( { id } ) => !! form.fields?.includes( id ) )
Expand Down
Loading