Skip to content

Commit eeda63d

Browse files
authored
Merge pull request #708
* fix(28713): fix the generated name of array items * test(28713): add tests * fix(28713): add stub * fix(28713): fix default name when no ui:name given
1 parent 03f37cd commit eeda63d

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

hivemq-edge/src/frontend/src/api/schemas/northbound.ui-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const northboundMappingListUISchema: UiSchema = {
1515
'ui:order': ['tagName', 'topic', '*'],
1616
'ui:collapsable': {
1717
titleKey: 'tagName',
18+
name: 'Mapping',
1819
},
1920
tagName: {
2021
'ui:widget': registerEntitySelectWidget(CustomFormat.MQTT_TAG),

hivemq-edge/src/frontend/src/components/rjsf/ArrayFieldItemTemplate.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { LuPanelTopClose, LuPanelTopOpen } from 'react-icons/lu'
77
import IconButton from '@/components/Chakra/IconButton.tsx'
88
import { CopyButton, MoveDownButton, MoveUpButton, RemoveButton } from '@/components/rjsf/__internals/IconButton.tsx'
99
import { useFormControlStore } from '@/components/rjsf/Form/useFormControlStore.ts'
10+
import { formatItemName } from '@/components/rjsf/utils/array-items.utils.ts'
1011

1112
// TODO[NVL] Need a better handling of the custom UISchema property, for the Adapter SDK
1213
interface ArrayFieldItemCollapsableUISchema {
1314
titleKey: string
15+
name: string
1416
}
1517

1618
// TODO[NVL] This is driven by subscription handling; use uiSchema to allow configuration for individual array property
@@ -49,9 +51,9 @@ export const ArrayFieldItemTemplate: FC<ArrayFieldTemplateItemType> = (props) =>
4951
const childrenFormData = collapsableItems?.titleKey
5052
? children.props.formData[collapsableItems?.titleKey]
5153
: undefined
52-
if (childrenFormData) return `${children.props.name} - ${childrenFormData}`
53-
return children.props.name
54-
}, [children.props.formData, children.props.name, collapsableItems?.titleKey])
54+
55+
return formatItemName(collapsableItems?.name, children.props.index, childrenFormData)
56+
}, [children.props.formData, children.props.index, collapsableItems?.name, collapsableItems?.titleKey])
5557

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

87+
// This is to override the hardcoded rendering of the item's indexed names
88+
const childrenWithCustomTitle = {
89+
...children,
90+
props: { ...children.props, title: formatItemName(collapsableItems?.name, children.props.index) },
91+
}
92+
8593
return (
8694
<HStack flexDirection="row-reverse" alignItems="flex-start" py={1} role="listitem">
8795
{hasToolbar && (
@@ -136,7 +144,7 @@ export const ArrayFieldItemTemplate: FC<ArrayFieldTemplateItemType> = (props) =>
136144
)}
137145

138146
<Box w="100%" {...rest}>
139-
{!collapsableItems || isOpen ? children : renderCollapsed()}
147+
{!collapsableItems || isOpen ? childrenWithCustomTitle : renderCollapsed()}
140148
</Box>
141149
</HStack>
142150
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect } from 'vitest'
2+
3+
import { formatItemName } from '@/components/rjsf/utils/array-items.utils.ts'
4+
5+
describe('formatItemName', () => {
6+
it.each([
7+
{
8+
stub: undefined,
9+
index: 1,
10+
content: undefined,
11+
result: 'item #1',
12+
},
13+
{
14+
stub: 'item',
15+
index: 1,
16+
content: undefined,
17+
result: 'item #1',
18+
},
19+
{
20+
stub: 'item',
21+
index: 1,
22+
content: 'the full name',
23+
result: 'item #1 - the full name',
24+
},
25+
])('should return $value for $path', ({ stub, index, content, result }) => {
26+
expect(formatItemName(stub, index, content)).toStrictEqual(result)
27+
})
28+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const FORMAT_INDEX_MARKER = '#'
2+
const FORMAT_SEPARATOR = '-'
3+
4+
import i18n from '@/config/i18n.config.ts'
5+
6+
export const formatItemName = (stub: string | undefined, index: number, description?: string) => {
7+
const token = stub || i18n.t('rjsf.ArrayFieldItem.item', { ns: 'components' })
8+
if (!description) return `${token} ${FORMAT_INDEX_MARKER}${index}`
9+
return `${token} ${FORMAT_INDEX_MARKER}${index} ${FORMAT_SEPARATOR} ${description}`
10+
}

hivemq-edge/src/frontend/src/locales/en/components.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
}
119119
},
120120
"ArrayFieldItem": {
121+
"item": "item",
121122
"Buttons": {
122123
"expanded_true": "Collapse Item",
123124
"expanded_false": "Expand Item"

0 commit comments

Comments
 (0)