Skip to content

Commit bda9042

Browse files
Joren BroekemadaKmoR
authored andcommitted
fix(field): add form elements in the correct order
1 parent 8a4dd7e commit bda9042

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

packages/field/src/FormRegistrarMixin.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ export const FormRegistrarMixin = dedupeMixin(
6363
this.__hasBeenRendered = true;
6464
}
6565

66-
addFormElement(child) {
66+
addFormElement(child, index) {
6767
// This is a way to let the child element (a lion-fieldset or lion-field) know, about its parent
6868
// eslint-disable-next-line no-param-reassign
6969
child.__parentFormGroup = this;
7070

71-
this.formElements.push(child);
71+
if (index > 0) {
72+
this.formElements.splice(index, 0, child);
73+
} else {
74+
this.formElements.push(child);
75+
}
7276
}
7377

7478
removeFormElement(child) {
@@ -89,7 +93,14 @@ export const FormRegistrarMixin = dedupeMixin(
8993
return;
9094
}
9195
ev.stopPropagation();
92-
this.addFormElement(child);
96+
97+
// Check for siblings to determine the right order to insert into formElements
98+
// If there is no next sibling, index is -1
99+
let indexToInsertAt = -1;
100+
if (this.formElements && Array.isArray(this.formElements)) {
101+
indexToInsertAt = this.formElements.indexOf(child.nextElementSibling);
102+
}
103+
this.addFormElement(child, indexToInsertAt);
93104
}
94105

95106
_onRequestToRemoveFormElement(ev) {

packages/field/test-suites/FormRegistrationMixins.suite.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,44 @@ export const runRegistrationSuite = customConfig => {
168168
expect(el.formElements.length).to.equal(1);
169169
});
170170

171+
it('adds elements to formElements in the right order', async () => {
172+
const el = await fixture(html`
173+
<${parentTag}>
174+
<${childTag}></${childTag}>
175+
<${childTag}></${childTag}>
176+
<${childTag}></${childTag}>
177+
</${parentTag}>
178+
`);
179+
180+
expect(el.formElements.length).to.equal(3);
181+
182+
// In the middle
183+
const secondChild = el.firstElementChild.nextElementSibling;
184+
const newField = await fixture(html`
185+
<${childTag}></${childTag}>
186+
`);
187+
secondChild.insertAdjacentElement('beforebegin', newField);
188+
189+
expect(el.formElements.length).to.equal(4);
190+
expect(el.formElements[1]).dom.to.equal(newField);
191+
192+
// Prepending
193+
const anotherField = await fixture(html`
194+
<${childTag}></${childTag}>
195+
`);
196+
el.prepend(anotherField);
197+
expect(el.formElements.length).to.equal(5);
198+
expect(el.formElements[0]).dom.to.equal(anotherField);
199+
200+
// Appending
201+
const yetAnotherField = await fixture(html`
202+
<${childTag}></${childTag}>
203+
`);
204+
el.appendChild(yetAnotherField);
205+
expect(el.formElements.length).to.equal(6);
206+
expect(el.formElements[5]).dom.to.equal(anotherField);
207+
});
208+
171209
// find a proper way to do this on polyfilled browsers
172210
it.skip('fires event "form-element-register" with the child as ev.target', async () => {
173211
const registerSpy = sinon.spy();

0 commit comments

Comments
 (0)