From 187840549c503b0146beacec7514a3f24235678e Mon Sep 17 00:00:00 2001 From: Andrea Vesterhus Date: Tue, 5 Mar 2024 17:13:11 +0100 Subject: [PATCH] refactor: FormTemplate -> ComplexAttributeTemplate --- .../templates/ArrayComplexTemplate.tsx | 20 +- .../ArrayPrimitiveListTemplate.tsx | 22 ++- .../ObjectModelContainedTemplate.tsx | 23 ++- .../ObjectModelUncontainedTemplate.tsx | 23 ++- .../ObjectStorageUncontainedTemplate.tsx | 20 +- .../ComplexAttributeTemplate.tsx | 12 ++ .../ComplexAttributeTemplateContent.tsx | 29 +++ .../ComplexAttributeTemplateHeader.tsx | 17 ++ .../ComplexAttributeTemplateHeaderActions.tsx | 27 +++ .../ComplexAttributeTemplateHeaderTitle.tsx | 106 +++++++++++ .../src/form/templates/index.ts | 1 + .../form/templates/shared/FormTemplate.tsx | 172 ------------------ 12 files changed, 251 insertions(+), 221 deletions(-) create mode 100644 packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplate.tsx create mode 100644 packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateContent.tsx create mode 100644 packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeader.tsx create mode 100644 packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderActions.tsx create mode 100644 packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderTitle.tsx create mode 100644 packages/dm-core-plugins/src/form/templates/index.ts delete mode 100644 packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx diff --git a/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayComplexTemplate.tsx b/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayComplexTemplate.tsx index 391cd964e..e2713b0b8 100644 --- a/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayComplexTemplate.tsx +++ b/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayComplexTemplate.tsx @@ -6,7 +6,7 @@ import AddObject from '../../../components/AddObjectButton' import { OpenObjectButton } from '../../../components/OpenObjectButton' import RemoveObject from '../../../components/RemoveObjectButton' import { useRegistryContext } from '../../../context/RegistryContext' -import FormTemplate from '../../../templates/shared/FormTemplate' +import { ComplexAttributeTemplate } from '../../../templates' import { TArrayTemplate } from '../../../types' import { getCanOpenOrExpand, @@ -34,9 +34,9 @@ export const ArrayComplexTemplate = (props: TArrayTemplate) => { onOpen ) return ( - - - + + { icon={list} uiAttribute={uiAttribute} /> - + {canOpen && ( { onAdd={() => setInitialValue([])} /> ))} - - - + + { onOpen={onOpen} viewConfig={getExpandViewConfig(uiAttribute)} /> - - + + ) } diff --git a/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayPrimitiveListTemplate/ArrayPrimitiveListTemplate.tsx b/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayPrimitiveListTemplate/ArrayPrimitiveListTemplate.tsx index 2f9cb297d..e7af91faf 100644 --- a/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayPrimitiveListTemplate/ArrayPrimitiveListTemplate.tsx +++ b/packages/dm-core-plugins/src/form/fields/ArrayField/templates/ArrayPrimitiveListTemplate/ArrayPrimitiveListTemplate.tsx @@ -1,7 +1,7 @@ import { list } from '@equinor/eds-icons' import { useState } from 'react' import { useRegistryContext } from '../../../../context/RegistryContext' -import FormTemplate from '../../../../templates/shared/FormTemplate' +import { ComplexAttributeTemplate } from '../../../../templates' import { TArrayTemplate, TPrimitive } from '../../../../types' import { PrimitiveArray } from './PrimitiveArray/PrimitiveArray' @@ -21,9 +21,9 @@ export const ArrayPrimitiveListTemplate = ( ) return ( - - - + + - - - + + + - - + + ) } diff --git a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelContainedTemplate.tsx b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelContainedTemplate.tsx index cb059c1a1..5a188793b 100644 --- a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelContainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelContainedTemplate.tsx @@ -6,7 +6,7 @@ import AddObjectBySearchButton from '../../../components/AddObjectBySearchButton import { OpenObjectButton } from '../../../components/OpenObjectButton' import RemoveObject from '../../../components/RemoveObjectButton' import { useRegistryContext } from '../../../context/RegistryContext' -import FormTemplate from '../../../templates/shared/FormTemplate' +import { ComplexAttributeTemplate } from '../../../templates' import { TObjectTemplate } from '../../../types' import { getCanOpenOrExpand as getCanOpenExpand, @@ -34,9 +34,9 @@ export const ObjectModelContainedTemplate = ( onOpen ) return ( - - - + + - + {canOpen && ( )} - - - + + + - - + + ) } diff --git a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelUncontainedTemplate.tsx b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelUncontainedTemplate.tsx index 3a5a3f7b8..c55fa9c0f 100644 --- a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelUncontainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectModelUncontainedTemplate.tsx @@ -10,7 +10,7 @@ import { OpenObjectButton } from '../../../components/OpenObjectButton' import RemoveObject from '../../../components/RemoveObjectButton' import { SelectReference } from '../../../components/SelectReference' import { useRegistryContext } from '../../../context/RegistryContext' -import FormTemplate from '../../../templates/shared/FormTemplate' +import { ComplexAttributeTemplate } from '../../../templates' import { TObjectTemplate } from '../../../types' import { getCanOpenOrExpand, @@ -46,9 +46,9 @@ export const ObjectModelUncontainedTemplate = ( onOpen ) return ( - - - + + - + {canOpen && ( )} - - - + + + - - + + ) } diff --git a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectStorageUncontainedTemplate.tsx b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectStorageUncontainedTemplate.tsx index 26ad1663d..aeb697e6d 100644 --- a/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectStorageUncontainedTemplate.tsx +++ b/packages/dm-core-plugins/src/form/fields/ObjectField/templates/ObjectStorageUncontainedTemplate.tsx @@ -8,7 +8,7 @@ import { useFormContext } from 'react-hook-form' import { OpenObjectButton } from '../../../components/OpenObjectButton' import RemoveObject from '../../../components/RemoveObjectButton' import { useRegistryContext } from '../../../context/RegistryContext' -import FormTemplate from '../../../templates/shared/FormTemplate' +import { ComplexAttributeTemplate } from '../../../templates' import { TObjectTemplate } from '../../../types' import { getCanOpenOrExpand, @@ -36,9 +36,9 @@ export const ObjectStorageUncontainedTemplate = (props: TObjectTemplate) => { ) return ( - - - + + { } uiAttribute={uiAttribute} /> - + {canOpen && referenceExists && ( { {attribute.optional && referenceExists && !config.readOnly && ( )} - - + +
- @@ -84,8 +84,8 @@ export const ObjectStorageUncontainedTemplate = (props: TObjectTemplate) => { onOpen={onOpen} viewConfig={getExpandViewConfig(uiAttribute)} /> - +
-
+ ) } diff --git a/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplate.tsx b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplate.tsx new file mode 100644 index 000000000..1c7beb0e1 --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplate.tsx @@ -0,0 +1,12 @@ +import { PropsWithChildren } from 'react' +import { ComplexAttributeTemplateContent } from './ComplexAttributeTemplateContent' +import { ComplexAttributeTemplateHeader } from './ComplexAttributeTemplateHeader' + +export const ComplexAttributeTemplate = ({ children }: PropsWithChildren) => { + return ( +
{children}
+ ) +} + +ComplexAttributeTemplate.Header = ComplexAttributeTemplateHeader +ComplexAttributeTemplate.Content = ComplexAttributeTemplateContent diff --git a/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateContent.tsx b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateContent.tsx new file mode 100644 index 000000000..c738da739 --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateContent.tsx @@ -0,0 +1,29 @@ +import React from 'react' + +type ComplexAttributeTemplateContentProps = { + children: React.ReactNode + expanded: boolean + canExpand: boolean + padding?: string +} + +export const ComplexAttributeTemplateContent = ({ + children, + padding, + expanded, + canExpand, +}: ComplexAttributeTemplateContentProps) => { + return ( + <> + {canExpand && ( +
+ {children} +
+ )} + + ) +} diff --git a/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeader.tsx b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeader.tsx new file mode 100644 index 000000000..4cb943e10 --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeader.tsx @@ -0,0 +1,17 @@ +import React from 'react' +import { ComplexAttributeTemplateHeaderActions } from './ComplexAttributeTemplateHeaderActions' +import { ComplexAttributeTemplateHeaderTitle } from './ComplexAttributeTemplateHeaderTitle' + +export const ComplexAttributeTemplateHeader = ({ + children, + objectIsNotEmpty = true, +}: { children: React.ReactNode; objectIsNotEmpty?: boolean }) => { + return ( + + {children} + + ) +} + +ComplexAttributeTemplateHeader.Title = ComplexAttributeTemplateHeaderTitle +ComplexAttributeTemplateHeader.Actions = ComplexAttributeTemplateHeaderActions diff --git a/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderActions.tsx b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderActions.tsx new file mode 100644 index 000000000..d74e04d7b --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderActions.tsx @@ -0,0 +1,27 @@ +import { EdsProvider, Icon, Tooltip } from '@equinor/eds-core-react' +import { info_circle } from '@equinor/eds-icons' +import { tokens } from '@equinor/eds-tokens' +import React from 'react' +import { TUiAttribute } from '../../types' + +export const ComplexAttributeTemplateHeaderActions = ({ + children, + uiAttribute, +}: { children?: React.ReactNode; uiAttribute?: TUiAttribute }) => { + return ( +
+ {uiAttribute?.tooltip && ( + + + + + + )} + {children} +
+ ) +} diff --git a/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderTitle.tsx b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderTitle.tsx new file mode 100644 index 000000000..8d5934bcc --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/ComplexAttributeTemplate/ComplexAttributeTemplateHeaderTitle.tsx @@ -0,0 +1,106 @@ +import { TAttribute } from '@development-framework/dm-core' +import { Icon, Typography } from '@equinor/eds-core-react' +import { IconData, file, file_description } from '@equinor/eds-icons' +import { useState } from 'react' +import ExpandChevron from '../../components/ExpandChevron' +import { TUiAttribute } from '../../types' +import { getDisplayLabel } from '../../utils' + +type ComplexAttributeTemplateHeaderTitleProps = { + canExpand: boolean | undefined + canOpen: boolean | undefined + isExpanded: boolean | undefined + attribute: TAttribute + objectIsNotEmpty: boolean + setIsExpanded?: (expanded: boolean) => void + onOpen?: () => void + icon?: IconData + uiAttribute?: TUiAttribute + namePath?: string +} + +export const ComplexAttributeTemplateHeaderTitle = ({ + canExpand, + canOpen, + isExpanded, + setIsExpanded, + attribute, + onOpen, + objectIsNotEmpty, + icon, + uiAttribute, + namePath, +}: ComplexAttributeTemplateHeaderTitleProps) => { + const [isHovering, setIsHovering] = useState(false) + + const hideOptional = uiAttribute?.hideOptionalLabel ?? false + + function handleLabelClick( + event: React.MouseEvent + ) { + if (!objectIsNotEmpty) return + if (event.ctrlKey || event.metaKey) { + event.preventDefault() + canOpen && onOpen?.() + return + } + if (canExpand) { + setIsExpanded?.(!isExpanded) + return + } + canOpen && onOpen?.() + } + + return ( +
+ {canExpand && ( + + setIsExpanded?.(exp)} + /> + + )} +
setIsHovering(true)} + onMouseLeave={() => setIsHovering(false)} + onClick={handleLabelClick} + > + + + {getDisplayLabel(attribute, true, uiAttribute)} + + {attribute.optional && !hideOptional && ( +

Optional

+ )} +
+
+ ) +} diff --git a/packages/dm-core-plugins/src/form/templates/index.ts b/packages/dm-core-plugins/src/form/templates/index.ts new file mode 100644 index 000000000..5da14ec7b --- /dev/null +++ b/packages/dm-core-plugins/src/form/templates/index.ts @@ -0,0 +1 @@ +export * from './ComplexAttributeTemplate/ComplexAttributeTemplate' diff --git a/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx b/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx deleted file mode 100644 index a16969ae8..000000000 --- a/packages/dm-core-plugins/src/form/templates/shared/FormTemplate.tsx +++ /dev/null @@ -1,172 +0,0 @@ -import { TAttribute, colors } from '@development-framework/dm-core' -import { EdsProvider, Icon, Tooltip, Typography } from '@equinor/eds-core-react' -import { - IconData, - file, - file_description, - info_circle, -} from '@equinor/eds-icons' -import { PropsWithChildren, useState } from 'react' -import ExpandChevron from '../../components/ExpandChevron' -import { TUiAttribute } from '../../types' -import { getDisplayLabel } from '../../utils/getDisplayLabel' - -const FormTemplate = ({ children }: PropsWithChildren) => { - return ( -
{children}
- ) -} - -const FormTemplateHeader = ({ - children, - objectIsNotEmpty = true, -}: PropsWithChildren & { objectIsNotEmpty?: boolean }) => { - return ( - - {children} - - ) -} - -const FormTemplateHeaderActions = ({ - children, - uiAttribute, -}: PropsWithChildren & { uiAttribute?: TUiAttribute }) => { - return ( -
- {uiAttribute?.tooltip && ( - - - - - - )} - {children} -
- ) -} - -const FormTemplateContent = ({ - children, - padding, - expanded, - canExpand, -}: PropsWithChildren & { - expanded: boolean - canExpand: boolean - padding?: string -}) => { - return ( - <> - {canExpand && ( -
- {children} -
- )} - - ) -} - -const FormTemplateHeaderTitle = ({ - canExpand, - canOpen, - isExpanded, - setIsExpanded, - attribute, - onOpen, - objectIsNotEmpty, - icon, - uiAttribute, - namePath, -}: { - canExpand: boolean | undefined - canOpen: boolean | undefined - isExpanded: boolean | undefined - attribute: TAttribute - objectIsNotEmpty: boolean - setIsExpanded?: (expanded: boolean) => void - onOpen?: () => void - icon?: IconData - uiAttribute?: TUiAttribute - namePath?: string -}) => { - const [isHovering, setIsHovering] = useState(false) - - const hideOptional = uiAttribute?.hideOptionalLabel ?? false - return ( -
- {canExpand && ( - - setIsExpanded?.(exp)} - /> - - )} -
setIsHovering(true)} - onMouseLeave={() => setIsHovering(false)} - onClick={(event) => { - if (!objectIsNotEmpty) return - if (event.ctrlKey || event.metaKey) { - event.preventDefault() - canOpen && onOpen?.() - return - } - if (canExpand) { - setIsExpanded?.(!isExpanded) - return - } - canOpen && onOpen?.() - }} - > - - - {getDisplayLabel(attribute, true, uiAttribute)} - - {attribute.optional && !hideOptional && ( -

Optional

- )} -
-
- ) -} - -FormTemplate.Header = FormTemplateHeader -FormTemplate.Content = FormTemplateContent - -FormTemplateHeader.Title = FormTemplateHeaderTitle -FormTemplateHeader.Actions = FormTemplateHeaderActions - -export default FormTemplate