Created
February 9, 2022 09:05
-
-
Save sjurgis/b5f342d67dcbd62ff1b56854291b06eb to your computer and use it in GitHub Desktop.
lightning web component tabset with url navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<lightning-tabset variant="vertical" active-tab-value={activeTabValue}> | |
<lightning-tab label="Item One" value="one" onactive={handleActive}> | |
First | |
</lightning-tab> | |
<lightning-tab label="Item 2" value="two" onactive={handleActive}> | |
second | |
</lightning-tab> | |
<lightning-tab label="Item 3" value="three" onactive={handleActive}> | |
third | |
</lightning-tab> | |
</lightning-tabset> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, wire } from 'lwc' | |
import { CurrentPageReference, NavigationMixin } from 'lightning/navigation' | |
export default class Main extends NavigationMixin(LightningElement) { | |
handleActive(event) { | |
event.preventDefault() | |
event.stopPropagation() | |
this[NavigationMixin.Navigate]( | |
this.getUpdatedPageReference({ | |
c__tab: event.target.value | |
}), true) | |
} | |
@wire(CurrentPageReference) currentPageReference | |
get activeTabValue() { | |
return this.currentPageReference?.state?.c__tab ?? 'two' | |
} | |
getUpdatedPageReference(stateChanges) { | |
return Object.assign({}, this.currentPageReference, { | |
state: Object.assign({}, this.currentPageReference.state, stateChanges) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment