From 242dd7ad308cf781ffa3c7abd4fabb920188fc0e Mon Sep 17 00:00:00 2001 From: Jonas Metzener Date: Mon, 2 Oct 2023 18:07:16 +0200 Subject: [PATCH] feat(linked-item): add current when argument for tab and subnav items --- addon/components/-private/linked-list-item.js | 46 +++++++++++-------- tests/dummy/app/components/option-select.hbs | 3 ++ tests/dummy/app/components/option-select.js | 3 +- .../app/controllers/docs/components/subnav.js | 1 + .../app/controllers/docs/components/tab.js | 1 + .../app/templates/docs/components/subnav.hbs | 19 ++++++++ .../app/templates/docs/components/tab.hbs | 20 ++++++++ .../-private/linked-list-item-test.js | 21 +++++++++ 8 files changed, 95 insertions(+), 19 deletions(-) diff --git a/addon/components/-private/linked-list-item.js b/addon/components/-private/linked-list-item.js index ad526232..ba364013 100644 --- a/addon/components/-private/linked-list-item.js +++ b/addon/components/-private/linked-list-item.js @@ -28,7 +28,27 @@ export default class LinkedListItemComponent extends Component { } get href() { - if (!this.args.href) return null; + return this.getAbsoluteHref(this.args.href); + } + + get route() { + return this.getRouteInfo(this.href, this.args.linkToIndex); + } + + get active() { + if (!this.route || this.args.active !== undefined) { + return this.args.active ?? false; + } + + const { name, args } = this.args.currentWhen + ? this.getRouteInfo(this.getAbsoluteHref(this.args.currentWhen)) + : this.route; + + return this.router.isActive(name, ...args); + } + + getAbsoluteHref(relativeHref) { + if (!relativeHref) return null; /* istanbul ignore next */ if (this.isEngineRouter) { @@ -36,17 +56,17 @@ export default class LinkedListItemComponent extends Component { // Append the engine root to the url in case the engine didn't already return this.args.href.startsWith(engineRoot) - ? this.args.href - : `${engineRoot}${this.args.href}`; + ? relativeHref + : `${engineRoot}${relativeHref}`; } - return this.args.href; + return relativeHref; } - get route() { - if (!this.href) return null; + getRouteInfo(absoluteHref, linkToIndex = false) { + if (!absoluteHref) return null; - let href = this.href; + let href = absoluteHref; /* istanbul ignore next */ if (this.router.location.implementation === "hash") { @@ -64,7 +84,7 @@ export default class LinkedListItemComponent extends Component { if (!routeInfo) return null; return { - name: this.args.linkToIndex + name: linkToIndex ? routeInfo.name : routeInfo.name.replace(/\.index$/, ""), args: [ @@ -76,16 +96,6 @@ export default class LinkedListItemComponent extends Component { }; } - get active() { - if (!this.route || this.args.active !== undefined) { - return this.args.active ?? false; - } - - const { name, args } = this.route; - - return this.router.isActive(name, ...args); - } - @action navigate(event) { event.preventDefault(); diff --git a/tests/dummy/app/components/option-select.hbs b/tests/dummy/app/components/option-select.hbs index 95cd7c92..813bcc0a 100644 --- a/tests/dummy/app/components/option-select.hbs +++ b/tests/dummy/app/components/option-select.hbs @@ -1,4 +1,7 @@