Skip to content

Commit 41b7991

Browse files
kchobantonovlucas-koehler
authored andcommitted
fix(vue): support elementLabelProp in array controls
1 parent 56c6628 commit 41b7991

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎packages/vue-vanilla/src/util/composition.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const useVanillaArrayControl = <I extends { control: any }>(
111111

112112
const childLabelForIndex = (index: number) => {
113113
const childLabelProp =
114+
input.control.value.uischema.options?.elementLabelProp ??
114115
input.control.value.uischema.options?.childLabelProp ??
115116
getFirstPrimitiveProp(input.control.value.schema);
116117
if (!childLabelProp) {

‎packages/vue-vanilla/tests/unit/array/ArrayListRenderer.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ describe('ArrayListRenderer.vue', () => {
154154
expect(labelText).to.equal('1');
155155
});
156156

157+
it('uses elementLabelProp for the item label', () => {
158+
const wrapper = mountJsonForms(
159+
[{ name: 'name1', rate: 5 }],
160+
schemaWithNameAndRate,
161+
{
162+
...uischema,
163+
options: {
164+
...uischema.options,
165+
elementLabelProp: 'rate',
166+
childLabelProp: 'name',
167+
},
168+
}
169+
);
170+
const label = wrapper.find('.array-list-item-label');
171+
172+
expect(label.text()).to.equal('5');
173+
});
174+
157175
it('compute default label with NaN', async () => {
158176
const wrapper = mountJsonForms(
159177
[{ count: Number(undefined), name: 'name1' }],

‎packages/vue-vuetify/src/util/composition.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ export const useVuetifyArrayControl = <
387387
return '';
388388
}
389389
const childLabelProp =
390+
input.control.value.uischema.options?.elementLabelProp ??
390391
input.control.value.uischema.options?.childLabelProp ??
391392
getFirstPrimitiveProp(input.control.value.schema);
392393
if (!childLabelProp) {

‎packages/vue-vuetify/tests/unit/util/composition.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ const makeError = (instancePath: string): ErrorObject => ({
1313
});
1414

1515
describe('useVuetifyArrayControl', () => {
16+
it('uses elementLabelProp before the legacy childLabelProp', () => {
17+
let captured: ReturnType<typeof useVuetifyArrayControl> | undefined;
18+
19+
const Child = defineComponent({
20+
setup() {
21+
const control = computed(() => ({
22+
label: '',
23+
required: false,
24+
config: {},
25+
uischema: {
26+
type: 'Control',
27+
scope: '#',
28+
options: {
29+
elementLabelProp: 'message2',
30+
childLabelProp: 'message1',
31+
},
32+
},
33+
schema: {
34+
type: 'object',
35+
properties: {
36+
message1: { type: 'string' },
37+
message2: { type: 'string' },
38+
},
39+
},
40+
data: [{ message1: 'Message one', message2: 'Message two' }],
41+
childErrors: [],
42+
i18nKeyPrefix: '',
43+
}));
44+
captured = useVuetifyArrayControl({ control });
45+
return () => h('div');
46+
},
47+
});
48+
49+
const Host = defineComponent({
50+
setup() {
51+
provide('jsonforms', {
52+
core: { data: {}, schema: {}, uischema: { type: 'Control' } },
53+
i18n: {
54+
translate: (_id: string, defaultMessage?: string) => defaultMessage,
55+
},
56+
});
57+
return () => h(Child);
58+
},
59+
});
60+
61+
mount(Host);
62+
63+
expect(captured!.childLabelForIndex(0)).toBe('Message two');
64+
});
65+
1666
it('exposes a reactive rawChildErrors that reflects child error changes', async () => {
1767
const childErrors = ref<ErrorObject[]>([]);
1868

0 commit comments

Comments
 (0)