From da0b8f4b04c8e51fb47c8a29585be170bec60006 Mon Sep 17 00:00:00 2001 From: Peter Bull Hove Date: Wed, 10 Jan 2024 14:37:35 +0100 Subject: [PATCH] feat: form label is configurable --- .../plugins/form/uncontained/company.recipe.json | 6 ++++-- .../blueprints/form/fields/Field.json | 7 +++++++ .../dm-core-plugins/src/form/fields/ArrayField.tsx | 3 ++- .../src/form/fields/NumberField.tsx | 5 +++-- .../src/form/fields/StringField.tsx | 4 ++-- .../src/form/templates/ArrayComplexTemplate.tsx | 1 + .../form/templates/ArrayPrimitiveListTemplate.tsx | 1 + .../templates/ObjectModelContainedTemplate.tsx | 1 + .../templates/ObjectModelUncontainedTemplate.tsx | 2 ++ .../templates/ObjectStorageUncontainedTemplate.tsx | 1 + .../src/form/templates/shared/FormTemplate.tsx | 5 ++++- packages/dm-core-plugins/src/form/types.tsx | 1 + .../src/form/utils/getDisplayLabel.tsx | 14 +++++++++++--- 13 files changed, 40 insertions(+), 11 deletions(-) diff --git a/example/app/data/DemoDataSource/recipes/plugins/form/uncontained/company.recipe.json b/example/app/data/DemoDataSource/recipes/plugins/form/uncontained/company.recipe.json index bee454f11..d772ea01c 100644 --- a/example/app/data/DemoDataSource/recipes/plugins/form/uncontained/company.recipe.json +++ b/example/app/data/DemoDataSource/recipes/plugins/form/uncontained/company.recipe.json @@ -65,7 +65,8 @@ "type": "PLUGINS:dm-core-plugins/form/FormFunctionalityConfig", "open": false, "expand": true - } + }, + "label": "The Trainee" }, { "name": "description", @@ -77,7 +78,8 @@ { "name": "averageSalary", "type": "PLUGINS:dm-core-plugins/form/fields/NumberField", - "hideOptionalLabel": true + "hideOptionalLabel": true, + "label": "Salary (avg)" } ], "fields": [ diff --git a/packages/dm-core-plugins/blueprints/form/fields/Field.json b/packages/dm-core-plugins/blueprints/form/fields/Field.json index f0a448ab5..d08d950a3 100644 --- a/packages/dm-core-plugins/blueprints/form/fields/Field.json +++ b/packages/dm-core-plugins/blueprints/form/fields/Field.json @@ -43,6 +43,13 @@ "attributeType": "boolean", "optional": true, "default": false + }, + { + "name": "label", + "type": "dmss://system/SIMOS/BlueprintAttribute", + "attributeType": "string", + "optional": true, + "description": "What will be displayed in form. If this is not set, then form will use the attribute 'name'." } ] } diff --git a/packages/dm-core-plugins/src/form/fields/ArrayField.tsx b/packages/dm-core-plugins/src/form/fields/ArrayField.tsx index 4b986d2b5..96743b573 100644 --- a/packages/dm-core-plugins/src/form/fields/ArrayField.tsx +++ b/packages/dm-core-plugins/src/form/fields/ArrayField.tsx @@ -28,7 +28,8 @@ export default function ArrayField(props: TArrayTemplate) { value={value} label={getDisplayLabel( attribute, - uiAttribute?.config?.hideOptionalLabel + uiAttribute?.config?.hideOptionalLabel, + uiAttribute )} /> ) diff --git a/packages/dm-core-plugins/src/form/fields/NumberField.tsx b/packages/dm-core-plugins/src/form/fields/NumberField.tsx index 09759481c..e31d3daf1 100644 --- a/packages/dm-core-plugins/src/form/fields/NumberField.tsx +++ b/packages/dm-core-plugins/src/form/fields/NumberField.tsx @@ -1,7 +1,7 @@ import React from 'react' import { Controller } from 'react-hook-form' import { getWidget } from '../context/WidgetContext' -import { TField, TUiAttributeString } from '../types' +import { TField } from '../types' import { useRegistryContext } from '../context/RegistryContext' import { getDisplayLabel } from '../utils/getDisplayLabel' const REGEX_FLOAT = /^\d+(\.\d+)?([eE][-+]?\d+)?$/ @@ -40,7 +40,8 @@ export const NumberField = (props: TField) => { !uiAttribute?.config?.hideLabel ? getDisplayLabel( attribute, - uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly + uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly, + uiAttribute ) : '' } diff --git a/packages/dm-core-plugins/src/form/fields/StringField.tsx b/packages/dm-core-plugins/src/form/fields/StringField.tsx index 176bef02e..a4a247d38 100644 --- a/packages/dm-core-plugins/src/form/fields/StringField.tsx +++ b/packages/dm-core-plugins/src/form/fields/StringField.tsx @@ -10,7 +10,6 @@ export const StringField = (props: TField) => { const Widget = getWidget(uiAttribute?.widget ?? 'TextWidget') const { config } = useRegistryContext() const readOnly = uiAttribute?.readOnly || config.readOnly - console.log(uiAttribute) return ( { !uiAttribute?.config?.hideLabel ? getDisplayLabel( attribute, - uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly + uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly, + uiAttribute ) : '' } diff --git a/packages/dm-core-plugins/src/form/templates/ArrayComplexTemplate.tsx b/packages/dm-core-plugins/src/form/templates/ArrayComplexTemplate.tsx index eb5907c39..f8a8a9aec 100644 --- a/packages/dm-core-plugins/src/form/templates/ArrayComplexTemplate.tsx +++ b/packages/dm-core-plugins/src/form/templates/ArrayComplexTemplate.tsx @@ -52,6 +52,7 @@ export const ArrayComplexTemplate = (props: TArrayTemplate) => { attribute={attribute} icon={list} hideOptionalLabel={uiAttribute?.hideOptionalLabel} + uiAttribute={uiAttribute} /> {canOpen && ( {isExpanded && ( diff --git a/packages/dm-core-plugins/src/form/templates/ObjectModelContainedTemplate.tsx b/packages/dm-core-plugins/src/form/templates/ObjectModelContainedTemplate.tsx index ad85aef3f..8bb1f6f69 100644 --- a/packages/dm-core-plugins/src/form/templates/ObjectModelContainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/templates/ObjectModelContainedTemplate.tsx @@ -51,6 +51,7 @@ export const ObjectModelContainedTemplate = ( ) } hideOptionalLabel={uiAttribute?.config?.hideOptionalLabel} + uiAttribute={uiAttribute} /> {canOpen && ( diff --git a/packages/dm-core-plugins/src/form/templates/ObjectModelUncontainedTemplate.tsx b/packages/dm-core-plugins/src/form/templates/ObjectModelUncontainedTemplate.tsx index db74724f6..dd1eb5a40 100644 --- a/packages/dm-core-plugins/src/form/templates/ObjectModelUncontainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/templates/ObjectModelUncontainedTemplate.tsx @@ -45,6 +45,7 @@ export const ObjectModelUncontainedTemplate = ( uiAttribute, onOpen ) + console.log(uiAttribute) return ( @@ -60,6 +61,7 @@ export const ObjectModelUncontainedTemplate = ( } icon={link} hideOptionalLabel={uiAttribute?.hideOptionalLabel} + uiAttribute={uiAttribute} /> {canOpen && ( diff --git a/packages/dm-core-plugins/src/form/templates/ObjectStorageUncontainedTemplate.tsx b/packages/dm-core-plugins/src/form/templates/ObjectStorageUncontainedTemplate.tsx index d8ffacd45..45ff97c00 100644 --- a/packages/dm-core-plugins/src/form/templates/ObjectStorageUncontainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/templates/ObjectStorageUncontainedTemplate.tsx @@ -53,6 +53,7 @@ export const ObjectStorageUncontainedTemplate = (props: TObjectTemplate) => { onOpen?.(namePath, getOpenViewConfig(uiAttribute), address) } hideOptionalLabel={uiAttribute?.hideOptionalLabel} + uiAttribute={uiAttribute} /> {canOpen && referenceExists && ( diff --git a/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx b/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx index b2b338449..5432376e8 100644 --- a/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx +++ b/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx @@ -4,6 +4,7 @@ import { IconData, file, file_description } from '@equinor/eds-icons' import ExpandChevron from '../../components/ExpandChevron' import { TAttribute } from '@development-framework/dm-core' import { getDisplayLabel } from '../../utils/getDisplayLabel' +import { TUiAttribute } from '../../types' const FormTemplate = ({ children }: PropsWithChildren) => { return
{children}
@@ -54,6 +55,7 @@ const FormTemplateHeaderTitle = ({ objectIsNotEmpty, icon, hideOptionalLabel, + uiAttribute, }: { canExpand: boolean | undefined canOpen: boolean | undefined @@ -64,6 +66,7 @@ const FormTemplateHeaderTitle = ({ onOpen?: () => void icon?: IconData hideOptionalLabel?: boolean + uiAttribute?: TUiAttribute }) => { const [isHovering, setIsHovering] = useState(false) @@ -124,7 +127,7 @@ const FormTemplateHeaderTitle = ({ : '' }`} > - {getDisplayLabel(attribute, true)} + {getDisplayLabel(attribute, true, uiAttribute)} {attribute.optional && !hideOptionalLabel && (

Optional

diff --git a/packages/dm-core-plugins/src/form/types.tsx b/packages/dm-core-plugins/src/form/types.tsx index 70b539b52..3f89c6f3e 100644 --- a/packages/dm-core-plugins/src/form/types.tsx +++ b/packages/dm-core-plugins/src/form/types.tsx @@ -49,6 +49,7 @@ type TUiAttributeBase = { open: boolean } hideOptionalLabel?: boolean + label?: string } export type TUiAttributeString = TUiAttributeBase & { widget: string diff --git a/packages/dm-core-plugins/src/form/utils/getDisplayLabel.tsx b/packages/dm-core-plugins/src/form/utils/getDisplayLabel.tsx index 8219f023f..7e5614f40 100644 --- a/packages/dm-core-plugins/src/form/utils/getDisplayLabel.tsx +++ b/packages/dm-core-plugins/src/form/utils/getDisplayLabel.tsx @@ -1,13 +1,21 @@ import { TAttribute } from '@development-framework/dm-core' import lodash from 'lodash' +import { TUiAttribute } from '../types' +import { label } from '@equinor/eds-icons' export const getDisplayLabel = ( attribute: TAttribute, - hideOptionalLabel: boolean | undefined + hideOptionalLabel?: boolean | undefined, + uiAttribute?: TUiAttribute ) => { - const { name, label } = attribute + const { name, label: attributeLabel } = attribute + + if (uiAttribute?.label) return uiAttribute?.label + const displayLabel = - label === undefined || label === '' ? lodash.startCase(name) : label + attributeLabel === undefined || attributeLabel === '' + ? lodash.startCase(name) + : attributeLabel return !hideOptionalLabel && attribute.optional ? displayLabel + ' (Optional)'