1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add change tab on keydown

Signed-off-by: DmitriyNoa <dmytro.zharkov@gmail.com>
This commit is contained in:
DmitriyNoa 2022-01-25 17:31:12 +01:00
parent 61345aaf72
commit 4eac1b180b

View File

@ -36,6 +36,11 @@ interface Dependencies {
dockStore: DockStore
}
enum Direction {
next = "next",
prev = "prev",
}
@observer
class NonInjectedDock extends React.Component<Props & Dependencies> {
private element = React.createRef<HTMLDivElement>();
@ -51,6 +56,7 @@ class NonInjectedDock extends React.Component<Props & Dependencies> {
onKeyDown = (evt: KeyboardEvent) => {
const { close, selectedTab, closeTab } = this.props.dockStore;
const { code, ctrlKey, metaKey, shiftKey } = evt;
// Determine if user working inside <Dock/> or using any other areas in app
const dockIsFocused = this.element?.current.contains(document.activeElement);
@ -64,6 +70,14 @@ class NonInjectedDock extends React.Component<Props & Dependencies> {
closeTab(selectedTab.id);
this.element?.current.focus(); // Avoid loosing focus when closing tab
}
if(ctrlKey && code === "Period") {
this.nextTab(Direction.next);
}
if(ctrlKey && code === "Comma") {
this.nextTab(Direction.prev);
}
};
onChangeTab = (tab: DockTab) => {
@ -74,6 +88,19 @@ class NonInjectedDock extends React.Component<Props & Dependencies> {
this.element?.current.focus();
};
nextTab = (direction: Direction) => {
const { tabs, selectedTab } = this.props.dockStore;
const currentIndex = tabs.indexOf(selectedTab);
const nextIndex = direction === Direction.next ? currentIndex + 1 : currentIndex - 1;
if (direction === Direction.next && currentIndex!== -1 && nextIndex >= tabs.length) return;
if (direction === Direction.prev && currentIndex!== -1 && nextIndex < 0) return;
const nextElement = tabs[nextIndex];
this.onChangeTab(nextElement);
};
renderTab(tab: DockTab) {
switch (tab.kind) {
case TabKind.CREATE_RESOURCE: