Select to view content in your preferred language

Get tab name onclick

1602
8
Jump to solution
03-31-2022 05:39 PM
LefterisKoumis
Regular Contributor II

Using the tabs and wondering of how to capture the title of the tab when it's clicked on.

Tried:

 document.querySelector("calcite-tab-title").addEventListener("click", handleActionBarClick1);
 
 
0 Kudos
1 Solution

Accepted Solutions
BenElan
Esri Contributor
0 Kudos
8 Replies
BenElan
Esri Contributor

Calcite Components have custom events which you can find on their doc page. For example, here is the event for `calcite-tab-title`:

https://developers.arcgis.com/calcite-design-system/components/tab-title/#component-api-events

 

calcite-tab-nav also has an event:

https://developers.arcgis.com/calcite-design-system/components/tab-nav/#component-api-events-calcite...

0 Kudos
LefterisKoumis
Regular Contributor II

Thanks.

I setup id: 

 

<calcite-tab-nav slot="tab-nav" id="thetabs">
 
//then wanted to capture the event:
const controlTabs = document.getElementById("thetabs")
  controlTabs.addEventListener("calciteTabChange", async (event) => { console.log(event.target.value) });

 

When you click the first tab it works. but when you click the other tabs it return undefined. 

0 Kudos
BenElan
Esri Contributor

The event's callback function doesn't need to be async in your code snippet. Also, the tab info is event.detail.tab. Here is the code:

 

document.addEventListener("calciteTabChange", (event) => console.log(event.detail.tab));

 

0 Kudos
BenElan
Esri Contributor
0 Kudos
Wittawat
New Contributor II

@BenElan could you please share the code with calcite v1.0.7? It seems like the event.detail.tab did not work with new version.

0 Kudos
LefterisKoumis
Regular Contributor II

Since version 1.0.3 the event.detail.tab is depreciated. 

See: 

https://github.com/Esri/calcite-components/blob/master/CHANGELOG.md

LefterisKoumis_0-1678044095795.png

use instead:

event.target.selectedTitle
Wittawat
New Contributor II

Thank you for your reply. I've tried the event.target.selectedTitle but I have received an incorrect tab name.

Please kindly see the example herehttps://codepen.io/aonwittawat/pen/vYzZozd 

0 Kudos
LefterisKoumis
Regular Contributor II

There are current issues with the selectedTitle as acknowledged by ESRI.

Please use this instead.

https://codepen.io/lkoumis1/pen/GRXEwyN?editors=1111

0 Kudos