Skip to content

Commit

Permalink
remove deprecated keyCodes and add additional "keys" (#5944)
Browse files Browse the repository at this point in the history
* remove deprecated keycodes and add additional keys

* Change files

* remove deprecated keyCode m fn
  • Loading branch information
chrisdholt authored and EisenbergEffect committed May 31, 2022
1 parent f2391f5 commit b623830
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 597 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "major",
"comment": "remove deprecated keycodes and add additional keys",
"packageName": "@microsoft/fast-web-utilities",
"email": "[email protected]",
"dependentChangeType": "patch"
}
165 changes: 0 additions & 165 deletions packages/utilities/fast-web-utilities/src/dom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import {
canUseFocusVisible,
canUseForcedColors,
getDisplayedNodes,
getKeyCode,
isHTMLElement,
resetDocumentCache,
} from "./dom";
import { KeyCodes } from "./key-codes";

chai.use(spies);

Expand Down Expand Up @@ -45,169 +43,6 @@ describe("getDisplayedNodes", () => {
});
});

describe("getKeyCode", () => {
it("should correctly handle null", () => {
expect(getKeyCode(null)).to.equal(null);
});

it("should correctly handle keyboard events with `keyCode` values", () => {
expect(
getKeyCode(
new KeyboardEvent("keypress", {
keyCode: 39,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowRight);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
keyCode: 37,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowLeft);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
keyCode: 38,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowUp);
expect(
getKeyCode(
new KeyboardEvent("keydown", {
keyCode: 40,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowDown);
expect(
getKeyCode(
new KeyboardEvent("keydown", {
keyCode: 13,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.enter);
expect(
getKeyCode(
new KeyboardEvent("keydown", {
keyCode: 32,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.space);
expect(
getKeyCode(
new KeyboardEvent("keydown", {
keyCode: 9,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.tab);
});

it("should correctly handle keyboard events with `which` values", () => {
// Since `which` is deprecated, it can no longer be set using KeyboardEvent
expect(
getKeyCode({
which: 39,
keyCode: 39,
charCode: 39,
} as any)
).to.equal(KeyCodes.arrowRight);
expect(
getKeyCode({
which: 37,
keyCode: 37,
charCode: 37,
} as any)
).to.equal(KeyCodes.arrowLeft);
expect(
getKeyCode({
which: 38,
keyCode: 38,
charCode: 38,
} as any)
).to.equal(KeyCodes.arrowUp);
expect(
getKeyCode({
which: 40,
keyCode: 40,
charCode: 40,
} as any)
).to.equal(KeyCodes.arrowDown);
expect(
getKeyCode({
which: 13,
keyCode: 13,
charCode: 13,
} as any)
).to.equal(KeyCodes.enter);
expect(
getKeyCode({
which: 32,
keyCode: 32,
charCode: 32,
} as any)
).to.equal(KeyCodes.space);
expect(
getKeyCode({
which: 9,
keyCode: 9,
charCode: 9,
} as any)
).to.equal(KeyCodes.tab);
});

it("should correctly handle keyboard events with `charCode` values", () => {
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 39,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowRight);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 37,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowLeft);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 38,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowUp);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 40,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.arrowDown);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 13,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.enter);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 32,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.space);
expect(
getKeyCode(
new KeyboardEvent("keypress", {
charCode: 9,
} as KeyboardEventInit)
)
).to.equal(KeyCodes.tab);
});
});

describe("canUseFocusVisible", () => {
beforeEach(() => {
resetDocumentCache();
Expand Down
8 changes: 0 additions & 8 deletions packages/utilities/fast-web-utilities/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export function getDisplayedNodes(
return nodes.filter((node: HTMLElement) => node.offsetParent !== null);
}

/**
* Gets the numeric key code associated with a keyboard event. This method is for use with DOM level 3 events
* that still use the deprecated keyCode property.
*/
export function getKeyCode(event: KeyboardEvent | null): number | null {
return event === null ? null : event.which || event.keyCode || event.charCode;
}

/**
* Returns the nonce used in the page, if any.
*
Expand Down
Loading

0 comments on commit b623830

Please sign in to comment.