Skip to content

Commit

Permalink
Keys.isKeyboardClick
Browse files Browse the repository at this point in the history
it's like a method on a java enum!
  • Loading branch information
Gilad Gray committed Nov 30, 2018
1 parent 9026515 commit 5b1bcf3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/common/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const ARROW_UP = 38;
export const ARROW_RIGHT = 39;
export const ARROW_DOWN = 40;
export const DELETE = 46;

/** Returns whether the key code is `enter` or `space`, the two keys that can click a button. */
export function isKeyboardClick(keyCode: number) {
return keyCode === ENTER || keyCode === SPACE;
}
8 changes: 2 additions & 6 deletions packages/core/src/components/button/abstractButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export abstract class AbstractButton<H extends React.HTMLAttributes<any>> extend
// argument because it is not a supertype of candidate
// 'KeyboardEvent<HTMLElement>'."
protected handleKeyDown = (e: React.KeyboardEvent<any>) => {
if (isKeyboardClick(e.which)) {
if (Keys.isKeyboardClick(e.which)) {
e.preventDefault();
if (e.which !== this.currentKeyDown) {
this.setState({ isActive: true });
Expand All @@ -136,7 +136,7 @@ export abstract class AbstractButton<H extends React.HTMLAttributes<any>> extend
};

protected handleKeyUp = (e: React.KeyboardEvent<any>) => {
if (isKeyboardClick(e.which)) {
if (Keys.isKeyboardClick(e.which)) {
this.setState({ isActive: false });
this.buttonRef.click();
}
Expand All @@ -159,7 +159,3 @@ export abstract class AbstractButton<H extends React.HTMLAttributes<any>> extend
];
}
}

function isKeyboardClick(keyCode: number) {
return keyCode === Keys.ENTER || keyCode === Keys.SPACE;
}
2 changes: 1 addition & 1 deletion packages/core/src/components/forms/numericInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class NumericInput extends AbstractPureComponent<HTMLInputProps & INumeri
// respond explicitly on key *up*, because onKeyDown triggers multiple
// times and doesn't always receive modifier-key flags, leading to an
// unintuitive/out-of-control incrementing experience.
if (e.keyCode === Keys.SPACE || e.keyCode === Keys.ENTER) {
if (Keys.isKeyboardClick(e.keyCode)) {
// prevent the page from scrolling (this is the default browser
// behavior for shift + space or alt + space).
e.preventDefault();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class Tabs extends AbstractPureComponent<ITabsProps, ITabsState> {

private handleKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => {
const targetTabElement = (e.target as HTMLElement).closest(TAB_SELECTOR) as HTMLElement;
if (targetTabElement != null && isEventKeyCode(e, Keys.SPACE, Keys.ENTER)) {
if (targetTabElement != null && Keys.isKeyboardClick(e.keyCode)) {
e.preventDefault();
targetTabElement.click();
}
Expand Down

0 comments on commit 5b1bcf3

Please sign in to comment.