Skip to content

Commit

Permalink
Merge pull request openfoodfoundation#12634 from chahmedejaz/bugfix/1…
Browse files Browse the repository at this point in the history
…2632-voucher-field-focus-issue

Fix Voucher Code Field Focus Issue
  • Loading branch information
drummer83 authored Jul 8, 2024
2 parents 47faedc + ced3408 commit d60d29b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
18 changes: 11 additions & 7 deletions app/webpacker/controllers/toggle_control_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Controller } from "stimulus";

const BUTTON_TYPES = ['submit', 'button'];

// Toggle state of a control based on a condition.
//
// 1. When an action occurs on an element,
Expand Down Expand Up @@ -57,10 +59,8 @@ export default class extends Controller {
target.disabled = disable;
});

// Focus first when enabled
if (!disable) {
this.controlTargets[0].focus();
}
// Focus first when enabled and it's not a button
if (!disable) this.#focusFieldControl();
}

#toggleDisplay(show) {
Expand All @@ -69,9 +69,7 @@ export default class extends Controller {
});

// Focus first when displayed
if (show) {
this.controlTargets[0].focus();
}
if (show) this.#focusFieldControl();
}

// Return input's value, but only if it would be submitted by a form
Expand All @@ -81,4 +79,10 @@ export default class extends Controller {
return input.value;
}
}

#focusFieldControl() {
const control = this.controlTargets[0];
const isButton = BUTTON_TYPES.includes(control.type);
if(!isButton) control.focus();
}
}
34 changes: 33 additions & 1 deletion spec/javascripts/stimulus/toggle_control_controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ describe("ToggleControlController", () => {
</div>`;
});

it("Enables when input is filled", () => {
it("Enables when input is filled and focuses the control", () => {
input.value = "a"
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(false);
expect(document.activeElement).toBe(control);
});

it("Disables when input is emptied", () => {
Expand All @@ -88,6 +89,37 @@ describe("ToggleControlController", () => {
expect(control.disabled).toBe(true);
});
});
describe("with button as control target", () => {
beforeEach(() => {
document.body.innerHTML = `<div data-controller="toggle-control">
<input id="input" value="" data-action="input->toggle-control#enableIfPresent" />
<button id="control" data-toggle-control-target="control">
</div>`;
});

it("Enables the button control when input is filled, focus remains on input", () => {
// Simulating click on input to focus it
input.focus();
input.value = "test"
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(false);
expect(document.activeElement).toBe(input);
});

it("Disables the button control when input is emptied, focus remains on input", () => {
// Simulating click on input to focus it
input.focus();
input.value = "test"
input.dispatchEvent(new Event("input"));

input.value = ""
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(true);
expect(document.activeElement).toBe(input);
});
})
});

describe("#displayIfMatch", () => {
Expand Down

0 comments on commit d60d29b

Please sign in to comment.