Skip to content

Commit b0b8afc

Browse files
committed
Fix perfornace issue with Combobox
1 parent 844458b commit b0b8afc

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

packages/controls/src/widget_string.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -436,32 +436,36 @@ class ComboboxView extends TextView {
436436
update(options?: any) {
437437
super.update(options);
438438
if (!this.datalist) {
439-
return;
439+
return;
440440
}
441441

442442
const valid = this.isValid(this.model.get('value'));
443443
this.highlightValidState(valid);
444444

445445
// Check if we need to update options
446-
if (options !== undefined && options.updated_view) {
447-
// Value update only, keep current options
448-
return;
446+
if ((options !== undefined && options.updated_view) || (
447+
!this.model.hasChanged('options') &&
448+
!this.isInitialRender
449+
)) {
450+
// Value update only, keep current options
451+
return;
449452
}
450453

451-
this.datalist.innerHTML = '';
452-
for (let opt of this.model.get('options') as string[]) {
453-
let el = document.createElement('option');
454-
el.value = opt;
455-
this.datalist.appendChild(el);
456-
}
454+
this.isInitialRender = false;
455+
456+
const opts = this.model.get('options') as string[];
457+
const optLines = opts.map(o => {
458+
return `<option value="${o}"></option>`;
459+
});
460+
this.datalist.innerHTML = optLines.join('\n');
457461
}
458462

459463
isValid(value: string): boolean {
460464
if (true === this.model.get('ensure_option')) {
461-
const options = this.model.get('options') as string[];
462-
if (options.indexOf(value) === -1) {
463-
return false;
464-
}
465+
const options = this.model.get('options') as string[];
466+
if (options.indexOf(value) === -1) {
467+
return false;
468+
}
465469
}
466470
return true;
467471
}
@@ -472,7 +476,7 @@ class ComboboxView extends TextView {
472476
const valid = this.isValid(target.value);
473477
this.highlightValidState(valid);
474478
if (valid) {
475-
super.handleChanging(e);
479+
super.handleChanging(e);
476480
}
477481
}
478482

@@ -482,7 +486,7 @@ class ComboboxView extends TextView {
482486
const valid = this.isValid(target.value);
483487
this.highlightValidState(valid);
484488
if (valid) {
485-
super.handleChanged(e);
489+
super.handleChanged(e);
486490
}
487491
}
488492

@@ -491,4 +495,6 @@ class ComboboxView extends TextView {
491495
}
492496

493497
datalist: HTMLDataListElement | undefined;
498+
499+
isInitialRender = true;
494500
}

0 commit comments

Comments
 (0)