Skip to content

Commit

Permalink
[ui] Keyboard shortcuts to switch regions (#17169)
Browse files Browse the repository at this point in the history
* Regions keynav

* Dont show if you only have a single region (global by default)
  • Loading branch information
philrenaud authored May 12, 2023
1 parent 17633d8 commit f2e603d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/17169.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: adds keyboard nav for switching between regions by pressing "r 1", "r 2", etc.
```
13 changes: 13 additions & 0 deletions ui/app/components/region-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ export default class RegionSwitcher extends Component {
queryParams: { region },
});
}

get keyCommands() {
if (this.sortedRegions.length <= 1) {
return [];
}
return this.sortedRegions.map((region, iter) => {
return {
label: `Switch to ${region} region`,
pattern: ['r', `${iter + 1}`],
action: () => this.gotoRegion(region),
};
});
}
}
2 changes: 2 additions & 0 deletions ui/app/templates/components/region-switcher.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
SPDX-License-Identifier: MPL-2.0
~}}

{{keyboard-commands this.keyCommands}}

{{#if this.system.shouldShowRegions}}
<span data-test-region-switcher-parent>
<PowerSelect
Expand Down
36 changes: 36 additions & 0 deletions ui/tests/acceptance/keyboard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,41 @@ module('Acceptance | keyboard', function (hooks) {
'Shift+ArrowRight takes you to the first tab in the loop'
);
});

test('Region switching', async function (assert) {
['Detroit', 'Halifax', 'Phoenix', 'Toronto', 'Windsor'].forEach((id) => {
server.create('region', { id });
});

await visit('/jobs');

// Regions are in the keynav modal
await triggerEvent('.page-layout', 'keydown', { key: '?' });
await triggerEvent('.page-layout', 'keydown', { key: '?' });
assert.ok(Layout.keyboard.modalShown);
assert
.dom('[data-test-command-label="Switch to Detroit region"]')
.exists('First created region is in the modal');

assert
.dom('[data-test-command-label="Switch to Windsor region"]')
.exists('Last created region is in the modal');

// Triggers a region switch to Halifax
triggerEvent('.page-layout', 'keydown', { key: 'r' });
await triggerEvent('.page-layout', 'keydown', { key: '2' });
assert.ok(
currentURL().includes('region=Halifax'),
'r 2 command takes you to the second region'
);

// Triggers a region switch to Phoenix
triggerEvent('.page-layout', 'keydown', { key: 'r' });
await triggerEvent('.page-layout', 'keydown', { key: '3' });
assert.ok(
currentURL().includes('region=Phoenix'),
'r 3 command takes you to the third region'
);
});
});
});

0 comments on commit f2e603d

Please sign in to comment.