Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(28713): change generated name for array items #708

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const northboundMappingListUISchema: UiSchema = {
'ui:order': ['tagName', 'topic', '*'],
'ui:collapsable': {
titleKey: 'tagName',
name: 'Mapping',
},
tagName: {
'ui:widget': registerEntitySelectWidget(CustomFormat.MQTT_TAG),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { LuPanelTopClose, LuPanelTopOpen } from 'react-icons/lu'
import IconButton from '@/components/Chakra/IconButton.tsx'
import { CopyButton, MoveDownButton, MoveUpButton, RemoveButton } from '@/components/rjsf/__internals/IconButton.tsx'
import { useFormControlStore } from '@/components/rjsf/Form/useFormControlStore.ts'
import { formatItemName } from '@/components/rjsf/utils/array-items.utils.ts'

// TODO[NVL] Need a better handling of the custom UISchema property, for the Adapter SDK
interface ArrayFieldItemCollapsableUISchema {
titleKey: string
name: string
}

// TODO[NVL] This is driven by subscription handling; use uiSchema to allow configuration for individual array property
Expand Down Expand Up @@ -49,9 +51,9 @@ export const ArrayFieldItemTemplate: FC<ArrayFieldTemplateItemType> = (props) =>
const childrenFormData = collapsableItems?.titleKey
? children.props.formData[collapsableItems?.titleKey]
: undefined
if (childrenFormData) return `${children.props.name} - ${childrenFormData}`
return children.props.name
}, [children.props.formData, children.props.name, collapsableItems?.titleKey])

return formatItemName(collapsableItems?.name, children.props.index, childrenFormData)
}, [children.props.formData, children.props.index, collapsableItems?.name, collapsableItems?.titleKey])

useEffect(() => {
if (props.children.props.idSchema.$id === expandItems.join('_')) onOpen()
Expand Down Expand Up @@ -82,6 +84,12 @@ export const ArrayFieldItemTemplate: FC<ArrayFieldTemplateItemType> = (props) =>
}
const { hidden, ...rest } = getDisclosureProps()

// This is to override the hardcoded rendering of the item's indexed names
const childrenWithCustomTitle = {
...children,
props: { ...children.props, title: formatItemName(collapsableItems?.name, children.props.index) },
}

return (
<HStack flexDirection="row-reverse" alignItems="flex-start" py={1} role="listitem">
{hasToolbar && (
Expand Down Expand Up @@ -136,7 +144,7 @@ export const ArrayFieldItemTemplate: FC<ArrayFieldTemplateItemType> = (props) =>
)}

<Box w="100%" {...rest}>
{!collapsableItems || isOpen ? children : renderCollapsed()}
{!collapsableItems || isOpen ? childrenWithCustomTitle : renderCollapsed()}
</Box>
</HStack>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'vitest'

import { formatItemName } from '@/components/rjsf/utils/array-items.utils.ts'

describe('formatItemName', () => {
it.each([
{
stub: undefined,
index: 1,
content: undefined,
result: 'item #1',
},
{
stub: 'item',
index: 1,
content: undefined,
result: 'item #1',
},
{
stub: 'item',
index: 1,
content: 'the full name',
result: 'item #1 - the full name',
},
])('should return $value for $path', ({ stub, index, content, result }) => {
expect(formatItemName(stub, index, content)).toStrictEqual(result)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const FORMAT_INDEX_MARKER = '#'
const FORMAT_SEPARATOR = '-'

import i18n from '@/config/i18n.config.ts'

export const formatItemName = (stub: string | undefined, index: number, description?: string) => {
const token = stub || i18n.t('rjsf.ArrayFieldItem.item', { ns: 'components' })
if (!description) return `${token} ${FORMAT_INDEX_MARKER}${index}`
return `${token} ${FORMAT_INDEX_MARKER}${index} ${FORMAT_SEPARATOR} ${description}`
}
1 change: 1 addition & 0 deletions hivemq-edge/src/frontend/src/locales/en/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
}
},
"ArrayFieldItem": {
"item": "item",
"Buttons": {
"expanded_true": "Collapse Item",
"expanded_false": "Expand Item"
Expand Down
Loading