From a5bc69ebfbcccdb0b1fa791359d590043cfdce77 Mon Sep 17 00:00:00 2001 From: Ingerid Gjeitnes Hellen Date: Fri, 15 Sep 2023 10:19:39 +0200 Subject: [PATCH] refactor: create TooltipButton --- .../src/common/TooltipButton.tsx | 36 +++++++++++++ .../src/form/components/OpenObjectButton.tsx | 19 ++++--- .../src/form/fields/ArrayField.tsx | 51 ++++++++---------- .../src/form/fields/ObjectField.tsx | 53 ++++++++----------- 4 files changed, 91 insertions(+), 68 deletions(-) create mode 100644 packages/dm-core-plugins/src/common/TooltipButton.tsx diff --git a/packages/dm-core-plugins/src/common/TooltipButton.tsx b/packages/dm-core-plugins/src/common/TooltipButton.tsx new file mode 100644 index 000000000..c8ccb2a39 --- /dev/null +++ b/packages/dm-core-plugins/src/common/TooltipButton.tsx @@ -0,0 +1,36 @@ +import { Button, Tooltip } from '@equinor/eds-core-react' +import React from 'react' + +type Prefix = { + [K in keyof T as `${P}-${string & K}`]: T[K] +} +type PrefixedButton = Prefix< + Omit, 'aria-label' | 'children'>, + 'button' +> +type PrefixedTooltip = Prefix< + Omit, 'title' | 'children'>, + 'tooltip' +> +type TProps = PrefixedButton & + PrefixedTooltip & { title: string; children: React.ReactElement } + +const getProps = (prefix: string, dict: { [k: string]: any }) => { + return Object.fromEntries( + Object.entries(dict) + .filter(([k]) => k.startsWith(prefix)) + .map(([k, v]) => [k.slice(prefix.length), v]) + ) +} + +const TooltipButton = (props: TProps) => { + return ( + + + + ) +} + +export default TooltipButton diff --git a/packages/dm-core-plugins/src/form/components/OpenObjectButton.tsx b/packages/dm-core-plugins/src/form/components/OpenObjectButton.tsx index fa7427f57..fb2b74bbb 100644 --- a/packages/dm-core-plugins/src/form/components/OpenObjectButton.tsx +++ b/packages/dm-core-plugins/src/form/components/OpenObjectButton.tsx @@ -3,9 +3,10 @@ import { TReferenceViewConfig, TViewConfig, } from '@development-framework/dm-core' -import { Button, Icon, Tooltip } from '@equinor/eds-core-react' +import { Icon } from '@equinor/eds-core-react' import { external_link } from '@equinor/eds-icons' import React from 'react' +import TooltipButton from '../../common/TooltipButton' import { useRegistryContext } from '../context/RegistryContext' export const OpenObjectButton = ({ @@ -20,14 +21,12 @@ export const OpenObjectButton = ({ const { onOpen } = useRegistryContext() return ( - - - + onOpen?.(viewId, view, idReference)} + > + + ) } diff --git a/packages/dm-core-plugins/src/form/fields/ArrayField.tsx b/packages/dm-core-plugins/src/form/fields/ArrayField.tsx index 88f2457c1..662951c25 100644 --- a/packages/dm-core-plugins/src/form/fields/ArrayField.tsx +++ b/packages/dm-core-plugins/src/form/fields/ArrayField.tsx @@ -6,12 +6,13 @@ import { getKey, useDMSS, } from '@development-framework/dm-core' -import { Button, Icon, Tooltip, Typography } from '@equinor/eds-core-react' +import { Icon, Typography } from '@equinor/eds-core-react' import { AxiosError } from 'axios' import React from 'react' import { useFieldArray, useFormContext } from 'react-hook-form' import { add, delete_forever } from '@equinor/eds-icons' +import TooltipButton from '../../common/TooltipButton' import { OpenObjectButton } from '../components/OpenObjectButton' import { useRegistryContext } from '../context/RegistryContext' import { ButtonRow } from '../styles' @@ -95,23 +96,20 @@ export default function ArrayField(props: TArrayFieldProps) { {displayLabel} {!readOnly && ( - - - + { + if (isPrimitiveType(type)) { + const defaultValue = isPrimitive(type) ? ' ' : {} + append(defaultValue) + } else { + handleAddObject() + } + }} + > + + )} {fields.map((item: any, index: number) => { @@ -136,16 +134,13 @@ export default function ArrayField(props: TArrayFieldProps) { /> {!readOnly && ( - - - + remove(index)} + > + + )} ) diff --git a/packages/dm-core-plugins/src/form/fields/ObjectField.tsx b/packages/dm-core-plugins/src/form/fields/ObjectField.tsx index c84496636..3415d5467 100644 --- a/packages/dm-core-plugins/src/form/fields/ObjectField.tsx +++ b/packages/dm-core-plugins/src/form/fields/ObjectField.tsx @@ -13,12 +13,13 @@ import { useBlueprint, useDMSS, } from '@development-framework/dm-core' -import { Button, Icon, Tooltip, Typography } from '@equinor/eds-core-react' +import { Icon, Typography } from '@equinor/eds-core-react' import { add, delete_forever, edit } from '@equinor/eds-icons' import { AxiosError, AxiosResponse } from 'axios' import React, { useState } from 'react' import { useFormContext } from 'react-hook-form' import styled from 'styled-components' +import TooltipButton from '../../common/TooltipButton' import { defaultConfig } from '../FormPlugin' import { AttributeList } from '../components/AttributeList' import { OpenObjectButton } from '../components/OpenObjectButton' @@ -68,15 +69,13 @@ const SelectReference = (props: { type: string; namePath: string }) => { return ( <> - - - + setShowModal(true)} + > + + - - + + + ) } @@ -164,16 +160,13 @@ const RemoveObject = (props: { namePath: string }) => { }) } return ( - - - + + + ) }