Skip to content

Commit

Permalink
Backport of [ui] Keyboard shortcuts to switch regions into release/1.…
Browse files Browse the repository at this point in the history
…5.x (#17171)

* no-op commit due to failed cherry-picking

* region switcher conflict resolution

---------

Co-authored-by: temp <temp@hashicorp.com>
Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>
  • Loading branch information
3 people committed May 31, 2023
1 parent c55823a commit d5364f7
Show file tree
Hide file tree
Showing 4 changed files with 59 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 @@ -19,4 +19,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),
};
});
}
}
7 changes: 7 additions & 0 deletions ui/app/templates/components/region-switcher.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{{!
Copyright (c) HashiCorp, Inc.
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 @@ -357,5 +357,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 d5364f7

Please sign in to comment.