Improve the field value parsing for choice widgets to handle null
values
The specification states that the field value is `null` if no item is selected and we didn't handle this case properly. Even though this did not break the rendering because we always convert the value to an array and the `includes` check in the display layer would simply not match, the field value would be `[null]` which is not expected and strange from an API perspective. This commit fixes that by ensuring that we return an empty array in case the field value is `null`. The API therefore still always gives an array for the field value, but now the code is more specific so that the value is either an empty array or an array of strings.
This commit is contained in:
parent
3ca037af78
commit
12c20772ac
@ -1610,11 +1610,14 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the field value. In this case, it may be a string or an
|
// The field value can be `null` if no item is selected, a string if one
|
||||||
// array of strings. For convenience in the display layer, convert the
|
// item is selected or an array of strings if multiple items are selected.
|
||||||
// string to an array of one string as well.
|
// For consistency in the API and convenience in the display layer, we
|
||||||
if (!Array.isArray(this.data.fieldValue)) {
|
// always make the field value an array with zero, one or multiple items.
|
||||||
|
if (isString(this.data.fieldValue)) {
|
||||||
this.data.fieldValue = [this.data.fieldValue];
|
this.data.fieldValue = [this.data.fieldValue];
|
||||||
|
} else if (!this.data.fieldValue) {
|
||||||
|
this.data.fieldValue = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process field flags for the display layer.
|
// Process field flags for the display layer.
|
||||||
|
@ -2479,8 +2479,8 @@ describe("annotation", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should convert the field value to an array", function (done) {
|
it("should convert the field value to an array", function (done) {
|
||||||
const inputs = ["Foo", ["Foo", "Bar"]];
|
const inputs = [null, "Foo", ["Foo", "Bar"]];
|
||||||
const outputs = [["Foo"], ["Foo", "Bar"]];
|
const outputs = [[], ["Foo"], ["Foo", "Bar"]];
|
||||||
|
|
||||||
let promise = Promise.resolve();
|
let promise = Promise.resolve();
|
||||||
for (let i = 0, ii = inputs.length; i < ii; i++) {
|
for (let i = 0, ii = inputs.length; i < ii; i++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user