From b27df0c0d8c19eb04483267f51a1f8bf36bfa184 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Mon, 24 Jun 2019 12:27:15 +0200 Subject: [PATCH] Allow to select connection profile with arrow keys --- .../ConnectionSetup/ProfileList/index.tsx | 39 +++++++++++++------ app/src/utils/KeyCodes.ts | 5 +++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/components/ConnectionSetup/ProfileList/index.tsx b/app/src/components/ConnectionSetup/ProfileList/index.tsx index 5bcbd59..c647eb6 100644 --- a/app/src/components/ConnectionSetup/ProfileList/index.tsx +++ b/app/src/components/ConnectionSetup/ProfileList/index.tsx @@ -8,28 +8,45 @@ import { connectionManagerActions } from '../../../actions' import { ConnectionOptions } from '../../../model/ConnectionOptions' import { List, ListSubheader } from '@material-ui/core' import { Theme, withStyles } from '@material-ui/core/styles' +import { useGlobalKeyEventHandler } from '../../../effects/useGlobalKeyEventHandler' +import { KeyCodes } from '../../../utils/KeyCodes' interface Props { classes: any selected?: string connections: { [s: string]: ConnectionOptions } - actions: any + actions: typeof connectionManagerActions } function ProfileList(props: 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 = ( + + + Connections + + ) + return ( - - - Connections - - } - > +
{Object.values(connections).map(connection => ( diff --git a/app/src/utils/KeyCodes.ts b/app/src/utils/KeyCodes.ts index 90640c0..9ae29b5 100644 --- a/app/src/utils/KeyCodes.ts +++ b/app/src/utils/KeyCodes.ts @@ -1,6 +1,11 @@ export enum KeyCodes { backspace = 8, + tab = 9, enter = 13, escape = 27, + arrow_left = 37, + arrow_up = 38, + arrow_right = 39, + arrow_down = 40, delete = 46, }