Skip to content

Commit

Permalink
feat: form label is configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pbullhove committed Jan 10, 2024
1 parent 292f1b3 commit da0b8f4
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"type": "PLUGINS:dm-core-plugins/form/FormFunctionalityConfig",
"open": false,
"expand": true
}
},
"label": "The Trainee"
},
{
"name": "description",
Expand All @@ -77,7 +78,8 @@
{
"name": "averageSalary",
"type": "PLUGINS:dm-core-plugins/form/fields/NumberField",
"hideOptionalLabel": true
"hideOptionalLabel": true,
"label": "Salary (avg)"
}
],
"fields": [
Expand Down
7 changes: 7 additions & 0 deletions packages/dm-core-plugins/blueprints/form/fields/Field.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'."
}
]
}
3 changes: 2 additions & 1 deletion packages/dm-core-plugins/src/form/fields/ArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function ArrayField(props: TArrayTemplate) {
value={value}
label={getDisplayLabel(
attribute,
uiAttribute?.config?.hideOptionalLabel
uiAttribute?.config?.hideOptionalLabel,
uiAttribute
)}
/>
)
Expand Down
5 changes: 3 additions & 2 deletions packages/dm-core-plugins/src/form/fields/NumberField.tsx
Original file line number Diff line number Diff line change
@@ -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+)?$/
Expand Down Expand Up @@ -40,7 +40,8 @@ export const NumberField = (props: TField) => {
!uiAttribute?.config?.hideLabel
? getDisplayLabel(
attribute,
uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly
uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly,
uiAttribute
)
: ''
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dm-core-plugins/src/form/fields/StringField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Controller
name={namePath}
Expand All @@ -36,7 +35,8 @@ export const StringField = (props: TField) => {
!uiAttribute?.config?.hideLabel
? getDisplayLabel(
attribute,
uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly
uiAttribute?.hideOptionalLabel || uiAttribute?.readOnly,
uiAttribute
)
: ''
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const ArrayComplexTemplate = (props: TArrayTemplate) => {
attribute={attribute}
icon={list}
hideOptionalLabel={uiAttribute?.hideOptionalLabel}
uiAttribute={uiAttribute}
/>
{canOpen && (
<OpenObjectButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const ArrayPrimitiveListTemplate = (
objectIsNotEmpty={true}
icon={list}
hideOptionalLabel={true}
uiAttribute={uiAttribute}
/>
</FormTemplate.Header>
{isExpanded && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const ObjectModelContainedTemplate = (
)
}
hideOptionalLabel={uiAttribute?.config?.hideOptionalLabel}
uiAttribute={uiAttribute}
/>
<FormTemplate.Header.Actions>
{canOpen && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const ObjectModelUncontainedTemplate = (
uiAttribute,
onOpen
)
console.log(uiAttribute)
return (
<FormTemplate>
<FormTemplate.Header>
Expand All @@ -60,6 +61,7 @@ export const ObjectModelUncontainedTemplate = (
}
icon={link}
hideOptionalLabel={uiAttribute?.hideOptionalLabel}
uiAttribute={uiAttribute}
/>
<FormTemplate.Header.Actions>
{canOpen && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const ObjectStorageUncontainedTemplate = (props: TObjectTemplate) => {
onOpen?.(namePath, getOpenViewConfig(uiAttribute), address)
}
hideOptionalLabel={uiAttribute?.hideOptionalLabel}
uiAttribute={uiAttribute}
/>
<FormTemplate.Header.Actions>
{canOpen && referenceExists && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div className='border border-[#dddddd] rounded-md'>{children}</div>
Expand Down Expand Up @@ -54,6 +55,7 @@ const FormTemplateHeaderTitle = ({
objectIsNotEmpty,
icon,
hideOptionalLabel,
uiAttribute,
}: {
canExpand: boolean | undefined
canOpen: boolean | undefined
Expand All @@ -64,6 +66,7 @@ const FormTemplateHeaderTitle = ({
onOpen?: () => void
icon?: IconData
hideOptionalLabel?: boolean
uiAttribute?: TUiAttribute
}) => {
const [isHovering, setIsHovering] = useState(false)

Expand Down Expand Up @@ -124,7 +127,7 @@ const FormTemplateHeaderTitle = ({
: ''
}`}
>
{getDisplayLabel(attribute, true)}
{getDisplayLabel(attribute, true, uiAttribute)}
</Typography>
{attribute.optional && !hideOptionalLabel && (
<p className='ps-1 mt-0.5 text-xs'>Optional</p>
Expand Down
1 change: 1 addition & 0 deletions packages/dm-core-plugins/src/form/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type TUiAttributeBase = {
open: boolean
}
hideOptionalLabel?: boolean
label?: string
}
export type TUiAttributeString = TUiAttributeBase & {
widget: string
Expand Down
14 changes: 11 additions & 3 deletions packages/dm-core-plugins/src/form/utils/getDisplayLabel.tsx
Original file line number Diff line number Diff line change
@@ -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)'
Expand Down

0 comments on commit da0b8f4

Please sign in to comment.