-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (117 loc) · 4.5 KB
/
index.ts
File metadata and controls
132 lines (117 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { html, unsafeCSS } from 'lit';
import { PieElement } from '@justeattakeaway/pie-webc-core/src/internals/PieElement';
import { safeCustomElement, validPropertyValues } from '@justeattakeaway/pie-webc-core';
import { classMap } from 'lit/directives/class-map.js';
import { property, queryAssignedElements } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import styles from './tabs.scss?inline';
import {
type TabsProps,
variants,
defaultProps,
orientations,
} from './defs';
import { type TabPanelProps } from './pie-tab-panel/defs';
const componentSelector = 'pie-tabs';
// Valid values available to consumers
export * from './defs';
/**
* @tagname pie-tabs
*/
@safeCustomElement(componentSelector)
export class PieTabs extends PieElement implements TabsProps {
@property({ type: String })
@validPropertyValues(componentSelector, variants, defaultProps.variant)
public variant = defaultProps.variant;
@property({ type: String })
@validPropertyValues(componentSelector, orientations, defaultProps.orientation)
public orientation = defaultProps.orientation;
@queryAssignedElements() _pieTabPanelSlots!: Array<HTMLElement & TabPanelProps>;
private _selectedTab = 0;
firstUpdated (): void {
this.requestUpdate();
}
updated (): void {
this.updateSelectedPanel();
}
/**
* Handles the click event on a tab.
* This method updates the selected tab index and updates the displayed panel accordingly.
*
* @param index The index of the tab that was clicked.
*
* @private
*/
private handleTabClick (index: number) {
this._selectedTab = index;
this.updateSelectedPanel();
this.requestUpdate();
}
/**
* Updates the selected state of each tab panel based on the currently selected tab index.
* This method iterates through all tab panels and sets the `selected` property accordingly.
*
* @private
*/
private updateSelectedPanel () {
this._pieTabPanelSlots.forEach((panel, index) => {
panel.selected = index === this._selectedTab;
});
}
render () {
const classes = {
'c-tabs': true,
'c-tabs-variant--contained': this.variant === 'contained',
[`c-tabs-orientation--${this.orientation}`]: true,
};
return html`
<div
data-test-id="pie-tabs"
class="${classMap(classes)}"
>
${this._pieTabPanelSlots && this._pieTabPanelSlots.length > 0 && (html`
<nav class="c-tabs-navigation">
<ul role="tablist">
${
/* eslint-disable indent */
repeat(
this._pieTabPanelSlots,
(element, index) => html`
<li
@click=${() => {
if (element.disabled) {
return;
}
this.handleTabClick(index);
}}
role="tab"
tabindex="${index}"
class="${classMap({
selected: this._selectedTab === index,
[`c-tabs-navigation-item--${this.orientation}`]: true,
[`c-tabs-navigation-item-variant--${this.variant}`]: true,
disabled: !!element.disabled,
})}"
>
<span>${element.title}</span>
</li>
`,
/* eslint-enable indent */
)}
</ul>
</nav>
`)}
<div class="c-tabs-panels">
<slot></slot>
</div>
</div>
`;
}
// Renders a `CSSResult` generated from SCSS by Vite
static styles = unsafeCSS(styles);
}
declare global {
interface HTMLElementTagNameMap {
[componentSelector]: PieTabs;
}
}