From c1582a22259565fa9d7f81b8174e9ffef2f5130b 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 12:28:08 +0200 Subject: [PATCH] Create a static and private registry for controls --- packages/dataviews/src/dataform.tsx | 43 +++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/packages/dataviews/src/dataform.tsx b/packages/dataviews/src/dataform.tsx index db0c621cc5566f..3ca845b2576dc7 100644 --- a/packages/dataviews/src/dataform.tsx +++ b/packages/dataviews/src/dataform.tsx @@ -18,7 +18,7 @@ type DataFormProps< Item > = { onUpdate: any; // TODO: fix this type. }; -type DataFormTextControlProps< Item > = { +type DataFormControlProps< Item > = { data: Item; field: NormalizedField< Item >; onUpdate: any; // TODO: fix this type. @@ -28,7 +28,7 @@ function DataFormTextControl< Item >( { data, field, onUpdate, -}: DataFormTextControlProps< Item > ) { +}: DataFormControlProps< Item > ) { const { id, header, placeholder } = field; const value = field.getValue( { item: data } ); @@ -51,6 +51,26 @@ function DataFormTextControl< Item >( { ); } +const controls: { + [ key: string ]: < Item >( + props: DataFormControlProps< Item > + ) => JSX.Element; +} = { + text: DataFormTextControl, +}; + +function getControlForField< Item >( field: NormalizedField< Item > ) { + if ( ! field.type ) { + return null; + } + + if ( ! Object.keys( controls ).includes( field.type ) ) { + return null; + } + + return controls[ field.type ]; +} + export default function DataForm< Item >( { data, fields, @@ -68,15 +88,14 @@ export default function DataForm< Item >( { ); return visibleFields.map( ( field ) => { - if ( field.type === 'text' ) { - return ( - - ); - } - return null; + const DataFormControl = getControlForField( field ); + return DataFormControl ? ( + + ) : null; } ); }