Skip to content

Commit e50d4c7

Browse files
authored
fix(ui5-switch): prevent toggle when Space is pressed with Shift or Escape
Toggle is prevented when Space is pressed with Shift or Escape
1 parent 7977d02 commit e50d4c7

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

packages/main/cypress/specs/Switch.cy.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,66 @@ describe("General events interactions", () => {
3838
cy.get("@switch")
3939
.should("not.have.attr", "checked");
4040
});
41+
42+
it("Should toggle checked state when SPACE is pressed", () => {
43+
cy.mount(<Switch onChange={cy.stub().as("changed")}>Click me</Switch>);
44+
45+
cy.get("[ui5-switch]")
46+
.as("switch");
47+
48+
cy.get("@switch")
49+
.shadow()
50+
.find(".ui5-switch-root")
51+
.focus()
52+
.should("be.focused")
53+
.realPress("Space");
54+
55+
cy.get("@changed")
56+
.should("have.been.calledOnce");
57+
58+
cy.get("@switch")
59+
.should("have.attr", "checked");
60+
});
61+
62+
it("Should not toggle checked state on SPACE + Shift are pressed", () => {
63+
cy.mount(<Switch onChange={cy.stub().as("changed")}>Click me</Switch>);
64+
65+
cy.get("[ui5-switch]")
66+
.as("switch");
67+
68+
cy.get("@switch")
69+
.shadow()
70+
.find(".ui5-switch-root")
71+
.focus()
72+
.should("be.focused")
73+
.realPress(["Space", "Shift"]);
74+
75+
cy.get("@changed")
76+
.should("not.have.been.called");
77+
78+
cy.get("@switch")
79+
.should("not.have.attr", "checked");
80+
});
81+
82+
it("Should not toggle checked state on SPACE + Escape are pressed", () => {
83+
cy.mount(<Switch onChange={cy.stub().as("changed")}>Click me</Switch>);
84+
85+
cy.get("[ui5-switch]")
86+
.as("switch");
87+
88+
cy.get("@switch")
89+
.shadow()
90+
.find(".ui5-switch-root")
91+
.focus()
92+
.should("be.focused")
93+
.realPress(["Space", "Escape"]);
94+
95+
cy.get("@changed")
96+
.should("not.have.been.called");
97+
98+
cy.get("@switch")
99+
.should("not.have.attr", "checked");
100+
});
41101
});
42102

43103
describe("General accesibility attributes", () => {
@@ -165,4 +225,5 @@ describe("General interactions in form", () => {
165225

166226
cy.get("#requiredTestSwitch:invalid").should("not.exist", "Checked required Switch should not have :invalid CSS class");
167227
});
168-
});
228+
229+
});

packages/main/src/Switch.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import property from "@ui5/webcomponents-base/dist/decorators/property.js";
44
import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js";
55
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
66
import type { IFormInputElement } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
7-
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
7+
import {
8+
isSpace, isEnter, isShift, isEscape,
9+
} from "@ui5/webcomponents-base/dist/Keys.js";
810
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
911
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
1012
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
@@ -77,6 +79,7 @@ class Switch extends UI5Element implements IFormInputElement {
7779
change: void
7880
"value-changed": void
7981
}
82+
8083
/**
8184
* Defines the component design.
8285
*
@@ -195,6 +198,9 @@ class Switch extends UI5Element implements IFormInputElement {
195198
@property()
196199
value = "";
197200

201+
@property({ type: Boolean, noAttribute: true })
202+
_cancelAction = false;
203+
198204
@i18n("@ui5/webcomponents")
199205
static i18nBundle: I18nBundle;
200206

@@ -227,6 +233,7 @@ class Switch extends UI5Element implements IFormInputElement {
227233
}
228234

229235
_onkeydown(e: KeyboardEvent) {
236+
this._cancelAction = isShift(e) || isEscape(e);
230237
if (isSpace(e)) {
231238
e.preventDefault();
232239
}
@@ -237,7 +244,7 @@ class Switch extends UI5Element implements IFormInputElement {
237244
}
238245

239246
_onkeyup(e: KeyboardEvent) {
240-
if (isSpace(e)) {
247+
if (isSpace(e) && !this._cancelAction) {
241248
this._onclick();
242249
}
243250
}

0 commit comments

Comments
 (0)