Allow to select connection profile with arrow keys

This commit is contained in:
Thomas Nordquist
2019-06-24 12:27:15 +02:00
parent 341ab44f9e
commit b27df0c0d8
2 changed files with 33 additions and 11 deletions

View File

@@ -8,28 +8,45 @@ import { connectionManagerActions } from '../../../actions'
import { ConnectionOptions } from '../../../model/ConnectionOptions' import { ConnectionOptions } from '../../../model/ConnectionOptions'
import { List, ListSubheader } from '@material-ui/core' import { List, ListSubheader } from '@material-ui/core'
import { Theme, withStyles } from '@material-ui/core/styles' import { Theme, withStyles } from '@material-ui/core/styles'
import { useGlobalKeyEventHandler } from '../../../effects/useGlobalKeyEventHandler'
import { KeyCodes } from '../../../utils/KeyCodes'
interface Props { interface Props {
classes: any classes: any
selected?: string selected?: string
connections: { [s: string]: ConnectionOptions } connections: { [s: string]: ConnectionOptions }
actions: any actions: typeof connectionManagerActions
} }
function ProfileList(props: Props) { function ProfileList(props: Props) {
const { actions, classes, connections, selected } = props const { actions, classes, connections, selected } = props
const selectConnection = (dir: 'next' | 'previous') => (event: KeyboardEvent) => {
if (!selected) {
return
}
const indexDirection = dir === 'next' ? 1 : -1
const connectionArray = Object.values(connections)
const selectedIndex = connectionArray.map(connection => connection.id).indexOf(selected)
const nextConnection = connectionArray[selectedIndex + indexDirection]
if (nextConnection) {
actions.selectConnection(nextConnection.id)
}
event.preventDefault()
}
useGlobalKeyEventHandler(KeyCodes.arrow_down, selectConnection('next'))
useGlobalKeyEventHandler(KeyCodes.arrow_up, selectConnection('previous'))
const createConectionButton = (
<ListSubheader component="div">
<AddButton action={actions.createConnection} />
Connections
</ListSubheader>
)
return ( return (
<List <List style={{ height: '100%' }} component="nav" subheader={createConectionButton}>
style={{ height: '100%' }}
component="nav"
subheader={
<ListSubheader component="div">
<AddButton action={actions.createConnection} />
Connections
</ListSubheader>
}
>
<div className={classes.list}> <div className={classes.list}>
{Object.values(connections).map(connection => ( {Object.values(connections).map(connection => (
<ConnectionItem connection={connection} key={connection.id} selected={selected === connection.id} /> <ConnectionItem connection={connection} key={connection.id} selected={selected === connection.id} />

View File

@@ -1,6 +1,11 @@
export enum KeyCodes { export enum KeyCodes {
backspace = 8, backspace = 8,
tab = 9,
enter = 13, enter = 13,
escape = 27, escape = 27,
arrow_left = 37,
arrow_up = 38,
arrow_right = 39,
arrow_down = 40,
delete = 46, delete = 46,
} }