Add keyboard events enter/escape to connect/disconnect

This commit is contained in:
Thomas Nordquist
2019-06-23 12:29:52 +02:00
parent 38b16daf51
commit ffdce6f825

View File

@@ -26,6 +26,7 @@ import {
TextField, TextField,
} from '@material-ui/core' } from '@material-ui/core'
import ConnectionHealthIndicator from '../helper/ConnectionHealthIndicator' import ConnectionHealthIndicator from '../helper/ConnectionHealthIndicator'
import { KeyCodes } from '../../utils/KeyCodes'
interface Props { interface Props {
connection: ConnectionOptions connection: ConnectionOptions
@@ -176,7 +177,7 @@ class ConnectionSettings extends React.Component<Props, State> {
) )
} }
private onClickConnect = () => { private connect() {
if (!this.props.connection) { if (!this.props.connection) {
return return
} }
@@ -187,6 +188,28 @@ class ConnectionSettings extends React.Component<Props, State> {
} }
} }
private onClickConnect = () => {
this.connect()
}
private handleKeyEvent = (event: KeyboardEvent) => {
if (event.keyCode === KeyCodes.enter) {
this.connect()
event.preventDefault()
} else if (event.keyCode === KeyCodes.escape) {
this.props.actions.disconnect()
event.preventDefault()
}
}
public componentDidMount() {
document.addEventListener('keydown', this.handleKeyEvent, false)
}
public componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyEvent, false)
}
public render() { public render() {
const { classes, connection } = this.props const { classes, connection } = this.props