diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index 61c2762c93..c7d4686228 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -9,7 +9,7 @@ = error_message_on variant, :sku %td -# empty -%td.field.on-hand__wrapper{'data-controller': "popout"} +%td.field.on-hand__wrapper{'data-controller': "popout", 'data-popout-update-display-value': "false"} = f.button :unit_to_display, class: "on-hand__button", 'aria-label': t('admin.products_page.columns.unit'), 'data-popout-target': "button" do = variant.unit_to_display # Show the generated summary of unit values %div.on-hand__popout{ style: 'display: none;', 'data-controller': 'toggle-control', 'data-popout-target': "dialog" } diff --git a/app/webpacker/controllers/popout_controller.js b/app/webpacker/controllers/popout_controller.js index 287ed98771..5a871e0367 100644 --- a/app/webpacker/controllers/popout_controller.js +++ b/app/webpacker/controllers/popout_controller.js @@ -3,6 +3,9 @@ import { Controller } from "stimulus"; // Allows a form section to "pop out" and show additional options export default class PopoutController extends Controller { static targets = ["button", "dialog"]; + static values = { + updateDisplay: { Boolean, default: true } + } connect() { this.first_input = this.dialogTarget.querySelector("input"); @@ -58,8 +61,11 @@ export default class PopoutController extends Controller { } // Update button to represent any changes - this.buttonTarget.innerText = this.#displayValue(); - this.buttonTarget.innerHTML ||= " "; // (with default space to help with styling) + console.log(this.updateDisplayValue); + if (this.updateDisplayValue) { + this.buttonTarget.innerText = this.#displayValue(); + this.buttonTarget.innerHTML ||= " "; // (with default space to help with styling) + } this.buttonTarget.classList.toggle("changed", this.#isChanged()); this.dialogTarget.style.display = "none"; diff --git a/spec/javascripts/stimulus/popout_controller_test.js b/spec/javascripts/stimulus/popout_controller_test.js index 82da807bfb..28f4e49a2c 100644 --- a/spec/javascripts/stimulus/popout_controller_test.js +++ b/spec/javascripts/stimulus/popout_controller_test.js @@ -11,24 +11,11 @@ describe("PopoutController", () => { application.register("popout", popout_controller); }); - beforeEach(() => { - document.body.innerHTML = ` -
- - -
- - `; - }); - describe("Show", () => { + beforeEach(() => { + document.body.innerHTML = htmlTemplate(); + }); + it("shows the dialog on click", () => { // button.click(); // For some reason this fails due to passive: true, but works in real life. button.dispatchEvent(new Event("click")); @@ -64,6 +51,10 @@ describe("PopoutController", () => { }); describe("Close", () => { + beforeEach(() => { + document.body.innerHTML = htmlTemplate(); + }); + // For some reason this has to be in a separate block beforeEach(() => { button.dispatchEvent(new Event("click")); // Dialog is open }) @@ -113,11 +104,43 @@ describe("PopoutController", () => { }); }); + describe("disable update-display", () => { + beforeEach(() => { + document.body.innerHTML = htmlTemplate({updateDisplay: "false" }); + }) + + it("doesn't update display value", () => { + expect(button.innerText).toBe("On demand"); + button.dispatchEvent(new Event("click")); // Dialog is open + input4.click(); //close dialog + + expectToBeClosed(dialog); + expect(button.innerText).toBe("On demand"); + }); + }); + describe("Cleaning up", () => { // unable to test disconnect }); }); +function htmlTemplate(opts = {updateDisplay: ""}) { + return ` +
+ + +
+ + `; +} + function expectToBeShown(element) { expect(element.style.display).toBe("block"); }