[JS] Handle correctly choice widgets where the display and the export values are different (issue #15815)

This commit is contained in:
Calixte Denizet 2022-12-13 00:07:45 +01:00
parent 64786b4c93
commit 0c1ec946aa
8 changed files with 155 additions and 30 deletions

View file

@ -242,6 +242,11 @@ class Field extends PDFObject {
}
set value(value) {
if (this._isChoice) {
this._setChoiceValue(value);
return;
}
if (value === "") {
this._value = "";
} else if (typeof value === "string") {
@ -260,23 +265,37 @@ class Field extends PDFObject {
} else {
this._value = value;
}
if (this._isChoice) {
if (this.multipleSelection) {
const values = new Set(value);
if (Array.isArray(this._currentValueIndices)) {
this._currentValueIndices.length = 0;
} else {
this._currentValueIndices = [];
}
this._items.forEach(({ displayValue }, i) => {
if (values.has(displayValue)) {
this._currentValueIndices.push(i);
}
});
}
_setChoiceValue(value) {
if (this.multipleSelection) {
if (!Array.isArray(value)) {
value = [value];
}
const values = new Set(value);
if (Array.isArray(this._currentValueIndices)) {
this._currentValueIndices.length = 0;
this._value.length = 0;
} else {
this._currentValueIndices = this._items.findIndex(
({ displayValue }) => value === displayValue
);
this._currentValueIndices = [];
this._value = [];
}
this._items.forEach((item, i) => {
if (values.has(item.exportValue)) {
this._currentValueIndices.push(i);
this._value.push(item.exportValue);
}
});
} else {
if (Array.isArray(value)) {
value = value[0];
}
const index = this._items.findIndex(
({ exportValue }) => value === exportValue
);
if (index !== -1) {
this._currentValueIndices = index;
this._value = this._items[index].exportValue;
}
}
}