Skip to content

Commit e0bd0f1

Browse files
dsorianoweaverryan
authored andcommitted
Fix option tag synchronization
1 parent 36c1aee commit e0bd0f1

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ function executeMorphdom(rootFromElement, rootToElement, modifiedFieldElements,
12371237
if (elementChanges) {
12381238
elementChanges.applyToElement(toEl);
12391239
}
1240-
if (fromEl.isEqualNode(toEl)) {
1240+
if (fromEl.nodeName.toUpperCase() !== 'OPTION' && fromEl.isEqualNode(toEl)) {
12411241
const normalizedFromEl = cloneHTMLElement(fromEl);
12421242
normalizeAttributesForComparison(normalizedFromEl);
12431243
const normalizedToEl = cloneHTMLElement(toEl);

src/LiveComponent/assets/src/morphdom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function executeMorphdom(
8484
}
8585

8686
// https://github.com/patrick-steele-idem/morphdom#can-i-make-morphdom-blaze-through-the-dom-tree-even-faster-yes
87-
if (fromEl.isEqualNode(toEl)) {
87+
if (fromEl.nodeName.toUpperCase() !== 'OPTION' && fromEl.isEqualNode(toEl)) {
8888
// the nodes are equal, but the "value" on some might differ
8989
// lets try to quickly compare a bit more deeply
9090
const normalizedFromEl = cloneHTMLElement(fromEl);

src/LiveComponent/assets/test/controller/render.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,58 @@ describe('LiveController rendering Tests', () => {
388388
// verify the component *did* render ok
389389
expect(test.element).toHaveTextContent('The season is: autumn');
390390
});
391+
392+
it('select the placeholder option tag after render', async () => {
393+
const test = await createTest({}, (data: any) => `
394+
<div ${initComponent(data)}>
395+
<form>
396+
<select id="select_option_1">
397+
<option value="">Choose option 1</option>
398+
<option value="1">One</option>
399+
<option value="2">Two</option>
400+
<option value="3">Three</option>
401+
</select>
402+
403+
<select id="select_option_2">
404+
<option value="">Choose option 2</option>
405+
<option value="1_1">One - One</option>
406+
<option value="1_2">One - Two</option>
407+
<option value="2_1">Two - One</option>
408+
<option value="2_2">Two - Two</option>
409+
<option value="3_1">Three - One</option>
410+
<option value="3_2">Three - Two</option>
411+
</select>
412+
</form>
413+
</div>
414+
`);
415+
416+
test.expectsAjaxCall()
417+
.willReturn((data) => `
418+
<div ${initComponent(data)}>
419+
<form>
420+
<select id="select_option_1">
421+
<option value="">Choose option 1</option>
422+
<option value="1">One</option>
423+
<option value="2" selected>Two</option>
424+
<option value="3">Three</option>
425+
</select>
426+
427+
<select id="select_option_2">
428+
<option value="">Choose option 2</option>
429+
<option value="2_1">Two - One</option>
430+
<option value="2_2">Two - Two</option>
431+
</select>
432+
</form>
433+
</div>
434+
`);
435+
436+
await test.component.render();
437+
const selectOption2 = test.element.querySelector('#select_option_2') as HTMLSelectElement;
438+
439+
// verify the placeholder of the select option 2 is selected
440+
expect(selectOption2.children[0].hasAttribute('selected')).toBe(true);
441+
442+
// verify the selectedIndex of the select option 2 is 0
443+
expect(selectOption2.selectedIndex).toBe(0);
444+
});
391445
});

0 commit comments

Comments
 (0)