From f57bf396deabfc90f77ff3628e2dc0db647cd456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Tue, 2 Jul 2024 10:29:00 +0200 Subject: [PATCH] Form API: add visibleFields --- packages/dataviews/src/dataform.tsx | 21 +++++++++++++++---- packages/dataviews/src/types.ts | 7 +++++++ .../src/components/post-actions/actions.js | 5 +++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/dataviews/src/dataform.tsx b/packages/dataviews/src/dataform.tsx index 05d6a526a7c217..98ef6df2913028 100644 --- a/packages/dataviews/src/dataform.tsx +++ b/packages/dataviews/src/dataform.tsx @@ -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. }; @@ -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 ( = Field< Item >[]; export type Data< Item > = Item[]; +/** + * The form configuration. + */ +export type Form = { + visibleFields?: string[]; +}; + /** * The filters applied to the dataset. */ diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 63afeda606a529..6d6bfef181f90f 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -50,6 +50,10 @@ const fields = [ }, ]; +const form = { + visibleFields: [ 'title' ], +}; + /** * Check if a template is removable. * @@ -780,6 +784,7 @@ const useDuplicatePostAction = ( postType ) => {