@duetds/components
Version:
This package includes Duet Core Components and related tools.
254 lines (253 loc) • 9.13 kB
JavaScript
import { Build, h } from "@stencil/core";
export class DuetTabGroup {
constructor() {
this.tabs = [];
this.selected = 0;
/**
* Theme of the tab. Can be one of: "default", "turva".
*/
this.theme = "";
/**
* Controls the margin of the component. Can be one of: "auto", "none".
*/
this.margin = "auto";
/**
* Controls the padding of the component. Can be one of: "auto", "none".
*/
this.padding = "auto";
/**
* Component event handling.
*/
this.handleKeyDown = ev => {
const prev = ev.target.previousElementSibling;
const next = ev.target.nextElementSibling;
if (prev) {
// Arrow left
if (ev.keyCode === 37) {
ev.preventDefault();
prev.click();
prev.focus();
}
// Arrow up
if (ev.keyCode === 38) {
ev.preventDefault();
prev.click();
prev.focus();
}
}
if (next) {
// Arrow right
if (ev.keyCode === 39) {
ev.preventDefault();
next.click();
next.focus();
}
// Arrow down
if (ev.keyCode === 40) {
ev.preventDefault();
next.click();
next.focus();
}
}
};
this.refreshTabs = () => {
// Grab tabs from this component
this.tabs = Array.from(this.element.querySelectorAll("duet-tab"));
if (this.tabs.length === 0) {
throw new Error("[duet-tabs] Must have at least one tab");
}
else {
this.tabs.map((tab, index) => {
tab.id = "duet-tab-" + index;
tab.setAttribute("aria-labelledby", "duet-tab-button-" + index);
if (tab.selected) {
this.selected = index;
}
});
}
};
}
/**
* This method allows you to open any of the tabs by calling the method and passing the index of the tab. Please note that index starts from zero.
*/
async openTab(index) {
if (index >= this.tabs.length) {
throw new Error(`[duet-tabs] Index ${index} is out of bounds of tabs length`);
}
this.tabs = this.tabs.map((tab, i) => {
tab.selected = i === index;
return tab;
});
this.selected = index;
this.duetChange.emit({
value: index,
component: "duet-tab-group",
});
}
/**
* Component lifecycle events.
*/
componentWillLoad() {
if (this.theme !== "default" && document.documentElement.classList.contains("duet-theme-turva")) {
this.theme = "turva";
}
this.refreshTabs();
this.element.forceUpdate();
}
/**
* Forces render() update for `duet-tab-group`. Use this when e.g. changing the global language.
*/
async refresh() {
this.refreshTabs();
}
/**
* render() function
* Always the last one in the class.
*/
render() {
if (Build.isBrowser) {
return (h("div", { class: {
"duet-tab-group": true,
"duet-theme-turva": this.theme === "turva",
} },
this.tabs.length > 3 ? (h("duet-select", { value: this.selected.toString(), expand: true, "label-hidden": true, theme: this.theme, onDuetChange: event => this.openTab(Number(event.detail.value)), items: this.tabs.map((tab, index) => {
return { label: tab.label, value: index };
}) })) : (""),
h("div", { role: "tablist", class: {
"duet-m-0": this.margin === "none",
"duet-tab-group-tabs": true,
"duet-tab-group-hidden": this.tabs.length > 3,
} }, this.tabs.map((tab, index) => {
return (h("button", { role: "tab", id: "duet-tab-button-" + index, "aria-controls": "duet-tab-" + index, "aria-selected": tab.selected ? "true" : "false", class: {
"duet-p-0": this.padding === "none",
"duet-tab-button": true,
selected: tab.selected,
}, onClick: () => this.openTab(index), onKeyDown: ev => this.handleKeyDown(ev) }, tab.label));
})),
h("div", { class: { "duet-tab-group-content": true, "duet-m-0": this.margin === "none" } },
h("slot", null))));
}
}
static get is() { return "duet-tab-group"; }
static get encapsulation() { return "scoped"; }
static get originalStyleUrls() { return {
"$": ["duet-tab-group.scss"]
}; }
static get styleUrls() { return {
"$": ["duet-tab-group.css"]
}; }
static get properties() { return {
"theme": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Theme of the tab. Can be one of: \"default\", \"turva\"."
},
"attribute": "theme",
"reflect": false,
"defaultValue": "\"\""
},
"margin": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Controls the margin of the component. Can be one of: \"auto\", \"none\"."
},
"attribute": "margin",
"reflect": false,
"defaultValue": "\"auto\""
},
"padding": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Controls the padding of the component. Can be one of: \"auto\", \"none\"."
},
"attribute": "padding",
"reflect": false,
"defaultValue": "\"auto\""
}
}; }
static get states() { return {
"tabs": {},
"selected": {}
}; }
static get events() { return [{
"method": "duetChange",
"name": "duetChange",
"bubbles": false,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Callback for when the value changed."
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}]; }
static get methods() { return {
"openTab": {
"complexType": {
"signature": "(index: number) => Promise<void>",
"parameters": [{
"tags": [],
"text": ""
}],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "This method allows you to open any of the tabs by calling the method and passing the index of the tab. Please note that index starts from zero.",
"tags": []
}
},
"refresh": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Forces render() update for `duet-tab-group`. Use this when e.g. changing the global language.",
"tags": []
}
}
}; }
static get elementRef() { return "element"; }
}