From 048b93186a4a7998fb6a7d123e325ddd859b368f Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Wed, 18 Jul 2018 16:08:29 +0200 Subject: [PATCH] Switch tabs using tab key --- packages/frontend/src/App.tsx | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/frontend/src/App.tsx b/packages/frontend/src/App.tsx index 56cca3c..fe15d73 100644 --- a/packages/frontend/src/App.tsx +++ b/packages/frontend/src/App.tsx @@ -54,4 +54,41 @@ export default class App extends React.Component<{}, State> { ); } + + public componentWillMount() { + window.addEventListener('keydown', this.onKeyPress); + } + + public componentWillUnmount() { + window.removeEventListener('keydown', this.onKeyPress); + } + + private onKeyPress = (event: KeyboardEvent) => { + if (event.keyCode !== 9) { // TAB KEY + return; + } + + event.preventDefault(); + + const { subscribed } = this.state; + const chains = Array.from(this.state.chains.keys()); + + let index = 0; + + if (subscribed) { + index = chains.indexOf(subscribed); + + // Do nothing if it's the same chain + if (chains[index] === subscribed) { + return; + } + + // Go to next tab + index = (index + 1) % chains.length; + } + + this.connection.then((connection) => { + connection.subscribe(chains[index]); + }) + } }