Skip to content

Commit

Permalink
Form API: add visibleFields
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 2, 2024
1 parent c0eeb17 commit f57bf39
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/dataviews/src/dataform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { useEffect } from '@wordpress/element';
import { useEffect, useMemo } from '@wordpress/element';
import { useDebouncedInput } from '@wordpress/compose';

/**
* Internal dependencies
*/
import type { NormalizedField } from './types';
import type { Form, Field, NormalizedField } from './types';
import { normalizeFields } from './normalize-fields';

type DataFormProps< Item > = {
data: Item;
fields: NormalizedField< Item >[]; // TODO: use Field. Normalize them first.
fields: Field< Item >[];
form: Form;
onUpdate: any; // TODO: fix this type.
};

Expand Down Expand Up @@ -53,9 +55,20 @@ function DataFormTextControl< Item >( {
export default function DataForm< Item >( {
data,
fields,
form,
onUpdate,
}: DataFormProps< Item > ) {
return fields.map( ( field ) => {
const visibleFields = useMemo(
() =>
normalizeFields(
fields.filter(
( { id } ) => !! form.visibleFields?.includes( id )
)
),
[ fields, form.visibleFields ]
);

return visibleFields.map( ( field ) => {
if ( field.type === 'text' ) {
return (
<DataFormTextControl
Expand Down
7 changes: 7 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export type Fields< Item > = Field< Item >[];

export type Data< Item > = Item[];

/**
* The form configuration.
*/
export type Form = {
visibleFields?: string[];
};

/**
* The filters applied to the dataset.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const fields = [
},
];

const form = {
visibleFields: [ 'title' ],
};

/**
* Check if a template is removable.
*
Expand Down Expand Up @@ -780,6 +784,7 @@ const useDuplicatePostAction = ( postType ) => {
<DataForm
data={ item }
fields={ fields }
form={ form }
onUpdate={ setItem }
/>
<HStack spacing={ 2 } justify="end">
Expand Down

0 comments on commit f57bf39

Please sign in to comment.