Skip to content

Commit 6e6d94a

Browse files
authored
Merge pull request #15020 from calixteman/1773680
Add an empty entry in combo list when nothing is selected (bug 1773680)
2 parents d45ce8d + bfe816d commit 6e6d94a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/display/annotation_layer.js

+20
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,8 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
14611461
selectElement.setAttribute("id", id);
14621462
selectElement.tabIndex = DEFAULT_TAB_INDEX;
14631463

1464+
let addAnEmptyEntry = this.data.combo && this.data.options.length > 0;
1465+
14641466
if (!this.data.combo) {
14651467
// List boxes have a size and (optionally) multiple selection.
14661468
selectElement.size = this.data.options.length;
@@ -1486,10 +1488,27 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
14861488
}
14871489
if (storedData.value.includes(option.exportValue)) {
14881490
optionElement.setAttribute("selected", true);
1491+
addAnEmptyEntry = false;
14891492
}
14901493
selectElement.appendChild(optionElement);
14911494
}
14921495

1496+
let removeEmptyEntry = null;
1497+
if (addAnEmptyEntry) {
1498+
const noneOptionElement = document.createElement("option");
1499+
noneOptionElement.value = " ";
1500+
noneOptionElement.setAttribute("hidden", true);
1501+
noneOptionElement.setAttribute("selected", true);
1502+
selectElement.insertBefore(noneOptionElement, selectElement.firstChild);
1503+
1504+
removeEmptyEntry = () => {
1505+
noneOptionElement.remove();
1506+
selectElement.removeEventListener("input", removeEmptyEntry);
1507+
removeEmptyEntry = null;
1508+
};
1509+
selectElement.addEventListener("input", removeEmptyEntry);
1510+
}
1511+
14931512
const getValue = (event, isExport) => {
14941513
const name = isExport ? "value" : "textContent";
14951514
const options = event.target.options;
@@ -1514,6 +1533,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
15141533
selectElement.addEventListener("updatefromsandbox", jsEvent => {
15151534
const actions = {
15161535
value(event) {
1536+
removeEmptyEntry?.();
15171537
const value = event.detail.value;
15181538
const values = new Set(Array.isArray(value) ? value : [value]);
15191539
for (const option of selectElement.options) {

0 commit comments

Comments
 (0)