Skip to content

Commit f5d56dc

Browse files
committed
Updates to the tab group
1 parent f654e23 commit f5d56dc

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050

5151
<div class="container">
5252
<h1>Tab Group</h1>
53-
<div style="background-color: var(--grey-10-color); padding: 5px;">
53+
<div style="background-color: #fcfcfc; padding: 5px;">
5454
<wc-tab-group backgroundColor="light">
55-
<wc-tab name="tab-tables" selected>Tables</wc-tab>
55+
<wc-tab name="tab-tables" selected><wc-icon name="table" size="medium"></wc-icon> Tables</wc-tab>
5656
<wc-tab name="tab-buttons">Buttons</wc-tab>
5757
<wc-tab name="tab-badges">Badges</wc-tab>
5858
<wc-tab name="tab-icons">Icons</wc-tab>

src/nav/TabElement.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ export class TabElement extends LitElement {
5454
li {
5555
vertical-align: middle;
5656
}
57-
li.selected {
58-
border-bottom: 2px solid var(--error-color);
59-
}
6057
`;
6158
}
6259

src/nav/TabGroupElement.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
LitElement, html, css, nothing,
33
} from 'lit';
4+
import { TabElement } from './TabElement';
45

56
/**
67
* TabGroupElement
@@ -29,7 +30,6 @@ export class TabGroupElement extends LitElement {
2930

3031
static get properties() {
3132
return {
32-
3333
/**
3434
* Background color of the tab group, one of primary, secondary, light, white, dark, black
3535
*
@@ -104,15 +104,19 @@ export class TabGroupElement extends LitElement {
104104
border-bottom: none;
105105
border-color: var(--grey-40-color);
106106
}
107-
.bg-color-light ::slotted(wc-tab:hover) {
107+
.bg-color-light ::slotted(wc-tab:hover),::slotted(wc-tab:active),::slotted(wc-tab[selected]) {
108108
background-color: var(--primary-color);
109109
color: var(--white-color);
110110
}
111111
.bg-color-light ::slotted(wc-tab:active) {
112-
background-color: var(--primary-color);
113-
color: var(--white-color);
114112
font-weight: var(--font-weight-bold);
115-
}
113+
}
114+
.bg-color-light ::slotted(wc-tab[disabled]) {
115+
background-color: inherit;
116+
font-weight: inherit;
117+
color: var(--grey-40-color);
118+
cursor: inherit;
119+
}
116120
`;
117121
}
118122

@@ -137,23 +141,35 @@ export class TabGroupElement extends LitElement {
137141
this.addEventListener(Event.EVENT_CLICK, (evt) => this.onClick(evt));
138142
}
139143

144+
/**
145+
* Select a tab by name, and deselect all other tabs
146+
* @param {String} name: The name of the tab to select
147+
* @returns The node that was selected, or null
148+
*/
140149
select(name) {
141150
const tabs = this.querySelectorAll('wc-tab');
151+
let selectedNode = null;
142152
tabs.forEach((tab) => {
143-
if (tab.name === name && !tab.disabled) {
144-
if (!tab.selected) {
145-
tab.setAttribute('selected', true);
146-
}
147-
} else if (tab.selected) {
148-
tab.selected = false;
153+
if (tab.name === name && !tab.selected) {
154+
tab.setAttribute('selected', 'selected');
155+
selectedNode = tab;
156+
} else if (tab.name !== name && tab.selected) {
157+
tab.removeAttribute('selected');
149158
}
150159
});
160+
return selectedNode;
151161
}
152162

153-
// eslint-disable-next-line class-methods-use-this
154163
onClick(event) {
155-
if (event.target && event.target.name && !event.target.disabled) {
156-
this.select(event.target.name);
164+
if (event.target && event.target.name) {
165+
const selected = this.select(event.target.name);
166+
if (selected) {
167+
this.dispatchEvent(new CustomEvent(Event.EVENT_CLICK, {
168+
bubbles: true,
169+
composed: true,
170+
detail: selected.name || selected.textContent,
171+
}));
172+
}
157173
}
158174
}
159175
}

0 commit comments

Comments
 (0)