Skip to content

Commit 6547f84

Browse files
authored
Merge pull request #3682 from jasongrout/replacedatalist
Replace datalist children instead of appending
2 parents e5420b8 + 1344dab commit 6547f84

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/controls/src/widget_string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ export class ComboboxView extends TextView {
671671
o.value = v;
672672
optionFragment.appendChild(o);
673673
}
674-
this.datalist.appendChild(optionFragment);
674+
this.datalist.replaceChildren(...optionFragment.children);
675675
}
676676

677677
isValid(value: string): boolean {

packages/controls/test/src/widget_string_test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,31 @@ describe('ComboboxView', function () {
8989
expect(el.getAttribute('value')).to.equal(input[i]);
9090
}
9191
});
92+
93+
it('updates datalist children when options are updated', function () {
94+
this.model.set({
95+
value: 'ABC',
96+
options: ['option1', 'option2', 'option3'],
97+
ensure_option: true,
98+
});
99+
const options = { model: this.model };
100+
const view = new widgets.ComboboxView(options);
101+
view.render();
102+
expect(view.datalist!.children.length).to.equal(3);
103+
for (let i = 0; i < view.datalist!.children.length; ++i) {
104+
const el = view.datalist!.children[i];
105+
expect(el.tagName.toLowerCase()).to.equal('option');
106+
expect(el.getAttributeNames()).to.eqls(['value']);
107+
expect(el.getAttribute('value')).to.equal(`option${i + 1}`);
108+
}
109+
110+
this.model.set({ options: ['option4', 'option5'] });
111+
expect(view.datalist!.children.length).to.equal(2);
112+
for (let i = 0; i < view.datalist!.children.length; ++i) {
113+
const el = view.datalist!.children[i];
114+
expect(el.tagName.toLowerCase()).to.equal('option');
115+
expect(el.getAttributeNames()).to.eqls(['value']);
116+
expect(el.getAttribute('value')).to.equal(`option${i + 4}`);
117+
}
118+
});
92119
});

0 commit comments

Comments
 (0)