forked from palantir/blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistent space and enter keystrokes between buttons and anchor butt…
…ons (palantir#459) * have consistent actions with space and enter between buttons and anchorbuttons * create `AbstractButton` class that implementations extend so code is easily shared.
- Loading branch information
Showing
3 changed files
with
90 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as React from "react"; | ||
|
||
import * as Keys from "../../common/keys"; | ||
import { IActionProps } from "../../common/props"; | ||
import { safeInvoke } from "../../common/utils"; | ||
|
||
export interface IButtonProps extends IActionProps { | ||
/** A ref handler that receives the native HTML element backing this component. */ | ||
elementRef?: (ref: HTMLElement) => any; | ||
|
||
/** Name of icon (the part after `pt-icon-`) to add to button. */ | ||
rightIconName?: string; | ||
|
||
/** | ||
* If set to true, the button will display a centered loading spinner instead of its contents. | ||
* The width of the button is not affected by the value of this prop. | ||
* @default false | ||
*/ | ||
loading?: boolean; | ||
} | ||
|
||
export interface IButtonState { | ||
isActive: boolean; | ||
} | ||
|
||
export abstract class AbstractButton<T> extends React.Component<React.HTMLProps<T> & IButtonProps, IButtonState> { | ||
public state = { | ||
isActive: false, | ||
}; | ||
|
||
protected buttonRef: HTMLElement; | ||
protected refHandlers = { | ||
button: (ref: HTMLElement) => { | ||
this.buttonRef = ref; | ||
safeInvoke(this.props.elementRef, ref); | ||
}, | ||
}; | ||
|
||
public abstract render(): JSX.Element; | ||
|
||
protected onKeyDown = (e: React.KeyboardEvent<HTMLElement>) => { | ||
switch (e.which) { | ||
case Keys.SPACE: | ||
e.preventDefault(); | ||
this.setState({ isActive: true }); | ||
break; | ||
case Keys.ENTER: | ||
this.buttonRef.click(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
protected onKeyUp = (e: React.KeyboardEvent<HTMLElement>) => { | ||
if (e.which === Keys.SPACE) { | ||
this.setState({ isActive: false }); | ||
this.buttonRef.click(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters