-
Notifications
You must be signed in to change notification settings - Fork 77
chore(TabStrip): add kb for adding and removing tabs #2998
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
Open
ntacheva
wants to merge
1
commit into
master
Choose a base branch
from
update-dynamic-tabs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
--- | ||
title: Add and Remove Tabs | ||
description: Learn how to dynamically add and remove tabs | ||
type: how-to | ||
page_title: Add and Remove Tabs | ||
slug: tabstrip-kb-add-remove-tabs | ||
tags: telerik,blazor,tabstrip,add tabs,remove tabs | ||
ticketid: | ||
res_type: kb | ||
previous_url: /knowledge-base/tabstrip-dynamic-tabs | ||
--- | ||
|
||
## Environment | ||
|
||
<table> | ||
<tbody> | ||
<tr> | ||
<td>Product</td> | ||
<td>TabStrip for Blazor</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
|
||
## Description | ||
|
||
I have a collection of items representing separate tabs. I am iterating through that collection to render a tab for each item as shown [here](slug:tabstrip-tabs-collection). I want to allow the user to add and remove tabs, how to achieve that? | ||
|
||
This KB article answers the following questions: | ||
|
||
* How to remove a tab with a "X" button in the tab header? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider rephrasing this. Do you mean that the user wants to remove the tab by using the X button, or do you mean that the user wants to remove a tab that has an X button in the header? |
||
* How to use a button to add tabs and position this button next to the last tab header (similar to the "+" button in the browser)? | ||
|
||
## Solution | ||
|
||
The example below shows how to: | ||
* Use a [`HeaderTemplate`](slug:tabstrip-header-template) for the tab to add a button that removes the tab. | ||
* Conditionally display the "X" button based on the tabs' count. | ||
* Declare a button for adding tabs. | ||
* Use custom styling and JavaScript to position the "+" button next to the last tab header. | ||
|
||
````RAZOR | ||
@inject IJSRuntime JS | ||
|
||
<style> | ||
.dynamic-tabstrip-wrapper { | ||
--add-tab-button-size: 32.8px; | ||
} | ||
|
||
.dynamic-tabstrip-wrapper .k-tabstrip-items { | ||
max-width: calc(100% - var(--add-tab-button-size)); | ||
} | ||
|
||
.dynamic-tabstrip-wrapper .add-tab-button { | ||
width: var(--add-tab-button-size); | ||
padding-block: 6px; | ||
z-index: 10000; | ||
} | ||
|
||
.remove-tab-button { | ||
padding: 0 !important; | ||
} | ||
</style> | ||
|
||
<div class="dynamic-tabstrip-wrapper k-relative"> | ||
<TelerikButton OnClick="@AddTab" | ||
Class="add-tab-button !k-absolute k-z-10 k-ratio-1" | ||
Icon="@SvgIcon.Plus" | ||
FillMode="@ThemeConstants.Button.FillMode.Flat" | ||
ThemeColor="@ThemeConstants.Button.ThemeColor.Primary" | ||
Rounded="@ThemeConstants.Button.Rounded.Full" /> | ||
<TelerikTabStrip @bind-ActiveTabId="@ActiveTabId" PersistTabContent="true"> | ||
@foreach (Tab tab in Tabs) | ||
{ | ||
<TabStripTab @key="tab.Id" Id="@tab.Id"> | ||
<HeaderTemplate> | ||
<div class="k-flex-layout k-gap-2"> | ||
@tab.Title | ||
@if (Tabs.Count > 1) | ||
{ | ||
<TelerikButton OnClick="@(() => RemoveTab(tab))" | ||
Class="remove-tab-button" | ||
Icon="@SvgIcon.X" | ||
FillMode="@ThemeConstants.Button.FillMode.Flat" | ||
ThemeColor="@ThemeConstants.Button.ThemeColor.Primary" /> | ||
} | ||
</div> | ||
</HeaderTemplate> | ||
<Content> | ||
Content for @tab.Title | ||
</Content> | ||
</TabStripTab> | ||
} | ||
</TelerikTabStrip> | ||
</div> | ||
|
||
@code { | ||
private string ActiveTabId { get; set; } | ||
|
||
private List<Tab> Tabs = new List<Tab>() | ||
{ | ||
new Tab { Id = Guid.NewGuid().ToString(), Title = "Tab 1" }, | ||
new Tab { Id = Guid.NewGuid().ToString(), Title = "Tab 2" }, | ||
new Tab { Id = Guid.NewGuid().ToString(), Title = "Tab 3" } | ||
}; | ||
|
||
private void AddTab() | ||
{ | ||
Tab tabToAdd = new Tab { Id = Guid.NewGuid().ToString(), Title = $"New Tab" }; | ||
|
||
Tabs.Add(tabToAdd); | ||
|
||
//In this example, we are always activating the newly added tab. Adjust the logic to activate a different tab if needed. | ||
ActiveTabId = tabToAdd.Id; | ||
} | ||
|
||
private void RemoveTab(Tab tab) | ||
{ | ||
if (Tabs.Count <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
Tabs.Remove(tab); | ||
|
||
//In this example, we are always activating the first tab. Adjust the logic to determine which tab to activate after removal. | ||
ActiveTabId = Tabs[0].Id; | ||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
await JS.InvokeVoidAsync("positionAddTabButton"); | ||
|
||
await base.OnAfterRenderAsync(firstRender); | ||
} | ||
|
||
public class Tab | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Title { get; set; } | ||
} | ||
|
||
} | ||
|
||
<script suppress-error="BL9992"> | ||
window.positionAddTabButton = () => { | ||
const tabStripItems = Array.from(document.querySelectorAll(".dynamic-tabstrip-wrapper .k-tabstrip-item")); | ||
const tabStripWrapperWidth = document.querySelector(".dynamic-tabstrip-wrapper").scrollWidth; | ||
|
||
let totalWidth = !tabStripItems ? 0 : tabStripItems.reduce( | ||
(accumulator, currentItem) => accumulator + parseFloat(currentItem.getBoundingClientRect().width), | ||
0, | ||
); | ||
|
||
const addTabButton = document.querySelector(".add-tab-button"); | ||
const addTabButtonWidth = addTabButton.getBoundingClientRect().width; | ||
|
||
// assure button is never positioned outside the boundaries of the wrapper | ||
if (totalWidth + addTabButtonWidth > tabStripWrapperWidth) { | ||
totalWidth = tabStripWrapperWidth - addTabButtonWidth; | ||
} | ||
|
||
addTabButton.style.left = `${totalWidth}px`; | ||
}; | ||
</script> | ||
```` | ||
|
||
## See Also | ||
|
||
* [Dynamic Tabs](slug:tabstrip-tabs-collection) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoiding generic words like "here" lets you improve the SEO and quality of the text.