Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace datalist children instead of appending #3682

Merged
merged 2 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/controls/src/widget_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class ComboboxView extends TextView {
o.value = v;
optionFragment.appendChild(o);
}
this.datalist.appendChild(optionFragment);
this.datalist.replaceChildren(...optionFragment.children);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be faster (and more consistent) with this.datalist.replaceChildren(optionFragment) ?

}

isValid(value: string): boolean {
Expand Down
27 changes: 27 additions & 0 deletions packages/controls/test/src/widget_string_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,31 @@ describe('ComboboxView', function () {
expect(el.getAttribute('value')).to.equal(input[i]);
}
});

it('updates datalist children when options are updated', function () {
this.model.set({
value: 'ABC',
options: ['option1', 'option2', 'option3'],
ensure_option: true,
});
const options = { model: this.model };
const view = new widgets.ComboboxView(options);
view.render();
expect(view.datalist!.children.length).to.equal(3);
for (let i = 0; i < view.datalist!.children.length; ++i) {
const el = view.datalist!.children[i];
expect(el.tagName.toLowerCase()).to.equal('option');
expect(el.getAttributeNames()).to.eqls(['value']);
expect(el.getAttribute('value')).to.equal(`option${i + 1}`);
}

this.model.set({ options: ['option4', 'option5'] });
expect(view.datalist!.children.length).to.equal(2);
for (let i = 0; i < view.datalist!.children.length; ++i) {
const el = view.datalist!.children[i];
expect(el.tagName.toLowerCase()).to.equal('option');
expect(el.getAttributeNames()).to.eqls(['value']);
expect(el.getAttribute('value')).to.equal(`option${i + 4}`);
}
});
});