Skip to content

Commit

Permalink
refactor(form): move AttributeList
Browse files Browse the repository at this point in the history
ref #276
  • Loading branch information
ingeridhellen committed Aug 1, 2023
1 parent 2750657 commit a71790c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
40 changes: 40 additions & 0 deletions packages/dm-core-plugins/src/form/components/AttributeList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Stack, TAttribute, TBlueprint } from '@development-framework/dm-core'
import React from 'react'
import { AttributeField } from '../fields/AttributeField'
import { TConfig } from '../types'

export const AttributeList = (props: {
namePath: string
config: TConfig | undefined
blueprint: TBlueprint | undefined
}) => {
const { namePath, config, blueprint } = props

const prefix = namePath === '' ? `` : `${namePath}.`

const attributes = blueprint?.attributes ?? []
const filteredAttributes =
config && config.fields.length
? config.fields
.map((name: string) =>
attributes.find((attribute) => attribute.name == name)
)
.filter((attribute): attribute is TAttribute => !!attribute)
: attributes

const attributeFields = filteredAttributes.map((attribute) => {
const uiAttribute = config?.attributes.find(
(uiAttribute) => uiAttribute.name === attribute.name
)
return (
<AttributeField
key={`${prefix}${attribute.name}`}
namePath={`${prefix}${attribute.name}`}
attribute={attribute}
uiAttribute={uiAttribute}
/>
)
})

return <Stack spacing={1}>{attributeFields}</Stack>
}
2 changes: 1 addition & 1 deletion packages/dm-core-plugins/src/form/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Button } from '@equinor/eds-core-react'
import { FormProvider, useForm } from 'react-hook-form'
import styled from 'styled-components'
import { RegistryProvider } from '../context/RegistryContext'
import { AttributeList } from '../fields/ObjectField'
import { TFormProps } from '../types'
import { AttributeList } from './AttributeList'

const Wrapper = styled.div`
max-width: 650px;
Expand Down
47 changes: 2 additions & 45 deletions packages/dm-core-plugins/src/form/fields/ObjectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
Loading,
NewEntityButton,
Stack,
TAttribute,
TBlueprint,
TLinkReference,
resolveRelativeAddress,
splitAddress,
Expand All @@ -19,16 +17,11 @@ import { AxiosError, AxiosResponse } from 'axios'
import React, { useState } from 'react'
import { Controller, useFormContext } from 'react-hook-form'
import { defaultConfig } from '../FormPlugin'
import { AttributeList } from '../components/AttributeList'
import { OpenObjectButton } from '../components/OpenObjectButton'
import { useRegistryContext } from '../context/RegistryContext'
import { getWidget } from '../context/WidgetContext'
import {
TConfig,
TContentProps,
TObjectFieldProps,
TUiRecipeForm,
} from '../types'
import { AttributeField } from './AttributeField'
import { TContentProps, TObjectFieldProps, TUiRecipeForm } from '../types'

const AddUncontained = (props: {
type: string
Expand Down Expand Up @@ -135,42 +128,6 @@ const RemoveObject = (props: { namePath: string; onRemove: () => void }) => {
)
}

export const AttributeList = (props: {
namePath: string
config: TConfig | undefined
blueprint: TBlueprint | undefined
}) => {
const { namePath, config, blueprint } = props

const prefix = namePath === '' ? `` : `${namePath}.`

const attributes = blueprint?.attributes ?? []
const filteredAttributes =
config && config.fields.length
? config.fields
.map((name: string) =>
attributes.find((attribute) => attribute.name == name)
)
.filter((attribute): attribute is TAttribute => !!attribute)
: attributes

const attributeFields = filteredAttributes.map((attribute) => {
const uiAttribute = config?.attributes.find(
(uiAttribute) => uiAttribute.name === attribute.name
)
return (
<AttributeField
key={`${prefix}${attribute.name}`}
namePath={`${prefix}${attribute.name}`}
attribute={attribute}
uiAttribute={uiAttribute}
/>
)
})

return <Stack spacing={1}>{attributeFields}</Stack>
}

export const ContainedAttribute = (props: TContentProps): JSX.Element => {
const {
type,
Expand Down

0 comments on commit a71790c

Please sign in to comment.