Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui5-tabcontainer): add noAutoSelection property #10877

Merged
merged 14 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/main/cypress/specs/TabContainer.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import TabContainer from "../../src/TabContainer.js";
import Tab from "../../src/Tab.js";

describe("TabContainer general interaction", () => {
it("Tests no auto selection", () => {
cy.mount(
<TabContainer no-auto-selection id="tcNoAuto">
<Tab text="Products ID">
tab1
</Tab>
<Tab text="Availability">
tab2
</Tab>
<Tab text="Expiration Date">
tab3
</Tab>
</TabContainer>
);

cy.get("#tcNoAuto")
.then(tc => {
const tabContainer = tc.get(0) as TabContainer;
const allItems = tabContainer.allItems;
allItems.forEach(item => {
cy.wrap(item).should("have.prop", "selected", false);
});
});
});
});
21 changes: 16 additions & 5 deletions packages/main/src/TabContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ class TabContainer extends UI5Element {
@property()
tabsPlacement: `${TabContainerTabsPlacement}` = "Top";

/**
* Defines if no tab will be selected initially.
*
* **Note:** By default, if none of the child tabs have the `selected` property set, the first tab will be automatically selected.
* Setting this property to `true` allows preventing this behavior.
* @default false
* @public
* @since 2.9.0
*/
@property({ type: Boolean })
noAutoSelection = false;

/**
* Defines the current media query size.
* @private
Expand Down Expand Up @@ -374,8 +386,10 @@ class TabContainer extends UI5Element {

if (selectedTab) {
this._selectedTab = selectedTab;
} else {
} else if (!this.noAutoSelection) {
this._selectedTab = this._itemsFlat[0] as Tab;
} else {
this._selectedTab = undefined;
}

walk(this.items, item => {
Expand Down Expand Up @@ -975,10 +989,6 @@ class TabContainer extends UI5Element {
const tabStrip = this._getTabStrip();
let allItemsWidth = 0;

if (!this._selectedTab) {
return;
}

const itemsDomRefs = this.items.map(item => item.getDomRefInStrip()) as Array<TabInStrip | TabSeparatorInStrip>;

// make sure the overflows are hidden
Expand Down Expand Up @@ -1428,6 +1438,7 @@ class TabContainer extends UI5Element {
root: {
"ui5-tc-root": true,
"ui5-tc--textOnly": this.textOnly,
"ui5-tc--noTabSelected": !this._selectedTab,
"ui5-tc--withAdditionalText": this.withAdditionalText,
"ui5-tc--standardTabLayout": this.standardTabLayout,
},
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/TabContainerTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function TabContainerTemplate(this: TabContainer, injectedPartial
"ui5-tc--textOnly": this.textOnly,
"ui5-tc--withAdditionalText": this.withAdditionalText,
"ui5-tc--standardTabLayout": this.standardTabLayout,
"ui5-tc--noTabSelected": !this._selectedTab,
}}
>
{this.tabsAtTheBottom && partials.contentArea.call(this)}
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/themes/TabContainer.css
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,7 @@
:host([media-range="XL"]) .ui5-tc__content {
padding: 1rem 3rem;
}

.ui5-tc-root.ui5-tc--noTabSelected .ui5-tc__content {
padding: 0;
}
48 changes: 48 additions & 0 deletions packages/main/test/pages/TabContainer.html
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,50 @@ <h3>Narrow Tab Container</h3>
</ui5-tabcontainer>
</section>

<section>
<h2>No tab initially selected</h2>
<ui5-tabcontainer no-auto-selection id="tcNoAuto">
<ui5-tab text="Products ID">
<ui5-button>Button 11</ui5-button>
<ui5-button>Button 12</ui5-button>
</ui5-tab>
<ui5-tab text="Availability">
<ui5-button>Button 2</ui5-button>
</ui5-tab>
<ui5-tab text="Expiration Date">
<ui5-button>Button 3</ui5-button>
</ui5-tab>
<ui5-tab text="Date of Delivery">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Date of Manufacture">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Value">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Price">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Net Profit">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Company Name">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Warehouse Location">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Address">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
<ui5-tab text="Country">
<ui5-button>Button 4</ui5-button>
</ui5-tab>
</ui5-tabcontainer>
<ui5-button id="resetBtn">Reset tab selection</ui5-button>
</section>

<script>
document.getElementById("tabContainer1").addEventListener("ui5-tab-select", async function (event) {
result.innerHTML = event.detail.tab.text;
Expand Down Expand Up @@ -1047,6 +1091,10 @@ <h3>Narrow Tab Container</h3>
tab.focus();
});

resetBtn.addEventListener("click", () => {
tcNoAuto.allItems.forEach(tab => tab.selected = false);
});

</script>
</body>

Expand Down