From c40fcba088d8b571df548954cbeb1c7377281761 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Wed, 23 Oct 2024 17:24:08 +0200 Subject: [PATCH] #872 - set default values in fields field type --- .../components/Fields/FieldCollection.tsx | 3 ++- .../components/Fields/WrapperField.tsx | 17 ++++++++++++----- src/panelWebView/components/Metadata.tsx | 4 +++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/panelWebView/components/Fields/FieldCollection.tsx b/src/panelWebView/components/Fields/FieldCollection.tsx index 40c74cd1..40f2c8f3 100644 --- a/src/panelWebView/components/Fields/FieldCollection.tsx +++ b/src/panelWebView/components/Fields/FieldCollection.tsx @@ -15,7 +15,8 @@ export interface IFieldCollectionProps { parentFields: string[], blockData?: BlockFieldData, onFieldUpdate?: (field: string | undefined, value: any, parents: string[]) => void, - parentBlock?: string | null + parentBlock?: string | null, + triggerUpdateOnDefault?: boolean ) => (JSX.Element | null)[] | undefined; onChange: (field: string | undefined, value: any, parents: string[]) => void; } diff --git a/src/panelWebView/components/Fields/WrapperField.tsx b/src/panelWebView/components/Fields/WrapperField.tsx index 92236d24..ab2f5a5d 100644 --- a/src/panelWebView/components/Fields/WrapperField.tsx +++ b/src/panelWebView/components/Fields/WrapperField.tsx @@ -55,8 +55,10 @@ export interface IWrapperFieldProps { parentFields: string[], blockData?: BlockFieldData, onFieldUpdate?: (field: string | undefined, value: any, parents: string[]) => void, - parentBlock?: string | null + parentBlock?: string | null, + triggerUpdateOnDefault?: boolean ) => (JSX.Element | null)[] | undefined; + triggerUpdateOnDefault?: boolean; } export const WrapperField: React.FunctionComponent = ({ @@ -72,7 +74,8 @@ export const WrapperField: React.FunctionComponent = ({ parentBlock, onSendUpdate, unsetFocus, - renderFields + renderFields, + triggerUpdateOnDefault }: React.PropsWithChildren) => { const [fieldValue, setFieldValue] = useState(undefined); const [customFields, setCustomFields] = useState<{ @@ -110,7 +113,9 @@ export const WrapperField: React.FunctionComponent = ({ value = getDate(value) || null; } - //onSendUpdate(field.name, value, parentFields); + if (triggerUpdateOnDefault) { + onSendUpdate(field.name, value, parentFields); + } } // Check if the field value contains a placeholder @@ -140,7 +145,7 @@ export const WrapperField: React.FunctionComponent = ({ } } } - }, [field, parent]); + }, [field, parent, triggerUpdateOnDefault]); useEffect(() => { if (window.fmExternal && window.fmExternal.getCustomFields) { @@ -437,7 +442,9 @@ export const WrapperField: React.FunctionComponent = ({ subMetadata, [...parentFields, field.name], blockData, - onSendUpdate + onSendUpdate, + null, + true )} diff --git a/src/panelWebView/components/Metadata.tsx b/src/panelWebView/components/Metadata.tsx index d4d6da76..6066c38e 100644 --- a/src/panelWebView/components/Metadata.tsx +++ b/src/panelWebView/components/Metadata.tsx @@ -61,7 +61,8 @@ const Metadata: React.FunctionComponent = ({ blockData?: BlockFieldData, // eslint-disable-next-line @typescript-eslint/no-explicit-any onFieldUpdate?: (field: string | undefined, value: any, parents: string[]) => void, - parentBlock?: string | null + parentBlock?: string | null, + triggerUpdateOnDefault?: boolean ): (JSX.Element | null)[] | undefined => { if (!ctFields || !settings) { return; @@ -83,6 +84,7 @@ const Metadata: React.FunctionComponent = ({ onSendUpdate={onFieldUpdate || onSendUpdate} unsetFocus={unsetFocus} renderFields={renderFields} + triggerUpdateOnDefault={triggerUpdateOnDefault} /> )); };