Skip to content

Commit 40c8ffa

Browse files
authored
feat: Remove the "pfe-" prefix from the history feature in pfe-tabs (#808)
Fixes #802
1 parent 36d31bf commit 40c8ffa

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

elements/pfe-tabs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ This will override any context being passed from a parent component and will add
7373
Updates window.history and the URL to create sharable links. With the
7474
`pfe-tab-history` attribute, the tabs and each tab *must* have an `id`.
7575

76-
The URL pattern will be `?pfe-{id-of-tabs}={id-of-selected-tab}`. In the example
77-
below, selecting "Tab 2" will update the URL as follows: `?pfe-my-tabs=tab2`.
76+
The URL pattern will be `?{id-of-tabs}={id-of-selected-tab}`. In the example
77+
below, selecting "Tab 2" will update the URL as follows: `?my-tabs=tab2`.
7878

7979
```html
8080
<pfe-tabs pfe-tab-history id="my-tabs">
@@ -99,7 +99,7 @@ By default, `pfe-tabs` will read the URL and look for a query string parameter
9999
that matches the id of a `pfe-tabs` component and a value of a specific
100100
`pfe-tab`.
101101

102-
For example, `?pfe-my-tabs=tab2` would open the second tab in the code sample below.
102+
For example, `?my-tabs=tab2` would open the second tab in the code sample below.
103103
"my-tabs" is equal to the id of the `pfe-tabs` component and "tab2" is equal to
104104
the id of the second tab in the tab set.
105105

elements/pfe-tabs/src/pfe-tabs.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class PfeTabs extends PFElement {
211211
const urlParams = new URLSearchParams(window.location.search);
212212
const hash = window.location.hash;
213213

214-
urlParams.set(`pfe-${this.id}`, tab.id);
214+
urlParams.set(`${this.id}`, tab.id);
215215
history.pushState({}, "", `${pathname}?${urlParams.toString()}${hash}`);
216216
}
217217

@@ -447,10 +447,17 @@ class PfeTabs extends PFElement {
447447
urlParams = new URLSearchParams(window.location.search);
448448
}
449449

450-
if (urlParams && urlParams.has(`pfe-${this.id}`)) {
451-
tabIndex = this._allTabs().findIndex(
452-
tab => tab.id === urlParams.get(`pfe-${this.id}`)
453-
);
450+
// @DEPRECATED
451+
// the "pfe-" prefix has been deprecated but we'll continue to support it
452+
// we'll give priority to the urlParams.has(`${this.id}`) attribute first
453+
// and fallback to urlParams.has(`pfe-${this.id}`) if it exists. We should
454+
// be able to remove the || part of the if statement in the future
455+
if (
456+
urlParams &&
457+
(urlParams.has(`${this.id}`) || urlParams.has(`pfe-${this.id}`))
458+
) {
459+
const id = urlParams.get(`${this.id}`) || urlParams.get(`pfe-${this.id}`);
460+
tabIndex = this._allTabs().findIndex(tab => tab.id === id);
454461
}
455462

456463
return tabIndex;

elements/pfe-tabs/test/pfe-tabs_test.html

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,38 @@ <h1>Tab 2 Content</h1>
420420
test("it should show the correct tab if there is a querystring parameter in the URL", done => {
421421
// the parameter should be equal to the id of the tabset
422422
// the value should be equal to the id of the tab you want opened
423-
const searchParams = new URLSearchParams(window.location.search)
423+
const searchParams = new URLSearchParams();
424+
searchParams.set("fromQueryString", "fromQueryStringTab2");
425+
var newPath = window.location.pathname + '?' + searchParams.toString();
426+
history.pushState(null, '', newPath);
427+
428+
const fragment = document.createRange().createContextualFragment(`
429+
<pfe-tabs id="fromQueryString">
430+
<pfe-tab role="heading" slot="tab" id="fromQueryStringTab1">Tab 1</pfe-tab>
431+
<pfe-tab-panel role="region" slot="panel">Content</pfe-tab-panel>
432+
<pfe-tab role="heading" slot="tab" id="fromQueryStringTab2">Tab 2</pfe-tab>
433+
<pfe-tab-panel role="region" slot="panel">Content</pfe-tab-panel>
434+
</pfe-tabs>
435+
`);
436+
437+
document.body.appendChild(fragment);
438+
439+
flush(() => {
440+
const tabs = document.querySelector("#fromQueryString");
441+
const tab2 = tabs.querySelector("#fromQueryStringTab2");
442+
assert.equal(tabs.selectedIndex, 1);
443+
assert.isTrue(tab2.hasAttribute("aria-selected"));
444+
445+
document.body.removeChild(tabs);
446+
done();
447+
});
448+
});
449+
450+
test("it should continue to support the \"pfe-\" prefix in the URL and show the correct tab if there is a querystring parameter in the URL", done => {
451+
// this supports any tabs that are currently using the "pfe-" prefix
452+
// in the URL to open to a specific tab. the "pfe-" prefix has been
453+
// deprecated and should no longer be used
454+
const searchParams = new URLSearchParams();
424455
searchParams.set("pfe-fromQueryString", "fromQueryStringTab2");
425456
var newPath = window.location.pathname + '?' + searchParams.toString();
426457
history.pushState(null, '', newPath);
@@ -483,7 +514,7 @@ <h1>Tab 2 Content</h1>
483514

484515
flush(() => {
485516
const urlSearchParams = new URLSearchParams(window.location.search);
486-
assert.equal(urlSearchParams.get("pfe-history"), "historyTab2");
517+
assert.equal(urlSearchParams.get("history"), "historyTab2");
487518
assert.equal(tabs.selectedIndex, 1);
488519
assert.isTrue(tab2.hasAttribute("aria-selected"));
489520
done();
@@ -500,7 +531,7 @@ <h1>Tab 2 Content</h1>
500531

501532
flush(() => {
502533
const urlSearchParams = new URLSearchParams(window.location.search);
503-
assert.equal(urlSearchParams.get("pfe-history"), "historyTab2");
534+
assert.equal(urlSearchParams.get("history"), "historyTab2");
504535
assert.equal(tabs.selectedIndex, 0);
505536
assert.isTrue(tab1.hasAttribute("aria-selected"));
506537
done();

0 commit comments

Comments
 (0)