Skip to content

Commit f0866ee

Browse files
authored
feat(entities-shared): improve display for config card json text (#2040)
1 parent 8748d98 commit f0866ee

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

packages/entities/entities-shared/fixtures/mockData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export const gatewayServiceRecord = {
119119
tls_verify: true,
120120
updated_at: 1686157266,
121121
write_timeout: 60000,
122+
extra: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
122123
}
123124

124125
export const emptyKey = 'preferred_chain'

packages/entities/entities-shared/sandbox/pages/ConfigCardItemPage.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@
133133
}"
134134
/>
135135

136+
<h2>JSON Text</h2>
137+
<ConfigCardItem
138+
:item="{
139+
type: ConfigurationSchemaType.Text,
140+
key: 'json-code',
141+
label: 'Cat Data',
142+
value: {
143+
name: 'TK Meowstersmith',
144+
species: 'Awesome cat',
145+
color: 'All black',
146+
awesome: 'true',
147+
}
148+
}"
149+
/>
150+
136151
<h2>JSON Object Array</h2>
137152
<ConfigCardItem
138153
:item="{

packages/entities/entities-shared/src/components/entity-base-config-card/ConfigCardItem.cy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,31 @@ describe('<ConfigCardItem />', () => {
246246
}
247247
})
248248

249+
it('renders JSON text field correctly', () => {
250+
const obj: Record<string, string> = {
251+
name: 'TK Meowstersmith',
252+
species: 'Awesome cat',
253+
color: 'All black',
254+
awesome: 'true',
255+
}
256+
const item: RecordItem = {
257+
type: ConfigurationSchemaType.Text,
258+
key: 'cat_data',
259+
label: 'Cat Data',
260+
value: obj,
261+
}
262+
263+
cy.mount(ConfigCardItem, {
264+
props: {
265+
item,
266+
},
267+
})
268+
269+
cy.get('.config-card-details-row').should('be.visible')
270+
cy.getTestId(`${item.key}-json-code`).should('be.visible')
271+
cy.getTestId(`${item.key}-json-code`).should('contain.text', JSON.stringify(obj, null, 2))
272+
})
273+
249274
it('renders a JSON Array field correctly', () => {
250275
const obj: Record<string, string>[] = [{
251276
name: 'TK Meowstersmith',

packages/entities/entities-shared/src/components/entity-base-config-card/ConfigCardItem.vue

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</div>
6969

7070
<div
71-
v-if="componentAttrsData.additionalComponent === 'KCopy'"
71+
v-else-if="componentAttrsData.additionalComponent === 'KCopy'"
7272
class="copy-uuid-array"
7373
:data-testid="`${item.key}-copy-uuid-array`"
7474
>
@@ -98,7 +98,7 @@
9898
</div>
9999

100100
<div
101-
v-if="componentAttrsData.additionalComponent === 'JsonCardItem'"
101+
v-else-if="componentAttrsData.additionalComponent === 'JsonCardItem'"
102102
:data-testid="`${props.item.key}-json-array-content`"
103103
>
104104
<JsonCardItem
@@ -131,7 +131,7 @@
131131

132132
<script setup lang="ts">
133133
import type { PropType, Ref } from 'vue'
134-
import { computed, ref, useSlots } from 'vue'
134+
import { computed, ref, useId, useSlots } from 'vue'
135135
import type { RecordItem, ComponentAttrsData } from '../../types'
136136
import { ConfigurationSchemaType } from '../../types'
137137
import composables from '../../composables'
@@ -162,6 +162,8 @@ const emit = defineEmits<{
162162
(e: 'navigation-click', record: RecordItem): void,
163163
}>()
164164
165+
const uniqueId = useId()
166+
165167
const slots = useSlots()
166168
const { i18n: { t, formatUnixTimeStamp } } = composables.useI18n()
167169
@@ -306,6 +308,20 @@ const componentAttrsData = computed((): ComponentAttrsData => {
306308
}
307309
308310
default:
311+
// Before fallback to plain text, check if the value is an object, if so, render it as a code block
312+
if (props.item.value != null && typeof props.item.value === 'object') {
313+
return {
314+
tag: 'KCodeBlock',
315+
attrs: {
316+
'data-testid': `${props.item.key}-json-code`,
317+
id: `json-code-${uniqueId}`,
318+
language: 'json',
319+
code: JSON.stringify(props.item.value, null, ' '),
320+
maxHeight: '480px',
321+
showLineNumbers: false,
322+
},
323+
}
324+
}
309325
return {
310326
tag: 'div',
311327
attrs: {

packages/entities/entities-shared/src/components/entity-base-config-card/EntityBaseConfigCard.cy.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const customizedKey = 'ca_certificates'
2929
const customTooltip = 'Custom tooltip'
3030
const customLabel = 'CA Certificates'
3131
const unconfiguredKey = 'client_certificate'
32+
const untypedKey = 'extra'
3233
const configSchema: ConfigurationSchema = {
3334
protocol: {
3435
section: ConfigurationSchemaSection.Basic,
@@ -68,6 +69,9 @@ const configSchema: ConfigurationSchema = {
6869
order: 6,
6970
section: ConfigurationSchemaSection.Advanced,
7071
},
72+
[untypedKey]: {
73+
order: 5,
74+
},
7175
[unconfiguredKey]: {
7276
// leave this object empty for tests!!
7377
},
@@ -221,7 +225,16 @@ describe('<EntityBaseConfigCard />', () => {
221225
// no tooltip
222226
cy.getTestId(`${unconfiguredKey}-label-tooltip`).should('not.exist')
223227
// type defaults to plain text
224-
cy.getTestId(`${unconfiguredKey}-plain-text`).should('be.visible')
228+
cy.getTestId(`${unconfiguredKey}-json-code`).should('be.visible')
229+
230+
// section defaults to Advanced (and hidden defaults to false)
231+
cy.get(`[data-testid="config-card-details-advanced-props"] [data-testid="${untypedKey}-label"]`).should('exist')
232+
// title cases key for the label
233+
cy.getTestId(`${untypedKey}-label`).should('contain.text', convertKeyToTitle(untypedKey))
234+
// no tooltip
235+
cy.getTestId(`${untypedKey}-label-tooltip`).should('not.exist')
236+
// type defaults to plain text
237+
cy.getTestId(`${untypedKey}-plain-text`).should('be.visible')
225238
})
226239

227240
it('allows customizing label and label tooltip', () => {

0 commit comments

Comments
 (0)