fix: 🐛 refocus on route changes when attempting to search (#2839)

* fix: 🐛 refocus on route changes when attempting to search

* refactor: 💡 update condition

* refactor: 💡 prevent refocus for more use cases like filtering

* refactor: 💡 use router service instead of window object

* refactor: 💡 PR feedback (use param finder instead of regex)

* test: 💍 update test title

* test: 💍 refactor tests to use test.each
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2025-05-22 16:34:40 -07:00
1 parent c12edc7f73
commit 308240be89
5 files changed
+103 -3

No files matched your search

+34 -2
View File
@@ -7,6 +7,8 @@ import Controller from '@ember/controller';
import { service } from '@ember/service';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import { defaultValidator } from 'ember-a11y-refocus';
import { paramValueFinder } from 'admin/utils/param-value-finder';
const THEMES = [
{
@@ -33,13 +35,18 @@ export default class ApplicationController extends Controller {
@service router;
/**
* Returns available themes
* Returns available themes.
* @type {array}
*/
get themes() {
return THEMES;
}
/**
* Shows side navigation only for routes nested under a scope
* and if user has been authenticated.
* @type {boolean}
*/
get showSideNav() {
return (
this.router.currentRouteName.startsWith('scopes.scope') &&
@@ -94,7 +101,7 @@ export default class ApplicationController extends Controller {
}
/**
*
* Toggles on/off specified features.
* @param {string} feature
*/
@action
@@ -105,4 +112,29 @@ export default class ApplicationController extends Controller {
this.features.enable(feature);
}
}
/**
* Add custom route change validation to prevent refocus when
* user is attempting to search, filter, or sort.
* @param {object} transition
* @returns {boolean}
*/
customRouteChangeValidator(transition) {
if (!transition.to || !transition.from) {
return true;
}
if (transition.to.name === transition.from.name) {
const toParams = paramValueFinder(
transition.to.localName,
transition.to.parent,
);
const fromParams = paramValueFinder(
transition.from.localName,
transition.from.parent,
);
// Return false to prevent refocus when routes have equivalent dynamic segments (params).
return JSON.stringify(toParams) !== JSON.stringify(fromParams);
}
return defaultValidator(transition);
}
}
+4 -1
View File
@@ -7,7 +7,10 @@
<Hds::AppFrame @hasHeader={{false}} @hasSidebar={{this.showSideNav}} as |Frame|>
<Frame.Sidebar class='app-frame-sidebar-style-overrides'>
<Hds::SideNav class='side-nav-style-overrides'>
<Hds::SideNav
@a11yRefocusRouteChangeValidator={{this.customRouteChangeValidator}}
class='side-nav-style-overrides'
>
<:header>
<Hds::SideNav::Header>
<:logo>
+1
View File
@@ -57,6 +57,7 @@
"concurrently": "^9.1.0",
"core": "workspace:*",
"doctoc": "^2.2.0",
"ember-a11y-refocus": "^4.1.4",
"ember-a11y-testing": "^7.1.2",
"ember-auto-import": "^2.10.0",
"ember-cli": "~5.12.0",
@@ -14,6 +14,7 @@ module('Unit | Controller | application', function (hooks) {
let featureEdition;
let featuresService;
let session;
let router;
hooks.beforeEach(async function () {
await authenticateSession({});
@@ -21,6 +22,7 @@ module('Unit | Controller | application', function (hooks) {
featureEdition = this.owner.lookup('service:featureEdition');
featuresService = this.owner.lookup('service:features');
session = this.owner.lookup('service:session');
router = this.owner.lookup('service:router');
});
test('it exists', function (assert) {
@@ -58,4 +60,65 @@ module('Unit | Controller | application', function (hooks) {
assert.true(featuresService.isEnabled('ssh-target'));
});
test.each(
'customRouteChangeValidator',
[
{
from: '/scopes',
to: '/scopes',
expectedResult: false,
},
{
from: '/scopes/global/scopes',
to: '/scopes/global/scopes',
expectedResult: false,
},
{
from: '/scopes/global/scopes?search=xyz',
to: '/scopes/global/scopes',
expectedResult: false,
},
{
from: '/scopes/global/scopes?search=xyz',
to: '/scopes/global/scopes?search=xy',
expectedResult: false,
},
{
to: '/scopes/global/scopes',
expectedResult: true,
},
{
from: '/scopes/global/scopes',
expectedResult: true,
},
{
from: '/scopes/global/scopes',
to: '/scopes/global/auth-methods',
expectedResult: true,
},
{
from: '/scopes/global/scopes',
to: '/scopes/o_12345/scopes',
expectedResult: true,
},
{
from: '/scopes/global/roles/r_123/scopes',
to: '/scopes/global/roles/r_321/scopes',
expectedResult: true,
},
],
async function (assert, input) {
const transition = {
from: input.from ? router.recognize(input.from) : undefined,
to: input.to ? router.recognize(input.to) : undefined,
};
assert.strictEqual(
controller.customRouteChangeValidator(transition),
input.expectedResult,
);
},
);
});
+1
View File
@@ -6478,6 +6478,7 @@ __metadata:
concurrently: "npm:^9.1.0"
core: "workspace:*"
doctoc: "npm:^2.2.0"
ember-a11y-refocus: "npm:^4.1.4"
ember-a11y-testing: "npm:^7.1.2"
ember-auto-import: "npm:^2.10.0"
ember-cli: "npm:~5.12.0"