From c01ef24541e1263d2e5f74c1e37b25b373f006cc Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sat, 6 Mar 2021 17:19:57 +0100 Subject: [PATCH] JS - reset correctly radio buttons --- src/display/annotation_layer.js | 12 ++++-------- src/scripting_api/field.js | 3 +++ test/integration/scripting_spec.js | 9 +++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 06ca8e9db..52e810429 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -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() { diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index 3435957a8..5432218cc 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -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]; diff --git a/test/integration/scripting_spec.js b/test/integration/scripting_spec.js index 039f9f193..2c9a00402 100644 --- a/test/integration/scripting_spec.js +++ b/test/integration/scripting_spec.js @@ -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); }) ); });