Skip to content

Commit 4c826bc

Browse files
authored
Merge pull request #238 from formio/FIO-9964-fixed-getComponent-when-form-has-several-components-with-same-keys
2.3.x: FIO-9964: fixed an issue where getComponent returns wrong component when the form includes several components with the same key
2 parents dfe3e6f + ebc2ee0 commit 4c826bc

2 files changed

Lines changed: 118 additions & 2 deletions

File tree

src/utils/__tests__/formUtil.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,122 @@ describe('formUtil', function () {
7474
expect(component?.key).to.equal(writtenNumber(n));
7575
}
7676
});
77+
78+
it('Should return correct component when the form has 2 components with the same key', function () {
79+
const component = getComponent(
80+
[
81+
{
82+
label: 'Tabs',
83+
components: [
84+
{
85+
label: 'Tab1',
86+
key: 'tab1',
87+
components: [
88+
{
89+
label: 'Text Field',
90+
tableView: true,
91+
validateWhenHidden: false,
92+
key: 'someDuplicatedKey',
93+
type: 'textfield',
94+
input: true,
95+
},
96+
],
97+
},
98+
{
99+
label: 'Tab2',
100+
key: 'tab2',
101+
components: [
102+
{
103+
label: 'Another container',
104+
tableView: false,
105+
key: 'anotherContainerKey',
106+
type: 'container',
107+
input: true,
108+
components: [
109+
{
110+
label:
111+
"A select with the same key of a container in tab1. Select the option 'more'",
112+
widget: 'choicesjs',
113+
tableView: true,
114+
data: {
115+
values: [
116+
{
117+
label: 'less',
118+
value: 'less',
119+
},
120+
{
121+
label: 'more',
122+
value: 'more',
123+
},
124+
],
125+
},
126+
validateWhenHidden: false,
127+
key: 'someDuplicatedKey',
128+
type: 'select',
129+
input: true,
130+
},
131+
{
132+
label: 'Here select yes, value will disappear',
133+
tableView: false,
134+
validateWhenHidden: false,
135+
key: 'additionalContainer',
136+
conditional: {
137+
show: true,
138+
conjunction: 'all',
139+
conditions: [
140+
{
141+
component: 'someDuplicatedKey',
142+
operator: 'isEqual',
143+
value: 'more',
144+
},
145+
],
146+
},
147+
type: 'container',
148+
input: true,
149+
components: [
150+
{
151+
label: 'Select yes here, the value will disappear upon save',
152+
labelPosition: 'left-left',
153+
optionsLabelPosition: 'right',
154+
inline: true,
155+
tableView: false,
156+
values: [
157+
{
158+
label: 'yes',
159+
value: 'yes',
160+
shortcut: '',
161+
},
162+
{
163+
label: 'no',
164+
value: 'no',
165+
shortcut: '',
166+
},
167+
],
168+
validateWhenHidden: false,
169+
key: 'aradiobutton',
170+
type: 'radio',
171+
input: true,
172+
},
173+
],
174+
},
175+
],
176+
},
177+
],
178+
},
179+
],
180+
key: 'tabs',
181+
type: 'tabs',
182+
input: false,
183+
tableView: false,
184+
},
185+
],
186+
'someDuplicatedKey',
187+
);
188+
expect(component).not.to.equal(null);
189+
expect(component).not.to.equal(undefined);
190+
expect(component).to.be.an('object');
191+
expect((component as any).type).to.equal('textfield');
192+
});
77193
});
78194

79195
describe('flattenComponents', function () {

src/utils/formUtil/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ export function getComponent(
421421
key: any,
422422
includeAll: any = false,
423423
): Component | undefined {
424-
let result;
424+
let result: Component | undefined;
425425
eachComponent(
426426
components,
427427
(component: Component, path: any) => {
428-
if (path === key || (component.input && component.key === key)) {
428+
if ((path === key || (component.input && component.key === key)) && !result) {
429429
result = component;
430430
return true;
431431
}

0 commit comments

Comments
 (0)