Skip to content

JS - reset correctly radio buttons #13055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 9 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']");

Expand All @@ -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);
})
);
});
Expand Down