JS - reset correctly radio buttons

This commit is contained in:
Calixte Denizet 2021-03-06 17:19:57 +01:00
parent 4b49db7c95
commit c01ef24541
3 changed files with 16 additions and 8 deletions

View File

@ -1002,7 +1002,6 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
if (value) {
element.setAttribute("checked", true);
}
element.setAttribute("pdfButtonValue", data.buttonValue);
element.setAttribute("id", id);
element.addEventListener("change", function (event) {
@ -1016,19 +1015,16 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
});
if (this.enableScripting && this.hasJSActions) {
const pdfButtonValue = data.buttonValue;
element.addEventListener("updatefromsandbox", event => {
const { detail } = event;
const actions = {
value() {
const fieldValue = detail.value;
const checked = pdfButtonValue === detail.value;
for (const radio of document.getElementsByName(event.target.name)) {
const radioId = radio.getAttribute("id");
if (fieldValue === radio.getAttribute("pdfButtonValue")) {
radio.setAttribute("checked", true);
storage.setValue(radioId, { value: true });
} else {
storage.setValue(radioId, { value: false });
}
radio.checked = radioId === id && checked;
storage.setValue(radioId, { value: radio.checked });
}
},
focus() {

View File

@ -481,6 +481,9 @@ class RadioButtonField extends Field {
}
set value(value) {
if (value === null) {
this._value = "";
}
const i = this.exportValues.indexOf(value);
if (0 <= i && i < this._radioIds.length) {
this._id = this._radioIds[i];

View File

@ -174,9 +174,15 @@ describe("Interaction", () => {
it("must reset all", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
// click on a radio button
await page.click("[data-annotation-id='449R']");
// this field has no actions but it must be cleared on reset
await page.type("#\\34 05R", "employee", { delay: 200 });
let checked = await page.$eval("#\\34 49R", el => el.checked);
expect(checked).toEqual(true);
// click on reset button
await page.click("[data-annotation-id='402R']");
@ -194,6 +200,9 @@ describe("Interaction", () => {
const sum = await page.$eval("#\\34 27R", el => el.value);
expect(sum).toEqual("");
checked = await page.$eval("#\\34 49R", el => el.checked);
expect(checked).toEqual(false);
})
);
});