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

Add isVisible option to fields within DataForm #65826

Merged
merged 6 commits into from
Oct 30, 2024
Merged
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
17 changes: 10 additions & 7 deletions packages/dataviews/src/components/dataform-combined-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
* Internal dependencies
*/
import type { DataFormCombinedEditProps, NormalizedField } from '../../types';
import FormFieldVisibility from '../form-field-visibility';

function Header( { title }: { title: string } ) {
return (
Expand Down Expand Up @@ -41,13 +42,15 @@ function DataFormCombinedEdit< Item >( {
);
const children = visibleChildren.map( ( child ) => {
return (
<div className="dataforms-combined-edit__field" key={ child.id }>
<child.Edit
data={ data }
field={ child }
onChange={ onChange }
/>
</div>
<FormFieldVisibility key={ child.id } data={ data } field={ child }>
<div className="dataforms-combined-edit__field">
<child.Edit
data={ data }
field={ child }
onChange={ onChange }
/>
</div>
</FormFieldVisibility>
);
} );

Expand Down
22 changes: 19 additions & 3 deletions packages/dataviews/src/components/dataform/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ import { useState } from '@wordpress/element';
* Internal dependencies
*/
import DataForm from '../index';
import type { CombinedFormField } from '../../../types';
import type { CombinedFormField, Field } from '../../../types';

type SamplePost = {
title: string;
order: number;
author: number;
status: string;
reviewer: string;
date: string;
birthdate: string;
password?: string;
};

const meta = {
title: 'DataViews/DataForm',
Expand Down Expand Up @@ -75,14 +86,18 @@ const fields = [
elements: [
{ value: 'draft', label: 'Draft' },
{ value: 'published', label: 'Published' },
{ value: 'private', label: 'Private' },
],
},
{
id: 'password',
label: 'Password',
type: 'text' as const,
isVisible: ( item: SamplePost ) => {
return item.status !== 'private';
},
},
];
] as Field< SamplePost >[];

export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
const [ post, setPost ] = useState( {
Expand All @@ -102,13 +117,14 @@ export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
'author',
'reviewer',
'status',
'password',
'date',
'birthdate',
],
};

return (
<DataForm
<DataForm< SamplePost >
data={ post }
fields={ fields }
form={ {
Expand Down
32 changes: 32 additions & 0 deletions packages/dataviews/src/components/form-field-visibility/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';

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

type FormFieldVisibilityProps< Item > = React.PropsWithChildren< {
field: NormalizedField< Item >;
data: Item;
} >;

export default function FormFieldVisibility< Item >( {
data,
field,
children,
}: FormFieldVisibilityProps< Item > ) {
const isVisible = useMemo( () => {
if ( field.isVisible ) {
return field.isVisible( data );
}
return true;
}, [ field.isVisible, data ] );

if ( ! isVisible ) {
return null;
}
return children;
}
12 changes: 9 additions & 3 deletions packages/dataviews/src/dataforms-layouts/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { closeSmall } from '@wordpress/icons';
import { normalizeFields } from '../../normalize-fields';
import { getVisibleFields } from '../get-visible-fields';
import type { DataFormProps, NormalizedField } from '../../types';
import FormFieldVisibility from '../../components/form-field-visibility';

interface FormFieldProps< Item > {
data: Item;
Expand Down Expand Up @@ -156,12 +157,17 @@ export default function FormPanel< Item >( {
<VStack spacing={ 2 }>
{ visibleFields.map( ( field ) => {
return (
<FormField
<FormFieldVisibility
key={ field.id }
data={ data }
field={ field }
onChange={ onChange }
/>
>
<FormField
data={ data }
field={ field }
onChange={ onChange }
/>
</FormFieldVisibility>
);
} ) }
</VStack>
Expand Down
12 changes: 9 additions & 3 deletions packages/dataviews/src/dataforms-layouts/regular/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useMemo } from '@wordpress/element';
import { normalizeFields } from '../../normalize-fields';
import { getVisibleFields } from '../get-visible-fields';
import type { DataFormProps } from '../../types';
import FormFieldVisibility from '../../components/form-field-visibility';

export default function FormRegular< Item >( {
data,
Expand All @@ -33,12 +34,17 @@ export default function FormRegular< Item >( {
<VStack spacing={ 4 }>
{ visibleFields.map( ( field ) => {
return (
<field.Edit
<FormFieldVisibility
key={ field.id }
data={ data }
field={ field }
onChange={ onChange }
/>
>
<field.Edit
data={ data }
field={ field }
onChange={ onChange }
/>
</FormFieldVisibility>
);
} ) }
</VStack>
Expand Down
5 changes: 5 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export type Field< Item > = {
*/
isValid?: ( item: Item, context?: ValidationContext ) => boolean;

/**
* Callback used to decide if a field should be displayed.
*/
isVisible?: ( item: Item ) => boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both of these options may not make sense in the DataViews context? In which case we should create a specific DataFormField type.

Copy link
Member

Choose a reason for hiding this comment

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

We have the same with isValid or sort. It's tempting to create separate "field types" for DataViews and DataForm but I don't think it's worth it right now. The consumers of this code will declare fields only once for both.


/**
* Whether the field is sortable.
*/
Expand Down
Loading